Skip to content
Toolkit

ADR-0143 — The contract is measured against the struct before anything is generated from it

Status: Accepted · Date: 2026-08-31 · Supersedes: —

Context

ROADMAP's standing repairs carry one line from ADR-0030 rule 1: promote shared/contracts to a generated-types package the web client consumes, so API drift is a compile error rather than a CI finding. The other three instances of that rule have landed — packages/product-model, packages/block-model, packages/capabilities — and each generated types from a file that was already true, because each was minted alongside the code it described.

shared/contracts/openapi.yaml was not. It was written early, and eight tracks have added columns since. Nobody has checked whether it still describes what the server sends, and the web client did not force the question: apps/web/src/types/api.ts is hand-written, so the two documents drift independently and neither notices.

They have drifted eighty-one ways across six schemas, and 52 of 133 properties agree.

The disagreements are not cosmetic. The contract calls a link's identifier link_id, a block's biolink_block_id and a QR code's qr_code_id; the wire has said id for all three since the port. It declares User.api_key as a string property — the struct marks that field json:"-", because what the column holds is a SHA-256 digest and handing one out would be a credential leak the document invites. It names four datetime fields the wire calls created_at. And it has never heard of lifecycle_state, archive_url, last_used_at, pixels_ids or workspace_id — 007 through 011's work, and the tenant boundary.

Generating a client from this document and consuming it would not catch drift. It would compile the web application against an API that does not exist.

Decision

Measure first, generate second — and the Go struct is the truth.

npm run check:contract-parity compares each gated schema's properties against the json: tags of the struct that serves it, and holds three rules:

  • C1. Every property a gated schema declares is served by its struct — a json: tag with that name, and never json:"-". A property that is neither served nor a ledger row fails the build.
  • C2. Every field the struct serialises is declared by its schema, or is a ledger row. This is the direction the drift actually travelled.
  • C3. Every ledger row still names a live disagreement, and names a gated schema. A row whose drift is closed is deleted in the commit that closed it.

C3 is what makes this a ratchet rather than an allowlist, which is ADR-0141's rule one subject over: a row is by definition open, there is no closed status, and with C1 and C2 refusing new drift the count can only fall.

The direction of truth is not a coin toss. The server serialises the struct, so the struct is what a caller receives; a contract that disagrees is wrong about an observable fact. This decides every future slice: a row closes by correcting the document, unless the wire itself is the defect and changing it is a breaking change under /api/v2/ (AGENTS.md), never in place.

Six schemas are gated, each an object schema one struct serves whole: User, Link, BiolinkBlock, QrCode, Plan, Payment. APIResponse, Error and PaginationMeta are the envelope — one document, three schemas, deliberately flattened for readers, with no single struct to compare field-for-field. LinkType is an enum and check:link-types already holds it.

Nothing is generated until the count is zero. The package this repair is named for is the second act, not the first, and it may not begin while the document is untrue.

Consequences

The repair is finite and visible. It was one ROADMAP line nobody could scope; it is 81 rows and a number in the success line — 52 of 133 properties agree. Every future slice moves that number or the build says nothing changed.

Landing the gate changes no served byte, deliberately: 81 contract edits in one commit would be 81 unreviewed claims about an API, and some of them are questions rather than corrections. Whether User.api_key should be deleted from the contract or the endpoint should serve a fresh key on regeneration is one; whether Link.short_url and Link.name are fields the wire ought to have is another. Those are slices with their own reasoning, not a find-and-replace.

The paths are not gated, only the schemas. Whether every route in the document exists, and whether every route in the router is in the document, is the larger half of the same finding — and it would fail on the generator at apps/api/scripts/gen_openapi_paths.py rather than on the schemas. It stays an open item under the same standing repair.

Watched failing five ways before it was trusted: a contract property nothing serves (C1), a struct field the contract never hears about (C2), a ledger row whose disagreement was closed (C3), a row naming an ungated schema (C3), and the direction a real regression travels — a served field silenced with json:"-" while the document still promises it, which is the api_key defect arriving by accident instead of by history.

Source: docs/decisions/0143-the-contract-is-measured-against-the-struct-before-anything-is-generated-from-it.md

Library