---
name: codemap-systematic-debugging
description: Use when encountering any bug, test failure, or unexpected behavior — before proposing fixes
---

# 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, 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 the issue seems simple** — simple bugs have root causes too.

## 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 skip past errors or warnings — they often contain the exact solution
   - Read stack traces completely; note line numbers, file paths, error codes

2. **Reproduce Consistently**
   - Can you trigger it reliably? What are the exact steps?
   - If not reproducible → gather more data, don't guess

3. **Check Recent Changes**
   - `diff(mode="working")` for uncommitted changes
   - `diff(mode="ref", from="HEAD~5")` for recent commits
   - What changed that could cause this?

4. **Gather Evidence in Multi-Component Systems**

   When the system has multiple components (CLI → MCP → core → code-index):

   **BEFORE proposing fixes, add diagnostic instrumentation:**
   - Use `explore_task` to map out the component boundary where the failure occurs
   - Use `symbol(action="callers")` to trace call flow through layers
   - Log what enters and exits each component boundary
   - Run once to gather evidence showing WHERE it breaks, then analyze

5. **Trace Data Flow**

   When error is deep in the call stack:
   - Use `symbol(action="context")` at each level to read actual code
   - Where does the bad value originate?
   - What called this with a 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:**

1. **Find Working Examples**
   - `search_codebase("similar pattern")` to locate working code
   - What works that's similar to what's broken?

2. **Compare Against References**
   - Read reference implementations COMPLETELY — don't skim
   - Understand the pattern fully before applying

3. **Identify Differences**
   - What's different between working and broken?
   - List every difference, however small — don't assume "that can't matter"

4. **Understand Dependencies**
   - `get_file(path, include=["outline"])` to see what a module exports/imports
   - What assumptions does the broken code make?

### Phase 3: Hypothesis and Testing

**Scientific method:**

1. **Form Single Hypothesis**
   - State clearly: "I think X is the root cause because Y"
   - Be specific, not vague

2. **Test Minimally**
   - Make the SMALLEST possible change to test the hypothesis
   - One variable at a time — don't fix multiple things at once

3. **Verify Before Continuing**
   - Did it work? Yes → Phase 4
   - Didn't work? Form NEW hypothesis
   - DON'T add more fixes on top

4. **When You Don't Know**
   - Say "I don't understand X" — don't pretend to know
   - Ask for help or research more

### Phase 4: Implementation

**Fix the root cause, not the symptom:**

1. **Create Failing Test Case** — use `codemap-test-driven-development`
2. **Implement Single Fix** — address the root cause; no "while I'm here" improvements
3. **Verify Fix** — test passes, no regressions, issue actually resolved

4. **If Fix Doesn't Work**
   - STOP. Count: How many fixes have you tried?
   - If < 3: Return to Phase 1 with new information
   - **If ≥ 3: STOP and question the architecture** (see below)

5. **If 3+ Fixes Failed: Question Architecture**

   Pattern indicating architectural problem:
   - Each fix reveals new coupling/problem in a different place
   - Fixes require massive refactoring to implement
   - Each fix creates new symptoms elsewhere

   **Discuss with the user 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 any of these, return to Phase 1:

- "Quick fix for now, investigate later"
- "Just try changing X and see if it works"
- "Add multiple changes, run tests"
- "It's probably X, let me fix that"
- "I don't fully understand but this might work"
- "One more fix attempt" (when already tried 2+)
- Proposing solutions before tracing data flow
- Each fix reveals a new problem in a different place

## 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. |
| "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, `diff`, `explore_task`, `symbol` | Understand WHAT and WHY |
| **2. Pattern** | `search_codebase`, compare working vs broken | Identify differences |
| **3. Hypothesis** | Form theory, test minimally, one change | Confirmed or new hypothesis |
| **4. Implementation** | `codemap-test-driven-development`, fix, verify | Bug resolved, tests pass |

## Related Skills

- **codemap-test-driven-development** — for creating the failing test case (Phase 4, Step 1)
- **codemap-verification-before-completion** — verify fix worked before claiming success
- **codemap-symbol-level-debugging** — for locating the exact symbol/function to inspect
