Skip to content
mentoor

Topic 3 of 5 · 4 min read

Testing one bug hypothesis at a time

Goal: Direct an agent to test one hypothesis with a single probe and read the result before any code changes.

Debug by listing possible causes, picking the cheapest one to test, and changing nothing but a probe until the result is in. An agent left alone tends to edit several things at once. If the bug disappears, you do not know which edit fixed it, and the others stay in your code.

Check yourself · 1 of 3

An agent suggests adding a null check, raising a timeout and wrapping the call in try/catch for one bug. What is the problem?

Choose one answer

Why it matters

Agents want to be useful, so they act. A common pattern is the shotgun edit: add a null check, bump a timeout, wrap the call in try/catch, and call it done. The symptom may vanish while the cause remains. You also get a diff full of changes nobody needs.

The loop

  1. Ask for a ranked list. "List the three most likely causes given this trace. For each, say what you would see if it were true."
  2. Pick one. Usually the cheapest to test, or the likeliest. Not all of them.
  3. Design a probe. A probe reads, it does not fix: a log line, a breakpoint, a print of a value, an assertion, a debugger session such as Python's pdb, or a one-off script.
  4. Run it and read the output together. Say what it shows and which hypotheses it rules out.
  5. Decide. Confirmed: now make the smallest fix. Rejected: go to the next hypothesis.
  6. Remove the probe when done.

Tell the agent the rule directly: "One hypothesis at a time. Do not change behaviour until a probe confirms the cause."

Before and after

Weak exchange:

User: The cache returns old prices. Fix it.
Agent: I lowered the TTL, added a flush on write and rewrote the key function.

Better:

User: List 3 likely causes for stale prices. Test only the first with a log line.
Agent: H1: the key ignores currency. Probe: log the key on each get and set.
Output: get key=price:42, set key=price:42:EUR. The keys differ.

H1 is confirmed in one run. The fix is one line, and nothing else moved.

What goes wrong

  • Shotgun edits. Many changes at once, with no way to tell which mattered.
  • Probes that change behaviour. A log that forces a flush or a sleep can hide a race.
  • Confirmation bias. The agent reads the output as support for its first idea. Ask: "What in this output would contradict H1?"
  • Leftover debug code. Check the diff for stray prints.

Examples

A single-probe prompt

Hypothesis: the retry runs twice because the handler is registered twice.
Probe: add one log line in registerHandler() printing the event name
and a counter. Run the failing test once. Do not change anything else.

The agent has a narrow task, and you get one clean result.

Reading the result

The log shows registerHandler(order.paid) #1 and #2. Hypothesis confirmed. The fix is to register once at startup, and the next step is to rerun the original reproduction.

Key points

  • List several causes and say what each would look like if true before testing any.
  • Test the cheapest or likeliest hypothesis with a probe that does not change behaviour.
  • Read the probe output and say which hypotheses it rules out.
  • Make the smallest fix only after the cause is confirmed.
  • Remove probes before finishing, and check the diff for leftovers.

Common mistakes

  • Letting the agent edit several things at once.
  • Starting from a fix instead of a list of causes.
  • Accepting the first plausible story without asking what would contradict it.
  • Forgetting to remove debug output.

Check yourself · 2 of 3

What is a probe in debugging?

Choose one answer

Check yourself · 3 of 3

After a probe contradicts your top hypothesis, what is the best next step?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic