Skip to main content

The five correction layers

How I make AI agents write code and docs to my standard without reading every line.

Updated 9 min readMarkdown
Part of seriesKarmi
The five correction layers

A bug that became a lint rule

On Sept 21st an agent was testing the Fork scenario on a deployed copy of the Playground, the example app that runs every karmi feature against a real model. A Fork copies a conversation (a Thread) so that it can go another way. Upload worked. The model's reply worked. Then "Fork the Thread" failed with an HTTP 500.

The cause was small. A Fork copied the Thread's whole event log in one SQL INSERT. A Durable Object refuses a statement with more than 100 bound values, and each event row binds five. So any log longer than 20 events could not be forked. A real model streams many small events, and the run that failed had 137 of them. The tests had only ever used short logs, so they all passed.

The fix was to write the log in batches. If the PR had stopped there, the same mistake would have been waiting in every other query that writes a list of unknown length. An agent copies what it sees in the codebase. One day it would write that query again.

So the same PR added a second commit. It put the limit in one helper, boundBatches(), and added a lint rule that refuses the risky shape. This is the rule, from the public repo:

const boundValues =
  "One SQL statement of a Durable Object binds at most 100 values. Split the list with boundBatches() from db/bound-values.ts.";

/** A list of unknown length must not reach one SQL statement, because each entry binds a value. */
export const boundValuesSyntax = [
  {
    selector:
      "CallExpression[callee.property.name='values'] > :matches(ArrayExpression:has(SpreadElement), CallExpression[callee.property.name=/^(map|slice|filter|flatMap)$/]).arguments",
    message: boundValues,
  },
  // ...the same check for inArray() and notInArray()
];

The message is written for the next agent. It says what the limit is and where the fix lives. No one has to remember this bug, because ESLint remembers it.

Five ways to correct an agent

That bug touched three of the five places where I correct an agent. Lauren Tan describes these five correction layers in her talk on shipping with coding agents. She ranks them by how little each one depends on someone reading a prompt. Here is how they look on karmi.

Five boxes in a row, strongest first: architecture, static analysis, rules, human review and verification. A dotted arrow runs from verification to a bug, "A Fork of a long Thread fails on the deployed Playground". A thick arrow runs from the bug back to static analysis, labelled "fix, then a lint rule so it cannot come back".

1. Architecture: make the wrong thing impossible

The strongest correction is code where the bad pattern cannot be written at all.

karmi serves many customers from one deployment, and each customer's data lives under a Scope. If an agent could build a storage name by hand, a single typo could leak one customer's data into another's. So user code never builds a storage name. One internal keys module mints every Durable Object name and storage key, and you can reach it only through the karmi.scope(id) handle. ADR 0001 is blunt about the cost. A bug in keys is a cross-tenant bug, so the module gets tests of its own.

The same idea runs through the rest of the core. Every Durable Object reads and writes storage through a typed db layer built on Drizzle. A Thread's state comes from one event log, so there is one source of truth. An agent working inside these modules has one obvious path, and it is the right one.

2. Static analysis: fail the build, not the reviewer

Some rules cannot live in the architecture, so the build enforces them.

The first batch came from a review I asked for on Sept 11th, a week into coding. I asked an agent to read the whole TypeScript codebase and list the bad habits that repeated. It found twelve. There were 127 lines longer than 160 characters, 40 error codes typed as loose strings in mixed casing, and unchecked casts in many places. I asked for tooling that blocks whatever tooling can block, and short guidelines for the rest.

That PR added Prettier, shared ESLint rules (no non-null assertions, no double casts, no JSON.parse(...) as T, no function over 60 lines) and a typed union of error codes, so a made-up code fails the type check. The first version grandfathered the 54 existing violations. I asked for every one of them to be fixed before merge instead, and only one commented exception stayed. The header of the guardrails file still sets the terms for the next rule. Existing violations get grandfathered, and then they are fixed, never added to.

More rules followed. On Sept 14th JSDoc became required on every export, and the same PR wrote the 310 missing blocks. On Sept 19th, after storage moved to Drizzle, a rule started to block raw SQL outside the db modules.

Docs are code too

Agents wrote nearly all of karmi's docs too, and prose drifts the same way code does. So the docs go through the same layer.

The writing rules are based on the rules of ASD-STE100, a controlled English standard from aerospace maintenance manuals (not its dictionary). Sentences stay short, each word has one meaning, and verbs are active. Vale runs those rules in CI. The rule I like most is generated. karmi's glossary marks words to avoid for each domain term, and a script turns those marks into a Vale rule. If a doc says "background task", CI fails and asks for "Job". If it says "cron job", it asks for "Schedule". The docs cannot slowly invent a second vocabulary.

Getting there took some tuning. The first version also banned single words such as "run" and "function", and that gave 191 false hits in the live docs. Now the rule matches only multi-word phrases. Sentence length and passive voice are warnings, not errors, because "the Turn is parked" is the clearest way to name a state.

The rest of CI checks the docs as well. lychee checks every relative link and anchor. A script type-checks each of the 61 TypeScript samples in the guide against the built packages. A generated llms.txt index of the docs must match the guide, or CI fails.

3. Rules: guidance an agent can skip

Some standards are matters of judgement, and a linter cannot check them. Those go into written rules: a principles file for every contributor, plus guides for writing, comments and TypeScript. My favourite line in the principles file asks contributors to "fight for the smallest model that makes the correct behavior unsurprising".

I kept these files short and general. The first draft of the TypeScript guide was full of karmi's own type names and file paths. I had it rewritten as plain TypeScript practice with no project names in it.

Rules sit in the middle because an agent can skip them. A rule works only if the agent reads it and follows it. The 100-value limit from the Fork bug went into the TypeScript guide and into a lint rule. Only the lint rule is sure to be read.

4. Human review: find the gaps

Agent PRs were reviewed on two axes. Does it follow the repo's standards? Does it do what the ticket asked for? The review findings were fixed in follow-up commits on the same PR before merge, and commits titled "review fixes" appear all through the history.

Review does not scale, and I don't want it to be the thing that keeps the codebase clean. I think its real job is to find gaps in the layers above it. A finding that comes back twice belongs in a lint rule or a guide, not in a third review comment.

5. Verification: prove it on the real thing

The last layer proves the behaviour. The test kit runs the real runtime against a scripted model, so tests need no model and no credential. The Playground runs every feature against real providers, locally and in a temporary deployment.

The Fork bug shows why both are needed. The test kit was green, and the Playground still found the bug, because a real model sends more events than a test script does. Verification found it. A lint rule now prevents it.

What it gave me

My review time went to design, not to style. I did not have to point out a long line, a loose cast or a doc that said "cron job", because the build had already rejected them. When I read a PR, I read it for whether the idea was right.

It also made agent-written docs safe to publish without reading every line twice. The 16-page guide reads as if one careful person wrote it, because the same rules checked every page.

What it cost

The guardrails came late. For the first week of code nothing stopped a bad habit from spreading, and by Sept 11th twelve of them repeated across the codebase. One PR fixed them, but I would add a formatter and the strict lint rules on day one next time.

Not every check ran on every machine. The prose linter and the link checker were not installed where some agents worked, so for those PRs the checks ran only in CI. CI still enforced them before merge, but an agent found out later than it could have.

And the strong layers are code that I maintain. The term rule needed tuning, the move to Drizzle took two days of PRs, and every new lint rule needs a fix for each existing violation. That cost is worth it to me, because I pay it once. A correction in a chat window costs me again every session.

Put the correction where the agent will meet it

When an agent gets something wrong, ask where the correction should live. If you put it in the chat, it lasts one session. If you put it in a guide, the agent can skip it. If you put it in a lint rule, a type or the architecture, every later session meets it whether or not it reads anything. Start at the strongest layer that can hold it.


Code: github.com/dsblabs/karmi-oss. The guardrails file is eslint.guardrails.js.

Previous: Trust before parallelism. Next: coming soon

What correction do you keep repeating to your agents? Tell me in the comments, and I'll tell you which layer I would put it in.

"The message is written for the next agent" is the part I'll borrow. A lint error that says what the limit is and where the fix lives does the job of a foreman's note on the drawing: the next person doesn't need the history, only the rule and where to look. Coming from carpentry, the strongest correction is the same as your layer 1. A jig that only lets the cut go one way beats any amount of telling people to be careful. I also like that you refused to grandfather the 54 existing violations, since an agent copies what it sees. One question: how do you decide a rule in the guide has earned promotion to a lint rule? The first occurrence, or the second?