Skip to content
mentoor

Topic 1 of 5 · 4 min read

Reproducing the bug before the fix

Goal: Ask an agent for a minimal failing reproduction and confirm that it fails for the reported reason.

Before an agent changes any code, make it produce a test or script that fails because of the bug, and check that it fails for the right reason. The failing check is the target. When it turns green after the fix, you have proof.

Check yourself · 1 of 3

An agent is asked to fix a bug from a one-line report. What should you ask for first?

Choose one answer

Why it matters

An agent that only reads a bug report is working from words. It will pick the most likely cause and write a fix that looks right. Claude Code, Cursor, Codex and Copilot all do this. Without a reproduction, nobody can say whether the fix worked or whether the agent only changed something nearby. Anthropic's own best-practices page names a way to verify work as the single most useful thing you can give an agent.

How to do it

  1. Write down the report in three parts: the steps, what you expected, what happened.
  2. Ask for the reproduction first, and forbid the fix. For example: "Do not change src/. Write one failing test that reproduces this bug."
  3. Run it yourself. Watch it fail.
  4. Read the failure message. It must match the symptom in the report. A test that fails with an import error or a typo proves nothing.
  5. Shrink it. Remove every line that is not needed to make it fail: fewer fields, fewer records, no network.
  6. Only then ask for the fix, and say "this test must pass and no other test may change".

If a test is hard to write (a UI glitch, a race), ask for a small script that prints the wrong value instead.

Before and after

Weak prompt:

Users say the invoice total is wrong sometimes. Fix it.

Better prompt:

Invoice 4471 shows 107.99 but should be 108.00. Do not edit src/.
Write one failing test in tests/invoice.test.ts that builds this invoice
and expects 108.00. Run it and show me the failure output.

The second prompt gives the agent a number to hit and gives you a failure to read.

What goes wrong

  • The test fails for the wrong reason. A missing fixture or a wrong import makes it red. After the "fix", it is green for a reason that has nothing to do with the bug.
  • The agent fixes and tests together. It writes a test that passes on its own code, and you never saw it fail.
  • The reproduction is too big. A test that needs the full database hides which input matters.
  • The bug only appears in production. Then the first job is to collect evidence, which is the next topic.

Examples

A rounding bug

import { total } from "../src/invoice";

test("rounds half-cent totals up", () => {
  const items = [{ price: 33.333, qty: 3 }];
  expect(total(items)).toBe(100.0);
});

Expected 100, received 99.99. That matches the report, so it is a valid target. Now the agent can fix total and you can watch this test flip.

A script when a test is hard

from app.cache import get_user

for _ in range(3):
    print(get_user(7).email)

The report says the second call returns a stale email. Printing three calls shows exactly when the value changes, and gives the agent something to run after each edit.

Key points

  • Ask for a failing reproduction first and tell the agent not to touch source code until it exists.
  • Run the reproduction yourself and see it fail before any fix is written.
  • The failure message must match the reported symptom, not an import or setup error.
  • Shrink the reproduction until every remaining line is needed.
  • Finish with the same check turned green and the rest of the suite unchanged.

Common mistakes

  • Asking for the fix and the test in the same prompt, so the test is never seen failing.
  • Accepting a red test without reading why it is red.
  • Leaving the reproduction large and noisy.
  • Letting the agent edit the test until it passes.

Check yourself · 2 of 3

A new test for a reported bug fails with 'Cannot find module ./fixtures'. What does that tell you?

Choose one answer

Check yourself · 3 of 3

Why tell the agent 'do not change src/' while it writes the reproduction?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic