| name | debug |
| description | Systematically debug issues using a structured REPRO → GATHER → HYPOTHESIZE → TEST → FIX → VERIFY workflow. Use when diagnosing bugs, investigating failures, tracing errors, or troubleshooting unexpected behavior in code. Do NOT use when the issue is already identified and a fix is obvious, for code exploration without a specific problem, or for proactive code review.
|
Debug
Apply a systematic, repeatable workflow to diagnose and resolve bugs.
Avoid guessing — every hypothesis must be grounded in evidence.
When to Use
- A test is failing and the root cause is unclear
- An error occurs in production or development
- Behavior doesn't match expected output
- A regression was introduced after a recent change
- Performance degrades without obvious cause
- An integration or API call returns unexpected results
When NOT to Use
- The bug is already identified and the fix is trivial
- You are exploring code without a specific problem to solve
- The task is feature implementation, not debugging
- The issue is a known limitation documented in project docs
- The user is asking for code review, not debugging
Workflow
Follow these six stages in order. Do not skip ahead — each stage
builds on the previous one.
Stage 1: REPRO — Reproduce the Bug
Before investigating, confirm you can trigger the bug reliably.
Actions:
- Identify the exact steps that trigger the issue
- Run the failing command, test, or code path
- Capture the exact error message, stack trace, or unexpected output
- Note the environment: OS, runtime version, dependencies, recent changes
Commands:
go test ./path/to/pkg -run TestFunctionName -v
<exact command the user reported>
git log --oneline -10 -- <affected-file>
go version
git log --oneline -3
Checkpoint: You can reproduce the bug consistently before moving on.
Stage 2: GATHER — Collect Evidence
Read the relevant code, logs, and context surrounding the failure.
Actions:
- Read the file(s) where the error originates
- Trace the call stack upward to understand the flow
- Check logs, error messages, and stack traces for clues
- Review related tests for expected behavior
- Look at git blame/log for recent changes to the area
Commands:
cat src/affected-file.ts
grep -rn "functionName" --include="*.ts" .
git log --oneline -5 -- src/affected-file.ts
git blame src/affected-file.ts | head -40
grep -rn "TestFunctionName\|functionName" --include="*_test.go" .
grep -B 5 -A 5 "errorMessage" src/affected-file.ts
Checkpoint: You understand what the code is doing, what it should do,
and where the gap is.
Stage 3: HYPOTHESIZE — Form Theories
Based on gathered evidence, propose possible root causes ranked by likelihood.
Actions:
- List 2–4 plausible hypotheses
- For each, identify what evidence would confirm or rule it out
- Prioritize: which hypothesis is easiest to test first?
Template:
## Hypothesis 1 (Most Likely)
- **Theory:** [description of suspected root cause]
- **Evidence for:** [what you've observed that supports this]
- **Evidence against:** [what contradicts this]
- **How to test:** [specific action to confirm or rule out]
## Hypothesis 2
- **Theory:** [description]
- **Evidence for:** [supporting observations]
- **Evidence against:** [contradicting observations]
- **How to test:** [specific action]
Checkpoint: You have at least one testable hypothesis with a clear
verification step.
Stage 4: TEST — Validate Hypotheses
Run targeted experiments to confirm or eliminate each hypothesis.
Actions:
- Start with the most likely hypothesis
- Add debugging output, assertions, or breakpoints as needed
- Run minimal experiments — change one thing at a time
- Record results for each hypothesis
Commands:
echo "DEBUG: variableName = $variableName" >&2
go test ./path -v -run TestName
git stash
go test ./...
git stash pop
go test ./...
Checkpoint: You have confirmed the root cause through targeted testing.
Stage 5: FIX — Implement the Solution
Apply the minimal fix that addresses the confirmed root cause.
Actions:
- Write the smallest change that fixes the bug
- Avoid unrelated refactors — keep the fix focused
- Add a regression test if one doesn't exist
- Check for similar issues in nearby code
Guidelines:
- Fix the root cause, not just the symptom
- Prefer explicit error handling over silent failures
- If the fix is complex, break it into smaller changes
- Document why the bug occurred if the reason is non-obvious
Stage 6: VERIFY — Confirm the Fix
Validate that the bug is resolved and nothing else broke.
Actions:
- Re-run the exact reproduction steps from Stage 1
- Run the full test suite to catch regressions
- Verify edge cases related to the fix
- Clean up any temporary debugging code
Commands:
go test ./path/to/pkg -run TestFunctionName -v
go test ./...
go vet ./...
grep -rn "DEBUG\|TODO.*fix\|HACK" --include="*.go" .
Checkpoint: The bug no longer reproduces, all tests pass, and no
debugging artifacts remain.
Examples
Example 1: Failing Unit Test
Scenario: TestUserCreate returns a nil pointer error.
REPRO:
go test ./internal/user -run TestUserCreate -v
GATHER:
cat internal/user/user.go | head -50
grep -rn "FindByEmail" --include="*.go" .
cat internal/user/repository.go | sed -n '25,35p'
HYPOTHESIS: The Create function calls FindByEmail to check for
duplicates, but doesn't handle the nil-return case when no existing user
is found.
TEST: Add a nil check before line 42 and re-run the test.
FIX: Add if result != nil { return nil, ErrAlreadyExists } before
accessing result.Email.
VERIFY:
go test ./internal/user -run TestUserCreate -v
go test ./internal/user/... -v
Example 2: Production API Error
Scenario: API returns 500 on POST /api/orders intermittently.
REPRO:
curl -X POST localhost:3000/api/orders \
-H "Content-Type: application/json" \
-d '{"items": [{"id": 1, "qty": 1}]}'
GATHER:
tail -f logs/app.log | grep "order"
cat config/database.go | grep -A 5 "Pool"
grep -rn "SELECT" --include="*.go" src/order/ | head -10
HYPOTHESIS: The connection pool is exhausted under load, causing
intermittent connection failures. The 30% failure rate matches peak
traffic patterns.
TEST: Increase pool to 20 and load-test locally.
FIX: Update MaxOpenConns to 20 and add connection timeout config.
VERIFY:
ab -n 1000 -c 50 http://localhost:3000/api/orders
Edge Cases
Cannot Reproduce
If the bug cannot be reproduced:
- Ask the user for exact steps, environment details, and error output
- Check for environment-specific issues (OS, versions, config)
- Look for race conditions or timing-dependent bugs
- Search for similar issues in project issue tracker or upstream docs
Bug Is in a Dependency
If the root cause is in a third-party library:
- Document the library version and the specific issue
- Check if a newer version fixes it
- Consider workarounds (monkey-patching, alternative libraries)
- File an upstream issue if no fix exists
Multiple Root Causes
If several independent issues contribute to the bug:
- Fix them one at a time, verifying after each fix
- Prioritize by impact — fix the most severe first
- Create separate commits for each fix if they are independent
Best Practices
- Reproduce before fixing — never guess at a fix without confirming the bug
- One change at a time — isolate variables to avoid confounding results
- Write regression tests — ensure the bug stays fixed
- Keep a decision log — note hypotheses tested and why they were ruled out
- Clean up after yourself — remove debug code before committing
- Ask for help early — if stuck after 30 minutes, consult a teammate or spawn
a subagent for a second perspective