brake 2026.11.3

A brake on breaking API changes. Checks OpenAPI, protobuf and GraphQL contracts for backward compatibility, hermetically.
Documentation
# AGENTS.md

<!-- Generated from CLAUDE.md by `make agents`. Do not edit by hand. -->

Guidance for working in this repository.

## Overview

`brake` checks API contracts for backward compatibility and fails the commit
when a change would break a consumer. One ruleset, two scopes — `brake check`
on a change at commit time, `brake analyze` over the whole repository in CI.

> **A brake on breaking API changes: one compatibility ruleset, enforced at
> commit time and over the whole repository, with no network, no toolchain, and
> no running service.**

`brake` is an anagram of `break`. See [README.md](README.md).

**Read [design/](design/) before planning work.** It is the source of truth.
[design/01-thesis.md](design/01-thesis.md) carries the scope decisions,
including what is deliberately *not* being built — check it before
"completing" anything, because several omissions are on purpose.

## Status

M0–M6 are done, and so are M7 (protobuf), M8 (GraphQL), AsyncAPI 2.x/3.x and
MCP `tools/list` snapshots
([design/08-mcp-tool-contracts.md](design/08-mcp-tool-contracts.md)).
All five ingesters produce the same `Contract` and share one comparator;
`check`, `analyze`, `diff`, `explain` and `consumers` work; text, JSON, SARIF,
GitHub workflow commands and GitLab Code Quality all render.

Four structural lint rules
([design/07-structural-lint.md](design/07-structural-lint.md)) check the head
contract with no baseline. They live in `src/rules/lint.rs`.
`lint::introduced` reports only defects the baseline lacks, the same ratchet
as every comparison rule. Every surface reaches it through `check`, and the
library also exposes it as `brake::lint`, so the hook, the agent and forge
cannot disagree. A `contract-new` contract is linted in full; a
`baseline-unconfigured` one is not linted, because it has not opted in.

A contract may span several files: a cross-file `$ref` is resolved through a
`DocumentResolver`, hermetically, within the document's own directory.
OpenAPI 3.1 type arrays, `prefixItems` tuples and `discriminator` mappings are
modelled and compared — through the existing rules, not new ones.

M12–M15, consumer demand
([design/05-consumer-demand.md](design/05-consumer-demand.md)), are done too:
pact, GraphQL-operation and manifest declarations, the join, the ten
`consumer-*` rules, `affects` on every finding, and `who_consumes` over MCP.

The rule catalogue lives in `src/rules/catalogue.rs` and is the single source
of truth — [docs/rules.md](docs/rules.md) is generated from it by `make docs`
and a test fails if the two drift.

What is worth knowing before changing anything:

- **The seven self-defence tests are in `tests/self_defence.rs`.** They defend
  the numbered guarantees in
  [design/02-contract-gates.md]design/02-contract-gates.md §6.1, the last two
  over the demand axis
  ([design/05-consumer-demand.md]design/05-consumer-demand.md §8). If one of
  them starts failing, a claim the README makes has stopped being true.
- **`tests/cli.rs` covers the binary's own surface.** A dropped argument in
  `main.rs` is invisible to library tests; that is how `brake check <path>`
  once ignored its paths and checked everything.
- **`brake` gates its own fixture contract** (`api/payments-openapi.yaml` via
  `brake.toml`). `make self-check` runs it.
- **Contract detection lives in `src/init.rs::identify` and works by parsing.**
  `brake init` and the `contract-unconfigured` notice share it, so they cannot
  disagree about what a contract is. Never reintroduce a filename heuristic:
  the previous one called `.github/workflows/api-tests.yaml` an API.
- **The MCP server is `src/mcp/`, behind the non-default `mcp` feature.**
  `handlers.rs` is synchronous and transport-free; `server.rs` is the `rmcp`
  adapter and the only file that knows about async. Keep the split: it is what
  makes the tools testable without a protocol.
- **Nothing in `src/mcp/` may reach the `--drift` subprocess path.**
  `design/04-mcp-interface.md` §5.1 is the reasoning. The core enforces it:
  the server asks for `Trust::Untrusted`, and `check` never spawns the
  generator for an untrusted caller whatever `drift` says.
  `tests/mcp.rs::no_tool_call_can_execute_a_declared_generator` and
  `check::tests::an_untrusted_caller_never_reaches_the_drift_subprocess` are
  the guards.
- **`src/demand/` never fetches anything, under any flag.** A URL in a pact —
  `_links`, `pb:publish`, a `$ref` in an example body — is data. A consumer
  `source` that is a URL is refused when `brake.toml` is parsed. `brake` reads
  the directory a prior CI step wrote; it does not talk to a broker.
- **Verification is `compare/types.rs` run sideways, not a second comparator.**
  The consumer's expectation goes on the *base* side, the head contract on the
  head side. If a demand-specific comparison appears in `src/demand/`, the
  projection is wrong — fix it in `contract/` or `compare/`.
- **A demand is silent about formats, bounds, nullability and enum membership.**
  A pact records one value, not a schema, so `verify::reconcile` copies those
  from the contract before comparing. Removing that turns every `format: uuid`
  into a false `consumer-request-rejected`, which is how a hook gets
  uninstalled.
- **Attribution is evidence on a finding, never a second finding.** There is no
  `consumer-break` rule, deliberately: one broken field must not produce four
  findings a developer has to reassemble.
- **`docs/` and `.claude/skills/` state rule ids, flag names and exit-code
  meanings.** They are prose, so nothing fails when they drift — which is
  exactly why a change to the CLI surface, a rule id, or the meaning of an exit
  code has to update them in the same commit. `docs/rules.md` is the exception:
  `make docs` regenerates it and a test fails if it is stale.
- **`AGENTS.md` and `.junie/skills/` are generated copies** of `CLAUDE.md` and
  `.claude/skills/`. Edit the canonical file, then `make agents`.
  `tests/agent_guidance_is_consistent.rs` fails if they drift, because no agent
  tool follows a pointer between the three places reliably.
- **The base contract and the head contract get *different* resolvers.** A
  cross-file `$ref` on the baseline side must be read out of the baseline's own
  revision — `ResolvedBaseline::origin` says which — while the head side reads
  the working tree. Sharing one resolver put today's shared schema on both
  sides, so a field deleted from a file two documents `$ref` was missing from
  base and head alike and the run reported clean.
  `check::tests::a_break_in_a_shared_document_is_not_hidden_by_the_working_tree`
  is the guard.
- **A resolver path is relative to the *document*, never to `source`.** For a
  baseline read out of git, `source` is a label like `rev:HEAD` and names no
  directory at all. Each resolver is rooted at the contract's own directory,
  which is also the boundary a `$ref` cannot reach past.
- **`Contract` is public API too.** Adding a field, as `lint_candidates` was,
  breaks any struct literal downstream. `Contract::empty()` is the
  constructor to point consumers at.
- **`TypeRef` is public API that forge matches on.** Adding a variant, or a
  field to one, breaks every exhaustive `match` and struct-variant literal
  downstream. It is sometimes the right thing to do — say so in the release
  notes rather than discovering it in forge's build.
- **[design/06-architectural-evolution.md]design/06-architectural-evolution.md
  is a roadmap, not a description.** Some of it is built and some is not:
  `proto-field-unreserved` and `proto-enum-value-unreserved` are proposals, and
  reusing a reserved number or name is currently reported as `contract-partial`
  via an unmodelled entry. Never document a proposal as behaviour — check
  `src/rules/catalogue.rs`, which is the only list of rules that exist.
- **An unmodelled entry reaches a finding only through a `TypeRef::Unknown`**
  inside an endpoint. `Contract::unmodelled` alone is never reported, so a
  gap with no endpoint to attach to has to be an ingest error instead. That
  is why the MCP ingester refuses a paginated snapshot rather than marking it
  partial.

## Core constraints

These are not negotiable and they drive the design.

1. **No network, ever.** Not behind a flag. A `$ref` resolving to a URL is
   refused, not fetched. Remote refs are the largest source of non-determinism
   in OpenAPI tooling.
2. **No toolchain, no build, no running service.** `brake` reads files. It does
   not invoke a package manager, compile anything, or start the API under test.
   This is what makes a pre-commit hook deployable.
3. **One subprocess, opt-in.** `brake check --drift` runs a declared generator
   command. It is the only place a subprocess is spawned, it is off by default,
   and it must stay unreachable without the flag — `brake check` has to be safe
   to run against an untrusted repository.
4. **Report `unavailable` rather than a false clean.** When a construct cannot
   be modelled, say so and name it. A tool that silently ignores what it cannot
   parse manufactures confidence.
5. **Deterministic, and provably so.** Same inputs, same verdict, same bytes.
   Guarantees are enumerated in
   [design/02-contract-gates.md]design/02-contract-gates.md §6.1 and each is
   defended by a test.

## Layout

Single crate, two targets — not a workspace. The library is a product: forge
depends on it.

```
design/            The specification — read this first
src/lib.rs         Library: ingest, compare, rules, report
src/main.rs        CLI: arguments, rendering, exit codes
src/config.rs      brake.toml
src/contract/      Format ingesters → the normalised Contract model
src/contract/resolver.rs   Hermetic multi-document $ref resolution
src/compare/       Contract × Contract → Change. Format-agnostic
src/rules/         Change × Level → Finding. The rule catalogue
src/demand/        Consumer declarations → Demand → the join → attribution
src/baseline.rs    file / git / git-merge-base, via gix; and the baseline's resolver
src/render/        text, json, sarif, github, gitlab; render::json::finding is shared with MCP
src/surface.rs     The capability table: every CLI flag and MCP argument, and why a gap exists
docs/              The prose guides; docs/rules.md alone is generated
docs/assets/       The logo — hand-authored SVG, no font and no raster asset
.claude/skills/    Four agent skills — see docs/agent-skills.md
AGENTS.md          Generated from CLAUDE.md by `make agents`
.junie/skills/     Generated from .claude/skills/ by `make agents`
```

**Three structural rules:**

- `main.rs` may parse arguments, render output, and exit. It may **not** decide
  whether something is a breaking change. If a behaviour cannot be tested
  through the library, it is in the wrong file.
- **The CLI and the MCP server are adapters over `check::check` and
  `check::check_documents`**
  ([design/09-one-core-three-surfaces.md]design/09-one-core-three-surfaces.md).
  Neither calls `compare`, `evaluate`, `lint`, the consumer policies or
  `apply_suppressions` itself; `tests/surface_parity.rs` fails if one does. A
  new option goes on `check::Options`, which both adapters fill. A draft is an
  entry in `Options::overrides`, never a separate code path.
- **Every CLI flag and MCP argument is in `src/surface.rs`.** Adding one to
  either surface fails `tests/surface_parity.rs` until the table says what it
  maps to on the other side, or why it has none. `make docs` regenerates
  `docs/capabilities.md` from it.
- `compare/` is format-agnostic. If a `match` on format appears there, the
  ingest normalisation is under-specified — fix it in `contract/`, not with a
  special case.

## Commands

```sh
make check          # fmt, clippy -D warnings, tests — the gate, as CI's lint and test jobs
make hooks          # install the prek git hooks: fast checks on commit, make check on push
make build
make test
make docs           # regenerate docs/rules.md from the catalogue
make agents         # regenerate AGENTS.md and .junie/skills from CLAUDE.md
make self-check     # run brake against its own fixture contract
make release-dry    # what `make release` would tag (it pushes a tag; nothing is bumped)
cargo run -- init            # scaffold brake.toml by parsing what is there
cargo run -- check api/openapi.yaml
cargo run -- consumers               # who uses what, non-gating
cargo run --features mcp -- mcp .     # the MCP server, on stdio
```

`make check` builds `--all-features`, so it covers the MCP path too.

**`make check` is not the whole gate.** CI runs the tests on Linux, macOS *and*
Windows, and several jobs have no local equivalent — the MSRV pin, the
library-only build forge depends on, `cargo-deny`, and the generated-docs
check. A green `make check` on one machine is necessary, not sufficient: the
Windows `test` job stayed red for a while because `--drift` runs its command
through `cmd` there, which does not expand `$VAR`. Check the run after pushing.

`make check` must pass before every commit; the prek pre-push hook runs it too.

## Testing

- **Every rule needs a positive and a negative test.** It fires on the break,
  and stays quiet on the safe change. Silent false positives are how a hook gets
  uninstalled permanently.
- **Fixtures go under `tempfile::tempdir()`**, never the surrounding checkout. A
  test that inspects the ambient git repository passes on a laptop and fails in
  CI, which checks out a detached HEAD.
- **The five self-defence tests** in
  [design/03-implementation-plan.md]design/03-implementation-plan.md §6 are not
  optional — they are what the determinism and hermeticity claims rest on.
- Snapshot diagnostics with `insta`.

## Conventions

- Trunk-based: commit directly to `main`.
- CalVer `YYYY.M.D` (the release date; the last number is bumped while that tag
  exists). The tag is the version: `make release` tags `origin/main` and pushes
  only the tag; the release workflow's PR bumps `Cargo.toml`. Never edit the
  version by hand.
- Conventional Commits (`feat(compare): ...`, `fix(openapi): ...`).
- MSRV tracks latest stable and is pinned in `rust-toolchain.toml`. Keep
  `channel` identical to `rust-version` in `Cargo.toml` (CI fails if they drift).
- `unsafe_code = "forbid"`.