## 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
- Stack traces: read bottom-up for chain, top for crash location
