## Systematic Debugging

### The Scientific Method for Bugs

1. **Observe**: Reproduce, capture logs/traces/environment
2. **Hypothesize**: Form 2-3 testable hypotheses
3. **Experiment**: Change ONE thing at a time
4. **Analyze**: Binary search, targeted instrumentation
5. **Conclude**: Document, verify fix, prevent regression

### Common Root Cause Categories

| Category | Signs | Approach |
|---|---|---|
| Race Condition | Intermittent, timing-dependent | Instrumentation, concurrent tests |
| State Corruption | Works initially, fails later | Trace state changes, invariant checks |
| Resource Leak | Degrades over time | Monitor memory, check cleanup |
| Off-by-One | Edge cases fail | Test boundaries: 0, 1, N-1, N, N+1 |
| Nil/Crash | Crashes in production | Guard external data, trace from source |

### Rules

- Never fix a bug without a test that reproduces it first
- Root cause first: symptom, hypothesis, origin `file:line`, an observed failure (test, log, trace); fix at the origin
- Trace back through callers to where the value first goes wrong
- 3 failed fixes: stop and question the design; no 4th patch
- Stack traces: read bottom-up for chain, top for crash location
