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
- verification-before-completion - Verify fix worked before claiming success
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---67# Systematic Debugging89## Overview1011Random fixes waste time and create new bugs. Quick patches mask underlying issues.1213**Core principle:** ALWAYS find root cause before attempting fixes. Symptom fixes are failure.1415**Violating the letter of this process is violating the spirit of debugging.**1617## The Iron Law1819```20NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST21```2223If you haven't completed Phase 1, you cannot propose fixes.2425## When to Use2627Use for ANY technical issue:28- Test failures29- Bugs in production30- Unexpected behavior31- Performance problems32- Build failures33- Integration issues3435**Use this ESPECIALLY when:**36- Under time pressure (emergencies make guessing tempting)37- "Just one quick fix" seems obvious38- You've already tried multiple fixes39- Previous fix didn't work40- You don't fully understand the issue4142**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)4647## The Four Phases4849You MUST complete each phase before proceeding to the next.5051### Phase 1: Root Cause Investigation5253**BEFORE attempting ANY fix:**54551. **Read Error Messages Carefully**56 - Don't skip past errors or warnings57 - They often contain the exact solution58 - Read stack traces completely59 - Note line numbers, file paths, error codes60612. **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 guess66673. **Check Recent Changes**68 - What changed that could cause this?69 - Git diff, recent commits70 - New dependencies, config changes71 - Environmental differences72734. **Gather Evidence in Multi-Component Systems**7475 **WHEN system has multiple components (CI → build → signing, API → service → database):**7677 **BEFORE proposing fixes, add diagnostic instrumentation:**78 ```79 For EACH component boundary:80 - Log what data enters component81 - Log what data exits component82 - Verify environment/config propagation83 - Check state at each layer8485 Run once to gather evidence showing WHERE it breaks86 THEN analyze evidence to identify failing component87 THEN investigate that specific component88 ```8990 **Example (multi-layer system):**91 ```bash92 # Layer 1: Workflow93 echo "=== Secrets available in workflow: ==="94 echo "IDENTITY: ${IDENTITY:+SET}${IDENTITY:-UNSET}"9596 # Layer 2: Build script97 echo "=== Env vars in build script: ==="98 env | grep IDENTITY || echo "IDENTITY not in environment"99100 # Layer 3: Signing script101 echo "=== Keychain state: ==="102 security list-keychains103 security find-identity -v104105 # Layer 4: Actual signing106 codesign --sign "$IDENTITY" --verbose=4 "$APP"107 ```108109 **This reveals:** Which layer fails (secrets → workflow ✓, workflow → build ✗)1101115. **Trace Data Flow**112113 **WHEN error is deep in call stack:**114115 See root-cause-tracing skill for backward tracing technique116117 **Quick version:**118 - Where does bad value originate?119 - What called this with bad value?120 - Keep tracing up until you find the source121 - Fix at source, not at symptom122123### Phase 2: Pattern Analysis124125**Find the pattern before fixing:**1261271. **Find Working Examples**128 - Locate similar working code in same codebase129 - What works that's similar to what's broken?1301312. **Compare Against References**132 - If implementing pattern, read reference implementation COMPLETELY133 - Don't skim - read every line134 - Understand the pattern fully before applying1351363. **Identify Differences**137 - What's different between working and broken?138 - List every difference, however small139 - Don't assume "that can't matter"1401414. **Understand Dependencies**142 - What other components does this need?143 - What settings, config, environment?144 - What assumptions does it make?145146### Phase 3: Hypothesis and Testing147148**Scientific method:**1491501. **Form Single Hypothesis**151 - State clearly: "I think X is the root cause because Y"152 - Write it down153 - Be specific, not vague1541552. **Test Minimally**156 - Make the SMALLEST possible change to test hypothesis157 - One variable at a time158 - Don't fix multiple things at once1591603. **Verify Before Continuing**161 - Did it work? Yes → Phase 4162 - Didn't work? Form NEW hypothesis163 - DON'T add more fixes on top1641654. **When You Don't Know**166 - Say "I don't understand X"167 - Don't pretend to know168 - Ask for help169 - Research more170171### Phase 4: Implementation172173**Fix the root cause, not the symptom:**1741751. **Create Failing Test Case**176 - Simplest possible reproduction177 - Automated test if possible178 - One-off test script if no framework179 - MUST have before fixing180181 ```bash182 # Create test with bun (preferred)183 bun test my-fix.test.ts184185 # Or with npm186 npm test -- my-fix.test.ts187 ```1881892. **Implement Single Fix**190 - Address the root cause identified191 - ONE change at a time192 - No "while I'm here" improvements193 - No bundled refactoring1941953. **Verify Fix**196 - Test passes now?197 - No other tests broken?198 - Issue actually resolved?199200 ```bash201 # Run full test suite202 bun test # or: npm test203204 # Run specific test205 bun test --grep "my fix"206 ```2072084. **If Fix Doesn't Work**209 - STOP210 - Count: How many fixes have you tried?211 - If < 3: Return to Phase 1, re-analyze with new information212 - **If ≥ 3: STOP and question the architecture (step 5 below)**213 - DON'T attempt Fix #4 without architectural discussion2142155. **If 3+ Fixes Failed: Question Architecture**216217 **Pattern indicating architectural problem:**218 - Each fix reveals new shared state/coupling/problem in different place219 - Fixes require "massive refactoring" to implement220 - Each fix creates new symptoms elsewhere221222 **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?226227 **Discuss with your human partner before attempting more fixes**228229 This is NOT a failed hypothesis - this is a wrong architecture.230231## Red Flags - STOP and Follow Process232233If 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 flow243- **"One more fix attempt" (when already tried 2+)**244- **Each fix reveals new problem in different place**245246**ALL of these mean: STOP. Return to Phase 1.**247248**If 3+ fixes failed:** Question the architecture (see Phase 4.5)249250## Common Rationalizations251252| 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. |262263## Quick Reference264265| 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 |271272## When Process Reveals "No Root Cause"273274If systematic investigation reveals issue is truly environmental, timing-dependent, or external:2752761. You've completed the process2772. Document what you investigated2783. Implement appropriate handling (retry, timeout, error message)2794. Add monitoring/logging for future investigation280281**But:** 95% of "no root cause" cases are incomplete investigation.282283## Integration with Other Skills284285This skill works with:286- root-cause-tracing - How to trace back through call stack287- defense-in-depth-validation - Add validation after finding root cause288- verification-before-completion - Verify fix worked before claiming success289290## Real-World Impact291292From debugging sessions:293- Systematic approach: 15-30 minutes to fix294- Random fixes approach: 2-3 hours of thrashing295- First-time fix rate: 95% vs 40%296- New bugs introduced: Near zero vs common