Skip to main content

Finding and fixing bugs

When something breaks, "fix this" is rarely enough. With 4RGE agents, the reliable path is the same as good engineering — just faster:

Symptom → repro → evidence → narrow fix → verify.

This lesson shows how to run that loop with an agent so you fix the root cause, not the loudest symptom or the most confident-sounding patch.

What this page is for

You want to…This page helps you…
Run the debug loopSymptom → repro → evidence → fix → verify
Start a debug run wellWrite a brief the agent can act on
Avoid guess-and-check loopsInvestigate before patching; reset when wrong
Land a real fixPatch in small steps and prove it with tests
Know when to resetStop chasing the wrong theory
Part of 4RGED Learn · Coding agents

Session habits — Working with agents. Explore unfamiliar code first — Understanding your codebase. For full prompt templates, see Prompting.

Who this page is for

You are…Focus on
New to agent debuggingWrite a debugging brief → Verify the fix
Daily developerReproduce before you fix + Fix in small steps
Chasing a flaky bugRepro steps + run the failing case multiple times before accepting a fix
Handing off a hard bugSame brief discipline for cloud agents

The debug loop in five moves

MoveYouAgent
1. SymptomState what breaks, for whom, since whenConfirms it understood the problem
2. ReproExact steps + environment (local, CI, staging)Repeats repro or asks for one missing detail
3. EvidencePaste stack trace, failing test, log, screenshotReads code, searches, runs tools — Tool calling
4. Narrow fixApprove the smallest change that matches confirmed causeEdits, shows diff — no drive-by refactors
5. VerifyRun the named test or repro againFixes failures in the same run if checks fail

You stay in control at every step: the agent investigates and drafts; you accept diffs and decide when the bug is actually gone.

You have…Start with
Stack trace or failing testPaste it — Write a debugging brief
UI bug, no error yetRepro steps + ask for logging or a failing test first
Flaky or intermittent bugHow often it fails + run the case multiple times before trusting a fix
Unfamiliar codeShort explore pass — Understanding your codebase

Write a debugging brief

You do not need a novel. You do need facts the agent cannot invent.

SectionWhat to include
SymptomWhat breaks, for whom, and since when (if known)
ReproExact steps, inputs, and environment (local vs staging)
Expected vs actualWhat should happen vs what happens
EvidenceStack trace, failing test name, log snippet, network error, screenshot
Hypothesis (optional)"Might relate to cart merge in #398" — helpful, not required

Example you can paste and edit:

The /checkout endpoint returns 500 intermittently.
Repro: POST /checkout with an empty cart on staging.
Stack trace: TypeError: cannot read 'total' of undefined at cart.ts:42
Expected: 400 with { error: "empty_cart" }
Actual: 500 with no body
Run: pnpm test apps/web -- checkout.test.ts

Point at the failing test or file with @ when you know it — Prompting.

Reproduce before you fix

If you cannot reproduce it, say so — and say what you did try.

SituationWhat helps
Always failsOne clear repro is enough to start
IntermittentNote how often it fails; ask the agent to add logging or run the case several times
Environment-specificName local vs CI vs staging; include versions if relevant
RegressionMention the last known good deploy or PR if you have it

Repro is how you and the agent know the bug is gone — not just "looks fine in chat."

Let the agent investigate

Give the agent room to read, search, and run — that is what Tool calling is for. A strong first message asks for ranked hypotheses after reviewing the evidence, not a patch from memory.

Good first askWeaker first ask
"Given this stack trace and test, list likely causes in order; then propose the smallest check to confirm #1""Fix the checkout bug"
"Find callers of mergeCart and explain how empty cart reaches line 42""Rewrite the cart module"

For unfamiliar code, a short explore pass first saves wrong fixes — Understanding your codebase.

On desktop, Run and Debug (breakpoints) still pairs well with agent investigation when you need to pause execution. For cloud runs, follow the run stream and Diff tab when reviewing what changed.

Example debug prompts

Copy, edit, and paste — attach @ files when you know them.

First message (investigate, don't patch yet):

Bug: POST /checkout with empty cart returns 500 on staging.
Repro: POST /checkout with {} cart body.
Evidence: TypeError at cart.ts:42 — cannot read 'total' of undefined.
Expected: 400 { error: "empty_cart" }.
 
Before changing code: list the 3 most likely causes in order, then propose
the smallest check to confirm #1. Run: pnpm test apps/web -- checkout.test.ts

After cause is confirmed (narrow fix):

Cause confirmed: mergeCart assumes items[0] exists when cart is empty.
Fix only cart.ts and checkout.test.ts — guard empty cart before reading total.
No refactors outside that path. Re-run checkout.test.ts when done.

Verify (prove the bug is gone):

1. checkout.test.ts must pass (including new empty-cart case).
2. Run pnpm test apps/web -- checkout.
3. Summarize what changed in one sentence — I'll merge if green.

When the guess was wrong (reset):

Repro still fails after the last patch. Revert speculative edits in cart.ts.
Add one log at mergeCart entry showing cart shape, re-run checkout.test.ts,
and restart with a new hypothesis — don't widen scope yet.

For review after the fix lands, see Reviewing and testing code.

Fix in small steps

Prefer the smallest change that addresses the confirmed cause.

HabitWhy
Confirm the cause firstOne log, one test, one read — then edit
One concern per patchEasier to review and revert
Avoid drive-by refactorsUnrelated edits hide regressions
Name the test to add or fix"Done" includes a failing test going green

If the agent proposes a large rewrite for a small bug, ask it to narrow — same as you would with a teammate.

Verify the fix

"Sounds fixed" is not verification.

StepWhat to run
1. Prove the bugThe narrowest failing test or repro you started with
2. Guard against regressionsRelated tests or package test script you trust
3. Broader check (when warranted)Lint, typecheck, or CI-minded command from your repo
4. Manual smokeUX paths automated tests miss

Run the narrow test before the full suite — it saves time and keeps the agent focused.

When checks pass and repro no longer fails, you decide merge — same as any other change. For a second pass on the diff, see Reviewing and testing code.

What good debugging looks like

SignalMeaning
Repro fails before the fix, passes afterYou proved cause and effect — not just chat
One focused diffFix matches the confirmed cause; no unrelated files
Test added or updatedEmpty cart (or your edge case) cannot silently regress
Named command went greenYou and the agent agreed on objective "done"
You can explain the fix in one sentenceYou understand it — not only the agent
Polished explanations ≠ fixed bugs

Agents can sound confident while missing edge cases — Hallucination & limitations.

When the guess is wrong

If the third "fix" still fails repro, stop widening:

  1. Revert speculative edits (checkpoint, Git, or discard)
  2. Add evidence — new log line, failing test output, or clearer repro
  3. Restart with a tighter brief — or a fresh message if context is noisy — Context

That beats ten follow-up messages on the wrong theory — Working with agents.

Habits that work

  1. Paste the stack trace or test output — do not paraphrase errors from memory.
  2. State expected vs actual — removes ambiguity.
  3. Name one command to run — agent and you agree on green.
  4. Fix the failing test first — then expand scope.
  5. Keep the diff small — review like any human PR.
  6. Use plugins when logs live elsewhere — e.g. "query our error tracker for checkout 5xx" if you have that integration — Tools.

Where to go next

Your goalNext step
Ship the feature that brokeDeveloping features
Review the fix diffReviewing and testing code
Prompt templatesPrompting
Setup or API issuesTroubleshooting
Long debug on a branchCloud agents overview
End-to-end workflowPutting it all together

Questions about finding and fixing bugs?

QuestionAnswer
Do I need a hypothesis?No — symptom + repro + evidence is enough. Hypothesis speeds things up when you have one.
Should the agent run tests?Yes — name the test or script in your brief so verification is objective.
What if there is no stack trace?Describe UI steps, network tab errors, or ask for targeted logging first.
Can it fix production-only bugs?Share staging repro or logs; avoid pasting secrets — Agent security.
What if it keeps changing unrelated files?Stop, revert, restate scope — same as feature work.
Does debugging use more tokens?Long investigations do — Tokens & pricing.
Agent says "fixed" but repro still fails?Do not merge — When the guess is wrong; add evidence and reset.
Should I explore before every bug?Only when the area is unfamiliar — otherwise lead with evidence and repro.
Can the agent review its own fix?Yes — ask for edge cases on the diff; you still own merge — Reviewing and testing code.