$npx -y skills add tody-agent/codymaster --skill cm-debuggingUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes
| 1 | # Systematic Debugging |
| 2 | |
| 3 | ## TL;DR |
| 4 | - **Use when** any bug, test failure, or unexpected behavior surfaces |
| 5 | - **Process**: reproduce → isolate → diagnose root cause → fix |
| 6 | - **Defense in depth**: also add a test that locks the bug |
| 7 | - **Next**: cm-tdd → cm-quality-gate |
| 8 | |
| 9 | ## Overview |
| 10 | |
| 11 | Random fixes waste time and create new bugs. Quick patches mask underlying issues. |
| 12 | |
| 13 | **Core principle:** ALWAYS find root cause before attempting fixes. Symptom fixes are failure. |
| 14 | |
| 15 | **Violating the letter of this process is violating the spirit of debugging.** |
| 16 | |
| 17 | ## The Iron Law |
| 18 | |
| 19 | ``` |
| 20 | NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST |
| 21 | ``` |
| 22 | |
| 23 | If you haven't completed Phase 1, you cannot propose fixes. |
| 24 | |
| 25 | ## When to Use |
| 26 | |
| 27 | Use for ANY technical issue: |
| 28 | - Test failures |
| 29 | - Bugs in production |
| 30 | - Unexpected behavior |
| 31 | - Performance problems |
| 32 | - Build failures |
| 33 | - Integration issues |
| 34 | |
| 35 | **Use this ESPECIALLY when:** |
| 36 | - Under time pressure (emergencies make guessing tempting) |
| 37 | - "Just one quick fix" seems obvious |
| 38 | - You've already tried multiple fixes |
| 39 | - Previous fix didn't work |
| 40 | - You don't fully understand the issue |
| 41 | |
| 42 | **Don't skip when:** |
| 43 | - Issue seems simple (simple bugs have root causes too) |
| 44 | - You're in a hurry (rushing guarantees rework) |
| 45 | - Manager wants it fixed NOW (systematic is faster than thrashing) |
| 46 | |
| 47 | ## The Four Phases |
| 48 | |
| 49 | You MUST complete each phase before proceeding to the next. |
| 50 | |
| 51 | ### Phase 0.5: Memory Integrity Check (BEFORE blaming code) |
| 52 | |
| 53 | > **BEFORE blaming code, ASK: "Could memory be causing this bug?"** |
| 54 | |
| 55 | 1. **SUSPECT** — Identify relevant memories: |
| 56 | - What module/file is the bug in? |
| 57 | - Read `.cm/learnings.json` filtered by that scope |
| 58 | - List all active learnings + decisions for this area |
| 59 | |
| 60 | 2. **INVESTIGATE** — Did AI follow a memory when writing buggy code? |
| 61 | - Check: Does the buggy code match a `prevention` pattern from any learning? |
| 62 | - Check: Does the buggy code follow a `decision` that may be outdated? |
| 63 | - If YES → that memory is a **suspect** |
| 64 | |
| 65 | 3. **VERIFY** — Is the suspect memory still correct? |
| 66 | - Compare learning with current codebase (not when it was recorded) |
| 67 | - Has the dependency/pattern/architecture changed since learning was recorded? |
| 68 | - If memory is WRONG → proceed to HEAL |
| 69 | |
| 70 | 4. **HEAL** (only if memory confirmed as cause): |
| 71 | - **Invalidate:** Set `status = "invalidated"` — learning is proven wrong |
| 72 | - **Correct:** Update `prevention` with correct info, set `status = "corrected"` |
| 73 | - **Scope-reduce:** Learning is right for smaller scope → narrow the scope |
| 74 | - **Record meta-learning** in `.cm/meta-learnings.json` |
| 75 | |
| 76 | ``` |
| 77 | IF memory caused the bug: |
| 78 | → HEAL memory FIRST |
| 79 | → THEN proceed to Phase 1 to fix code |
| 80 | → The code fix will be correct because memory is now correct |
| 81 | |
| 82 | IF memory did NOT cause the bug: |
| 83 | → Proceed to Phase 1 normally |
| 84 | ``` |
| 85 | |
| 86 | > **WHY PHASE 0.5?** Fix memory first → code fix will be correct. |
| 87 | > Without fixing memory → bug will return next session (bug loop). |
| 88 | |
| 89 | ### Phase 1: Root Cause Investigation |
| 90 | |
| 91 | **BEFORE attempting ANY fix:** |
| 92 | |
| 93 | 1. **Read Error Messages Carefully** |
| 94 | - Don't skip past errors or warnings |
| 95 | - They often contain the exact solution |
| 96 | - Read stack traces completely |
| 97 | - Note line numbers, file paths, error codes |
| 98 | |
| 99 | 2. **Reproduce Consistently** |
| 100 | - Can you trigger it reliably? |
| 101 | - What are the exact steps? |
| 102 | - Does it happen every time? |
| 103 | - If not reproducible → gather more data, don't guess |
| 104 | |
| 105 | 3. **Check Recent Changes** |
| 106 | - What changed that could cause this? |
| 107 | - Git diff, recent commits |
| 108 | - New dependencies, config changes |
| 109 | - Environmental differences |
| 110 | |
| 111 | 4. **Gather Evidence in Multi-Component Systems** |
| 112 | |
| 113 | **WHEN system has multiple components (CI → build → signing, API → service → database):** |
| 114 | |
| 115 | **BEFORE proposing fixes, add diagnostic instrumentation:** |
| 116 | ``` |
| 117 | For EACH component boundary: |
| 118 | - Log what data enters component |
| 119 | - Log what data exits component |
| 120 | - Verify environment/config propagation |
| 121 | - Check state at each layer |
| 122 | |
| 123 | Run once to gather evidence showing WHERE it breaks |
| 124 | THEN analyze evidence to identify failing component |
| 125 | THEN investigate that specific component |
| 126 | ``` |
| 127 | |
| 128 | 5. **Trace Data Flow** |
| 129 | |
| 130 | **WHEN error is deep in call stack:** |
| 131 | |
| 132 | - Where does bad value originate? |
| 133 | - What called this with bad value? |
| 134 | - Keep tracing up until you find the source |
| 135 | - Fix at source, not at symptom |
| 136 | |
| 137 | ### Phase 2: Pattern Analysis |
| 138 | |
| 139 | **Find the pattern before fixing:** |
| 140 | |
| 141 | 1. **Find Working Examples** |
| 142 | - Locate similar working code in same codebase |
| 143 | - What works that's similar to what's broken? |
| 144 | |
| 145 | 2. **Compare Against References** |
| 146 | - If implementing pattern, read reference implementation COMPLETELY |
| 147 | - Don't skim - read every line |
| 148 | - Understand the pattern fully before applying |
| 149 | |
| 150 | 3. **Identify Differences** |
| 151 | - What's different between working and broken? |
| 152 | - List every difference, however small |