automated-instrumented-debugging
Automated Instrumented Debugging
Core Philosophy: Don't guess. Instrument, measure, and let the data reveal the root cause.
Overview
This skill empowers you to debug complex issues by systematically injecting lightweight probes (fetch calls) into the codebase. These probes stream real-time execution data (function entries, variable states, errors) to a local debug server, allowing you to reconstruct the exact execution flow without relying on scattered console logs or interactive debuggers.
When to Use
- Complexity: The bug involves data flow across 3+ functions or asynchronous chains.
- Invisibility: Code runs in blind environments (Docker, CI, remote servers).
- Persistency: You need to analyze the chronology of events post-execution.
- Systematic Analysis: You need hard evidence to prove a hypothesis.
Systematic Debugging Process
Follow this 4-phase loop to resolve issues efficiently.
Phase 1: Strategy & Setup
Don't rush to code. First, define what you need to capture.
- Start Server: Ensure
bootstrap.jsis running (node .agent/skills/automated-instrumented-debugging/scripts/bootstrap.js). - Hypothesize: What variable or flow is likely broken?
- Plan Probes: Decide where to place
#region DEBUGblocks (Entry, Exit, Error, State).
Phase 2: Instrumentation (The "Probe")
Inject probes using the standard templates. Always use the #region DEBUG wrapper for easy cleanup.
- Trace Flow: Log Function Entry/Exit to see the path.
- Snapshot State: Log local variables and arguments.
- Catch Errors: Log
try-catchblocks in critical paths.
(See "Instrumentation Templates" below for code patterns)
Phase 3: Evidence Collection & Analysis
Run the reproduction case and let the data speak.
- Trigger: Run the test/script to reproduce the bug.
- Query: Fetch logs via
curl http://localhost:9876/logs/{session}. - Analyze:
- Did the function get called? (Check Entry logs)
- Was the data correct? (Check Variable logs)
- Where did it crash? (Check Error logs/Last successful log)
Phase 4: Resolution & Cleanup
Fix the root cause and restore the codebase.
- Fix: Apply the fix based on evidence.
- Verify: Run tests to confirm the fix.
- Cleanup: Crucial! Remove all
#region DEBUGblocks using the cleanup script:node .agent/skills/automated-instrumented-debugging/scripts/cleanup.js - Shutdown: Stop the debug server if no longer needed.
Instrumentation Templates
1. Function Entry (Trace Path)
// #region DEBUG - {SESSION}
fetch('http://localhost:9876/log', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session: '{SESSION}',
type: 'enter',
fn: '{FUNC}',
file: '{FILE_PATH}', // Use absolute path or relative to project root
data: { arg1, arg2 }, // Snapshot arguments
}),
}).catch(() => {});
// #endregion
2. Variable Snapshot (Inspect State)
// #region DEBUG - {SESSION}
fetch('http://localhost:9876/log', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session: '{SESSION}',
type: 'var',
fn: '{FUNC}',
file: '{FILE_PATH}',
data: { varName: value },
}),
}).catch(() => {});
// #endregion
3. Function Exit (Result & Timing)
// #region DEBUG - {SESSION}
fetch('http://localhost:9876/log', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session: '{SESSION}',
type: 'exit',
fn: '{FUNC}',
data: { result },
}),
}).catch(() => {});
// #endregion
4. Error Capture (Catch & Report)
// #region DEBUG - {SESSION}
fetch('http://localhost:9876/log', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session: '{SESSION}',
type: 'error',
fn: '{FUNC}',
data: { error: err.message, stack: err.stack },
}),
}).catch(() => {});
// #endregion
Anti-Patterns
❌ Sync Fetch Trap: Using fetch without considering execution order in critical paths.
-> Fix: Always use .catch(() => {}) and place probes after variable definitions.
❌ Committing Probes: Forgetting to run cleanup.js.
-> Fix: Add cleanup as a mandatory step in your "Resolution & Cleanup" workflow.