Skip to content

Automated Bug Hunting

Quickly locate the root cause of a subtle bug and apply a verified fix. OpenCode reads the stack trace, traces execution paths, and proposes targeted changes.

When to use this recipe

  • You have a stack trace or failing test but no clear cause.
  • The bug is intermittent or involves concurrency.
  • You want a fix with an explanation, not a guess.

Prerequisites

  • The failing test case or a reliable reproduction step.
  • OpenCode set up in your project with LSP enabled.

Steps

  1. Provide the failure signal

    Paste the stack trace or the failing test command into the session.

    FAIL src/workers/queue.test.ts
    > Timeout: message processed twice under load
  2. Ask for root-cause analysis

    This test fails intermittently under load. Trace the message flow from producer to consumer and find why a message can be processed twice.

  3. Review the trace

    OpenCode walks the call path and points to the exact race condition — for example, a missing mutex around a shared channel.

  4. Apply the fix

    Confirm the proposed change (e.g. adding a lock) and let OpenCode implement it.

  5. Verify with the reproduction

    Re-run the test under load to confirm the fix holds.

    npm test -- --grep queue

Key prompt

Here is the stack trace: [paste]. Trace the execution path through the code and identify the root cause. Propose the smallest fix that resolves it without introducing side effects.

Common pitfalls

  • Fixing symptoms: Ask OpenCode for the root cause first; a superficial patch may hide the bug elsewhere.
  • Concurrency bugs: Re-run the test many times — a single pass doesn’t prove the race is gone.
  • Environment-only bugs: If it can’t reproduce, share logs and timing details so OpenCode can reason about the path.

Next up