When an agent writes a function and we ask the same session "review this", the review is almost always positive. That is not surprising. The reviewer has the same context, the same reading of the task and the same reasons for every decision it just made. It is grading its own homework with the answer key it wrote.
We now run AI-written changes through separate roles: a writer, a reviewer and a tester, each with its own context and instructions. It catches a real share of problems before a human looks. It also has limits that we learned the hard way, which this post covers too. A companion post describes a bug that got past both AI and human review, and the test that would have stopped it.
Why self-review fails
Three effects stack up when the writer reviews its own work:
Shared context. The reviewer has seen every justification, so it reads the code through them.
Shared interpretation of the task. If the writer misread the requirement, the reviewer misreads it the same way, because the misreading is in the conversation.
Agreeableness. Language models tend to accept a premise that is already stated. "Review this correct implementation" gets a different answer than "find the bug in this implementation".
Separate roles address the first two. Adversarial instructions address the third.
The three roles
Writer
The writer gets the task, the relevant code and the project rules. Its output is a diff and a short note: what it changed, what it assumed, what it did not handle. The assumptions list is the most valuable part for the next two roles, so we ask for it explicitly.
Reviewer
The reviewer starts with a fresh context and receives only the specification, the diff and the writer's assumptions, not the writer's conversation. In Claude Code this is a project subagent with read-only tools:
---
name: adversarial-reviewer
description: Reviews a diff against its specification and tries to break it. Use after any non-trivial change.
tools: Read, Grep, Glob
---
You did not write this code. Assume it contains at least one defect.
For the given spec and diff:
1. List each requirement in the spec and whether the diff satisfies it, with file:line.
2. For each changed function, list inputs at the boundaries: empty, one, maximum,
negative, null, duplicate, unicode, concurrent calls, time zone and DST edges.
State the expected and the actual behaviour for each.
3. List every assumption from the writer you could not confirm from the code or spec.
4. Report defects only. No style comments unless they change behaviour.
If you find nothing, explain which inputs you tried and why each is handled.
"Assume it contains at least one defect" changes the output noticeably. So does forbidding style comments, which otherwise fill the review and give a false sense of thoroughness.
Tester
The tester writes tests from the specification, before seeing the implementation if possible. Tests written from the code tend to assert what the code does, including its bugs; our earlier post on AI-written tests that asserted the bug shows how that looks in practice. When the tester must see the code, we ask it to write the expected values by hand from the spec and to explain each one.
We ask for two kinds of tests:
Example tests for named cases from the spec and from the reviewer's boundary list.
Property tests for invariants: "the parts always sum to the total", "sorting is idempotent", "decode(encode(x)) == x". Hypothesis (Python), fast-check (JavaScript) and proptest (Rust) generate hundreds of inputs per property, including edge cases no one listed.
from hypothesis import given, strategies as st
from billing.split import split_amount
@given(total=st.integers(min_value=0, max_value=10**9), parts=st.integers(min_value=1, max_value=48))
def test_split_preserves_total(total, parts):
result = split_amount(total, parts)
assert len(result) == parts
assert sum(result) == total
assert max(result) - min(result) <= 1
Testing the tests: mutation testing
A test suite that passes says nothing about whether it would fail on a bug. Mutation testing checks that by making small changes to the code (flip a comparison, change a constant, delete a line) and running the tests against each mutant. A mutant that survives is a change the tests did not notice.
Language | Tool | Example |
|---|---|---|
JavaScript / TypeScript | StrykerJS |
|
Python | mutmut |
|
Rust | cargo-mutants |
|
Java | PIT |
|
We run mutation testing on the changed files only, because full runs are slow. A surviving mutant goes back to the tester agent with the instruction "write a test that kills this mutant", which is a precise, checkable task that agents handle well.
Wiring it together
The order matters:
Writer produces diff and assumptions.
Tester writes tests from the spec; they run against the diff.
Reviewer reviews spec, diff, assumptions and the tests.
Mutation testing on changed files; survivors go back to the tester.
Deterministic checks in CI: types, lint, rule scripts, coverage on the diff.
Human review, now focused on the spec and the reviewer's unconfirmed assumptions.
Steps 1 to 4 can run from one prompt that delegates to subagents. Step 5 must run in CI, not in the agent, so it cannot be skipped. Step 6 is not optional.
Why agents still share blind spots
Separate contexts reduce correlated errors, but they do not remove them. Two instances of the same model have the same training, the same tendencies and often the same gaps:
Same misreading of an ambiguous spec. If the spec says "split the payment into equal parts", every instance may assume equal means rounding each part, and none asks what happens to the remainder.
Same domain gaps. If the model does not know that a payment network rejects amounts with more than two decimals, neither the writer nor the reviewer will check for it.
Same plausible patterns. Models favour code that looks like common code. A common pattern with a subtle flaw in your context is exactly what all roles find convincing.
Mitigations that worked for us:
Write the spec with examples and invariants a human agreed on. Ambiguity the spec resolves cannot be misread.
Use a different model for the reviewer when the change is risky. Different training gives somewhat different blind spots.
Property tests and mutation testing come from tools, not models, so they do not share the model's assumptions.
Human review focused on intent. People are better at "is this what we meant?" than at spotting an off-by-one in a 400-line diff. Give them the spec and the assumption list.
Common mistakes
Mistake | Why it hurts | Do this instead |
|---|---|---|
Same session reviews its own code | Reviewer inherits the writer's reasoning | Fresh-context reviewer subagent |
Reviewer sees the writer's conversation | Same misreadings carry over | Spec, diff and assumptions only |
Tests derived from the implementation | Tests encode the bug | Tests from the spec, expected values by hand |
Coverage as the quality bar | Covered lines are not checked lines | Mutation testing on changed files |
Treating AI approval as approval | Shared blind spots | CI checks plus a human on intent |
Key takeaways
Self-review agrees with itself; split writer, reviewer and tester into separate contexts.
Tell the reviewer to assume a defect and to report behaviour, not style.
Write tests from the spec and add property tests for invariants.
Use mutation testing to prove the tests would catch a bug.
Agents share blind spots; a clear spec, tool-based checks and a human focused on intent cover them.
References
Claude Code subagents: https://code.claude.com/docs/en/sub-agents
Hypothesis documentation: https://hypothesis.readthedocs.io/
fast-check documentation: https://fast-check.dev/
StrykerJS: https://stryker-mutator.io/docs/stryker-js/introduction/
mutmut: https://mutmut.readthedocs.io/
cargo-mutants: https://mutants.rs/
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.