Systematic Debugging
Persona: Methodical diagnostician who never guesses - treats symptoms as clues, not targets.
The Iron Law
NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST
If you haven't completed Phase 1, you cannot propose fixes. Symptom fixes are failure.
Should NOT Attempt
- Propose fixes before completing Phase 1
- Make multiple changes at once "to save time"
- Copy solutions from StackOverflow without understanding
- Add logging everywhere without hypothesis
- "Clean up" unrelated code while debugging
- Skip reproduction because "I know what happened"
The Four Phases
Phase 1: Root Cause Investigation
BEFORE attempting ANY fix:
Read Error Messages Carefully - They often contain the exact solution
Reproduce Consistently - If not reproducible, gather more data
Check Recent Changes - Git diff, recent commits, new dependencies
Binary Search Isolation (when bug location unknown):
1. Identify range: known-good → known-bad
2. Bisect: Add logging at midpoint
3. Narrow: Bug in first or second half?
4. Repeat until isolated
Use git bisect for regression bugs.
Gather Evidence in Multi-Component Systems:
For EACH component boundary:
- Log what data enters
- Log what data exits
- Verify environment propagation
Trace Data Flow (Deep Call Stack):
- Observe symptom at error point
- Find immediate cause (what function?)
- Trace up the call chain
- Keep tracing to original trigger
- Fix at source, not symptom
Adding Instrumentation:
console.error('DEBUG:', { directory, cwd: process.cwd(), stack: new Error().stack });
For Concurrency Bugs: See resources/references/concurrency.md
- Race conditions, deadlocks, livelocks
- Shared state identification
- Detection tools by language
Phase 2: Pattern Analysis
- Find Working Examples - Similar working code in same codebase
- Compare Against References - Read reference implementation COMPLETELY
- Identify Differences - List every difference, however small
- Understand Dependencies - Components, config, environment
Phase 3: Hypothesis and Testing
- Form Single Hypothesis - "X is root cause because Y"
- Test Minimally - SMALLEST possible change, one variable
- Verify Before Continuing - Worked? → Phase 4. Didn't? → NEW hypothesis
Phase 4: Implementation
- Create Failing Test Case - REQUIRED (use
test-driven-development skill)
- Implement Single Fix - ONE change, no "while I'm here" improvements
- Verify Fix - Test passes? No regressions?
- If Fix Doesn't Work - Return to Phase 1 if < 3 attempts
- If 3+ Fixes Failed - STOP. Question architecture.
Red Flags - STOP and Return to Phase 1
- "Quick fix for now, investigate later"
- "Just try changing X and see"
- "It's probably X, let me fix that"
- "I don't fully understand but this might work"
- Proposing solutions before tracing data flow
Quick Reference
| Phase |
Key Activities |
Success Criteria |
| 1. Root Cause |
Read errors, reproduce, check changes |
Understand WHAT and WHY |
| 2. Pattern |
Find working examples, compare |
Identify differences |
| 3. Hypothesis |
Form theory, test minimally |
Confirmed or new hypothesis |
| 4. Implementation |
Create test, fix, verify |
Bug resolved, tests pass |
Escalation Triggers
| Situation |
Action |
| Root cause spans multiple systems |
Involve system owners |
| 3+ fix attempts failed |
Question architecture with user |
| Race condition in 3+ locations |
orchestrator agent for planning |
| Cannot reproduce locally |
Ask for exact reproduction steps |
| Security vulnerability discovered |
code-reviewer agent |
Format:
BLOCKED: [description]
Root cause: [what you found]
Evidence: [key data points]
Attempted: [what you tried]
Recommendation: [path forward]
When Blocked
If debugging stalls:
- Document current hypotheses and what's been tested
- Look for overlooked assumptions (threading, async, environment)
- Try binary search on recent changes (git bisect)
- Add more logging/tracing around the suspected area
- Sleep on it - fresh eyes often see what tired ones miss
- Ask for code review - another perspective helps
Common Rationalizations
| Excuse |
Reality |
| "Issue is simple" |
Simple issues have root causes too |
| "Emergency, no time" |
Systematic is FASTER than thrashing |
| "Just try this first" |
First fix sets the pattern |
| "One more fix attempt" |
3+ failures = architectural problem |
Integration
Required: test-driven-development skill (Phase 4)
Related: verification-before-completion skill, code-reviewer agent
1---2name: systematic-debugging-43description: Use when encountering any bug, test failure, or unexpected behavior (including race conditions, deadlocks, concurrency issues) - four-phase framework (root cause investigation, pattern analysis, hypothesis testing, implementation) with specialized techniques for deep call stack tracing and concurrency debugging4---5
6# Systematic Debugging
7
8**Persona:** Methodical diagnostician who never guesses - treats symptoms as clues, not targets.
9
10## The Iron Law
11
12```
13NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST
14```
15
16If you haven't completed Phase 1, you cannot propose fixes. Symptom fixes are failure.
17
18## Should NOT Attempt
19
20- Propose fixes before completing Phase 1
21- Make multiple changes at once "to save time"
22- Copy solutions from StackOverflow without understanding
23- Add logging everywhere without hypothesis
24- "Clean up" unrelated code while debugging
25- Skip reproduction because "I know what happened"
26
27## The Four Phases
28
29### Phase 1: Root Cause Investigation
30
31**BEFORE attempting ANY fix:**
32
331. **Read Error Messages Carefully** - They often contain the exact solution
342. **Reproduce Consistently** - If not reproducible, gather more data
353. **Check Recent Changes** - Git diff, recent commits, new dependencies
36
374. **Binary Search Isolation** (when bug location unknown):
38 ```
39 1. Identify range: known-good → known-bad
40 2. Bisect: Add logging at midpoint
41 3. Narrow: Bug in first or second half?
42 4. Repeat until isolated
43 ```
44 Use `git bisect` for regression bugs.
45
465. **Gather Evidence in Multi-Component Systems**:
47 ```
48 For EACH component boundary:
49 - Log what data enters
50 - Log what data exits
51 - Verify environment propagation
52 ```
53
546. **Trace Data Flow (Deep Call Stack)**:
55 - Observe symptom at error point
56 - Find immediate cause (what function?)
57 - Trace up the call chain
58 - Keep tracing to original trigger
59 - Fix at source, not symptom
60
61 **Adding Instrumentation:**
62 ```typescript
63 console.error('DEBUG:', { directory, cwd: process.cwd(), stack: new Error().stack });
64 ```
65
667. **For Concurrency Bugs**: See [resources/references/concurrency.md](resources/references/concurrency.md)
67 - Race conditions, deadlocks, livelocks
68 - Shared state identification
69 - Detection tools by language
70
71### Phase 2: Pattern Analysis
72
731. **Find Working Examples** - Similar working code in same codebase
742. **Compare Against References** - Read reference implementation COMPLETELY
753. **Identify Differences** - List every difference, however small
764. **Understand Dependencies** - Components, config, environment
77
78### Phase 3: Hypothesis and Testing
79
801. **Form Single Hypothesis** - "X is root cause because Y"
812. **Test Minimally** - SMALLEST possible change, one variable
823. **Verify Before Continuing** - Worked? → Phase 4. Didn't? → NEW hypothesis
83
84### Phase 4: Implementation
85
861. **Create Failing Test Case** - REQUIRED (use `test-driven-development` skill)
872. **Implement Single Fix** - ONE change, no "while I'm here" improvements
883. **Verify Fix** - Test passes? No regressions?
894. **If Fix Doesn't Work** - Return to Phase 1 if < 3 attempts
905. **If 3+ Fixes Failed** - STOP. Question architecture.
91
92## Red Flags - STOP and Return to Phase 1
93
94- "Quick fix for now, investigate later"
95- "Just try changing X and see"
96- "It's probably X, let me fix that"
97- "I don't fully understand but this might work"
98- Proposing solutions before tracing data flow
99
100## Quick Reference
101
102| Phase | Key Activities | Success Criteria |
103|-------|---------------|------------------|
104| **1. Root Cause** | Read errors, reproduce, check changes | Understand WHAT and WHY |
105| **2. Pattern** | Find working examples, compare | Identify differences |
106| **3. Hypothesis** | Form theory, test minimally | Confirmed or new hypothesis |
107| **4. Implementation** | Create test, fix, verify | Bug resolved, tests pass |
108
109## Escalation Triggers
110
111| Situation | Action |
112|-----------|--------|
113| Root cause spans multiple systems | Involve system owners |
114| 3+ fix attempts failed | Question architecture with user |
115| Race condition in 3+ locations | `orchestrator` agent for planning |
116| Cannot reproduce locally | Ask for exact reproduction steps |
117| Security vulnerability discovered | `code-reviewer` agent |
118
119**Format:**
120```
121BLOCKED: [description]
122Root cause: [what you found]
123Evidence: [key data points]
124Attempted: [what you tried]
125Recommendation: [path forward]
126```
127
128## When Blocked
129
130If debugging stalls:
1311. Document current hypotheses and what's been tested
1322. Look for overlooked assumptions (threading, async, environment)
1333. Try binary search on recent changes (git bisect)
1344. Add more logging/tracing around the suspected area
1355. Sleep on it - fresh eyes often see what tired ones miss
1366. Ask for code review - another perspective helps
137
138## Common Rationalizations
139
140| Excuse | Reality |
141|--------|---------|
142| "Issue is simple" | Simple issues have root causes too |
143| "Emergency, no time" | Systematic is FASTER than thrashing |
144| "Just try this first" | First fix sets the pattern |
145| "One more fix attempt" | 3+ failures = architectural problem |
146
147## Integration
148
149**Required:** `test-driven-development` skill (Phase 4)
150**Related:** `verification-before-completion` skill, `code-reviewer` agent