AI CLI

Take Claude Code Through a Real Bug-Fix Flow

An AI coding assistant is not "let it auto-fix" but a disciplined workflow. Using a real project bug, this guide walks the full loop from reproduction, adding a test, surgical fixes, to regression verification, with the exact instructions to issue at each step.

By LaoHand Team·8 min read·Updated 2026-09-06

Set the Rule First: Reproduce Before You Change

The most common failure: a user reports a bug, Claude edits the code and hands back a "this looks right" fix that nobody ever verified against the real bug. The mindset fix is to make the tool reproduce first — force the problem out with a minimal input before talking about fixing.

When issuing instructions, say the context and expectation clearly: what environment, the input that errors out, expected vs actual. Being explicit matters more than knowing internally. If the project has a test framework, step one should be asking Claude to write a failing test that captures the current buggy behavior.

The three-part opening of a good prompt: reproduction steps, the concrete symptom you observed, and the system’s expected behavior. The more concrete each is, the fewer "auto-hallucinated" wrong fixes appear.

# 给 Claude Code 的任务指令模板
"复现 [BUG]: 在当前分支先写一个测试,用输入 X 验证它如今失败(红)。
只写测试,不要改业务代码,跑通这个失败用例后再回来告诉我。"
# 期望看到:一个红测(失败测试) 作为 bug 的铁证

Pin the Expected Behavior with a Test, Then Make Code Pass It

With a failing test that reproduces, the fix becomes "make it green" rather than free play. Ask Claude to declare its diagnosis (root-cause hypothesis) before it makes any change, so you can judge whether the direction is sound before any edit lands.

Then instruct: touch only the minimal scope to pass the related tests, introduce no new dependencies, and do not touch unrelated files. Run the full suite to confirm all green, and keep the failing test as a regression guard.

The point is to have the tool give a diagnosis explanation first rather than output a patch directly. That distinguishes "diagnose correctly, then fix" from "blindly edit until tests pass".

# 追加指令
"先给出根因诊断(一两句),再最小改动让测试变绿。
不要加新依赖,不要碰无关文件;完成后跑 npm test 回贴结果。"

Done Is Not Finished: Run a Review and Regression

After Claude edits, do at least one reverse review yourself: what changed, does it exceed the reported problem, and are there side effects and boundary cases not covered? A recommended move is letting the tool narrate its own diff via git diff and then reviewing manually.

Make the full suite and type check hard gates (pnpm test, pnpm typecheck), plus the linter if present. Then ask Claude to add a change note explaining why it fixed it this way, recording decisions rather than just piling up code.

In a real flow, review often catches concurrency, null-safety or backward-compat issues the AI underhandled, so do not skip this step.

claude "用 git diff 列出本次所有改动,逐条解释作用;
跑 pnpm test 与 pnpm typecheck 并报告是否通过;
补充一行变更说明(为什么这样改,而非改了什么)。"

The Classic Miss: The Tool "Fixed" It and Broke Something Else

The most dangerous AI fix is not that it fails, but that it rewrites adjacent logic beyond recognition. This usually stems from an undefined scope — just saying "fix this bug" lets the tool expand the blast radius on its own.

The counter is to pin scope: whitelist which files/functions may be modified, explicitly list what is forbidden to touch, and target a minimal diff. When reviewing the diff, if you see unrelated refactors, suspiciously deleted logic, or formatting rewrites, revert and redo with tighter scope.

Also keep a baseline before it edits. Use version control diff/back or copy the original files to .bak so the tool has an escape route if it breaks things.

diff --git a/src/payment.js b/src/payment.js
# 审查要点:仅应为修复目标而变;出现无关重构/删除立即回退
# 隐藏改坏的退路
cp src/payment.js src/payment.js.bak   # 改动前手动留底

Settle: Write This Root Cause and Fix into Comments and Docs

The last step of the fix loop is not committing but settling. Ask Claude to add a one-line "why here" comment at the critical spot (explaining the pitfall, not restating the code), and record the root cause, trigger condition and solution in the project docs or the issue.

The value is that the next person — or AI — facing a similar problem can read the historical decision instead of re-treading the same trap. This converts a one-off fix into reusable knowledge and marks the line between firefighting and governance.

If the fix touches a shared convention (e.g. parameter validation or boundary handling), update the relevant CONTRIBUTING or README section as well, so rules and code evolve consistently.

// 修复痕记实例(注释里写"为什么",而非复述做了什么)
// 此前 dateStr 允许为空导致 endDate 非法,parse 前必须判空
// 根因见 docs/bugfix/2026-09-outdated-enddate.md
claude "在这个修复处加注释说明 出发原因,并把根因记录到 docs 对应文件。"

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.