Systematic Debugging
Random fixes waste time. Quick patches mask issues.
Core principle: ALWAYS find root cause before fixes. Symptom fixes are failure.
The Iron Law
NO FIXES WITHOUT ROOT CAUSE INVESTIGATION
When to Use
ANY technical issue: test failures, bugs, unexpected behavior, performance, builds, integration.
ESPECIALLY when:
- Under time pressure
- "Just one quick fix" seems obvious
- Already tried multiple fixes
- Previous fix didn't work
- Don't fully understand issue
Don't skip when:
- Seems simple (simple bugs have root causes)
- You're hurrying (systematic is faster than thrashing)
- Manager wants NOW (systematic prevents rework)
Four Phases
Phase 1: Root Cause Investigation
For test failures, check flakiness FIRST:
Test fails → Run 5x
├─ Passes 5/5: Not flaky, investigate as bug
├─ Fails 5/5: Consistent, investigate as bug
└─ Mixed (3/5): FLAKY TEST - fix test first
Flaky test checklist:
| Check |
How |
Fix |
| Isolated/connected? |
Run single vs suite |
State pollution |
| Timing-dependent? |
Look for timeouts/sleeps |
Condition-based waiting |
| Environment-specific? |
CI vs local |
Mock env vars |
| Order-dependent? |
Different order |
Setup/teardown |
| Race condition? |
Async without waits |
Proper async/await |
Then continue:
- Read Errors Carefully - Stack traces, line numbers, error codes
- Reproduce Consistently - Exact steps, happens every time?
- Check Recent Changes - Git diff, dependencies, config
- Multi-Component Systems - Add diagnostic instrumentation at boundaries BEFORE proposing fixes
- Trace Data Flow - Where does bad value originate? (See pop-root-cause-tracing)
Phase 2: Pattern Analysis
- Find Working Examples - Similar code that works
- Compare References - Read reference implementations COMPLETELY
- Identify Differences - List ALL differences
- Understand Dependencies - Config, environment, assumptions
Phase 3: Hypothesis & Testing
- Form Single Hypothesis - "I think X causes Y because Z"
- Test Minimally - Smallest change, one variable
- Verify - Worked? → Phase 4. Didn't? → New hypothesis
- When Unknown - Say "I don't understand X", ask for help
Phase 4: Implementation
- Create Failing Test - Use test-driven-development skill
- Implement Single Fix - Address root cause, ONE change
- Verify Fix - Test passes, no other tests broken
- If Fix Doesn't Work
- STOP. Count fixes tried.
- If < 3: Return to Phase 1 with new info
- If >= 3: STOP. Question architecture (see below)
- If 3+ Fixes Failed: Question Architecture
- Each fix reveals new problems elsewhere
- Fixes require "massive refactoring"
- Pattern fundamentally unsound?
- Discuss with user before more fixes
Red Flags
STOP if thinking:
- "Quick fix for now"
- "Just try X and see"
- "Add multiple changes"
- "Skip test, manually verify"
- "It's probably X"
- "Don't fully understand but..."
- "One more fix" (after 2+)
ALL → Return to Phase 1
3+ failures → Question architecture
Quick Reference
| Phase |
Key Activities |
Success |
| 1. Root Cause |
Read errors, reproduce, gather evidence |
Understand WHAT & WHY |
| 2. Pattern |
Find working examples, compare |
Identify differences |
| 3. Hypothesis |
Form theory, test minimally |
Confirmed or new |
| 4. Implement |
Test, fix, verify |
Resolved, tests pass |
Real-World Impact
- Systematic: 15-30min to fix, 95% first-time success, near-zero new bugs
- Random: 2-3h thrashing, 40% success, common new bugs
Cross-References
- Flaky tests: pop-test-driven-development (Condition-Based Waiting)
- Root cause tracing: pop-root-cause-tracing (backward tracing)
- Defense: pop-defense-in-depth (multi-layer validation)
Examples
See examples/ for:
flaky-test-patterns.md - Common flaky test causes & fixes
debugging-flowchart.pdf - Visual decision tree
multi-component-diagnostic.md - Instrumentation strategy
1---2name: systematic-debugging-43description: Four-phase debugging: root cause → patterns → hypothesis → implement. For complex bugs, test failures, multi-component issues. NOT for obvious syntax errors.4---5
6# Systematic Debugging
7
8Random fixes waste time. Quick patches mask issues.
9
10**Core principle:** ALWAYS find root cause before fixes. Symptom fixes are failure.
11
12## The Iron Law
13
14```
15NO FIXES WITHOUT ROOT CAUSE INVESTIGATION
16```
17
18## When to Use
19
20ANY technical issue: test failures, bugs, unexpected behavior, performance, builds, integration.
21
22**ESPECIALLY when:**
23
24- Under time pressure
25- "Just one quick fix" seems obvious
26- Already tried multiple fixes
27- Previous fix didn't work
28- Don't fully understand issue
29
30**Don't skip when:**
31
32- Seems simple (simple bugs have root causes)
33- You're hurrying (systematic is faster than thrashing)
34- Manager wants NOW (systematic prevents rework)
35
36## Four Phases
37
38### Phase 1: Root Cause Investigation
39
40**For test failures, check flakiness FIRST:**
41
42```
43Test fails → Run 5x
44├─ Passes 5/5: Not flaky, investigate as bug
45├─ Fails 5/5: Consistent, investigate as bug
46└─ Mixed (3/5): FLAKY TEST - fix test first
47```
48
49**Flaky test checklist:**
50
51| Check | How | Fix |
52| --------------------- | ------------------------ | ----------------------- |
53| Isolated/connected? | Run single vs suite | State pollution |
54| Timing-dependent? | Look for timeouts/sleeps | Condition-based waiting |
55| Environment-specific? | CI vs local | Mock env vars |
56| Order-dependent? | Different order | Setup/teardown |
57| Race condition? | Async without waits | Proper async/await |
58
59**Then continue:**
60
611. **Read Errors Carefully** - Stack traces, line numbers, error codes
622. **Reproduce Consistently** - Exact steps, happens every time?
633. **Check Recent Changes** - Git diff, dependencies, config
644. **Multi-Component Systems** - Add diagnostic instrumentation at boundaries BEFORE proposing fixes
655. **Trace Data Flow** - Where does bad value originate? (See pop-root-cause-tracing)
66
67### Phase 2: Pattern Analysis
68
691. **Find Working Examples** - Similar code that works
702. **Compare References** - Read reference implementations COMPLETELY
713. **Identify Differences** - List ALL differences
724. **Understand Dependencies** - Config, environment, assumptions
73
74### Phase 3: Hypothesis & Testing
75
761. **Form Single Hypothesis** - "I think X causes Y because Z"
772. **Test Minimally** - Smallest change, one variable
783. **Verify** - Worked? → Phase 4. Didn't? → New hypothesis
794. **When Unknown** - Say "I don't understand X", ask for help
80
81### Phase 4: Implementation
82
831. **Create Failing Test** - Use test-driven-development skill
842. **Implement Single Fix** - Address root cause, ONE change
853. **Verify Fix** - Test passes, no other tests broken
864. **If Fix Doesn't Work**
87 - STOP. Count fixes tried.
88 - If < 3: Return to Phase 1 with new info
89 - **If >= 3: STOP. Question architecture** (see below)
905. **If 3+ Fixes Failed: Question Architecture**
91 - Each fix reveals new problems elsewhere
92 - Fixes require "massive refactoring"
93 - Pattern fundamentally unsound?
94 - Discuss with user before more fixes
95
96## Red Flags
97
98STOP if thinking:
99
100- "Quick fix for now"
101- "Just try X and see"
102- "Add multiple changes"
103- "Skip test, manually verify"
104- "It's probably X"
105- "Don't fully understand but..."
106- "One more fix" (after 2+)
107
108**ALL → Return to Phase 1**
109
110**3+ failures → Question architecture**
111
112## Quick Reference
113
114| Phase | Key Activities | Success |
115| ------------- | --------------------------------------- | --------------------- |
116| 1. Root Cause | Read errors, reproduce, gather evidence | Understand WHAT & WHY |
117| 2. Pattern | Find working examples, compare | Identify differences |
118| 3. Hypothesis | Form theory, test minimally | Confirmed or new |
119| 4. Implement | Test, fix, verify | Resolved, tests pass |
120
121## Real-World Impact
122
123- Systematic: 15-30min to fix, 95% first-time success, near-zero new bugs
124- Random: 2-3h thrashing, 40% success, common new bugs
125
126## Cross-References
127
128- **Flaky tests:** pop-test-driven-development (Condition-Based Waiting)
129- **Root cause tracing:** pop-root-cause-tracing (backward tracing)
130- **Defense:** pop-defense-in-depth (multi-layer validation)
131
132## Examples
133
134See `examples/` for:
135
136- `flaky-test-patterns.md` - Common flaky test causes & fixes
137- `debugging-flowchart.pdf` - Visual decision tree
138- `multi-component-diagnostic.md` - Instrumentation strategy