AgentSkillsCN

debugging

系统化的调试框架——结构化的根本原因分析、基于假设的调试、证据收集、纵深防御式验证、回归问题预防。适用于修复 Bug、排查故障、诊断性能问题,或在生产环境中进行故障排查。

SKILL.md
--- frontmatter
name: debugging
description: Systematic debugging framework — structured root-cause analysis, hypothesis-driven debugging, evidence collection, defense-in-depth validation, regression prevention. Use when fixing bugs, investigating failures, diagnosing performance issues, or troubleshooting production incidents.
license: MIT

Systematic Debugging Mastery

A rigorous, evidence-based approach to finding and fixing bugs. Eliminates random trial-and-error with structured investigation.

The 4 Debugging Disciplines

1. Systematic Debugging

Problem: Random code changes waste time and introduce new bugs. Solution: Structured investigation with binary search and hypothesis testing.

code
OBSERVE → HYPOTHESIZE → TEST → CONCLUDE → VERIFY

Investigation Protocol

markdown
1. **Reproduce** — Create minimal reproduction case
   - Exact steps to trigger the bug
   - Expected vs actual behavior
   - Environment (OS, browser, versions)

2. **Isolate** — Narrow the scope
   - Binary search: disable half the code, which half fails?
   - Recent changes: `git bisect` to find the breaking commit
   - Dependencies: does it fail with mocked deps?

3. **Hypothesize** — Form a testable theory
   - "I believe X is causing Y because Z"
   - List ALL plausible hypotheses, rank by likelihood
   - Design a test that distinguishes between them

4. **Test** — Gather evidence (don't just "try things")
   - Add targeted logging/breakpoints
   - Check ONE hypothesis at a time
   - Record results

5. **Fix** — Apply minimal, targeted change
   - Fix the ROOT cause, not the symptom
   - Write a regression test
   - Verify the fix doesn't break anything else

2. Root Cause Tracing

Never fix symptoms. Always find the root cause.

code
Symptom: "Login page shows blank screen"
SURFACE: The React component isn't rendering
  WHY? → An error is thrown during render
    WHY? → `user.name` is undefined
      WHY? → API returns null for user object
        WHY? → Database query has wrong WHERE clause
          ROOT CAUSE: Migration changed column name but query wasn't updated

5 Whys Template

markdown
**Symptom:** [What the user sees]
1. Why? → [Immediate cause]
2. Why? → [Deeper cause]
3. Why? → [System-level cause]
4. Why? → [Process-level cause]
5. Why? → [Root cause]

**Fix:** [Address root cause, not symptom]
**Prevention:** [How to prevent recurrence]

3. Defense-in-Depth Validation

One test is not enough. Validate at every layer.

markdown
After fixing a bug, verify at ALL levels:
- [ ] Unit test: Does the fix work in isolation?
- [ ] Integration test: Does it work with real dependencies?
- [ ] E2E test: Does the user flow work end-to-end?
- [ ] Regression test: Did the fix break anything else?
- [ ] Edge cases: Does it handle null, empty, max values?
- [ ] Concurrency: Does it work under load?

4. Verification Before Completion

Never claim "it works" without evidence.

markdown
## Verification Checklist
- [ ] Bug is reproduced (I can trigger it consistently)
- [ ] Root cause is identified (not just the symptom)
- [ ] Fix is applied (minimal change, well-documented)
- [ ] Test is passing (regression test added)
- [ ] Related tests pass (full test suite green)
- [ ] Build succeeds (no compile errors, no warnings)
- [ ] Manual verification (I tested the exact user flow)

Reference Navigation

Anti-Patterns to Avoid

❌ Anti-Pattern✅ Correct Approach
Random code changesHypothesize → Test → Conclude
"It works on my machine"Reproduce in isolated environment
Fix the symptomFix the root cause
Skip verificationVerify at every layer
"I think I fixed it"Prove with tests and evidence
Blame the frameworkCheck your code first
Debug in productionReproduce locally, then fix

Quick Decision: Which Tool?

SituationTool
"When did this break?"git bisect
"What's the state at this point?"Debugger breakpoint
"What's being sent/received?"Network tab / logging
"Why is this slow?"Performance profiler
"What's the call sequence?"Stack trace / tracing
"Is this a known issue?"Error monitoring (Sentry)

Related Skills

SkillWhen to Use
code-reviewFinding bugs before they ship, review checklists
testingWriting regression tests after fixes
devopsMonitoring, logging, production debugging
databasesQuery performance analysis with EXPLAIN