DagrA guide to binary formats, and to Dagrdraft

19 Chapter nineteen

How Dagr was built: a specification, golden bytes, and an AI

A serialization format is a small idea and a large amount of work. The same rules have to be written out again for every language, for writing and for reading, and all of the copies have to agree to the byte. Dagr has ten of them. Nearly all of that code was typed by an AI coding agent, Claude Code, directed by one person. This chapter is about the method that made the result trustworthy, with figures taken from the project's history.

The problem with ten implementations

Dagr is a schema language and a set of code generators. From one schema it writes a library in Swift, Rust, TypeScript, Go, C++, C, Java, Mojo or Odin, and it has a reference implementation in Python. Each must write the same bytes for the same value, and read what any of the others wrote.

Writing the tenth is not interesting work. It is the ninth again in different syntax, with the four layouts of chapter 5 multiplied by every kind of field, by optional and required, by several ways of reading. A person tires of it and starts to make mistakes. That is exactly the kind of work an AI agent is suited to, if there is a way to know that what it wrote is right.

So the method is about that second half. Three things carry it.

One: write it down first

Nothing is built in Dagr before it is written down. The repository has a directory of numbered documents. Some are the specification of the format itself, each rule with its reason. The others are plans: before a new language is started, a document says what will be built in what order, which decisions are open, and how each step will be tested.

This would be good practice with any team. With an AI it is a necessity, for a reason that is easy to overlook: the agent remembers nothing. Every working session starts from zero. What it knows about the project is what it can read at that moment. So the plan has a progress log at its end, kept up to date as the work proceeds, and a short file of standing instructions says what must never be got wrong and where each topic is written up.

The documents are the project's memory. When the code and a numbered document disagree, that is treated as a defect in one of them and resolved, and not left as prose that has gone stale.

Two: golden bytes

A specification in English can be read two ways. Bytes cannot. So beside the documents there is a body of test cases, each a small schema, a value, and the exact bytes that value must become.

The test is a comparison of bytes, and that makes it a good match for an AI. The answer is yes or no. It cannot be argued with, and nobody has to judge whether the output looks reasonable. When it fails, it says at which byte, which is usually enough to find the cause. And it turns "implement the format in Java" from a matter of judgement into a task with an end: the work is done when every case matches.

Earlier chapters met this already. The panels in this guide are small writers in JavaScript, and each is checked in the same way against Dagr's reference before it is published.

Three: tests that test the tests

Identical bytes show that valid data is handled correctly. They say nothing about data that is not valid, and a reader in C or C++ that trusts a length it was given will read memory it should not.

The second of these matters more with an AI than without. An agent asked to make tests pass will sometimes write a test that cannot fail. A mutation check does not look at how a test reads. It looks at whether the test notices the fault.

What the history shows

The figures below are read from the repository's commits by a script. Choose the whole project or one of the four languages that were added most recently.

    Read the times with some care.

    Each of these ports is several thousand lines of generator and several thousand of runtime library, tested on the whole corpus. That it now takes days is not because the agent types quickly, although it does. It is because by the fourth new language, the plan had a known shape, the tests existed, and the specification had been corrected by three earlier attempts to follow it.

    What a new language finds

    An implementation written from the specification is also a test of the specification, and of the older implementations.

    What the tests did not catch

    An honest account has a list of these as well.

    The pattern in what followed is the useful part: each of these became a test. A fault that is found is turned into a case that every language must pass from then on, so that it is found once.

    Who does what

    Almost every commit in the history names an AI model as co-author. That does not describe how the decisions were made.

    The same holds for this guide. Its text and panels were written with the same agent, under the same rule: a panel is published when its bytes match the reference.

    What carries over

    Little of this is special to serialization.

    What changes with an AI is the price of thoroughness. A corpus of a thousand cases, a sweep over every damaged byte, a tenth implementation, a tool for looking inside a file: each was too much work to be worth it for one person, and is now a few days. The last chapter is about the tools that came out of that.

    What this panel is.

    Try it

    1. Look at Java's steps. Which step followed the one before it most quickly, and what might the earlier one have made easy?
    2. C was started with the shared buffer and Java with the reader. Find the first step of each. What might decide the order?
    3. Compare the length of each plan with the size of what was built from it. Which language needed the longest plan?
    4. Think of a project of your own. What would its golden bytes be?
    5. A test passes before and after a safety check is removed. Name two different things that could mean.

    What comes next

    Chapter 20, the last, returns to the question the guide began with. Text formats won on convenience: you can open them, search them, compare them. It shows the tools that give a binary format the same, and that were affordable to build for the reason this chapter gave.