
Tests as Guardrails for AI-Generated Code
Verification you commit first is a dependable check on agent mistakes — characterization tests, acceptance tests from the spec, and a locked set the agent cannot edit.
TL;DR: Verification you commit before the agent starts is a dependable way to catch agent mistakes. Characterization tests freeze the current behavior of code the agent will touch, acceptance tests turn each spec criterion into a failing test the agent must make pass, and a locked set of tests the agent cannot edit keeps it from weakening the guardrail tests themselves. An agent can still make tests pass trivially by changing code the tests depend on, so the implementation diff still needs a look. Tests written after seeing the output can inherit the output's assumptions; tests written first encode intent the output cannot argue with. Guardrails narrow the review — they never replace it.
Why guardrails precede delegation
Review is the obvious defense against agent mistakes, and it is a real one, but it has a ceiling: review catches what attention catches. Attention is finite, it degrades across a stack of plausible diffs, and every additional diff an agent produces draws on it. A test does not degrade. It runs on every change, checks the same thing every time, and costs no attention once written. That is the whole case for shifting verification from the reviewer's eyes into the test suite: mechanical checks scale with the volume of generated code in a way human reading cannot.
The ordering is the part that matters and the part most easily lost. A guardrail written before the agent starts encodes what you already know to be true — the behaviors that must survive, the criteria that define done. A test written after the output arrives is written by someone who has just read the agent's code and can absorb its framing without noticing. If the agent quietly changed the boundary condition, a test authored while looking at the new code tends to assert the new boundary. The check passes, and it was never independent of the thing it was supposed to check.
This is the before/after asymmetry, and it is the spine of the whole practice. Tests committed in advance are an independent statement of intent. Tests written in reaction to output are, at best, a description of what the output does. The two look identical in the suite; only their authorship order tells them apart, and only the first kind is independent of the output it checks.
Characterization tests: freezing current behavior
When the code an agent is asked to touch has no test coverage, delegating changes to it is risky — there is nothing to say whether the change preserved what mattered. A characterization test fills that gap. Michael Feathers's book Working Effectively with Legacy Code describes the technique: a characterization test pins down what code currently does rather than what it should do; the goal is to detect change, not to certify correctness.
That distinction is the point. You are not asserting the behavior is right. You are asserting it is this, so that if the agent's edit makes it something else, a test goes red and the change surfaces instead of shipping silently. Oddities get pinned along with everything else — a rounding quirk, an off-by-one in a legacy path, an empty-input result nobody loves. If real code depends on those behaviors, freezing them is a direct way to know the refactor did not disturb them.
Concretely, before handing an agent a function with no coverage, you run it against representative inputs and lock the outputs it actually produces. A hypothetical example:
# Not "what should discount() return" — "what does it return right now"
def test_characterize_discount():
assert discount(100, tier="gold") == 82 # includes the legacy 3% stacking quirk
assert discount(0, tier="gold") == 0
assert discount(100, tier=None) == 100 # None currently means no discount, not an errorThe scope discipline keeps this from becoming its own project. Pin the surfaces the task can plausibly disturb — the function being changed and its immediate contract — not the entire module. A characterization suite that tries to freeze everything can cost more than the delegation saves and can still miss the one interaction that breaks. Blast radius sets the boundary: what could this change reach, and what along that path must not move.
Acceptance tests: the spec made executable
Characterization guards what exists; acceptance tests define what the task must add. The move is to take each criterion from the spec and write it as a test that fails today and must be green when the work is done. "Rejects expired tokens" stops being a sentence in a ticket and becomes an executable assertion the agent has to satisfy.
This inverts the trust problem that makes reviewing agent output tiring. Left to narrate, an agent can tell you fluently that it handled the expired-token case — and the narration is produced by the same process that produced the code, so it is a claim, not evidence. A failing acceptance test the agent must turn green replaces the narration with a demonstration. The agent no longer says it is done; the suite says so, or it does not.
There is a useful diagnostic hidden in this step. When a criterion resists being written as a test — when you cannot state, mechanically, what would prove it satisfied — that is a signal the criterion is underspecified, and the right time to resolve it is before delegating, not after reading a plausible implementation of something you had not actually pinned down. A spec you cannot turn into acceptance tests is a spec the agent will interpret for you, and you will discover its interpretation at review time.
Tests the agent must not touch
Every guardrail in the two sections above shares a failure mode: an agent that can edit the tests can satisfy them by weakening them. Assertions get deleted, tolerances get loosened, a stubborn case gets marked skip. None of this is malice — it is an agent doing what it was pointed at, which is making the suite green, by whatever path is available. If the tests are in the editable surface, weakening them is one such path.
The defense is a locked set: guardrail tests are read-only to the agent. The agent implements against them and cannot change them, and any proposed change to a guardrail is, by definition, an escalation to a human rather than something the agent decides mid-task. Making the boundary explicit in the task instructions matters, because "make the tests pass" and "make the tests pass without editing the tests" are very different instructions, and only the second one addresses weakening the tests themselves.
This makes the test-file diff a mandatory checkpoint even when implementation review is light. You may skim a low-risk implementation, but you read every line of change to the guardrail files, because that is where a weakened check can masquerade as a passing one. It is not the only place: an agent can also make tests pass from outside the test files — for example by ending the run early where the setup allows it, or by changing code the tests depend on — so a green run still needs at least a look at the implementation diff. A green suite whose assertions were quietly trimmed is worse than a red one — it reports safety that is no longer there.
What guardrails do not cover
Guardrails are powerful because they are mechanical, and limited for the same reason: they verify the behaviors someone thought to pin. Agents fail in dimensions nobody encoded — an unhandled interaction, a performance cliff, a security-relevant edge no test named. Green guardrails prove the pinned behaviors held; they say nothing about the ones you did not anticipate, and treating a passing suite as full coverage is how the unanticipated failure ships.
So guardrails narrow the review, they do not remove it. With behavior preservation and spec criteria mechanically checked, human attention is freed to go where machines are weak — design quality, duplication against what the codebase already has, security reasoning, the judgment calls that were never expressible as assertions. Vendor guidance on agent workflows makes a related recommendation — give the agent a check it can run, such as a test suite (Anthropic, "Best practices for Claude Code", as of November 2026). The same guidance says that once Claude is carrying a long task on its own, the parts that stay with you are starting it, deciding what "done" means this time, and approving the merge at the end. Deciding which behaviors deserve a guardrail belongs on that list too.
And the guardrails cost something to write. A characterization suite and a set of acceptance tests are real work, and the honest accounting is that the work must stay proportionate to the blast radius. For a broad, silent-failure-prone change, thorough guardrails are cheap insurance. For a small, self-evident one, an elaborate suite is over-engineering, and the better answer is sometimes a smaller task the review can simply cover directly. The guardrail is a tool for the changes big enough to earn it — used everywhere, it becomes its own kind of ceremony.