Skip to content

fix(baseline): build the canonical corrections form as JSON.stringify does - #109

Merged
bketelsen merged 3 commits into
mainfrom
lucilla/corrections-canonical-bytes
Nov 14, 2026
Merged

bketelsen merged 3 commits into
mainfrom
lucilla/corrections-canonical-bytes

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The B2 baseline check (tests/baseline/baseline_test.go) ports core's scripts/check-corrections.mjs (core ADR-0081). It built the canonical bytes with encoding/json, which breaks the ADR-0081 byte rules:

  • encoding/json always escapes U+2028 and U+2029; JSON.stringify writes them as themselves. A canonical record with a raw U+2028 failed here, and an escaped \u2028 passed.
  • json.Unmarshal decodes a lone surrogate escape (\ud800) to U+FFFD; core keeps the code unit and writes it as \udxxx in lowercase. A canonical record with \ud800 failed here.
  • Member names were decoded the same lossy way, so 2 different lone-surrogate names looked like a duplicate key.

The check now walks the top-level members itself (after json.Valid), decodes each string to UTF-16 code units as JSON.parse does, and writes the JSON.stringify bytes by the ADR-0081 rules (only ", \ and characters below U+0020 escaped, a lone surrogate as \udxxx, everything else as UTF-8). encoding/json no longer makes the canonical form. Standard library only.

Risk tier

Risk tier: 1 — test-only package (the baseline contract test) and its cases. No risk path is touched.

Verification

  • make check green (fmt, vet, lint, docs, unit tests).
  • 1 table case per byte rule, a raw form that passes and an escaped form that fails: U+2028, U+2029, lone lead and trail surrogates (\ud800 passes, \uD800 fails), emoji raw and as an escaped pair, <>&/ and \u003c, \/, U+007F, \^_ and \^_, \n and \u000a, é and \u00e9; and 2 lone-surrogate names that give "unknown key(s)" and no "duplicate key".
  • Each case agrees with core's check-corrections.mjs at 3d3d5c703fc24db3734408765171938a97840f4c (the same tree, node check-corrections.mjs --root <tree>). The 1 known difference is on purpose: B2 requires the folder; core passes a tree with no records.
  • Revert test: with the old encoding/json code, 7 new cases fail (raw and escaped U+2028 and U+2029, \ud800, \udc00, the 2 names).
  • The real .memory/corrections/ passes in Go and in core.
  • Blocking review point fixed: guard added (the cases above) and 1 file added under .memory/corrections/.
  • make ci: CI runs it.

Cross-review

… does

The B2 port made the canonical bytes with encoding/json, which always
escapes U+2028 and U+2029 and decodes a lone surrogate escape to
U+FFFD. Core check-corrections.mjs writes U+2028 and U+2029 as
themselves and a lone surrogate as a lowercase \udxxx escape (core
ADR-0081), so the port refused canonical records that core accepts and
accepted escaped forms that core refuses. The port now decodes strings
to UTF-16 code units and writes the JSON.stringify bytes itself, and
decodes member names the same way for the duplicate check.
@github-actions github-actions Bot added the level:L3 Skim label Nov 14, 2026
Core check-corrections.mjs fails a symbolic link at .memory before it
reads either form (linkedMemory). The B2 port checked links only at
.memory/corrections and its entries, so it followed a linked .memory.
Also add the cases for an escaped U+007F, an escaped required name, a
duplicate written as a required name and its escaped form, and a nested
non-string value.
@bketelsen
bketelsen added this pull request to the merge queue Nov 14, 2026
Merged via the queue into main with commit 7db10e9 Nov 14, 2026
12 checks passed
@bketelsen
bketelsen deleted the lucilla/corrections-canonical-bytes branch November 14, 2026 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants