| name | systematic-debugging |
| description | Use when encountering any bug, test failure, or unexpected behavior. Investigate root cause BEFORE proposing any fix. |
Systematic Debugging
The Iron Law
NO FIX WITHOUT ROOT CAUSE INVESTIGATION FIRST
After 3 failed fix attempts: STOP and question the architecture.
The Four Phases
Phase 1: Root Cause Investigation
1. Read the error message carefully
- Full stack trace — don't skip lines
- Exact line of error
- Exception type
2. Reproduce consistently
- If it doesn't reproduce reliably, debugging cannot begin
- Isolate: smallest test case that reproduces the problem
3. Check recent changes
git log --oneline -20
git diff HEAD~1 -- <suspect-file>
4. Collect evidence by layer
Django Backend:
cd backend && python manage.py shell
>>> from django.test import Client; c = Client()
>>>
>>> from django.db import connection; connection.queries
docker compose logs -f celery
React Frontend:
cd frontend && npx tsc --noEmit
Celery/Redis:
docker compose exec redis redis-cli monitor
cd backend && celery -A config inspect active
Phase 2: Pattern Analysis
- Find working examples — similar code in the same codebase that works
- Compare with reference — diff between what works and what doesn't
- Identify differences — what changed, what's missing
- Understand dependencies — error may be in a different layer than the symptom
Phase 3: Hypothesis and Testing
Form a single hypothesis — the most likely cause
Test minimally:
- Write a test that reproduces the bug
- Verify the test fails for the correct reason
- Apply a minimal fix
When you don't know — say so explicitly:
"I don't know the root cause. My best hypothesis is X, but I need more evidence."
Phase 4: Fix Implementation
- Create a regression test that reproduces the bug
- Confirm the test fails (RED)
- Implement a single fix — no multiple simultaneous changes
- Verify the test passes (GREEN) + all other tests still pass
- If fix doesn't work after 3 attempts — STOP
After 3+ failed fixes: Question the Architecture
- Does the current design support the required behavior?
- Does it need structural refactoring?
- Escalate to the user with collected evidence
Bug Patterns by Stack
Django — Common Bugs
items = MyModel.objects.filter(user=user)
if items:
process(items[0])
items = list(MyModel.objects.filter(user=user).select_related('user'))
Celery — Common Bugs
from django.db import transaction
transaction.on_commit(lambda: my_task.delay(obj.id))
LiteLLM/AI — Common Bugs
import re, json
match = re.search(r'\{.*\}', content, re.DOTALL)
if match:
data = json.loads(match.group())
TypeScript/React — Common Bugs
const value = data?.field ?? defaultValue;
useEffect(() => {
doSomething(currentValue);
}, [currentValue]);
const { data } = useQuery({
queryKey: ['resource', userId, filter],
queryFn: () => fetchResource(userId, filter),
});
Bug Report Format
After identifying root cause:
**Bug**: [short description]
**Root Cause**: [identified cause with evidence]
**Fix Applied**: [description of fix]
**Regression Test**: `tests/path/test_bug_fix.py::test_name`
**Verified**: [test passes + all other tests still pass]