Skip to content

/debug

Analyze and debug errors, exceptions, or unexpected behavior with systematic investigation and root cause analysis. This is a streamlined version of /fix focused on diagnosis and investigation.

Terminal window
/debug [error message or description]
  • [error message] - Stack trace or error output
  • [description] - Natural language description of unexpected behavior

The /debug command follows a systematic 3-step debugging process:

  1. Parse Error Message and Stack Trace

    • Extracts error type (TypeError, ValueError, etc.)
    • Identifies file and line number
    • Reads stack trace for execution path
  2. Identify Error Location

    • Pinpoints the exact failing line
    • Examines surrounding code
    • Checks function context
  3. Understand Error Type

    • Categorizes error (logic, runtime, type, etc.)
    • Identifies common causes for this error type
    • Notes relevant language-specific behavior
  1. Trace Execution Path

    • Follows code flow from entry to error
    • Maps variable states at each step
    • Identifies where state becomes invalid
  2. Check Related Code

    • Examines caller functions
    • Reviews recent changes to area
    • Searches for similar patterns
  3. Form Hypotheses

    • Lists possible root causes
    • Ranks by likelihood
    • Identifies tests to validate each hypothesis
  1. Implement Minimal Fix

    • Addresses root cause
    • Keeps changes focused
    • Preserves existing behavior
  2. Verify Fix Works

    • Tests the specific error case
    • Runs related tests
    • Checks for side effects
  3. Add Regression Test

    • Creates test that would catch this bug
    • Documents the error scenario
    • Ensures test fails without fix
## Debug Report
### Error

TypeError: Cannot read property ‘email’ of undefined at getUserEmail (src/services/UserService.ts:45) at processUser (src/api/users.ts:23)

### Analysis
The error occurs when trying to access `user.email` but `user` is undefined.
### Location
**File**: `src/services/UserService.ts`
**Line**: 45
**Function**: `getUserEmail`
### Execution Trace
1. API receives request → `processUser()`
2. Calls `getUserEmail(userId)`
3. Inside getUserEmail:
```typescript
const user = this.fetchUser(userId); // Missing await!
return user.email; // user is a Promise, not User object

Missing await keyword - fetchUser() is async but wasn’t awaited, so user is a Promise object instead of a User object.

  1. ✅ Missing await - Confirmed by checking function signature
  2. ❌ User doesn’t exist - Would be null, not undefined
  3. ❌ Database error - Would throw different error
// Before
async getUserEmail(userId: string) {
const user = this.fetchUser(userId);
return user.email;
}
// After
async getUserEmail(userId: string) {
const user = await this.fetchUser(userId);
if (!user) {
throw new NotFoundError(`User ${userId} not found`);
}
return user.email;
}
Terminal window
# Test the fix
pnpm test src/services/UserService.test.ts
# Run full suite
pnpm test

Added regression test: test_getUserEmail_awaits_user_fetch

## Common Debug Patterns
### Null/Undefined Errors
```typescript
// Symptom
Cannot read property 'X' of undefined
// Common Causes
- Missing await on async function
- Object not initialized
- API returned null
- Array/object access out of bounds
// Investigation
1. Check if value should exist
2. Trace where it's set
3. Verify async operations
# Symptom
TypeError: unsupported operand type(s) for +: 'int' and 'str'
# Common Causes
- Mixed types in operation
- Missing type conversion
- Unexpected input type
# Investigation
1. Print types at error line
2. Check input sources
3. Verify type conversions
// Symptom
Intermittent failures, works sometimes
// Common Causes
- Async operations out of order
- Shared state without locking
- Component unmounted during async
// Investigation
1. Add logging to trace timing
2. Check for missing awaits
3. Look for shared mutable state
# Symptom
RecursionError: maximum recursion depth exceeded
# Common Causes
- Missing base case
- Base case never reached
- Infinite loop condition
# Investigation
1. Check base/termination condition
2. Verify condition can be met
3. Add iteration counter logging
FlagDescriptionExample
--depth=[1-5]Investigation depth (1=quick, 5=exhaustive)--depth=4
--traceShow detailed execution trace--trace
--hypothesisShow all hypotheses considered--hypothesis
--format=[fmt]Output format (concise/detailed)--format=concise
Terminal window
# Quick debug with minimal output
/debug --depth=1 --format=concise "error message"
# Deep investigation with full trace
/debug --depth=5 --trace --hypothesis "intermittent failure"
# Detailed analysis
/debug --format=detailed "TypeError in auth.ts"

Input:

Terminal window
/debug IndexError: list index out of range in process_items at line 42

Output:

  1. Identifies array access issue
  2. Traces loop bounds
  3. Finds off-by-one error
  4. Suggests fix with correct range

Input:

Terminal window
/debug "User authentication succeeds but session not created"

Output:

  1. Traces auth flow
  2. Finds session creation after response sent
  3. Identifies async timing issue
  4. Recommends await before response

Input:

Terminal window
/debug --depth=5 "Test fails randomly, passes on retry"

Output:

  1. Analyzes for race conditions
  2. Checks for time-dependent logic
  3. Reviews shared state access
  4. Identifies missing await in test setup

Use /debug when you need to:

  • Investigate an error without immediately fixing it
  • Understand why something breaks
  • Diagnose intermittent issues
  • Trace execution paths
  • Form hypotheses about root causes

Use /fix instead when you want to:

  • Fix the issue immediately
  • Add regression tests
  • Complete the full fix workflow

After running /debug, you receive:

  1. Error Analysis - Detailed breakdown of the error
  2. Execution Trace - Step-by-step flow to error
  3. Root Cause - Explanation of why it fails
  4. Hypotheses - Tested theories about cause
  5. Fix Recommendation - Suggested solution (optional)
  6. Prevention Strategy - How to avoid similar issues
  • /fix - Complete fix workflow with tests
  • /test - Generate tests after debugging
  • /review - Review code for potential issues
  • /refactor - Improve code after fix