Debugging
Systematic methodology for finding and fixing bugs. Prioritizes root cause analysis over symptom treatment, evidence over intuition, and prevention over recurrence.
Iron Law
No fix without root cause. Never apply a fix until you can explain WHY the bug exists, not just WHERE it manifests. Symptom-level fixes create new bugs.
When to Use
- Bug report from QA or production alert
- Test failure with unclear cause
- Intermittent/flaky behavior
- Performance degradation
- Unexpected behavior that "used to work"
- Integration failures between components
Workflow
Phase 1: Reproduce
Establish a reliable reproduction before investigating.
- Collect all evidence — error messages, stack traces, logs, screenshots, user steps
- Identify the exact conditions: environment, data state, user actions, timing
- Create a minimal reproduction — strip away everything that isn't needed to trigger the bug
- Confirm reproduction is consistent (if intermittent, note frequency and conditions)
- Write down the reproduction steps precisely — someone else should be able to follow them
Output: Documented reproduction steps, minimal test case
If you cannot reproduce: Document what you tried, check environment differences, add instrumentation and wait for next occurrence. Do not proceed to Phase 2 on guesswork — unreproducible bugs get logged, not "fixed."
Phase 2: Investigate
Gather evidence systematically. Do NOT form hypotheses yet — this phase is about observation, not explanation.
- Read the full error message and stack trace — every line, not just the first one
- Check git history — what changed recently? (
git log --since="2 weeks ago", git bisect)
- Trace the data flow — follow the input from entry point to failure point
- Check boundaries — where does data cross component/service/layer boundaries?
- Collect environmental context — versions, configuration, dependencies, resource state
- Map the blast radius — what else is affected? Is this an isolated failure or systemic?
Production vs development debugging:
- Production: Prioritize impact assessment and mitigation first. Can you reduce blast radius before investigating? Read-only access only — never debug by modifying production state.
- Development: You have full control. Use breakpoints, modify state, add temporary logging freely.
Output: Evidence log (what you found, where, timestamps), affected component map
Phase 3: Hypothesize
Form competing hypotheses ranked by evidence strength.
- List ALL plausible causes — do not anchor on the first idea
- Classify each hypothesis by bug category (see bug categories reference)
- Rate each: evidence strength (strong/medium/weak), testability (easy/hard), likelihood
- Pick the most likely AND most testable hypothesis first
- Define what would CONFIRM and what would FALSIFY each hypothesis
Example hypothesis table:
| # |
Hypothesis |
Category |
Evidence |
Testability |
Test Plan |
| 1 |
Cache returns stale data after update |
State |
Log shows old value 2s after write |
Easy |
Bypass cache and compare |
| 2 |
Race condition between two workers |
Race condition |
Intermittent, high load correlation |
Medium |
Add locking, stress test |
| 3 |
Upstream API returns unexpected format |
Integration |
No evidence yet |
Easy |
Log raw response |
Output: Ranked hypothesis list with evidence and test plan
Phase 4: Test
Validate one hypothesis at a time. Single-variable changes only.
- Change ONE thing and observe the result
- If confirmed — proceed to Phase 5
- If falsified — update evidence log, return to next hypothesis
- If inconclusive — add more instrumentation, gather more evidence
- After 3 failed hypotheses — STOP. Re-examine your assumptions. The bug model may be wrong.
Red flags (return to Phase 2 immediately):
- "Quick fix for now, investigate later"
- Changing multiple things at once
- Fixing without understanding
- Copy-pasting a fix from the internet without understanding why it works
Output: Confirmed root cause with evidence chain
Phase 5: Fix
Implement the fix at the source, not at the symptom.
- Write a failing test that reproduces the bug FIRST
- Implement the fix — single, focused change addressing the root cause
- Verify the failing test now passes
- Run the full test suite — ensure no regressions
- Review your own fix: is this the simplest correct solution?
Fix principles:
- Fix at the SOURCE where bad data/state originates, not where the error appears
- Add defense-in-depth: validate at boundaries even after fixing the source
- Prefer making invalid states unrepresentable over runtime validation
- One bug = one fix = one commit = one test
Output: Fix with regression test, clean test suite
Phase 6: Prevent
Ensure this class of bug cannot recur.
- Add defensive validation at the boundary where bad data entered
- Improve error messages — would future-you understand this error immediately?
- Update monitoring/alerting if this was a production issue
- Write a post-mortem if the bug was significant (see post-mortem template)
- Share findings with the team — this is how institutional knowledge grows
Output: Prevention measures, post-mortem (if significant)
Bug Category Strategies
Different bug types need different investigation approaches. See bug categories reference for the full guide.
| Category |
First Move |
Key Technique |
| Logic error |
Read the code, trace conditions |
Rubber duck walkthrough, truth tables |
| Data issue |
Inspect actual vs expected data at each boundary |
Boundary logging, data flow trace |
| State/race condition |
Add timestamps to all state mutations |
Sequence diagram, concurrency analysis |
| Integration failure |
Check API contract compliance |
Request/response logging, contract tests |
| Performance |
Profile before guessing |
Profiler, flame graphs, query analysis |
| Environment |
Compare working vs broken env |
Differential analysis, config audit |
| Intermittent/flaky |
Increase observability first |
Statistical logging, stress testing |
Escalation Criteria
Stop debugging and escalate when:
- You have spent more than 2x your initial time estimate without meaningful progress
- The fix requires architectural changes beyond your component
- The root cause is in a dependency you do not control
- You have found 3+ bugs in the same area — the code needs redesign, not more patches
- The bug exposes a fundamental design flaw
- Production impact is growing and a workaround/rollback is faster than a fix
Escalate to:
| Situation |
Escalate To |
| Design or architecture issues |
Architect |
| Cannot reproduce, need more info |
QA team |
| Scope, priority, or trade-off questions |
PM / Product Owner |
| Dependency or infrastructure issues |
Platform / DevOps team |
| Security implications discovered |
Security team immediately |
Decision Framework
Fix depth
- Fix at the SOURCE where bad data/state originates, not where the error appears
- Add defense-in-depth: validate at boundaries even after fixing the source
- Prefer making invalid states unrepresentable over runtime validation
Scope of fix
- Fix the specific bug, not the surrounding code
- If you see other issues nearby, file them separately — do not scope-creep a bug fix
- One bug = one fix = one commit = one test
When to rewrite vs patch
- Patch: isolated bug, clear root cause, code is otherwise sound
- Rewrite: 3+ bugs in same module, root cause is structural, fix would be more complex than rewrite
- Rollback: production is burning and the previous version worked — roll back first, debug second
Integration with Team Roles
This debugging workflow connects to broader team processes:
| Phase |
Team Integration |
| Reproduce |
QA provides bug reports with reproduction steps; request more detail if insufficient |
| Investigate |
Architect can help map component dependencies and blast radius |
| Fix |
Code review by a peer before merge — a second pair of eyes catches fix-induced regressions |
| Prevent |
Post-mortem shared with the team; action items tracked in the backlog |
When using other code-virtuoso skills:
| Situation |
Recommended Skill |
| Bug fix reveals design problems |
Install design-patterns-virtuoso from krzysztofsurdy/code-virtuoso |
| Fix involves refactoring |
Install refactoring-virtuoso from krzysztofsurdy/code-virtuoso |
| SOLID violation is root cause |
Install solid-virtuoso from krzysztofsurdy/code-virtuoso |
| PR for the fix |
Use pr-message-writer from krzysztofsurdy/code-virtuoso |
Quality Checklist
Before marking a bug fix done:
Critical Rules
No fix without root cause. This is the iron law. If you cannot explain why the bug exists, you are not done investigating.
Reproduce first. Do not investigate what you cannot reproduce. If reproduction fails, add observability and wait.
Single-variable testing. Change one thing at a time during hypothesis testing. Changing multiple variables makes results uninterpretable.
Evidence over intuition. Log your evidence. "I think it might be X" is not a hypothesis — "Log line Y shows value Z when it should show W" is.
Test before and after. A fix without a regression test is a fix that will break again.
Escalate without ego. Knowing when to stop and ask for help is a skill, not a weakness. See the escalation criteria above.
Document for the next person. The next person debugging this area might be you in six months. Leave the codebase more observable than you found it.
Never debug production by modifying production. Read-only investigation. Fixes go through the normal deployment pipeline.
Scope discipline. Fix the bug. Only the bug. Other improvements are separate tickets.
Share what you learn. Every significant bug is a learning opportunity for the team. Post-mortems are not blame — they are institutional memory.
1---2name: debugging3description: Systematic debugging methodology for finding and fixing bugs through root cause analysis. Use when the user encounters a bug, test failure, flaky behavior, production error, performance degradation, or integration failure. Covers the reproduce-investigate-hypothesize-fix-prevent workflow, evidence-based diagnosis, bug category strategies (logic errors, race conditions, memory leaks, integration failures), and post-mortem documentation.4---56# Debugging78Systematic methodology for finding and fixing bugs. Prioritizes root cause analysis over symptom treatment, evidence over intuition, and prevention over recurrence.910## Iron Law1112**No fix without root cause.** Never apply a fix until you can explain WHY the bug exists, not just WHERE it manifests. Symptom-level fixes create new bugs.1314## When to Use1516- Bug report from QA or production alert17- Test failure with unclear cause18- Intermittent/flaky behavior19- Performance degradation20- Unexpected behavior that "used to work"21- Integration failures between components2223## Workflow2425### Phase 1: Reproduce2627Establish a reliable reproduction before investigating.28291. Collect all evidence — error messages, stack traces, logs, screenshots, user steps302. Identify the exact conditions: environment, data state, user actions, timing313. Create a minimal reproduction — strip away everything that isn't needed to trigger the bug324. Confirm reproduction is consistent (if intermittent, note frequency and conditions)335. Write down the reproduction steps precisely — someone else should be able to follow them3435**Output**: Documented reproduction steps, minimal test case3637**If you cannot reproduce**: Document what you tried, check environment differences, add instrumentation and wait for next occurrence. Do not proceed to Phase 2 on guesswork — unreproducible bugs get logged, not "fixed."3839### Phase 2: Investigate4041Gather evidence systematically. Do NOT form hypotheses yet — this phase is about observation, not explanation.42431. Read the full error message and stack trace — every line, not just the first one442. Check git history — what changed recently? (`git log --since="2 weeks ago"`, `git bisect`)453. Trace the data flow — follow the input from entry point to failure point464. Check boundaries — where does data cross component/service/layer boundaries?475. Collect environmental context — versions, configuration, dependencies, resource state486. Map the blast radius — what else is affected? Is this an isolated failure or systemic?4950**Production vs development debugging:**51- **Production**: Prioritize impact assessment and mitigation first. Can you reduce blast radius before investigating? Read-only access only — never debug by modifying production state.52- **Development**: You have full control. Use breakpoints, modify state, add temporary logging freely.5354**Output**: Evidence log (what you found, where, timestamps), affected component map5556### Phase 3: Hypothesize5758Form competing hypotheses ranked by evidence strength.59601. List ALL plausible causes — do not anchor on the first idea612. Classify each hypothesis by bug category (see [bug categories reference](references/bug-categories.md))623. Rate each: evidence strength (strong/medium/weak), testability (easy/hard), likelihood634. Pick the most likely AND most testable hypothesis first645. Define what would CONFIRM and what would FALSIFY each hypothesis6566**Example hypothesis table:**6768| # | Hypothesis | Category | Evidence | Testability | Test Plan |69|---|---|---|---|---|---|70| 1 | Cache returns stale data after update | State | Log shows old value 2s after write | Easy | Bypass cache and compare |71| 2 | Race condition between two workers | Race condition | Intermittent, high load correlation | Medium | Add locking, stress test |72| 3 | Upstream API returns unexpected format | Integration | No evidence yet | Easy | Log raw response |7374**Output**: Ranked hypothesis list with evidence and test plan7576### Phase 4: Test7778Validate one hypothesis at a time. Single-variable changes only.79801. Change ONE thing and observe the result812. If confirmed — proceed to Phase 5823. If falsified — update evidence log, return to next hypothesis834. If inconclusive — add more instrumentation, gather more evidence845. After 3 failed hypotheses — STOP. Re-examine your assumptions. The bug model may be wrong.8586**Red flags** (return to Phase 2 immediately):87- "Quick fix for now, investigate later"88- Changing multiple things at once89- Fixing without understanding90- Copy-pasting a fix from the internet without understanding why it works9192**Output**: Confirmed root cause with evidence chain9394### Phase 5: Fix9596Implement the fix at the source, not at the symptom.97981. Write a failing test that reproduces the bug FIRST992. Implement the fix — single, focused change addressing the root cause1003. Verify the failing test now passes1014. Run the full test suite — ensure no regressions1025. Review your own fix: is this the simplest correct solution?103104**Fix principles:**105- Fix at the SOURCE where bad data/state originates, not where the error appears106- Add defense-in-depth: validate at boundaries even after fixing the source107- Prefer making invalid states unrepresentable over runtime validation108- One bug = one fix = one commit = one test109110**Output**: Fix with regression test, clean test suite111112### Phase 6: Prevent113114Ensure this class of bug cannot recur.1151161. Add defensive validation at the boundary where bad data entered1172. Improve error messages — would future-you understand this error immediately?1183. Update monitoring/alerting if this was a production issue1194. Write a post-mortem if the bug was significant (see [post-mortem template](references/post-mortem-template.md))1205. Share findings with the team — this is how institutional knowledge grows121122**Output**: Prevention measures, post-mortem (if significant)123124---125126## Bug Category Strategies127128Different bug types need different investigation approaches. See [bug categories reference](references/bug-categories.md) for the full guide.129130| Category | First Move | Key Technique |131|---|---|---|132| Logic error | Read the code, trace conditions | Rubber duck walkthrough, truth tables |133| Data issue | Inspect actual vs expected data at each boundary | Boundary logging, data flow trace |134| State/race condition | Add timestamps to all state mutations | Sequence diagram, concurrency analysis |135| Integration failure | Check API contract compliance | Request/response logging, contract tests |136| Performance | Profile before guessing | Profiler, flame graphs, query analysis |137| Environment | Compare working vs broken env | Differential analysis, config audit |138| Intermittent/flaky | Increase observability first | Statistical logging, stress testing |139140---141142## Escalation Criteria143144Stop debugging and escalate when:145146- You have spent more than 2x your initial time estimate without meaningful progress147- The fix requires architectural changes beyond your component148- The root cause is in a dependency you do not control149- You have found 3+ bugs in the same area — the code needs redesign, not more patches150- The bug exposes a fundamental design flaw151- Production impact is growing and a workaround/rollback is faster than a fix152153**Escalate to:**154155| Situation | Escalate To |156|---|---|157| Design or architecture issues | Architect |158| Cannot reproduce, need more info | QA team |159| Scope, priority, or trade-off questions | PM / Product Owner |160| Dependency or infrastructure issues | Platform / DevOps team |161| Security implications discovered | Security team immediately |162163---164165## Decision Framework166167### Fix depth168- Fix at the SOURCE where bad data/state originates, not where the error appears169- Add defense-in-depth: validate at boundaries even after fixing the source170- Prefer making invalid states unrepresentable over runtime validation171172### Scope of fix173- Fix the specific bug, not the surrounding code174- If you see other issues nearby, file them separately — do not scope-creep a bug fix175- One bug = one fix = one commit = one test176177### When to rewrite vs patch178- **Patch**: isolated bug, clear root cause, code is otherwise sound179- **Rewrite**: 3+ bugs in same module, root cause is structural, fix would be more complex than rewrite180- **Rollback**: production is burning and the previous version worked — roll back first, debug second181182---183184## Integration with Team Roles185186This debugging workflow connects to broader team processes:187188| Phase | Team Integration |189|---|---|190| Reproduce | QA provides bug reports with reproduction steps; request more detail if insufficient |191| Investigate | Architect can help map component dependencies and blast radius |192| Fix | Code review by a peer before merge — a second pair of eyes catches fix-induced regressions |193| Prevent | Post-mortem shared with the team; action items tracked in the backlog |194195When using other code-virtuoso skills:196197| Situation | Recommended Skill |198|---|---|199| Bug fix reveals design problems | Install `design-patterns-virtuoso` from `krzysztofsurdy/code-virtuoso` |200| Fix involves refactoring | Install `refactoring-virtuoso` from `krzysztofsurdy/code-virtuoso` |201| SOLID violation is root cause | Install `solid-virtuoso` from `krzysztofsurdy/code-virtuoso` |202| PR for the fix | Use `pr-message-writer` from `krzysztofsurdy/code-virtuoso` |203204---205206## Quality Checklist207208Before marking a bug fix done:209210- [ ] Root cause is identified and documented211- [ ] Failing test existed before the fix212- [ ] Fix addresses root cause, not symptom213- [ ] Full test suite passes214- [ ] Fix is the simplest correct solution215- [ ] Error messages improved where relevant216- [ ] Post-mortem written for significant bugs217- [ ] Team notified if the bug affects shared components218219---220221## Critical Rules2222231. **No fix without root cause.** This is the iron law. If you cannot explain why the bug exists, you are not done investigating.2242252. **Reproduce first.** Do not investigate what you cannot reproduce. If reproduction fails, add observability and wait.2262273. **Single-variable testing.** Change one thing at a time during hypothesis testing. Changing multiple variables makes results uninterpretable.2282294. **Evidence over intuition.** Log your evidence. "I think it might be X" is not a hypothesis — "Log line Y shows value Z when it should show W" is.2302315. **Test before and after.** A fix without a regression test is a fix that will break again.2322336. **Escalate without ego.** Knowing when to stop and ask for help is a skill, not a weakness. See the escalation criteria above.2342357. **Document for the next person.** The next person debugging this area might be you in six months. Leave the codebase more observable than you found it.2362378. **Never debug production by modifying production.** Read-only investigation. Fixes go through the normal deployment pipeline.2382399. **Scope discipline.** Fix the bug. Only the bug. Other improvements are separate tickets.24024110. **Share what you learn.** Every significant bug is a learning opportunity for the team. Post-mortems are not blame — they are institutional memory.