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
Build a Red-Capable Feedback Loop (do this before theorizing — it is the heart of Phase 1)
- Construct one command that drives the bug's code path and asserts the user's exact symptom, so it goes red on this bug and green once fixed — not "runs without erroring"
- Make it fast and deterministic; for flaky bugs, raise the reproduction rate (loop 100×, add stress, narrow timing) until it's debuggable
- Once it's red, minimise the repro to the smallest scenario that still fails — cut inputs/callers/config one at a time until every remaining element is load-bearing
- No red-capable loop yet? Do not hypothesise. See
references/feedback-loops.md for the construction menu, tightening, non-deterministic bugs, and performance regressions
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 ✗)
Tag every diagnostic log with a unique prefix (e.g. [DEBUG-a4f2]) so removing them later is a single grep — untagged logs get left behind.
Trace Data Flow
WHEN error is deep in call stack:
See references/root-cause-tracing.md for the complete 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
At every hop, check whether the function does what its callers assume:
- Can it fail silently — return normally having done nothing?
- Does the answer change by environment or deployment shape?
- Would the test still pass if this were broken?
Answer from the code, not from guesswork. See references/root-cause-tracing.md → Interrogate Every Hop.
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 3–5 Ranked, Falsifiable Hypotheses
- Generate several before testing any — a single hypothesis anchors on the first plausible idea
- Each must make a prediction: "If X is the cause, changing Y makes the bug disappear / changing Z makes it worse." No prediction = a vibe; sharpen or discard it
- Rank by likelihood; test the cheapest-to-falsify first
- Show the ranked list to your human partner before deep testing — they often re-rank instantly ("we just deployed #3"). Don't block on it if they're AFK
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? Move to the next ranked hypothesis (regenerate the list only when it's exhausted)
- 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 (your minimised repro from Phase 1)
- Automated test if possible
- One-off test script if no framework
- MUST have before fixing
- Use the
superpowers:test-driven-development skill for writing proper failing tests
- Write it at a correct seam — one that exercises the real bug pattern at the call site. If the only available seam is too shallow to replicate what triggered the bug, that absence is itself a finding: note it and flag for architecture review (Phase 4.5) rather than settling for a test that gives false confidence
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?
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)
your human partner's Signals You're Doing It Wrong
Watch for these redirections:
- "Is that not happening?" - You assumed without verifying
- "Will it show us...?" - You should have added evidence gathering
- "Stop guessing" - You're proposing fixes without understanding
- "Ultrathink this" - Question fundamentals, not just symptoms
- "We're stuck?" (frustrated) - Your approach isn't working
When you see these: STOP. Return to Phase 1.
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.
Supporting Techniques
These techniques are part of systematic debugging and available in references/:
references/feedback-loops.md - Build a tight, red-capable reproduction loop (the heart of Phase 1): construction menu, tightening, non-deterministic bugs, performance regressions, minimising the repro. Includes the scripts/hitl-loop.template.sh human-in-the-loop harness
references/root-cause-tracing.md - Trace bugs backward through call stack to find original trigger
references/defense-in-depth.md - Add validation at multiple layers after finding root cause
references/condition-based-waiting.md - Replace arbitrary timeouts with condition polling
Related skills:
- superpowers:test-driven-development - For creating failing test case (Phase 4, Step 1)
- superpowers: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: Use when encountering any bug, test failure, performance regression, or unexpected behavior, before proposing fixes4tags: [workflow, debugging]5---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:2829- Test failures30- Bugs in production31- Unexpected behavior32- Performance problems33- Build failures34- Integration issues3536**Use this ESPECIALLY when:**3738- Under time pressure (emergencies make guessing tempting)39- "Just one quick fix" seems obvious40- You've already tried multiple fixes41- Previous fix didn't work42- You don't fully understand the issue4344**Don't skip when:**4546- Issue seems simple (simple bugs have root causes too)47- You're in a hurry (rushing guarantees rework)48- Manager wants it fixed NOW (systematic is faster than thrashing)4950## The Four Phases5152You MUST complete each phase before proceeding to the next.5354### Phase 1: Root Cause Investigation5556**BEFORE attempting ANY fix:**57581. **Read Error Messages Carefully**59 - Don't skip past errors or warnings60 - They often contain the exact solution61 - Read stack traces completely62 - Note line numbers, file paths, error codes63642. **Build a Red-Capable Feedback Loop** *(do this before theorizing — it is the heart of Phase 1)*65 - Construct one command that drives the bug's code path and asserts the user's **exact** symptom, so it goes **red** on this bug and **green** once fixed — not "runs without erroring"66 - Make it fast and deterministic; for flaky bugs, raise the reproduction rate (loop 100×, add stress, narrow timing) until it's debuggable67 - Once it's red, **minimise** the repro to the smallest scenario that still fails — cut inputs/callers/config one at a time until every remaining element is load-bearing68 - No red-capable loop yet? **Do not hypothesise.** See `references/feedback-loops.md` for the construction menu, tightening, non-deterministic bugs, and performance regressions69703. **Check Recent Changes**71 - What changed that could cause this?72 - Git diff, recent commits73 - New dependencies, config changes74 - Environmental differences75764. **Gather Evidence in Multi-Component Systems**7778 **WHEN system has multiple components (CI → build → signing, API → service → database):**7980 **BEFORE proposing fixes, add diagnostic instrumentation:**8182 ```83 For EACH component boundary:84 - Log what data enters component85 - Log what data exits component86 - Verify environment/config propagation87 - Check state at each layer8889 Run once to gather evidence showing WHERE it breaks90 THEN analyze evidence to identify failing component91 THEN investigate that specific component92 ```9394 **Example (multi-layer system):**9596 ```bash97 # Layer 1: Workflow98 echo "=== Secrets available in workflow: ==="99 echo "IDENTITY: ${IDENTITY:+SET}${IDENTITY:-UNSET}"100101 # Layer 2: Build script102 echo "=== Env vars in build script: ==="103 env | grep IDENTITY || echo "IDENTITY not in environment"104105 # Layer 3: Signing script106 echo "=== Keychain state: ==="107 security list-keychains108 security find-identity -v109110 # Layer 4: Actual signing111 codesign --sign "$IDENTITY" --verbose=4 "$APP"112 ```113114 **This reveals:** Which layer fails (secrets → workflow ✓, workflow → build ✗)115116 **Tag every diagnostic log** with a unique prefix (e.g. `[DEBUG-a4f2]`) so removing them later is a single grep — untagged logs get left behind.1171185. **Trace Data Flow**119120 **WHEN error is deep in call stack:**121122 See `references/root-cause-tracing.md` for the complete backward tracing technique.123124 **Quick version:**125 - Where does bad value originate?126 - What called this with bad value?127 - Keep tracing up until you find the source128 - Fix at source, not at symptom129130 **At every hop, check whether the function does what its callers assume:**131 - Can it fail silently — return normally having done nothing?132 - Does the answer change by environment or deployment shape?133 - Would the test still pass if this were broken?134135 Answer from the code, not from guesswork. See `references/root-cause-tracing.md` → *Interrogate Every Hop*.136137### Phase 2: Pattern Analysis138139**Find the pattern before fixing:**1401411. **Find Working Examples**142 - Locate similar working code in same codebase143 - What works that's similar to what's broken?1441452. **Compare Against References**146 - If implementing pattern, read reference implementation COMPLETELY147 - Don't skim - read every line148 - Understand the pattern fully before applying1491503. **Identify Differences**151 - What's different between working and broken?152 - List every difference, however small153 - Don't assume "that can't matter"1541554. **Understand Dependencies**156 - What other components does this need?157 - What settings, config, environment?158 - What assumptions does it make?159160### Phase 3: Hypothesis and Testing161162**Scientific method:**1631641. **Form 3–5 Ranked, Falsifiable Hypotheses**165 - Generate several *before* testing any — a single hypothesis anchors on the first plausible idea166 - Each must make a prediction: "If X is the cause, changing Y makes the bug disappear / changing Z makes it worse." No prediction = a vibe; sharpen or discard it167 - Rank by likelihood; test the cheapest-to-falsify first168 - Show the ranked list to your human partner before deep testing — they often re-rank instantly ("we just deployed #3"). Don't block on it if they're AFK1691702. **Test Minimally**171 - Make the SMALLEST possible change to test hypothesis172 - One variable at a time173 - Don't fix multiple things at once1741753. **Verify Before Continuing**176 - Did it work? Yes → Phase 4177 - Didn't work? Move to the next ranked hypothesis (regenerate the list only when it's exhausted)178 - DON'T add more fixes on top1791804. **When You Don't Know**181 - Say "I don't understand X"182 - Don't pretend to know183 - Ask for help184 - Research more185186### Phase 4: Implementation187188**Fix the root cause, not the symptom:**1891901. **Create Failing Test Case**191 - Simplest possible reproduction (your minimised repro from Phase 1)192 - Automated test if possible193 - One-off test script if no framework194 - MUST have before fixing195 - Use the `superpowers:test-driven-development` skill for writing proper failing tests196 - Write it at a **correct seam** — one that exercises the real bug pattern at the call site. If the only available seam is too shallow to replicate what triggered the bug, that absence is itself a finding: note it and flag for architecture review (Phase 4.5) rather than settling for a test that gives false confidence1971982. **Implement Single Fix**199 - Address the root cause identified200 - ONE change at a time201 - No "while I'm here" improvements202 - No bundled refactoring2032043. **Verify Fix**205 - Test passes now?206 - No other tests broken?207 - Issue actually resolved?2082094. **If Fix Doesn't Work**210 - STOP211 - Count: How many fixes have you tried?212 - If < 3: Return to Phase 1, re-analyze with new information213 - **If ≥ 3: STOP and question the architecture (step 5 below)**214 - DON'T attempt Fix #4 without architectural discussion2152165. **If 3+ Fixes Failed: Question Architecture**217218 **Pattern indicating architectural problem:**219 - Each fix reveals new shared state/coupling/problem in different place220 - Fixes require "massive refactoring" to implement221 - Each fix creates new symptoms elsewhere222223 **STOP and question fundamentals:**224 - Is this pattern fundamentally sound?225 - Are we "sticking with it through sheer inertia"?226 - Should we refactor architecture vs. continue fixing symptoms?227228 **Discuss with your human partner before attempting more fixes**229230 This is NOT a failed hypothesis - this is a wrong architecture.231232## Red Flags - STOP and Follow Process233234If you catch yourself thinking:235236- "Quick fix for now, investigate later"237- "Just try changing X and see if it works"238- "Add multiple changes, run tests"239- "Skip the test, I'll manually verify"240- "It's probably X, let me fix that"241- "I don't fully understand but this might work"242- "Pattern says X but I'll adapt it differently"243- "Here are the main problems: [lists fixes without investigation]"244- Proposing solutions before tracing data flow245- **"One more fix attempt" (when already tried 2+)**246- **Each fix reveals new problem in different place**247248**ALL of these mean: STOP. Return to Phase 1.**249250**If 3+ fixes failed:** Question the architecture (see Phase 4.5)251252## your human partner's Signals You're Doing It Wrong253254**Watch for these redirections:**255256- "Is that not happening?" - You assumed without verifying257- "Will it show us...?" - You should have added evidence gathering258- "Stop guessing" - You're proposing fixes without understanding259- "Ultrathink this" - Question fundamentals, not just symptoms260- "We're stuck?" (frustrated) - Your approach isn't working261262**When you see these:** STOP. Return to Phase 1.263264## Common Rationalizations265266| Excuse | Reality |267| -------------------------------------------- | ----------------------------------------------------------------------- |268| "Issue is simple, don't need process" | Simple issues have root causes too. Process is fast for simple bugs. |269| "Emergency, no time for process" | Systematic debugging is FASTER than guess-and-check thrashing. |270| "Just try this first, then investigate" | First fix sets the pattern. Do it right from the start. |271| "I'll write test after confirming fix works" | Untested fixes don't stick. Test first proves it. |272| "Multiple fixes at once saves time" | Can't isolate what worked. Causes new bugs. |273| "Reference too long, I'll adapt the pattern" | Partial understanding guarantees bugs. Read it completely. |274| "I see the problem, let me fix it" | Seeing symptoms ≠ understanding root cause. |275| "One more fix attempt" (after 2+ failures) | 3+ failures = architectural problem. Question pattern, don't fix again. |276277## Quick Reference278279| Phase | Key Activities | Success Criteria |280| --------------------- | ------------------------------------------------------ | --------------------------- |281| **1. Root Cause** | Read errors, reproduce, check changes, gather evidence | Understand WHAT and WHY |282| **2. Pattern** | Find working examples, compare | Identify differences |283| **3. Hypothesis** | Form theory, test minimally | Confirmed or new hypothesis |284| **4. Implementation** | Create test, fix, verify | Bug resolved, tests pass |285286## When Process Reveals "No Root Cause"287288If systematic investigation reveals issue is truly environmental, timing-dependent, or external:2892901. You've completed the process2912. Document what you investigated2923. Implement appropriate handling (retry, timeout, error message)2934. Add monitoring/logging for future investigation294295**But:** 95% of "no root cause" cases are incomplete investigation.296297## Supporting Techniques298299These techniques are part of systematic debugging and available in `references/`:300301- **`references/feedback-loops.md`** - Build a tight, red-capable reproduction loop (the heart of Phase 1): construction menu, tightening, non-deterministic bugs, performance regressions, minimising the repro. Includes the `scripts/hitl-loop.template.sh` human-in-the-loop harness302- **`references/root-cause-tracing.md`** - Trace bugs backward through call stack to find original trigger303- **`references/defense-in-depth.md`** - Add validation at multiple layers after finding root cause304- **`references/condition-based-waiting.md`** - Replace arbitrary timeouts with condition polling305306**Related skills:**307308- **superpowers:test-driven-development** - For creating failing test case (Phase 4, Step 1)309- **superpowers:verification-before-completion** - Verify fix worked before claiming success310311## Real-World Impact312313From debugging sessions:314315- Systematic approach: 15-30 minutes to fix316- Random fixes approach: 2-3 hours of thrashing317- First-time fix rate: 95% vs 40%318- New bugs introduced: Near zero vs common