Index of every page on this site: llms.txt
All skills
obra avatar

/systematic-debugging

@22f0b46 official
by Jesse Vincentobra/superpowers312k stars
27,751
Run it yourself

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes

Use this Skill: https://skilld.dev/gh/obra/superpowers/systematic-debugging

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

condition-based-waiting.md

≈879 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Condition-Based Waiting

Overview

Flaky tests often guess at timing with arbitrary delays. This creates race conditions where tests pass on fast machines but fail under load or in CI.

Core principle: Wait for the actual condition you care about, not a guess about how long it takes.

When to Use

digraph when_to_use {
    "Test uses setTimeout/sleep?" [shape=diamond];
    "Testing timing behavior?" [shape=diamond];
    "Document WHY timeout needed" [shape=box];
    "Use condition-based waiting" [shape=box];

    "Test uses setTimeout/sleep?" -> "Testing timing behavior?" [label="yes"];
    "Testing timing behavior?" -> "Document WHY timeout needed" [label="yes"];
    "Testing timing behavior?" -> "Use condition-based waiting" [label="no"];
}

Use when:

  • Tests have arbitrary delays (setTimeout, sleep, time.sleep())
  • Tests are flaky (pass sometimes, fail under load)
  • Tests timeout when run in parallel
  • Waiting for async operations to complete

Don't use when:

  • Testing actual timing behavior (debounce, throttle intervals)
  • Always document WHY if using arbitrary timeout

Core Pattern

// ❌ BEFORE: Guessing at timing
await new Promise(r => setTimeout(r, 50));
const result = getResult();
expect(result).toBeDefined();

// ✅ AFTER: Waiting for condition
await waitFor(() => getResult() !== undefined);
const result = getResult();
expect(result).toBeDefined();

Quick Patterns

Scenario Pattern
Wait for event waitFor(() => events.find(e => e.type === 'DONE'))
Wait for state waitFor(() => machine.state === 'ready')
Wait for count waitFor(() => items.length >= 5)
Wait for file waitFor(() => fs.existsSync(path))
Complex condition waitFor(() => obj.ready && obj.value > 10)

Implementation

Generic polling function:

async function waitFor<T>(
  condition: () => T | undefined | null | false,
  description: string,
  timeoutMs = 5000
): Promise<T> {
  const startTime = Date.now();

  while (true) {
    const result = condition();
    if (result) return result;

    if (Date.now() - startTime > timeoutMs) {
      throw new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`);
    }

    await new Promise(r => setTimeout(r, 10)); // Poll every 10ms
  }
}

See condition-based-waiting-example.ts in this directory for complete implementation with domain-specific helpers (waitForEvent, waitForEventCount, waitForEventMatch) from actual debugging session.

Common Mistakes

❌ Polling too fast: setTimeout(check, 1) - wastes CPU ✅ Fix: Poll every 10ms

❌ No timeout: Loop forever if condition never met ✅ Fix: Always include timeout with clear error

❌ Stale data: Cache state before loop ✅ Fix: Call getter inside loop for fresh data

When Arbitrary Timeout IS Correct

// Tool ticks every 100ms - need 2 ticks to verify partial output
await waitForEvent(manager, 'TOOL_STARTED'); // First: wait for condition
await new Promise(r => setTimeout(r, 200));   // Then: wait for timed behavior
// 200ms = 2 ticks at 100ms intervals - documented and justified

Requirements:

  1. First wait for triggering condition
  2. Based on known timing (not guessing)
  3. Comment explaining WHY

Real-World Impact

From debugging session (2025-10-03):

  • Fixed 15 flaky tests across 3 files
  • Pass rate: 60% → 100%
  • Execution time: 40% faster
  • No more race conditions

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning3d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub4d

    The skill provides debugging methodologies and a shell script for identifying problematic tests. A vulnerability in the shell script could allow for command injection if an untrusted search pattern is provided.

  • Socket3d

    No alerts

  • Snyk4d

    Risk: LOW · No issues

  • Runlayer8mo

    2/11 files flagged

  • ZeroLeaks6mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 22f0b46. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 5 days ago.

Activeupdated last month
  • Debugging
  • Testing
  • root-cause-analysis
  • troubleshooting
  • process
  • error-handling
  • verification
  • test-driven-development

README badge

README badge for obra/superpowers/systematic-debugging

Guides investigation of bugs, test failures, and unexpected behavior through four mandatory phases: root cause investigation, pattern analysis, hypothesis testing, and implementation. Emphasizes finding the actual source before attempting fixes, with explicit checks to stop and reassess if three fixes fail without resolution.

Generated from the current SKILL.md.

When should I use this debugging skill instead of just trying fixes?
Use it for any technical issue: test failures, bugs, performance problems, build failures. Especially critical when under time pressure, when you've already tried multiple fixes, or when the issue seems simple. Skipping it when rushing actually wastes more time than following the process.
What does 'root cause investigation' actually involve?
Read error messages completely, reproduce the issue consistently, check recent changes via git diff, and gather diagnostic evidence at component boundaries. For multi-layer systems, add logging at each boundary to show where data flow breaks before proposing any fix.
What should I do if I've tried 3 or more fixes and nothing works?
Stop attempting fixes and question the architecture instead. When multiple fixes each reveal new problems in different places, the issue is likely architectural rather than a simple bug. Discuss with your partner before attempting more fixes.
Do I need to write a test before fixing the bug?
Yes. Phase 4 requires creating a failing test case—automated if possible, otherwise a one-off script. The test must pass after your fix and serve as proof the issue is resolved.
What's the difference between fixing symptoms versus fixing root cause?
Symptom fixes mask the real problem and waste time. Root cause fixes prevent the issue from recurring. The process enforces finding root cause (where bad data originates) before any code changes.

Generated from the current SKILL.md. These answers refresh after source changes.