Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Enforce root-cause investigation before any fix—structured debugging protocol for bugs and test failures
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
root-cause-tracing.md
1# Root Cause Tracing23## Overview45Bugs often manifest deep in the call stack (git init in wrong directory, file created in wrong location, database opened with wrong path). Your instinct is to fix where the error appears, but that's treating a symptom.67**Core principle:** Trace backward through the call chain until you find the original trigger, then fix at the source.89## When to Use1011```dot12digraph when_to_use {13"Bug appears deep in stack?" [shape=diamond];14"Can trace backwards?" [shape=diamond];15"Fix at symptom point" [shape=box];16"Trace to original trigger" [shape=box];17"BETTER: Also add defense-in-depth" [shape=box];1819"Bug appears deep in stack?" -> "Can trace backwards?" [label="yes"];20"Can trace backwards?" -> "Trace to original trigger" [label="yes"];21"Can trace backwards?" -> "Fix at symptom point" [label="no - dead end"];22"Trace to original trigger" -> "BETTER: Also add defense-in-depth";23}24```2526**Use when:**27- Error happens deep in execution (not at entry point)28- Stack trace shows long call chain29- Unclear where invalid data originated30- Need to find which test/code triggers the problem3132## The Tracing Process3334### 1. Observe the Symptom35```36Error: git init failed in ~/project/packages/core37```3839### 2. Find Immediate Cause40**What code directly causes this?**41```typescript42await execFileAsync('git', ['init'], { cwd: projectDir });43```4445### 3. Ask: What Called This?46```typescript47WorktreeManager.createSessionWorktree(projectDir, sessionId)48→ called by Session.initializeWorkspace()49→ called by Session.create()50→ called by test at Project.create()51```5253### 4. Keep Tracing Up54**What value was passed?**55- `projectDir = ''` (empty string!)56- Empty string as `cwd` resolves to `process.cwd()`57- That's the source code directory!5859### 5. Find Original Trigger60**Where did empty string come from?**61```typescript62const context = setupCoreTest(); // Returns { tempDir: '' }63Project.create('name', context.tempDir); // Accessed before beforeEach!64```6566## Adding Stack Traces6768When you can't trace manually, add instrumentation:6970```typescript71// Before the problematic operation72async function gitInit(directory: string) {73const stack = new Error().stack;74console.error('DEBUG git init:', {75directory,76cwd: process.cwd(),77nodeEnv: process.env.NODE_ENV,78stack,79});8081await execFileAsync('git', ['init'], { cwd: directory });82}83```8485**Critical:** Use `console.error()` in tests (not logger - may not show)8687**Run and capture:**88```bash89npm test 2>&1 | grep 'DEBUG git init'90```9192**Analyze stack traces:**93- Look for test file names94- Find the line number triggering the call95- Identify the pattern (same test? same parameter?)9697## Finding Which Test Causes Pollution9899If something appears during tests but you don't know which test:100101Use the bisection script `find-polluter.sh` in this directory:102103```bash104./find-polluter.sh '.git' 'src/**/*.test.ts'105```106107Runs tests one-by-one, stops at first polluter. See script for usage.108109## Real Example: Empty projectDir110111**Symptom:** `.git` created in `packages/core/` (source code)112113**Trace chain:**1141. `git init` runs in `process.cwd()` ← empty cwd parameter1152. WorktreeManager called with empty projectDir1163. Session.create() passed empty string1174. Test accessed `context.tempDir` before beforeEach1185. setupCoreTest() returns `{ tempDir: '' }` initially119120**Root cause:** Top-level variable initialization accessing empty value121122**Fix:** Made tempDir a getter that throws if accessed before beforeEach123124**Also added defense-in-depth:**125- Layer 1: Project.create() validates directory126- Layer 2: WorkspaceManager validates not empty127- Layer 3: NODE_ENV guard refuses git init outside tmpdir128- Layer 4: Stack trace logging before git init129130## Key Principle131132```dot133digraph principle {134"Found immediate cause" [shape=ellipse];135"Can trace one level up?" [shape=diamond];136"Trace backwards" [shape=box];137"Is this the source?" [shape=diamond];138"Fix at source" [shape=box];139"Add validation at each layer" [shape=box];140"Bug impossible" [shape=doublecircle];141"NEVER fix just the symptom" [shape=octagon, style=filled, fillcolor=red, fontcolor=white];142143"Found immediate cause" -> "Can trace one level up?";144"Can trace one level up?" -> "Trace backwards" [label="yes"];145"Can trace one level up?" -> "NEVER fix just the symptom" [label="no"];146"Trace backwards" -> "Is this the source?";147"Is this the source?" -> "Trace backwards" [label="no - keeps going"];148"Is this the source?" -> "Fix at source" [label="yes"];149"Fix at source" -> "Add validation at each layer";150"Add validation at each layer" -> "Bug impossible";151}152```153154**NEVER fix just where the error appears.** Trace back to find the original trigger.155156## Stack Trace Tips157158**In tests:** Use `console.error()` not logger - logger may be suppressed159**Before operation:** Log before the dangerous operation, not after it fails160**Include context:** Directory, cwd, environment variables, timestamps161**Capture stack:** `new Error().stack` shows complete call chain162163## Real-World Impact164165From debugging session (2025-10-03):166- Found root cause through 5-level trace167- Fixed at source (getter validation)168- Added 4 layers of defense169- 1847 tests passed, zero pollution170