--- name: debugging description: Use when encountering any bug, test failure, or unexpected behavior, before proposing a fix — requires root-cause investigation before any code change --- # Debugging — root cause before fix ## The Iron Law ``` NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST ``` Random fixes waste time and create new bugs. Quick patches mask the real problem and the same bug shows up later in a different form. If you have not completed Phase 1 below, you cannot propose fixes. ## When to invoke this skill Any technical issue: - Test failures - Bugs in dev or production - Unexpected behavior - Performance problems - Build / typecheck failures - Integration issues **Especially when:** - Time pressure (emergencies make guessing tempting) - "Just one quick fix" looks obvious - You've already tried multiple fixes - Previous fix didn't work - You don't fully understand what broke **Don't skip when:** - Issue seems simple — simple bugs have root causes too - You're in a hurry — systematic is faster than thrashing - The user wants it fixed NOW — guess-and-check makes it slower ## The four phases You must complete each phase before proceeding to the next. ### Phase 1: Root cause investigation Before attempting any fix: 1. **Read error messages carefully.** Don't skim past errors or warnings. Read stack traces completely. Note line numbers, file paths, error codes. The exact answer is often in the message. 2. **Reproduce consistently.** Can you trigger it reliably? What are the exact steps? Every time? If not reproducible, gather more data — don't guess. 3. **Check recent changes.** What changed that could cause this? `git log --oneline -10`. New deps, config changes, environment differences. 4. **Multi-component systems: gather evidence at boundaries.** When the system has layers (CI → build → signing, API → service → DB), add diagnostic logging at each layer before proposing fixes. Run once to see WHERE it breaks, then investigate that specific layer. Example layering: ``` For each component boundary: - Log what data enters - Log what data exits - Verify env / config propagation - Check state at each layer Run once → identify failing layer → investigate that layer ``` 5. **Trace data flow backward.** When the error is deep in the call stack: where does the bad value originate? What called this with the bad value? Keep tracing up until you find the source. Fix at the source, not at the symptom. ### Phase 2: Pattern analysis 1. **Find a working example.** Locate similar code in the same codebase that works. What does it do that the broken code doesn't? 2. **Compare against references.** If the broken code follows a pattern (a library's example, a sister module), read the reference completely. Don't skim. Understand it before applying it. 3. **Identify differences.** What's different between working and broken? List every difference, however small. Don't assume "that can't matter" — that assumption is usually how the bug got there. 4. **Understand dependencies.** What other components does this need? What config / env / assumptions does it make? ### Phase 3: Hypothesis and minimal test 1. **Form a single hypothesis.** State it: "I think X is the root cause because Y." Be specific. Vague hypothesis = vague fix. 2. **Test minimally.** Make the smallest possible change to test the hypothesis. One variable at a time. Don't bundle multiple changes "while you're in there." 3. **Verify before continuing.** Did it work? Yes → Phase 4. No → form a NEW hypothesis. Don't pile fixes on top of fixes. 4. **When you don't know, say so.** "I don't understand why X happens" is a valid status. Don't pretend. Ask, research, or surface to the human. ### Phase 4: Fix and verify 1. **Write a failing test first.** Simplest possible reproduction. Automated if the project has a framework; one-off script otherwise. The test must fail with the current code — that's how you know it actually targets the bug. 2. **Implement a single fix.** Address the root cause from Phase 1. One change. No "while I'm here" cleanups, no bundled refactors. 3. **Verify.** Test passes? Other tests still pass? The original reported symptom is gone? 4. **If the fix doesn't work:** stop. Count: how many fixes have you tried? - **<3**: return to Phase 1 with what you learned. - **≥3**: stop and question the architecture (next step). Do **not** try fix #4. 5. **3+ fixes failed = architectural problem, not a fix problem.** Pattern indicators: - Each fix reveals a new shared-state / coupling issue elsewhere. - Fixes require "massive refactoring" to land cleanly. - Each fix creates new symptoms in different places. Surface to the human: "I've tried 3 approaches, each surfaces a different downstream issue. I think the underlying pattern is wrong. Want to discuss before I try more fixes?" ## Red Flags — STOP and follow the process If you catch yourself thinking any of these, you are skipping the process. Stop. Return to Phase 1. | Thought | Reality | |---------|---------| | "Quick fix for now, investigate later" | "Later" never comes; the bug returns | | "Just try changing X and see if it works" | That's guess-and-check, not debugging | | "Add multiple changes, run tests" | Can't isolate what worked | | "Skip the test, I'll manually verify" | Manual ≠ regression-proof | | "It's probably X, let me fix that" | "Probably" is not a hypothesis | | "Pattern says X but I'll adapt it" | Partial pattern = bug | | "Here are the main problems: " (without investigation) | Symptoms ≠ causes | | Proposing fixes before tracing data flow | Always wrong | | "One more fix attempt" (after 2+ failures) | Architectural problem | | Each fix reveals new problem in different place | Architectural problem | ## Common rationalizations | Excuse | Reality | |--------|---------| | "Issue is simple, no need for process" | Simple issues have root causes too. The process is fast for simple bugs. | | "Emergency, no time for process" | Systematic debugging is **faster** than guess-and-check. | | "Just try this first, then investigate" | First fix sets the pattern. Do it right from the start. | | "I'll write the test after confirming the fix" | Untested fixes don't stick. Test first proves the fix. | | "Multiple fixes at once saves time" | Can't isolate what worked. Causes new bugs. | | "I see the problem, let me fix it" | Seeing symptoms ≠ understanding root cause. | | "One more fix" (after 2+ failures) | Three failures = architectural problem. Don't fix again. | ## Quick reference | Phase | Activity | Done when | |-------|---------|-----------| | 1. Root cause | Read errors, reproduce, check changes, trace data | You can describe WHAT and WHY | | 2. Pattern | Find working example, compare, list differences | You know what to change | | 3. Hypothesis | State theory, test minimally | Confirmed or new hypothesis | | 4. Fix | Failing test → fix → verify | Original symptom is gone, no regressions | ## When the process reveals "no root cause" If systematic investigation reveals the issue is genuinely environmental, timing-dependent, or external: 1. You've completed the process — that's the answer. 2. Document what you investigated. 3. Implement appropriate handling: retry, timeout, clear error message, monitoring. But: 95% of "no root cause" diagnoses are incomplete investigation. Be honest about which you're in. ## Related skills - `verification` — verify the fix actually worked before claiming done - `nodejs-project` — Phase 3 of nodejs SOP says "fail twice, invoke this skill"