Skip to content

Compare protocol schemas by position, halving the protocol check - #3286

Merged
max-sixty merged 8 commits into
mainfrom
protocol-diff
Oct 20, 2026
Merged

max-sixty merged 8 commits into
mainfrom
protocol-diff

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

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

max-sixty and others added 2 commits October 20, 2026 00:31
`protocol.mjs` checked each schema change semantically: it resolved
references, intersected a union's shared keywords into each branch,
matched a value's branch to the closest consumer branch, guarded
recursion, and walked a separate field-retention pass. Each review
round of #3263 found another place where that machinery let a
tightening through, and the file reached 549 lines.

It now lays each baseline schema beside its current form and compares
them keyword by keyword. A reference compares by its text, and its
target where it is declared: served `$defs` by name, and each event
record as its own root, compared both ways since the served state
carries every kind. Union, `allOf` and `prefixItems` positions compare
by index, a union on one side only as a union of one, and a browser
contract as an overlay that narrows its record. That removes the
resolver, `intersect`, `command`, the recursion guard, the closest-branch
heuristic and the retention walk: 549 lines become 261.

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 reports once where it is declared, not at
every served path that reaches it.

Every fault the earlier walk's tests caught still fails a test, and the
current tree compared with itself passes. Review of drafts found four
cases this comparison first got wrong, now covered: a contract dropping a
field leaves it to its record; a contract may bound at once a field new
to its record; a served branch dropped takes the fields no other branch
names; and a position `prefixItems` stops naming holds what `items`
allows.

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 dropped-branch field check only inspects immediate properties. If a served anyOf ends with a branch containing payload.x, while an earlier branch contains payload.y, removing the last branch leaves payload named elsewhere and this check reports no break. I reproduced protocolBreaks returning [] for that change, although the old page can still read payload.x from that served variant. The field-retention check needs to follow nested paths within a removed branch, including when another branch retains only their parent.

A union branch the new schema drops took with it only the fields it
named at its own top level, so dropping a served variant whose
`payload.x` no other branch names passed while another branch kept
`payload`. The check now follows every field the dropped branch names,
through nested objects, items and branches.

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 dropped-branch check can still miss a removed field and report a field that remains available.

Comment thread build/browser/protocol.mjs Resolved
Comment thread build/browser/protocol.mjs Resolved
The dropped-branch check missed fields a branch names under
`prefixItems`, `dependentSchemas` or a map, and reported a field gone
that the union's shared schema still names. It now follows those
children, and counts the shared schema among what remains.

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 prefixItems path in fields can also report a field as gone when a surviving items schema still names it. With served anyOf branches [{items: {properties: {x: {}}}}, {prefixItems: [{properties: {x: {}}}]}], removing the second branch reports served state.pick[0].x: the field is gone; the first branch still covers x at index 0. The retention check needs to compare coverage of an array position by items as well as by prefixItems before requiring a protocol bump.

Comment thread build/browser/protocol.mjs Resolved
…a dropped branch

A field a dropped branch named at `prefixItems[0]` read as gone though a
surviving branch's `items` still named it, and a field named under
`if`/`then`/`else` went unread. Every array position is now one field
path, `[]`, and the conditional children are read like the others.

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 dropped-branch field check still misses a field removed at one array position.

Comment thread build/browser/protocol.mjs Resolved
… each

Reading every array position as one path let a field a dropped branch
named at `prefixItems[0]` pass as kept where a surviving branch named it
only at `prefixItems[1]`. Positions are distinct again, and a field a
surviving branch's `items` names counts at every position.

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 dropped-branch check still treats items as covering a position occupied by prefixItems. In a surviving branch with prefixItems[0] as a closed empty object and items naming x, removing a branch whose prefixItems[0] names x returns []: the items field is counted at index 0 even though it applies only after the prefix. The old page can lose pick[0].x without a protocol bump. I reproduced this with protocolBreaks; the field-retention comparison needs to account for where each branch's items starts, rather than replacing every numeric index with [].

Each fix to how a dropped union branch's fields at tuple positions are
kept opened another case: positions collapsed, then `items` covering a
position a prefix still held. No protocol schema puts a field in a
tuple position (`prefixItems` holds only the drawing's numbers), so the
comparison now refuses a field inside `prefixItems`, failing loudly, and
a field's path names no position.

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 tuple-field refusal misses a removed served union branch. diff returns early for that branch because its new schema is false, and fields no longer visits prefixItems, so removing a branch that alone declares pick[0].x returns [] without a protocol bump. I reproduced this with two served anyOf tuple branches, one declaring x and the other y. The refusal needs to run while collecting a removed branch's fields too.

Comment thread build/browser/protocol.mjs Resolved
The keyword check and the refusal of fields inside `prefixItems` ran
only on the nodes the comparison visited, so a union branch the new
schema drops, which it doesn't descend, escaped both. `readable` now
walks every compared schema once, at the edge, and the comparison
itself checks nothing.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 8e39f45 into main Oct 20, 2026
5 checks passed
@max-sixty
max-sixty deleted the protocol-diff branch October 20, 2026 09:18
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