Back to blog
Verification
Superpowers
Claude Code

Why Your Agent's First Fix Attempt Is Usually Wrong

Thang Doan
Thang Doan

An agent hits a bug. It guesses a fix. Tests pass. It reports done. Two hours later the same bug reappears in a different form, because the first fix treated a symptom, not the cause.

The second attempt is worse. The agent guesses again, this time on top of the first wrong fix. The code now has two layers of patch. The actual root cause is still there, buried under the patches.

This is what random fixes do. They feel productive. They add bugs.

The iron law

No fixes without root cause investigation first.

If you have not completed the investigation phase, you cannot propose a fix. Period.

Agents resist this. Time pressure makes guessing feel efficient. The fix-then-test loop produces quick green checks. Each green check is a false signal. The bug is still there. It will surface again.

Phase one: root cause investigation

Before any fix, the agent must do four things.

Read the error message carefully. Error messages often contain the exact solution. They point to the file, the line, the variable, and sometimes the failed assumption. Agents skim error messages and miss the answer.

Reproduce the issue consistently. A bug that appears sometimes cannot be fixed reliably. Run the reproduction. Confirm it fails the same way each time. If it does not, the bug is environmental, not code.

Check recent changes. git diff. Recent commits. New dependencies. Most bugs arrive with a change. The diff usually contains the bug.

For multi-component systems, add diagnostic instrumentation at each component boundary before proposing fixes. Log what enters, what exits, verify environment propagation, check state at each layer. This reveals exactly which layer fails.

Skipping any of these steps means guessing. Guessing is what causes the second-attempt-worse pattern.

Phase two: pattern analysis

Find working examples in the same codebase. The bug is almost always a difference between working code and broken code that look similar.

Compare against the reference implementation. Read every line, do not skim. List every difference between working and broken, however small. The small differences are usually the cause.

Understand all dependencies, settings, and environmental assumptions. The bug may live in the gap between what the code assumes and what the environment provides.

This phase takes longer than guessing. It produces correct fixes instead of patches.

Phase three: hypothesis and test

Form a single, specific hypothesis. Write it down.

"I think X is the root cause because Y."

Test minimally. Make the smallest possible change that tests one variable. If the hypothesis is correct, the bug disappears. If not, form a new hypothesis.

Do not stack fixes on top of each other. Agents do this when they are unsure. They try fix A, then fix B on top, then fix C. When the bug disappears, they do not know which fix worked. The next regression is untraceable.

One hypothesis. One minimal change. One observation. Then decide.

Phase four: implementation

Create a failing test case first. This is the TDD discipline applied to debugging. The test reproduces the bug.

Implement a single fix addressing the root cause. Not a patch over the symptom. The fix should make the failing test pass and not break any other test.

If the fix does not work after three attempts, stop. The architecture is wrong, not the fix. Continuing to guess inside a wrong architecture compounds the damage.

This rule prevents the failure mode where the agent tries six variations of a fix, each slightly worse, until the context window is full of dead attempts.

Red flags that mean stop

Certain phrases from an agent mean return to phase one.

"Quick fix for now, investigate later." The later never comes. The quick fix becomes the permanent code.

"Just try changing X and see if it works." This is guessing. Stop.

"Add multiple changes, run tests." Stacked fixes. Stop.

"I do not fully understand but this might work." If you do not understand, you cannot fix. Stop.

"One more fix attempt." When the agent has already tried twice, the third attempt will not work either. Stop. Reinvestigate.

All of these mean the same thing. The agent has stopped investigating and started guessing. The fix it produces will be wrong in a way that costs more time later.

The metric that matters

From debugging sessions tracked on projects that enforce this process: first-time fix rate is around ninety-five percent. The systematic approach takes fifteen to thirty minutes per bug.

Without the process: first-time fix rate around forty percent. Random fixes take two to three hours of thrashing. They introduce new bugs in the process.

Ninety-five percent of "no root cause" cases are actually incomplete investigation. The cause exists. The investigator stopped before finding it.

When this discipline is too much

For a typo, root cause investigation is overkill. The error message says "unexpected token ;". You see the typo. You fix it.

For a CSS class name mismatch, the same. Read the error, fix the name, move on.

The judgment is whether the bug has hidden depth. A missing semicolon does not. An intermittent test failure does. A bug that appears only in production does. A bug that disappears when you add a print statement definitely does.

The discipline scales with the bug's complexity. Match the investigation to the mystery.

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

By subscribing you agree to receive the newsletter. See the Privacy page.