The Instinct Is Wrong
When an agent finds a bug, the natural thing to say is "fix it."
Stop. Ask for a test instead.
This is a small change in phrasing that produces a disproportionately large change in outcome, and it takes no extra time. It's probably the highest-leverage habit I've picked up working with coding agents.
What Happens When You Ask for a Fix
The agent looks at the buggy code. It makes a change it believes fixes the bug. The change ships.
And now consider what you actually have:
- You don't know if the bug is fixed. You know the agent thinks it is.
- You don't know if the fix introduced a new bug.
- There's no record of what the bug was.
- There's no record of why this fix was chosen over another.
- If it regresses next month, nothing catches it.
The fix is ephemeral. The understanding evaporated the moment the session ended.
What Happens When You Ask for a Test
Same bug, different request. Now:
- The agent writes a test that reproduces the bug.
- The test fails, because the bug is real. This step is the one people skip, and it's the one that matters.
- The agent fixes the code until the test passes.
- You have a permanent record of what the bug was, what the expected behavior is, and evidence the fix works.
- The test catches the regression when it comes back.
The test is the specification of what "fixed" means. Without it, "fixed" is a claim. With it, "fixed" is a thing you can run.
The Specific Prompt
Here's the exact phrasing I use:
"Write a test that reproduces this bug. Do not fix the code yet. Just write the test and confirm it fails. Then fix the code and confirm the test passes."
The order is the whole trick. Writing the test first forces the agent to engage with the specification before it touches the implementation. It has to articulate what correct behavior is before it starts changing things toward it.
And "confirm it fails" is not a formality. A test that passes before the fix is testing the wrong thing, and agents produce these constantly: a test that exercises adjacent, already-working behavior and reports green. If you skip the red step, you can ship a fix, a test, and a completely unearned sense of confidence.
Why This Works
Because it converts an ephemeral fix into a durable artifact.
The conversation where you explained the bug is gone. The agent's reasoning about the fix is gone. The context window is gone. The test is still there, still running, still failing loudly if anyone reintroduces the problem.
That test is also the thing a reviewer can actually evaluate. Reviewers are bad at finding bugs by reading, but they're good at telling whether the intent behind a change was written down. A test that names the bug does that work for them. And once it exists, it doubles as a tripwire for the next time the agent forgets.
Same effort. One of them survives.
Header photo by Louis Reed on Unsplash.
Content on this blog was created using human and AI-assisted workflows described in my standards and workflow posts. Original ideas and editorial decisions by Justin Quaintance.