Skip to content

Hold every protocol and record schema change to a JSON schema baseline - #3263

Merged
max-sixty merged 3 commits into
mainfrom
protocol-schema-baseline
Oct 20, 2026
Merged

max-sixty merged 3 commits into
mainfrom
protocol-schema-baseline

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

The protocol check compared TypeScript generated from the schemas with a snapshot of that TypeScript (protocol-contract.d.ts). TypeScript drops pattern, length and value bounds, uniqueItems and conditional clauses, so tightening one of those in $events, the served state or schema.VERB_SCHEMA passed the check while a page vendored at the same protocol started failing. Records kept outside a page (#3244) had no check: nothing stopped a state.Record schema changing while its since stayed put.

What changes

The baseline is now the schemas themselves, build/browser/protocol-baseline.json: $events, the served state, the x-state verb and every state.Record with its since, as they last stood. build/browser/protocol.mjs walks it beside the current schemas and names each place where the side that must accept the other's values no longer would:

  • what a vendored page sends or holds (browser commands, log events): the current schema takes everything the baseline did;
  • what Leaf serves the page (served state, view answer): the baseline takes everything the current schema allows, so an event record the page is also served holds both ways;
  • a record: both, while its since is unchanged; a since may move only to the current protocol, and never past it.

A field the old schema declares, at any depth and in any branch, and the new one lacks breaks either way. Tightening is a narrower type, enum or const, a changed pattern or format, a raised minimum or lowered maximum, a newly required field or dependentRequired entry, uniqueItems, a field newly named on a map that the map's schema wouldn't take, or, at the browser door (the only consumer that validates), a closed object that stops naming a field, a newly closed object, or any change to a oneOf. The walk throws on any keyword it doesn't read, compares patterns and the conjunctive keywords (allOf, not, if/then/else, dependentSchemas) only for equality, and refuses to compare a conjunct holding a $ref.

The baseline is a build output: pnpm run build:browser refuses a breaking change and otherwise moves it forward, and check:browser names it stale until rebuilt, including after a record's schema changes in Python. On the real tree, tightening comment.anchor.source's pattern fails with event comment.anchor.source: pattern "^[a-z][a-z0-9-]*$" → "^[a-z]+$" plus each served path that carries it; changing the claim record's cwd type fails with record claim.cwd: type ["string"] → ["integer"]; adding an optional field to an event record or a served object passes.

protocol.test.ts and protocol-contract.d.ts go, since the walk covers their dropped-field and type checks; contract.d.ts is still generated, from the same protocolSchemas reading. state.RECORDS lists every declared record for the build to read. schemaReference is the one parser of the served state's references, read by both the type generator and the walk.

No maintained dependency fits: json-schema-diff reads neither pattern, enum nor const, and is-json-schema-subset was last released in 2022 on ajv 6.

Behaviour to know

Because event records are also served, any bound or enum change on one now needs a bump, in either direction, as type changes already did; so does adding an event kind, since the served state carries every kind. Changing a oneOf in anything the browser sends needs a bump even where it loosens.

This was written by Claude Code on behalf of max-sixty

The protocol check compared TypeScript generated from the schemas with a
snapshot of that TypeScript (`protocol-contract.d.ts`). TypeScript drops
`pattern`, length and value bounds, `uniqueItems` and conditional
clauses, so tightening one of those in `$events`, the served state or
`schema.VERB_SCHEMA` passed the check while a page vendored at the same
protocol started failing. Records kept outside a page had no check at
all: nothing stopped a `state.Record` schema changing while its `since`
stayed put.

The baseline is now the schemas themselves,
`build/browser/protocol-baseline.json`: `$events`, the served state, the
x-state verb and every `state.Record` with its `since`, as they last
stood. `protocol.mjs` walks it beside the current schemas and names each
place where the side that must accept the other's values no longer
would:

- what a vendored page sends or holds (browser commands, log events):
  the current schema takes everything the baseline did;
- what Leaf serves the page (served state, view answer): the baseline
  takes everything the current schema allows, so an event record the
  page is also served holds both ways;
- a record: both, while its `since` is unchanged; a `since` may move
  only to the current protocol, and never past it.

A field the old schema declares, at any depth and in any branch, and
the new one lacks breaks either way. Tightening is a narrower type, enum
or const, a changed pattern or format, a raised minimum or lowered
maximum, a newly required field or `dependentRequired` entry,
`uniqueItems`, a field newly named on a map that the map's schema
wouldn't take, or, at the browser door, the only consumer that
validates, a newly closed object or any change to a `oneOf`, whose
exclusivity a branch-by-branch comparison can't show. A union's shared keywords, a reference's siblings and a
command's record join each branch, target and browser contract as an
intersection. The walk throws on any keyword it doesn't read, compares
patterns and the conjunctive keywords (`allOf`, `not`,
`if`/`then`/`else`, `dependentSchemas`) only for equality, and refuses
to compare a conjunct holding a `$ref`.

The baseline is a build output: `pnpm run build:browser` refuses a
breaking change and otherwise moves it forward, and `check:browser`
names it stale until rebuilt, including after a record's schema changes
in Python. The generated types stay (`contract.d.ts`, built from the
same `protocolSchemas` reading), while `protocol.test.ts` and
`protocol-contract.d.ts` go: the walk covers their dropped-field and
type checks. `state.RECORDS` lists every declared record so the build
can read them, and refuses two records declaring one kind.
`schemaReference` is now the one parser of the served state's
references, which both the type generator and the walk read.

No maintained dependency fits: `json-schema-diff` reads neither
`pattern`, `enum` nor `const`, and `is-json-schema-subset` was last
released in 2022 on ajv 6. Neither knows Leaf's references or that a
field the old schema declares must survive.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two changes to accepted browser events pass the new compatibility check without a protocol bump. Both leave a page vendored at the same protocol able to send an event the current admission rejects.

Comment thread build/browser/protocol.mjs Resolved
Comment thread build/browser/protocol.mjs Resolved
Two edits to what a page sends passed the protocol check while admission
would refuse an older page's event. Deleting a field from one closed
`anyOf` variant while another variant keeps it left the field in the
branch-agnostic retention set, and the walk ignored a producer field the
consumer's object doesn't name. Closing an open object that names some
fields passed too, since the walk took such an object to hold only its
named fields.

At the browser door, the one consumer that validates, a closed object
now refuses each field the old schema named and it doesn't, and closing
an object the old schema left open breaks whatever fields it named.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new closure check still misses a browser event value the old schema admitted, so this change needs a regression case before the protocol gate can be relied on.

Comment thread build/browser/protocol.mjs Resolved
A schema-valued `additionalProperties` evaluates the fields it matches,
so `unevaluatedProperties: false` beside it closes nothing. The walk
read such an object as closed, so changing its map to `false` at the
browser door passed though admission would then refuse a field it took.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit d057b0e into main Oct 20, 2026
5 checks passed
@max-sixty
max-sixty deleted the protocol-schema-baseline branch October 20, 2026 07:03
max-sixty added a commit that referenced this pull request Oct 20, 2026
…alone (#3282)

The composer's draft in browser storage carried `leaf-draft/<protocol>`,
and a runtime reading a draft at another format keeps its words but
drops its drawing, so every protocol bump dropped the drawing of every
unsent draft, whatever the bump changed. #3244 fixed the same problem
for `state.Record`s; this does it for the draft.

The draft now carries `$events.draft_since` (registry.json), the
protocol at which its drawing last changed shape. `runtime/protocol.js`
exports `DRAFT_FORMAT` generated from it, replacing the
`recordFormat(kind)` only the draft used.

The protocol baseline (#3263) holds it like a record: `protocolSchemas`
adds a `draft` entry whose schema is the part of a draft its format
guards, the comment record's `drawing` plus the `at` placement a draft
adds to it. Changing the drawing's shape while `draft_since` stays put
fails the build (`record draft.drawing.says: maxLength 500 → 100`);
moving `draft_since` to the current protocol clears it.

The draft's `at` schema is written in `contract-types.mjs` beside a
pointer to `captured` in `runtime/composing/drawing.js`, its writer,
since no schema declared it before.

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 20, 2026
The protocol check from #3263 compared schemas semantically: it resolved
references, intersected a union's shared keywords into each branch,
matched each value to the closest branch, guarded recursion, and ran a
separate field-retention walk. Each review round found another hole in
that machinery, and `protocol.mjs` reached 549 lines. This replaces it
with a positional comparison: **549 → 314 lines**.

## How it compares

Each baseline schema sits beside its current form and is compared
keyword by keyword:

- A reference compares by its text; its target is compared where it is
declared. Served `$defs` compare by name, and each event record as its
own root, both ways, since the served state carries every kind.
- Union, `allOf` and `prefixItems` entries compare by index, and a union
on one side only compares as a union of one.
- A browser contract compares as an overlay that narrows its record.
- A field inside `prefixItems` is refused, so a field's path names no
tuple position; no protocol schema has one.
- Bounds, types, values, `required`, maps and closedness follow the same
direction rules as before. `pattern`, `format`, `not` and
`if`/`then`/`else` compare only for equality, and so does `oneOf` at the
browser door.

## What changes in behaviour

The comparison is conservative where the walk was exact. Reordering a
union's branches, or replacing a reference with its target inline, now
fails. A changed definition is reported once, where it is declared,
rather than at every served path that reaches it.

## Evidence

- Every fault the earlier walk's tests caught still fails a test: 22
single-line mutations of the new file each fail at least one test.
- The current tree compared with itself passes, as does adding an
optional field to an event record, a served object or a record.
- Two Codex reviews of drafts and the PR reviewer found cases the
positional comparison first got wrong; each is fixed and has a test:
  - a contract that drops a field leaves it to its record;
  - a contract may bound at once a field that is new to its record;
- a dropped served branch takes with it the fields no other branch
names, at any depth;
  - a position `prefixItems` stops naming holds what `items` allows.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants