Skip to content

Latest commit

 

History

History
145 lines (106 loc) · 4.2 KB

File metadata and controls

145 lines (106 loc) · 4.2 KB

Root cause tracing

Overview

Bugs 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.

Core principle: Trace backward through the call chain until you find the original trigger, then fix at the source.

When to use

  • Error happens deep in execution (not at entry point)
  • Stack trace shows long call chain
  • Unclear where invalid data originated
  • Need to find which test/code triggers the problem

If you can trace backwards, trace to the original trigger. If you hit a dead end, fix at the symptom point as a last resort. Once you find the root cause, also add defense-in-depth validation (see defense-in-depth.md).

The tracing process

1. Observe the symptom

Error: git init failed in /Users/jesse/project/packages/core

2. Find immediate cause

What code directly causes this?

await execFileAsync('git', ['init'], { cwd: projectDir });

3. Ask: what called this?

WorktreeManager.createSessionWorktree(projectDir, sessionId)
   called by Session.initializeWorkspace()
   called by Session.create()
   called by test at Project.create()

4. Keep tracing up

What value was passed?

  • projectDir = '' (empty string!)
  • Empty string as cwd resolves to process.cwd()
  • That's the source code directory!

5. Find original trigger

Where did empty string come from?

const context = setupCoreTest(); // Returns { tempDir: '' }
Project.create('name', context.tempDir); // Accessed before beforeEach!

Adding stack traces

When you can't trace manually, add instrumentation:

// Before the problematic operation
async function gitInit(directory: string) {
  const stack = new Error().stack;
  console.error('DEBUG git init:', {
    directory,
    cwd: process.cwd(),
    nodeEnv: process.env.NODE_ENV,
    stack,
  });

  await execFileAsync('git', ['init'], { cwd: directory });
}

Run and capture:

npm test 2>&1 | grep 'DEBUG git init'

Analyze stack traces:

  • Look for test file names
  • Find the line number triggering the call
  • Identify the pattern (same test? same parameter?)

Finding which test causes pollution

If something appears during tests but you don't know which test, use bisection: run tests one-by-one and stop at the first polluter.

# Run each test file in isolation to find which one pollutes
for f in src/**/*.test.ts; do
  npx jest "$f" --silent 2>/dev/null
  if [ -d ".git" ]; then  # or whatever artifact you're looking for
    echo "POLLUTER: $f"
    break
  fi
done

Real example: empty projectDir

Symptom: .git created in packages/core/ (source code)

Trace chain:

  1. git init runs in process.cwd() -- empty cwd parameter
  2. WorktreeManager called with empty projectDir
  3. Session.create() passed empty string
  4. Test accessed context.tempDir before beforeEach
  5. setupCoreTest() returns { tempDir: '' } initially

Root cause: Top-level variable initialization accessing empty value

Fix: Made tempDir a getter that throws if accessed before beforeEach

Also added defense-in-depth:

  • Layer 1: Project.create() validates directory
  • Layer 2: WorkspaceManager validates not empty
  • Layer 3: NODE_ENV guard refuses git init outside tmpdir
  • Layer 4: Stack trace logging before git init

Key principle

Never fix just where the error appears. The process is:

  1. Find the immediate cause
  2. Trace one level up -- keep going until you find the source
  3. Fix at the source
  4. Add validation at each layer the data passes through
  5. The bug becomes structurally impossible

Stack trace tips

  • In tests: Use console.error() not logger -- logger may be suppressed
  • Before operation: Log before the dangerous operation, not after it fails
  • Include context: Directory, cwd, environment variables, timestamps
  • Capture stack: new Error().stack shows complete call chain

Real-world impact

From debugging session (2025-10-03):

  • Found root cause through 5-level trace
  • Fixed at source (getter validation)
  • Added 4 layers of defense
  • 1847 tests passed, zero pollution