跳转到内容

diagnosing-bugs

这是一套排查疑难 bug 的方法。只有明确说明理由时,才跳过阶段。

探索代码库时,如果有 GLOSSARY.md,先读它,弄清相关模块的概念;也要查看你要改动区域的架构决策记录(ADR)。

这个 skill 会让你展示命令、输出和捕获的产物。先把所有密钥脱敏,用 <REDACTED> 替代。围绕环境变量构建循环,让凭据留在环境变量里,不要出现在展示内容中。捕获的产物可能带有认证头,只引用能体现问题的行。

如果脱敏后的输出不足以诊断 bug,就说明这一点并询问用户。

这就是整个 skill 的核心。 针对 bug 建立一个紧凑的通过/失败信号。只要它能因为这个 bug 而失败,你就能找到原因;二分查找、假设验证和插桩都只是利用这个信号的手段。如果没有这个信号,盯着代码看多久也没用。

把大部分精力花在这里。要积极尝试,发挥创意,不要轻易放弃。

构建反馈循环的方法,大致按优先顺序排列

Section titled “构建反馈循环的方法,大致按优先顺序排列”
  1. 失败的测试:在能触及 bug 的接缝处编写,可以是单元测试、集成测试或端到端测试。
  2. Curl / HTTP 脚本:针对正在运行的开发服务器。
  3. CLI 调用:使用夹具输入,并将标准输出与已知正确的快照进行比较。
  4. 无头浏览器脚本:用 Playwright / Puppeteer 驱动界面,并对 DOM、控制台或网络请求作出断言。
  5. 重放捕获的追踪记录。 把真实网络请求、负载或事件日志存到磁盘,再单独通过代码路径重放。
  6. 临时 harness。 启动系统的最小子集,例如一个服务和模拟依赖,用一次函数调用触发 bug 的代码路径。
  7. 属性 / 模糊测试循环。 如果 bug 表现为「有时输出错误」,就运行 1000 个随机输入,寻找失败模式。
  8. 二分查找 harness。 如果 bug 出现在两个已知状态之间,例如提交、数据集或版本之间,就自动执行「在状态 X 启动并检查,然后重复」,以便运行 git bisect run。
  9. 差异对比循环。 用相同输入运行旧版本与新版本,或两种配置,并比较输出差异。
  10. 人工参与的 bash 脚本。 最后才用。如果必须由人点击,就用 scripts/hitl-loop.template.sh 驱动操作,让整个循环仍有条理。捕获的输出会反馈给你。

建立合适的反馈循环,bug 就解决了九成。

把循环当作一个产品来打磨。建立循环后,继续收紧它:

  • 能不能更快?比如缓存初始化结果、跳过无关的初始化、缩小测试范围。
  • 能不能让信号更明确?断言具体症状,而不是只检查「没有崩溃」。
  • 能不能让结果更确定?固定时间、随机数种子,隔离文件系统,冻结网络。

一个 30 秒且不稳定的循环,只比没有好一点;一个 2 秒且确定的循环,就是调试利器。

目标不是得到干净的复现,而是提高复现率。把触发操作循环 100 次、并行运行、增加压力、缩小时间窗口、插入休眠。复现率为 50% 的 bug 可以调试;1% 的复现率不行,所以要继续提高,直到能调试为止。

明确说明并停下来。列出你尝试过的办法,并向用户索取:(a)能复现问题的环境访问权限;(b)脱敏后的捕获产物,例如 HAR 文件、日志转储、核心转储或带时间戳的屏幕录制;或(c)添加临时生产环境插桩的许可。没有循环,就不要继续猜测。

完成标准:循环紧凑,而且能触发失败

Section titled “完成标准:循环紧凑,而且能触发失败”

循环紧凑且能触发失败时,阶段 1 才算完成:你能说出一条命令,例如脚本路径、测试调用或 curl,并且已经至少运行过一次(展示调用方式和脱敏后的输出)。这条命令还必须满足:

  • 能触发失败:它会走过实际的 bug 代码路径,并断言用户描述的确切症状。这样 bug 出现时它会失败,修好后会通过。不能只检查「有没有报错」,必须能抓住这个具体 bug。
  • **结果确定:**每次运行都有相同结论。对于不稳定的 bug,要按前文所述固定高复现率。
  • **速度快:**几秒,而不是几分钟。
  • **代理可以运行:**无需人工值守即可运行;只有 scripts/hitl-loop.template.sh 中的人机循环可以由人参与。

如果你发现自己在这条命令成形前就开始读代码、建立理论,那就停下来。跳过这一步直接猜原因,正是这个 skill 要避免的。没有能触发失败的命令,就不要进入阶段 2。

运行循环,观察 bug 出现时它如何失败。

确认:

  • 循环产生的是用户描述的故障模式,而不是碰巧出现在附近的另一个故障。找错 bug,就会修错问题。
  • 多次运行都能复现故障;如果 bug 不确定,复现率要高到足以据此调试。
  • 已捕获确切症状,例如错误消息、错误输出或耗时异常,以便后续阶段确认修复确实有效。

循环失败后,把复现缩减到仍会失败的最小场景。每次只删减一项输入、调用方、配置、数据或步骤;每删一项,都重新运行循环。只留下故障真正依赖的部分。

为什么要这样做:最小复现能减少阶段 3 的假设范围,因为可疑因素变少了;它也能成为阶段 5 的干净回归测试。

当剩下的每个要素都不可或缺时,就算完成:删掉任何一个,循环都会通过。

复现并缩小范围之前,不要继续。

测试任何假设之前,先列出 3 到 5 个按可能性排序的假设。只列一个假设,很容易被第一个看似合理的想法带偏。

每个假设都必须可以证伪:写明它预测会发生什么。

格式:「如果原因是 ,那么改动 会让 bug 消失;改动 会让情况变糟。」

如果无法写出预测,那这只是直觉,应该舍弃或进一步明确。

测试前先把排序后的列表展示给用户。 用户可能掌握领域知识,能立刻调整排序,比如「我们刚部署了第 3 项相关改动」;也可能知道哪些假设已经排除。这是一个成本很低、能省很多时间的检查点。不要因此停下;如果用户暂时不在,就按你的排序继续。

每个探查都必须对应阶段 3 中的一个具体预测。每次只改一个变量。

工具优先顺序:

  1. 调试器 / REPL 检查:如果环境支持就用。一个断点胜过十条日志。
  2. 针对性的日志:加在能区分不同假设的边界处。
  3. 不要「什么都记下来再用 grep 搜」。

给每条调试日志加唯一前缀,例如 [DEBUG-a4f2]。最后清理时,grep 这个前缀就能一次找全。没加前缀的日志可能会遗留;加了前缀的则要清掉。

性能分支。 性能回归时通常不该用日志。先建立基准测量,例如计时 harness、performance.now()、分析器或查询计划,然后再做二分查找。先测量,再修复。

先写回归测试,再修复,但前提是存在合适的接缝。

所谓合适的接缝,是指测试能在 bug 实际发生的调用点复现真正的 bug 模式。如果可用的接缝太浅,例如只测单个调用方,但 bug 需要多个调用方,或单元测试无法复现触发 bug 的调用链,那么在那里添加回归测试只会带来虚假的信心。

如果没有合适的接缝,这本身就是发现。 这说明代码库架构让 bug 无法被锁定。把它留给下一阶段处理。

如果存在合适的接缝:

  1. 在该接缝处把最小复现改成失败测试。
  2. 确认测试失败。如果你通过修改代码或夹具强行让测试变红,先用 diff 和未改动的副本比较,确认修改确实生效,再相信测试结果。
  3. 应用修复。
  4. 确认测试通过。
  5. 再用原始的、未缩减的场景运行阶段 1 的反馈循环。

宣布完成前必须做到:

  • 原始复现已无法重现问题(重新运行阶段 1 的循环)。
  • 回归测试通过(或已记录不存在合适接缝)。
  • 已移除所有 [DEBUG-...] 插桩(用 grep 搜索此前缀)。
  • 已删除临时 prototype(或移到清楚标记的调试位置)。
  • 在提交 / PR 说明中写明最终确认的假设,让下一位调试者也能学到经验。

读者可以点名用它,模型也可以自己调用它。它在 Claude Code 插件里。

介绍一套排查疑难 bug 和性能回归的方法,重点是建立快速、确定且能触发故障的反馈循环,再逐步复现、验证和修复问题。

Diagnosis loop for hard bugs and performance regressions. Use when the user says "diagnose"/"debug this", or reports something broken/throwing/failing/slow.
英文 SKILL.md
---
name: diagnosing-bugs
description: Diagnosis loop for hard bugs and performance regressions. Use when the user says "diagnose"/"debug this", or reports something broken/throwing/failing/slow.
---
# Diagnosing Bugs
A discipline for hard bugs. Skip phases only when explicitly justified.
When exploring the codebase, read `GLOSSARY.md` (if it exists) to get a clear mental model of the relevant modules, and check ADRs in the area you're touching.
## Redact
This skill has you show commands, outputs and captured artifacts. **Redact every secret first**: write `<REDACTED>` in its place. Build loops against env vars, so the credential stays in the environment rather than in what you show. Captured artifacts carry auth headers: quote only the lines that carry the signal.
If the redacted output is not enough to diagnose the bug, say so and ask the user.
## Phase 1: Build a feedback loop
**This is the skill.** Everything else is mechanical. If you have a **tight** pass/fail signal for the bug (one that goes red on _this_ bug), you will find the cause; bisection, hypothesis-testing, and instrumentation all just consume it. 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, in roughly this order
1. **Failing test** at whatever seam reaches the bug: unit, integration, e2e.
2. **Curl / HTTP script** against a running dev server.
3. **CLI invocation** with a fixture input, diffing stdout against a known-good snapshot.
4. **Headless browser script** (Playwright / Puppeteer) that drives the UI and asserts on DOM/console/network.
5. **Replay a captured trace.** Save a real network request / payload / event log to disk; replay it through the code path in isolation.
6. **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.
7. **Property / fuzz loop.** If the bug is "sometimes wrong output", run 1000 random inputs and look for the failure mode.
8. **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 run` it.
9. **Differential loop.** Run the same input through old-version vs new-version (or two configs) and diff outputs.
10. **HITL bash script.** Last resort. If a human must click, drive _them_ with `scripts/hitl-loop.template.sh` so the loop is still structured. Captured output feeds back to you.
Build the right feedback loop, and the bug is 90% fixed.
### Tighten the loop
Treat the loop as a product. Once you have _a_ loop, **tighten** it:
- 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 one is tight, 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, so 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 redacted 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.
### Completion criterion: a tight loop that goes red
Phase 1 is done when the loop is **tight** and **red-capable**: you can name **one command** (a script path, a test invocation, a curl) that you have **already run at least once** (show the invocation and its output, redacted), and that is:
- [ ] **Red-capable**: it drives the actual bug code path and asserts the **user's exact symptom**, so it can go red on this bug and green once fixed. Not "runs without erroring"; it must be able to _catch this specific bug_.
- [ ] **Deterministic**: same verdict every run (flaky bugs: a pinned, high reproduction rate, per above).
- [ ] **Fast**: seconds, not minutes.
- [ ] **Agent-runnable**: you can run it unattended; a human in the loop only via `scripts/hitl-loop.template.sh`.
If you catch yourself reading code to build a theory before this command exists, **stop: jumping straight to a hypothesis is the exact failure this skill prevents.** No red-capable command, no Phase 2.
## Phase 2: Reproduce + minimise
Run the loop. Watch it go red as the bug appears.
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.
### Minimise
Once it's red, shrink the repro to the **smallest scenario that still goes red**. Cut inputs, callers, config, data, and steps **one at a time**, re-running the loop after each cut, and keep only what's load-bearing for the failure.
Why bother: a minimal repro shrinks the hypothesis space in Phase 3 (fewer moving parts left to suspect) and becomes the clean regression test in Phase 5.
Done when **every remaining element is load-bearing**: removing any one of them makes the loop go green.
Do not proceed until you have reproduced **and** minimised.
## 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 <X> is the cause, then <changing Y> will make the bug disappear / <changing Z> 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:
1. **Debugger / REPL inspection** if the env supports it. One breakpoint beats ten logs.
2. **Targeted logs** at the boundaries that distinguish hypotheses.
3. 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:
1. Turn the minimised repro into a failing test at that seam.
2. Watch it fail. If you forced the red by mutating code or a fixture, `diff` against a pristine copy to prove the mutation landed before you trust it.
3. Apply the fix.
4. Watch it pass.
5. Re-run the Phase 1 feedback loop against the original (un-minimised) scenario.
## Phase 6: Cleanup
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 (`grep` the 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

非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。

技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock

鲁ICP备2025189036号