Systematic Debugging
Overview
Random fixes waste time and create new bugs. Quick patches mask underlying issues.
Core principle: ALWAYS find root cause before attempting fixes. Symptom fixes are failure.
Violating the letter of this process is violating the spirit of debugging.
The Iron Law
NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST
If you haven't completed Phase 1, you cannot propose fixes.
When to Use
Use for ANY technical issue:
- Test failures
- Bugs in production
- Unexpected behavior
- Performance problems
- Build failures
- Integration issues
Use this ESPECIALLY when:
- Under time pressure (emergencies make guessing tempting)
- "Just one quick fix" seems obvious
- You've already tried multiple fixes
- Previous fix didn't work
- You don't fully understand the issue
Don't skip when:
- Issue seems simple (simple bugs have root causes too)
- You're in a hurry (rushing guarantees rework)
- Manager wants it fixed NOW (systematic is faster than thrashing)
The Four Phases
You MUST complete each phase before proceeding to the next.
Phase 1: Root Cause Investigation
BEFORE attempting ANY fix:
Read Error Messages Carefully
- Don't skip past errors or warnings
- They often contain the exact solution
- Read stack traces completely
- Note line numbers, file paths, error codes
Reproduce Consistently
- Can you trigger it reliably?
- What are the exact steps?
- Does it happen every time?
- If not reproducible → gather more data, don't guess
Check Recent Changes
- What changed that could cause this?
- Git diff, recent commits
- New dependencies, config changes
- Environmental differences
Gather Evidence in Multi-Component Systems
WHEN system has multiple components (CI → build → signing, API → service → database):
BEFORE proposing fixes, add diagnostic instrumentation:
For EACH component boundary:
- Log what data enters component
- Log what data exits component
- Verify environment/config propagation
- Check state at each layer
Run once to gather evidence showing WHERE it breaks
THEN analyze evidence to identify failing component
THEN investigate that specific component
Example (multi-layer system):
# Layer 1: Workflow
echo "=== Secrets available in workflow: ==="
echo "IDENTITY: ${IDENTITY:+SET}${IDENTITY:-UNSET}"
# Layer 2: Build script
echo "=== Env vars in build script: ==="
env | grep IDENTITY || echo "IDENTITY not in environment"
# Layer 3: Signing script
echo "=== Keychain state: ==="
security list-keychains
security find-identity -v
# Layer 4: Actual signing
codesign --sign "$IDENTITY" --verbose=4 "$APP"
This reveals: Which layer fails (secrets → workflow ✓, workflow → build ✗)
Trace Data Flow
WHEN error is deep in call stack:
See root-cause-tracing skill for backward tracing technique
Quick version:
- Where does bad value originate?
- What called this with bad value?
- Keep tracing up until you find the source
- Fix at source, not at symptom
Phase 2: Pattern Analysis
Find the pattern before fixing:
Find Working Examples
- Locate similar working code in same codebase
- What works that's similar to what's broken?
Compare Against References
- If implementing pattern, read reference implementation COMPLETELY
- Don't skim - read every line
- Understand the pattern fully before applying
Identify Differences
- What's different between working and broken?
- List every difference, however small
- Don't assume "that can't matter"
Understand Dependencies
- What other components does this need?
- What settings, config, environment?
- What assumptions does it make?
Phase 3: Hypothesis and Testing
Scientific method:
Form Single Hypothesis
- State clearly: "I think X is the root cause because Y"
- Write it down
- Be specific, not vague
Test Minimally
- Make the SMALLEST possible change to test hypothesis
- One variable at a time
- Don't fix multiple things at once
Verify Before Continuing
- Did it work? Yes → Phase 4
- Didn't work? Form NEW hypothesis
- DON'T add more fixes on top
When You Don't Know
- Say "I don't understand X"
- Don't pretend to know
- Ask for help
- Research more
Phase 4: Implementation
Fix the root cause, not the symptom:
Create Failing Test Case
- Simplest possible reproduction
- Automated test if possible
- One-off test script if no framework
- MUST have before fixing
# Create test with bun (preferred)
bun test my-fix.test.ts
# Or with npm
npm test -- my-fix.test.ts
Implement Single Fix
- Address the root cause identified
- ONE change at a time
- No "while I'm here" improvements
- No bundled refactoring
Verify Fix
- Test passes now?
- No other tests broken?
- Issue actually resolved?
# Run full test suite
bun test # or: npm test
# Run specific test
bun test --grep "my fix"
If Fix Doesn't Work
- STOP
- Count: How many fixes have you tried?
- If < 3: Return to Phase 1, re-analyze with new information
- If ≥ 3: STOP and question the architecture (step 5 below)
- DON'T attempt Fix #4 without architectural discussion
If 3+ Fixes Failed: Question Architecture
Pattern indicating architectural problem:
- Each fix reveals new shared state/coupling/problem in different place
- Fixes require "massive refactoring" to implement
- Each fix creates new symptoms elsewhere
STOP and question fundamentals:
- Is this pattern fundamentally sound?
- Are we "sticking with it through sheer inertia"?
- Should we refactor architecture vs. continue fixing symptoms?
Discuss with your human partner before attempting more fixes
This is NOT a failed hypothesis - this is a wrong architecture.
Red Flags - STOP and Follow Process
If you catch yourself thinking:
- "Quick fix for now, investigate later"
- "Just try changing X and see if it works"
- "Add multiple changes, run tests"
- "Skip the test, I'll manually verify"
- "It's probably X, let me fix that"
- "I don't fully understand but this might work"
- "Pattern says X but I'll adapt it differently"
- "Here are the main problems: [lists fixes without investigation]"
- Proposing solutions before tracing data flow
- "One more fix attempt" (when already tried 2+)
- Each fix reveals new problem in different place
ALL of these mean: STOP. Return to Phase 1.
If 3+ fixes failed: Question the architecture (see Phase 4.5)
Common Rationalizations
| Excuse |
Reality |
| "Issue is simple, don't need process" |
Simple issues have root causes too. Process is fast for simple bugs. |
| "Emergency, no time for process" |
Systematic debugging is FASTER than guess-and-check thrashing. |
| "Just try this first, then investigate" |
First fix sets the pattern. Do it right from the start. |
| "I'll write test after confirming fix works" |
Untested fixes don't stick. Test first proves it. |
| "Multiple fixes at once saves time" |
Can't isolate what worked. Causes new bugs. |
| "Reference too long, I'll adapt the pattern" |
Partial understanding guarantees bugs. Read it completely. |
| "I see the problem, let me fix it" |
Seeing symptoms ≠ understanding root cause. |
| "One more fix attempt" (after 2+ failures) |
3+ failures = architectural problem. Question pattern, don't fix again. |
Quick Reference
| Phase |
Key Activities |
Success Criteria |
| 1. Root Cause |
Read errors, reproduce, check changes, gather evidence |
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 |
When Process Reveals "No Root Cause"
If systematic investigation reveals issue is truly environmental, timing-dependent, or external:
- You've completed the process
- Document what you investigated
- Implement appropriate handling (retry, timeout, error message)
- Add monitoring/logging for future investigation
But: 95% of "no root cause" cases are incomplete investigation.
Integration with Other Skills
This skill works with:
- root-cause-tracing - How to trace back through call stack
- defense-in-depth-validation - Add validation after finding root cause
Real-World Impact
From debugging sessions:
- Systematic approach: 15-30 minutes to fix
- Random fixes approach: 2-3 hours of thrashing
- First-time fix rate: 95% vs 40%
- New bugs introduced: Near zero vs common
1---2name: systematic-debugging3description: Four-phase debugging framework that ensures root cause investigation before attempting fixes. Never jump to solutions. Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes.4license: MIT5---6
7# Systematic Debugging
8
9## Overview
10
11Random fixes waste time and create new bugs. Quick patches mask underlying issues.
12
13**Core principle:** ALWAYS find root cause before attempting fixes. Symptom fixes are failure.
14
15**Violating the letter of this process is violating the spirit of debugging.**
16
17## The Iron Law
18
19```
20NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST
21```
22
23If you haven't completed Phase 1, you cannot propose fixes.
24
25## When to Use
26
27Use for ANY technical issue:
28- Test failures
29- Bugs in production
30- Unexpected behavior
31- Performance problems
32- Build failures
33- Integration issues
34
35**Use this ESPECIALLY when:**
36- Under time pressure (emergencies make guessing tempting)
37- "Just one quick fix" seems obvious
38- You've already tried multiple fixes
39- Previous fix didn't work
40- You don't fully understand the issue
41
42**Don't skip when:**
43- Issue seems simple (simple bugs have root causes too)
44- You're in a hurry (rushing guarantees rework)
45- Manager wants it fixed NOW (systematic is faster than thrashing)
46
47## The Four Phases
48
49You MUST complete each phase before proceeding to the next.
50
51### Phase 1: Root Cause Investigation
52
53**BEFORE attempting ANY fix:**
54
551. **Read Error Messages Carefully**
56 - Don't skip past errors or warnings
57 - They often contain the exact solution
58 - Read stack traces completely
59 - Note line numbers, file paths, error codes
60
612. **Reproduce Consistently**
62 - Can you trigger it reliably?
63 - What are the exact steps?
64 - Does it happen every time?
65 - If not reproducible → gather more data, don't guess
66
673. **Check Recent Changes**
68 - What changed that could cause this?
69 - Git diff, recent commits
70 - New dependencies, config changes
71 - Environmental differences
72
734. **Gather Evidence in Multi-Component Systems**
74
75 **WHEN system has multiple components (CI → build → signing, API → service → database):**
76
77 **BEFORE proposing fixes, add diagnostic instrumentation:**
78 ```
79 For EACH component boundary:
80 - Log what data enters component
81 - Log what data exits component
82 - Verify environment/config propagation
83 - Check state at each layer
84
85 Run once to gather evidence showing WHERE it breaks
86 THEN analyze evidence to identify failing component
87 THEN investigate that specific component
88 ```
89
90 **Example (multi-layer system):**
91 ```bash
92 # Layer 1: Workflow
93 echo "=== Secrets available in workflow: ==="
94 echo "IDENTITY: ${IDENTITY:+SET}${IDENTITY:-UNSET}"
95
96 # Layer 2: Build script
97 echo "=== Env vars in build script: ==="
98 env | grep IDENTITY || echo "IDENTITY not in environment"
99
100 # Layer 3: Signing script
101 echo "=== Keychain state: ==="
102 security list-keychains
103 security find-identity -v
104
105 # Layer 4: Actual signing
106 codesign --sign "$IDENTITY" --verbose=4 "$APP"
107 ```
108
109 **This reveals:** Which layer fails (secrets → workflow ✓, workflow → build ✗)
110
1115. **Trace Data Flow**
112
113 **WHEN error is deep in call stack:**
114
115 See root-cause-tracing skill for backward tracing technique
116
117 **Quick version:**
118 - Where does bad value originate?
119 - What called this with bad value?
120 - Keep tracing up until you find the source
121 - Fix at source, not at symptom
122
123### Phase 2: Pattern Analysis
124
125**Find the pattern before fixing:**
126
1271. **Find Working Examples**
128 - Locate similar working code in same codebase
129 - What works that's similar to what's broken?
130
1312. **Compare Against References**
132 - If implementing pattern, read reference implementation COMPLETELY
133 - Don't skim - read every line
134 - Understand the pattern fully before applying
135
1363. **Identify Differences**
137 - What's different between working and broken?
138 - List every difference, however small
139 - Don't assume "that can't matter"
140
1414. **Understand Dependencies**
142 - What other components does this need?
143 - What settings, config, environment?
144 - What assumptions does it make?
145
146### Phase 3: Hypothesis and Testing
147
148**Scientific method:**
149
1501. **Form Single Hypothesis**
151 - State clearly: "I think X is the root cause because Y"
152 - Write it down
153 - Be specific, not vague
154
1552. **Test Minimally**
156 - Make the SMALLEST possible change to test hypothesis
157 - One variable at a time
158 - Don't fix multiple things at once
159
1603. **Verify Before Continuing**
161 - Did it work? Yes → Phase 4
162 - Didn't work? Form NEW hypothesis
163 - DON'T add more fixes on top
164
1654. **When You Don't Know**
166 - Say "I don't understand X"
167 - Don't pretend to know
168 - Ask for help
169 - Research more
170
171### Phase 4: Implementation
172
173**Fix the root cause, not the symptom:**
174
1751. **Create Failing Test Case**
176 - Simplest possible reproduction
177 - Automated test if possible
178 - One-off test script if no framework
179 - MUST have before fixing
180
181 ```bash
182 # Create test with bun (preferred)
183 bun test my-fix.test.ts
184
185 # Or with npm
186 npm test -- my-fix.test.ts
187 ```
188
1892. **Implement Single Fix**
190 - Address the root cause identified
191 - ONE change at a time
192 - No "while I'm here" improvements
193 - No bundled refactoring
194
1953. **Verify Fix**
196 - Test passes now?
197 - No other tests broken?
198 - Issue actually resolved?
199
200 ```bash
201 # Run full test suite
202 bun test # or: npm test
203
204 # Run specific test
205 bun test --grep "my fix"
206 ```
207
2084. **If Fix Doesn't Work**
209 - STOP
210 - Count: How many fixes have you tried?
211 - If < 3: Return to Phase 1, re-analyze with new information
212 - **If ≥ 3: STOP and question the architecture (step 5 below)**
213 - DON'T attempt Fix #4 without architectural discussion
214
2155. **If 3+ Fixes Failed: Question Architecture**
216
217 **Pattern indicating architectural problem:**
218 - Each fix reveals new shared state/coupling/problem in different place
219 - Fixes require "massive refactoring" to implement
220 - Each fix creates new symptoms elsewhere
221
222 **STOP and question fundamentals:**
223 - Is this pattern fundamentally sound?
224 - Are we "sticking with it through sheer inertia"?
225 - Should we refactor architecture vs. continue fixing symptoms?
226
227 **Discuss with your human partner before attempting more fixes**
228
229 This is NOT a failed hypothesis - this is a wrong architecture.
230
231## Red Flags - STOP and Follow Process
232
233If you catch yourself thinking:
234- "Quick fix for now, investigate later"
235- "Just try changing X and see if it works"
236- "Add multiple changes, run tests"
237- "Skip the test, I'll manually verify"
238- "It's probably X, let me fix that"
239- "I don't fully understand but this might work"
240- "Pattern says X but I'll adapt it differently"
241- "Here are the main problems: [lists fixes without investigation]"
242- Proposing solutions before tracing data flow
243- **"One more fix attempt" (when already tried 2+)**
244- **Each fix reveals new problem in different place**
245
246**ALL of these mean: STOP. Return to Phase 1.**
247
248**If 3+ fixes failed:** Question the architecture (see Phase 4.5)
249
250## Common Rationalizations
251
252| Excuse | Reality |
253|--------|---------|
254| "Issue is simple, don't need process" | Simple issues have root causes too. Process is fast for simple bugs. |
255| "Emergency, no time for process" | Systematic debugging is FASTER than guess-and-check thrashing. |
256| "Just try this first, then investigate" | First fix sets the pattern. Do it right from the start. |
257| "I'll write test after confirming fix works" | Untested fixes don't stick. Test first proves it. |
258| "Multiple fixes at once saves time" | Can't isolate what worked. Causes new bugs. |
259| "Reference too long, I'll adapt the pattern" | Partial understanding guarantees bugs. Read it completely. |
260| "I see the problem, let me fix it" | Seeing symptoms ≠ understanding root cause. |
261| "One more fix attempt" (after 2+ failures) | 3+ failures = architectural problem. Question pattern, don't fix again. |
262
263## Quick Reference
264
265| Phase | Key Activities | Success Criteria |
266|-------|---------------|------------------|
267| **1. Root Cause** | Read errors, reproduce, check changes, gather evidence | Understand WHAT and WHY |
268| **2. Pattern** | Find working examples, compare | Identify differences |
269| **3. Hypothesis** | Form theory, test minimally | Confirmed or new hypothesis |
270| **4. Implementation** | Create test, fix, verify | Bug resolved, tests pass |
271
272## When Process Reveals "No Root Cause"
273
274If systematic investigation reveals issue is truly environmental, timing-dependent, or external:
275
2761. You've completed the process
2772. Document what you investigated
2783. Implement appropriate handling (retry, timeout, error message)
2794. Add monitoring/logging for future investigation
280
281**But:** 95% of "no root cause" cases are incomplete investigation.
282
283## Integration with Other Skills
284
285This skill works with:
286- root-cause-tracing - How to trace back through call stack
287- defense-in-depth-validation - Add validation after finding root cause
288
289## Real-World Impact
290
291From debugging sessions:
292- Systematic approach: 15-30 minutes to fix
293- Random fixes approach: 2-3 hours of thrashing
294- First-time fix rate: 95% vs 40%
295- New bugs introduced: Near zero vs common