Debugging

From tmcfarlane/oh-my-cursor · View interactive version

Debugging is an AI agent skill from the GitHub repository tmcfarlane/oh-my-cursor. Systematic 4-phase debugging with root cause investigation. Use when fixing bugs to prevent random fixes.

How to use this skill

Skill file

Mode: Cognitive/Prompt-Driven — No standalone utility script; use via agent context.

Systematic Debugging

Overview

Random fixes waste time and create new bugs. Quick patches mask underlying issues.

Core principle: ALWAYS find root cause before attempting fixes. Symptom fixes are failure.

Violating the letter of this process is violating the spirit of debugging.

Iron Laws

  1. NEVER propose or implement a fix before completing Phase 1 root cause investigation — a fix without root cause is a guess that will fail or create new bugs.
  2. ALWAYS reproduce the bug reliably before debugging — if you can't reproduce it consistently, you're not debugging the real issue.
  3. NEVER make more than one change at a time when testing a hypothesis — multiple simultaneous changes make it impossible to determine which change fixed the problem.
  4. ALWAYS stop and question the architecture after 3 failed fix attempts — if each fix reveals a new problem, the issue is architectural, not symptomatic.
  5. NEVER skip creating a failing test case before implementing the fix — without a test, you cannot verify the fix worked or that it won't regress.

When to Use

When to Use

Use for ANY technical issue:

Use this ESPECIALLY when:

Don't skip when:

When to Use: debugging vs smart-debug

| Scenario | Use debugging | Use smart-debug |

| ------------------------------------ | --------------- | ----------------- |

| Simple, locally reproducible bug | Yes | Overkill |

| Root cause area already known | Yes | Optional |

| Static analysis / code review bug | Yes | No |

| Runtime / production issue | Start here | Preferred |

| Intermittent / hard-to-reproduce | Escalate | Yes |

| Needs hypothesis ranking gate | No | Yes (blocking) |

| Needs instrumentation + log analysis | No | Yes |

| Observability-driven (traces, APM) | No | Yes |

Rule of thumb: Start with debugging for straightforward bugs. Escalate to smart-debug when you need hypothesis ranking, structured instrumentation, or the bug is intermittent/production-only.

See also: .claude/skills/smart-debug/SKILL.md

The Four Phases

You MUST complete each phase before proceeding to the next.

Phase 1: Root Cause Investigation

BEFORE attempting ANY fix:

  1. Read Error Messages Carefully
  1. Reproduce Consistently
  1. Check Recent Changes
  1. Gather Evidence in Multi-Component Systems

WHEN system has multiple components (CI - build - signing, API - service - database):

BEFORE proposing fixes, add diagnostic instrumentation:

```

For EACH component boundary:

Run once to gather evidence showing WHERE it breaks

THEN analyze evidence to identify failing component

THEN investigate that specific component

```

Example (multi-layer system):

```bash

# Layer 1: Workflow

echo "=== Secrets available in workflow: ==="

echo "IDENTITY: ${IDENTITY:+SET}${IDENTITY:-UNSET}"

# Layer 2: Build script

echo "=== Env vars in build script: ==="

env | grep IDENTITY || echo "IDENTITY not in environment"

# Layer 3: Signing script

echo "=== Keychain state: ==="

security list-keychains

security find-identity -v

# Layer 4: Actual signing

codesign --sign "$IDENTITY" --verbose=4 "$APP"

```

This reveals: Which layer fails (secrets - workflow OK, workflow - build FAIL)

For distributed/microservice systems — prefer OpenTelemetry traces:

```bash

# Query traces by component (preferred over manual echo/env logging)

pnpm trace:query --component <service-name> --event <event-name> --since <ISO-8601> --limit 200

# When trace ID is already known

pnpm trace:query --trace-id <traceId> --compact --since <ISO-8601> --limit 200

```

Fragmented traces (each service has its own root span, trace IDs don't match across boundaries)

= broken context propagation. Fix traceparent/tracestate header forwarding before investigating business logic.

> Instrumentation Gate (before hypothesis generation): If runtime behavior remains unclear after static analysis, add targeted log statements at key decision nodes before generating hypotheses. Use session-scoped log files (.claude/context/tmp/debug-{sessionId}.log) to capture runtime state. Human-in-the-loop: ask the user to reproduce the bug after instrumentation is added, before analyzing results. Only proceed to Phase 2 once runtime evidence is collected.

  1. Trace Data Flow

WHEN error is deep in call stack:

See root-cause-tracing.md in this directory for the complete backward tracing technique.

Quick version:

Phase 2: Pattern Analysis

Find the pattern before fixing:

  1. Find Working Examples
  1. Compare Against References
  1. Identify Differences
  1. Understand Dependencies

Phase 3: Hypothesis and Testing

Scientific method:

  1. Form Single Hypothesis
  1. Test Minimally
  1. Verify Before Continuing
  1. When You Don't Know

Phase 4: Implementation

Fix the root cause, not the symptom:

  1. Create Failing Test Case
  1. Implement Single Fix
  1. Verify Fix
  1. Cleanup
  1. If Fix Doesn't Work
  1. If 3+ Fixes Failed: Question Architecture

Pattern indicating architectural problem:

STOP and question fundamentals:

Discuss with your human partner before attempting more fixes

This is NOT a failed hypothesis - this is a wrong architecture.

Red Flags - STOP and Follow Process

If you catch yourself thinking:

ALL of these mean: STOP. Return to Phase 1.

If 3+ fixes failed: Question the architecture (see Phase 4.5)

Your Human Partner's Signals You're Doing It Wrong

Watch for these redirections:

When you see these: STOP. Return to Phase 1.

Common Rationalizations

| Excuse | Reality |

| -------------------------------------------- | ----------------------------------------------------------------------- |

| "Issue is simple, don't need process" | Simple issues have root causes too. Process is fast for simple bugs. |

| "Emergency, no time for process" | Systematic debugging is FASTER than guess-and-check thrashing. |

| "Just try this first, then investigate" | First fix sets the pattern. Do it right from the start. |

| "I'll write test after confirming fix works" | Untested fixes don't stick. Test first proves it. |

| "Multiple fixes at once saves time" | Can't isolate what worked. Causes new bugs. |

| "Reference too long, I'll adapt the pattern" | Partial understanding guarantees bugs. Read it completely. |

| "I see the problem, let me fix it" | Seeing symptoms does not equal understanding root cause. |

| "One more fix attempt" (after 2+ failures) | 3+ failures = architectural problem. Question pattern, don't fix again. |

Quick Reference

| Phase | Key Activities | Success Criteria |

| --------------------- | ------------------------------------------------------ | --------------------------- |

| 1. Root Cause | Read errors, reproduce, check changes, gather evidence | Understand WHAT and WHY |

| 2. Pattern | Find working examples, compare | Identify differences |

| 3. Hypothesis | Form theory, test minimally | Confirmed or new hypothesis |

| 4. Implementation | Create test, fix, verify | Bug resolved, tests pass |

When Process Reveals "No Root Cause"

If systematic investigation reveals issue is truly environmental, timing-dependent, or external:

  1. You've completed the process
  2. Document what you investigated
  3. Implement appropriate handling (retry, timeout, error message)
  4. Add monitoring/logging for future investigation

But: 95% of "no root cause" cases are incomplete investigation.

Supporting Techniques

These techniques are part of systematic debugging and available in this directory:

Related skills:

Real-World Impact

From debugging sessions:

AI-Assisted Debugging & Modern Observability (2025+)

OpenTelemetry: The New Stack Trace

For distributed systems, OpenTelemetry traces replace manual echo/env evidence gathering. A trace shows the complete request journey across service boundaries via span IDs and trace IDs (W3C Trace Context standard: traceparent/tracestate headers).

Evidence hierarchy for distributed failures (prefer in order):

1. Distributed traces (OpenTelemetry spans, correlated trace IDs)
2. Structured logs with correlation IDs
3. Metrics with timestamps
4. Manual instrumentation (Phase 1 Step 4 bash examples)

Common symptom — fragmented traces:

Each service shows its own root span, trace IDs don't match across boundaries. This means context propagation is broken — fix header forwarding before investigating business logic.

AI-Assisted Root Cause Analysis

LLM-based debugging agents (2025 pattern) augment Phase 1 by reading production traces and correlating with codebase context to suggest minimal reproduction cases.

Use AI assistance for:

Do NOT skip Phase 1 when using AI assistance. AI suggestions are hypotheses — apply Phase 3 (hypothesis testing) before implementing any AI-suggested fix. AI cannot replace systematic investigation; it accelerates evidence gathering.

Anti-Patterns

| Anti-Pattern | Why It Fails | Correct Approach |

| -------------------------------------------- | --------------------------------------------------------------------------------- | -------------------------------------------------------------------- |

| "Quick fix for now, investigate later" | The quick fix becomes permanent; the root cause resurfaces as a different symptom | Always complete Phase 1 before touching production code |

| Making multiple changes at once | Can't determine which change fixed or broke the system; creates regressions | One change per hypothesis test; verify before the next change |

| Proposing AI-suggested fixes without testing | AI suggestions are hypotheses, not facts; applying them blindly skips Phase 3 | Treat AI suggestions as hypotheses to test, not answers to implement |

| Attempting a 4th fix after 3 failures | N+1 fix attempts on a broken approach compound the problem | After 3 failed fixes, escalate to architecture review |

| Skipping the failing test before the fix | You can't verify the fix worked, and regressions are invisible | Create the failing test first; it proves root cause and verifies fix |

Memory Protocol (MANDATORY)

Before starting:

Read .claude/context/memory/learnings.md

After completing:

ASSUME INTERRUPTION: If it's not in memory, it didn't happen.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.