Documentation that checks itself against the cartridge
This post is part of a series on Sailor Moon S - FrenchName edition. It digs deeper on a topic from It’s 1994 and Sailor Uranus is broken.
On 3 September I was reading one of the explainer pages the agent had drawn for this project, one frame, as the cartridge runs it, and I stopped on this sentence:
“Three idioms account for most of the surprise”
There is so much wrong in that single sentence that I didn’t know where to start. So I asked the agent to explain it to me. The agent went and found the sentence in the page generator and proceeded to answer. To its credit it did correctly remark the sentence was “badly written”, and listed three faults.
The first two were lack of context/glossary (out of context use of the word “idiom”) and lack of focus (the document was supposed to be a description of a frame, not a post-mortem on the work); the the third one can be described as unmeasured data claimed as factual truth and is at the core of this post:
“Account for most of the” sounds quantitative and really isn’t. Nobody counted surprises. In a project whose first rule is that every number comes from a run, a sentence borrowing the cadence of a measurement without being one is at best a very bad habit and at worst a bad attempt at making believe your statements are rooted in facts.
A coding agent writes confident prose at exactly the speed it writes correct prose. And possibly because it’s trained on human materiel and tries to maximize its usefulness, it’s prone to cut corners in an attempt to please more.
In documentation, the two look the same. This post is about the roughly 400 KB of documentation of the original game that came out of this project, and the machinery that makes it hard for a sentence like that to survive.
(Unexpectedly) prose was strong. Tests however…
Outside of code, my feedback of the agents after two months was lopsided. Claude’s documentation and explanations, written on demand, were remarkably readable, given proper guidance and feedback. Its tests however were much weaker, which felt strange given that in the end they could be boiled down to scripts and coded instructions. And they kept depending on wording and internal names that had nothing to do with what was being tested:
- The health script decided whether the doc checker had passed by grepping its output for
ALL PASS (...). The checker colourised its output, so an ANSI escape code sat inside that phrase. Health reported FAIL while the checker passed! (d20ae9a) - A training-mode test counted lines of output to report how many checks ran. In one instance, ‘14 checks’ were actually 4 real + 10 DBG”: literally debug prints were being counted as checks without any justification (
6ee181e). - A hand-pinned byte in the regression suite’s detection table “silently skipped eleven tests for a week” when a stub moved.
- Issue #2 was titled “Regression suite always exits 0 — the documented shipping gate cannot fail”.
I can only assume that at least part of the problem came from the fact that the harness had few proper hooks and scripted parts from the start and most of what was organically built to address the needs was ultimately built on English requirements, not algorithmic premises. Did it read better than most of gstack? I’d say so but that tells you more about Claude’s improvements than my own.
This being said, when given proper directives not just on the goal of the documentation but on how it should read and look and how it should be tested, things shifted dramatically. And as usual, if you can it’s better to set up guidelines and guardrails from the start but if you can’t just remember to improve all of your project and not just the one occurence you detected. After all, unlike humans, agents don’t mind spending ressources on alledgedly boring stuff!
Most of what follows is about closing my measurement gap: making the documentation itself testable, so that a test asks the cartridge, not the wording.
Two halves, one rule
The first decision was where documents go. On 8 August docs/ was split in two, with a one-line rule (docs/README.md):
The rule of thumb when adding a file: would this still be true, and still useful, to someone who had never heard of this project? If yes, it belongs in
game/.
docs/game/ is about the retail cartridge, true whatever anyone patches. docs/project/ is my edition: patch notes, builds, decisions, the Sailor Saturn backport. The test sounds trivial. In practice it forces each sentence to say which thing it is about: the game, or my changes to it. (The same question later shaped the generic test harness that came out of the next project.)
The centrepiece of game/ is sms_data_architecture.md. It is organised by the console’s four memories (ROM, work RAM, video RAM, audio RAM), not by the order we discovered things, and its first section is the one idea a stranger needs: “This engine is data-driven.” A character is a manifest, box tables, animation tables and a palette, and generic interpreters walk them. Next to it sit the engine internals, the damage system, the stat system, every special move of every character, and one generated page per fighter.
What a “memory map” is. The console sees ROM, RAM and the video and audio chips through addresses. A memory map says which address range holds what, for example “the two fighters’ data structures start at
$7E:1000and$7E:1080”. With no source code, the map is the documentation.
The holes (acknowledging what is unknown )
An important section of that document is the last one, §13, “The holes — what is genuinely not known”:
A map that hides its gaps is worse than no map. These are the parts of the cartridge this project cannot yet read, listed so nobody re-derives the same dead ends.
It lists a compression codec nobody seems to have publicly documented: about 180 KB of graphics after the last fighter, a bank that is 87% 00/FF with no pointer into it, and about a third of the object structure that every fighter and projectile lives in.
That last row carries its own retraction. Bytes +0x79/+0x7A used to be in the “unmapped” list. In August an experimental build parked a timer there, because unmapped looked like free. A write census and a full-session watch then showed the engine uses those bytes as a per-object hit counter (e1b9809). The row now says so, with a warning: that is “what ‘unmapped’ invites”. The section ends by flagging one of its own entries as “inferred from bytes alone — this project has been wrong about exactly that kind of evidence before.”
checkdocs: 31 checks, then five dead facts
The documents had been rewritten and reorganised all through 8 August. That afternoon, re-measuring for the data-architecture document had already disagreed with the older docs in five places (36a376b). Then I asked the question that produced tools/checkdocs.py. The commit message frames it better than I did (769b536):
when documentation is reorganised and rewritten, is every fact still measured, or has an elegant-sounding sentence replaced a measured one somewhere? Re-reading my own prose is the weakest possible check on that, so this re-derives the claims from the ROM instead.
It started with 31 checks over six documents. Each check has three steps:
- Assert that the claim is still in the document, quoted. If someone edits the doc, the check fails loudly instead of testing a claim nobody makes any more.
- Derive the same fact from the cartridge.
- Compare.
All 31 held. The first error it found was in the checker itself, not the docs.
Forty-two minutes later it had implemented 76 checks and a new rule. Every documented table gets a validator for its shape, and that validator is also run at the wrong address and must fail there. “The pointers look plausible” survives a two-byte shift; a stride or an ordering does not. Writing those checks meant re-deriving every documented table, and five documented facts did not survive (0d4bbfd):
- A special-move record is 7 bytes, not 8. The “field” at byte 7 was the next record’s attack ID.
- An instruction was documented two bytes late, “in four places across three documents”. It came from the commit that proved there is no randomness in damage (post 4).
- A
jmpwas really ajsr. - A screen engine draws eight screens, not nine.
- One velocity was written as its raw 16-bit word instead of its signed value.
Two days later it passed 200 checks. Today a health run counts 249 checks across 20 documents. The tool also prints what it cannot check, “prose, reasoning, causal claims (‘this is why X’)”, so that, in its own words, “the green line is not over-read”.
One judgement from that growth is worth keeping. The cheap way to cover more addresses was to make each doc quote the instruction at each address it names, then check the quote. The agent declined: “a quote stamped from the address it is meant to test is a tautology”. It can catch rot later, but never an address that is already wrong. Only claims where a human supplied independent redundancy catch present errors.
Why “fail at the wrong address” matters. A test that passes on correct input proves little. If it would also pass on wrong input, it proves nothing. Mutation testing formalises this: plant a fault and make sure the test notices.
A disassembler checked by someone else’s disassembler
Many checks need to decode 65816 instructions, so the decoder became a single point of failure. On 10 August it was compared with DisPel, a disassembler by a different author, and they agreed on 5,051 consecutive instructions across nine ROM ranges (befd7a9). Two of the nine ranges are data, on purpose, because “data exercises rare opcodes that hand written code never reaches”. This is differential testing: two independent implementations, and you look at where they disagree.
The same commit records that the first two negative controls were wrong. One passed vacuously because every sampled range began with an instruction that reset the very thing the control had corrupted. The other perturbed an opcode the workload never reached. Both now pick their target from what the workload actually executes.
The confession
Back to the evening of 8 August. Right after checkdocs landed, the agent committed the ANSI-colour fix above, with this:
Worth recording that I committed and pushed the previous change with health failing, because I read the commit as done once the push succeeded rather than re-running the gate after wiring it in.
git commitdoes not run health.sh — nothing does but me.
Of note: the repo did have a CI health workflow, since 6 August. But a hosted runner has no ROM, so every cartridge check is reported as SKIP there. The workflow says so at the top:
a green tick here means “the tree is self-consistent”, not “the builds are good”.
Not putting in place a check around this limitation was definitely a failure on my part. However it is worth noticing that, especially for processes that are not your super usual web development kind, the agents really saw no problem with the situation. Could they have detected the issue? I’m confident they could have… if and only if specifically directed to; hence the need to put in guardrails and guidelines early and propagating them throughout the project when they’re introduced.
Drawn pages, and the feedback that shaped them
Two of the documents are also published as drawn pages, built by committed generators and rendered by CI on every push:
- The data architecture, from
tools/mkarchpage.py. The geometry is computed, “never hand-typed … hand-authoring SVG coordinates is how off-by-one diagrams happen”, and the accent colour is read from Sailor Uranus’s palette in the clean ROM. - One frame, as the cartridge runs it, from
tools/mkenginepage.py. It draws the in-match loop stage by stage, with every patch pinned to the stage it hooks. Its--checkre-reads every instruction it draws from the ROM. Three stages are drawn as grey boxes labelled UNIDENTIFIED, because “a grey box is worth more than a confident guess.”
These pages are where “given proper guidance and feedback” shows most clearly in the git log. On 8 and 9 August:
- The architecture page opened with “HiROM, FastROM, no copier header”. “Maintainer read it and asked”, and it gained a footnote that explains the address model from scratch (
873ff33). - The footer claimed the page used “Sailor Uranus’s own sixteen colours”. When I asked what that meant, the agent checked the ROM and found only some of them were hers (
853c2d6). - The frame page’s first cut had two reported formatting faults: a label running out of its box, and a row of patch markers landing in the next stage. Rather than nudge coordinates, the generator now measures every string and refuses to write a page where text overflows or boxes overlap (
915632c).
One final word
There is one last benefit I didn’t touch upon because it felt obvious: regenerating your documents’ data from directly from the source of truth means one less source of rot: the document might rot eventually but at least each part of it that refers to data stays correct and relevant and that’s no small victory in my book.