diagnose
- 信任分
- 88/100
- 兼容 Agent
- 1
- 领域
- 工程开发
- 兼容 Agent
- Claude Code
- 信任分
- 88 / 100 · 社区维护
- 作者 / 版本 / 许可
- @mattpocock · 未声明 license
- 安装命令数
- 1 条
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
想读作者英文原文? ↓ 滚到正文区切换 · 在 GitHub 查看 ↗
设计思路
diagnose 是 mattpocock 总结的「Bug 诊断六阶段」工作流,把模糊的「调一下」拆成结构化的从假设到根因的链路。先建 feedback loop,再下诊断结论——所有阶段都基于「能稳定复现」这个前提。
六个阶段(按作者原文)
- Phase 1:Build a feedback loop 把 bug 变成一个可以一次次跑、跑得快、信号明确的 reproducer。没这个就不要往下走。
- Phase 2:Reproduce 最小化复现路径——剥掉一切非必要因素,让信号清晰。
- Phase 3:Hypothesise 列出假设并按可能性排序;问用户有没有自己的怀疑("jump to #3")或已经排除过哪些——便宜的 checkpoint,省时间。用户 AFK 就先按你自己的排序往下走。
- Phase 4:Instrument
- 每个 probe 都要对应一个具体假设。
- 一次只改一个变量。
- 工具优先级:debugger / REPL > 边界处的 targeted log >「全打 log 再 grep」(最次)。
- 每条 debug log 都打唯一前缀(如
[DEBUG-a4f2]),结尾一次性 grep 清干净——untagged 留下,tagged 死掉。 - 性能问题分支:log 通常不对路,先建立 baseline 测量(timing harness、
performance.now()、profiler、query plan)再 bisect——先量后修。
- Phase 5:Fix + regression test
- 先写回归测试再写 fix——但只在「正确缝隙」存在时。
- 「正确缝隙」= 测试覆盖到真实 bug 模式发生的那个调用点。
- 如果只有过浅的缝隙(单 caller 写测试 / 单元测试无法复现 chain)——这本身就是发现:架构在阻碍 bug 被锁定,记下来交给
improve-codebase-architecture。 - 有正确缝隙时:把 minimised repro 变成 failing test → 看它失败 → 应用 fix → 看它通过 → 用 Phase 1 loop 在原始(未最小化)场景再跑一次。
- Phase 6:Cleanup + post-mortem
完成清单:原始复现已不再复现 / 回归测试通过(或缝隙缺失被记下)/ 所有
[DEBUG-...]都grep清掉 / 临时 prototype 删掉或挪到明确的 debug 目录 / commit 或 PR 写明「最终被验证的假设」让下一个调试者受益。 再问一句:如果答案涉及架构改动(缺好缝隙、调用方纠缠、隐藏耦合),把这个交给improve-codebase-architectureskill——但修完之后才提,因为现在你比开始时知道得多。
适合谁
- 接到「线上偶现」类 bug 的工程师
- 性能回归调查(务必走 perf 分支)
- 想把调试经验做成可教学的教练 / Tech Lead
何时不该用
- 一眼能看出来的低级错误——直接改
- 没有可重复跑的环境——先去搭,否则 Phase 1 都过不去
配套
systematic-debugging(更基础的姊妹工作流)、improve-codebase-architecture(Phase 5 / 6 暴露架构问题时的下一站)、tdd(缝隙存在时的回归测试母法)。
Diagnose
A discipline for hard bugs. Skip phases only when explicitly justified.
When exploring the codebase, use the project's domain glossary to get a clear mental model of the relevant modules, and check ADRs in the area you're touching.
Phase 1 — Build a feedback loop
This is the skill. Everything else is mechanical. If you have a fast, deterministic, agent-runnable pass/fail signal for the bug, you will find the cause — bisection, hypothesis-testing, and instrumentation all just consume that signal. If you don't have one, no amount of staring at code will save you.
Spend disproportionate effort here. Be aggressive. Be creative. Refuse to give up.
Ways to construct one — try them in roughly this order
- Failing test at whatever seam reaches the bug — unit, integration, e2e.
- Curl / HTTP script against a running dev server.
- CLI invocation with a fixture input, diffing stdout against a known-good snapshot.
- Headless browser script (Playwright / Puppeteer) — drives the UI, asserts on DOM/console/network.
- Replay a captured trace. Save a real network request / payload / event log to disk; replay it through the code path in isolation.
- Throwaway harness. Spin up a minimal subset of the system (one service, mocked deps) that exercises the bug code path with a single function call.
- Property / fuzz loop. If the bug is "sometimes wrong output", run 1000 random inputs and look for the failure mode.
- Bisection harness. If the bug appeared between two known states (commit, dataset, version), automate "boot at state X, check, repeat" so you can
git bisect runit. - Differential loop. Run the same input through old-version vs new-version (or two configs) and diff outputs.
- HITL bash script. Last resort. If a human must click, drive them with
scripts/hitl-loop.template.shso the loop is still structured. Captured output feeds back to you.
Build the right feedback loop, and the bug is 90% fixed.
Iterate on the loop itself
Treat the loop as a product. Once you have a loop, ask:
- Can I make it faster? (Cache setup, skip unrelated init, narrow the test scope.)
- Can I make the signal sharper? (Assert on the specific symptom, not "didn't crash".)
- Can I make it more deterministic? (Pin time, seed RNG, isolate filesystem, freeze network.)
A 30-second flaky loop is barely better than no loop. A 2-second deterministic loop is a debugging superpower.
Non-deterministic bugs
The goal is not a clean repro but a higher reproduction rate. Loop the trigger 100×, parallelise, add stress, narrow timing windows, inject sleeps. A 50%-flake bug is debuggable; 1% is not — keep raising the rate until it's debuggable.
When you genuinely cannot build a loop
Stop and say so explicitly. List what you tried. Ask the user for: (a) access to whatever environment reproduces it, (b) a captured artifact (HAR file, log dump, core dump, screen recording with timestamps), or (c) permission to add temporary production instrumentation. Do not proceed to hypothesise without a loop.
Do not proceed to Phase 2 until you have a loop you believe in.
Phase 2 — Reproduce
Run the loop. Watch the bug appear.
Confirm:
- The loop produces the failure mode the user described — not a different failure that happens to be nearby. Wrong bug = wrong fix.
- The failure is reproducible across multiple runs (or, for non-deterministic bugs, reproducible at a high enough rate to debug against).
- You have captured the exact symptom (error message, wrong output, slow timing) so later phases can verify the fix actually addresses it.
Do not proceed until you reproduce the bug.
Phase 3 — Hypothesise
Generate 3–5 ranked hypotheses before testing any of them. Single-hypothesis generation anchors on the first plausible idea.
Each hypothesis must be falsifiable: state the prediction it makes.
Format: "If
is the cause, then will make the bug disappear / will make it worse."
If you cannot state the prediction, the hypothesis is a vibe — discard or sharpen it.
Show the ranked list to the user before testing. They often have domain knowledge that re-ranks instantly ("we just deployed a change to #3"), or know hypotheses they've already ruled out. Cheap checkpoint, big time saver. Don't block on it — proceed with your ranking if the user is AFK.
Phase 4 — Instrument
Each probe must map to a specific prediction from Phase 3. Change one variable at a time.
Tool preference:
- Debugger / REPL inspection if the env supports it. One breakpoint beats ten logs.
- Targeted logs at the boundaries that distinguish hypotheses.
- Never "log everything and grep".
Tag every debug log with a unique prefix, e.g. [DEBUG-a4f2]. Cleanup at the end becomes a single grep. Untagged logs survive; tagged logs die.
Perf branch. For performance regressions, logs are usually wrong. Instead: establish a baseline measurement (timing harness, performance.now(), profiler, query plan), then bisect. Measure first, fix second.
Phase 5 — Fix + regression test
Write the regression test before the fix — but only if there is a correct seam for it.
A correct seam is one where the test exercises the real bug pattern as it occurs at the call site. If the only available seam is too shallow (single-caller test when the bug needs multiple callers, unit test that can't replicate the chain that triggered the bug), a regression test there gives false confidence.
If no correct seam exists, that itself is the finding. Note it. The codebase architecture is preventing the bug from being locked down. Flag this for the next phase.
If a correct seam exists:
- Turn the minimised repro into a failing test at that seam.
- Watch it fail.
- Apply the fix.
- Watch it pass.
- Re-run the Phase 1 feedback loop against the original (un-minimised) scenario.
Phase 6 — Cleanup + post-mortem
Required before declaring done:
- Original repro no longer reproduces (re-run the Phase 1 loop)
- Regression test passes (or absence of seam is documented)
- All
[DEBUG-...]instrumentation removed (grepthe prefix) - Throwaway prototypes deleted (or moved to a clearly-marked debug location)
- The hypothesis that turned out correct is stated in the commit / PR message — so the next debugger learns
Then ask: what would have prevented this bug? If the answer involves architectural change (no good test seam, tangled callers, hidden coupling) hand off to the /improve-codebase-architecture skill with the specifics. Make the recommendation after the fix is in, not before — you have more information now than when you started.