| name | diagnose |
| description | Disciplined diagnosis loop for hard bugs and performance regressions. Reproduce, minimise, hypothesise, instrument, fix, regression-test. Use when user says "diagnose this", "debug this", reports a bug, says something is broken / throwing / failing, or describes a performance regression. Triggers: "diagnose", "debug this", "why is X broken", "X is throwing", "X is failing", "perf regression", "this got slow", "intermittent failure", "flaky test". |
Purpose
A discipline for hard bugs — the ones that don't yield to a quick read of the stack trace. Forces a fast deterministic feedback loop first, then ranked falsifiable hypotheses, then targeted instrumentation, then a regression test that locks the fix in place. Skip phases only with explicit justification. The goal is not "the bug stopped happening" but "we know why it happened and we can prove it won't return."
When exploring the codebase during diagnosis, use the project's domain glossary to build a clear mental model of the relevant modules, and check ADRs in the area you're touching.
Workflow
- Build a Feedback Loop
- This is the skill. Everything else is mechanical. If you have a fast, deterministic, agent-runnable pass/fail signal for the bug, you will find the cause — bisection, hypothesis-testing, and instrumentation all just consume that signal. If you don't have one, no amount of staring at code will save you.
- Spend disproportionate effort here. Be aggressive. Be creative. Refuse to give up.
- Try construction methods in roughly this order (stop at the first one that works):
- Failing test at whatever seam reaches the bug — unit, integration, e2e.
- Curl / HTTP script against a running dev server.
- CLI invocation with a fixture input, diffing stdout against a known-good snapshot.
- Headless browser script (Playwright / Puppeteer) — drives the UI, asserts on DOM/console/network.
- Replay a captured trace. Save a real network request / payload / event log to disk; replay it through the code path in isolation.
- Throwaway harness. Spin up a minimal subset of the system (one service, mocked deps) that exercises the bug code path with a single function call.
- Property / fuzz loop. If the bug is "sometimes wrong output", run 1000 random inputs and look for the failure mode.
- Bisection harness. If the bug appeared between two known states (commit, dataset, version), automate "boot at state X, check, repeat" so you can
git bisect run it.
- Differential loop. Run the same input through old-version vs new-version (or two configs) and diff outputs.
- HITL bash script. Last resort. If a human must click, drive them with
./scripts/hitl-loop.template.sh so the loop is still structured. Captured output feeds back to you.
- Build the right feedback loop, and the bug is 90% fixed.
- Example: User reports "the Export button sometimes throws" → start with a failing Playwright test that clicks Export 20× and asserts no console errors. If the bug is too deep in the stack for the UI to reach reliably, drop to a Node harness that calls the export function directly with a captured payload.
- Example: "API returns 500 under load" → curl loop hammering the endpoint 100×, asserting status==200, capturing the first failing response body.
- IF: bug only reproduces in production environment → see step 1c (cannot build a loop).
1a. Iterate on the Loop Itself
- Treat the loop as a product. Once you have a loop, sharpen it:
- Can I make it faster? Cache setup, skip unrelated init, narrow the test scope.
- Can I make the signal sharper? Assert on the specific symptom, not "didn't crash".
- Can I make it more deterministic? Pin time, seed RNG, isolate filesystem, freeze network.
- A 30-second flaky loop is barely better than no loop. A 2-second deterministic loop is a debugging superpower.
- Example: First-pass loop boots the whole app (45s) and clicks through 5 screens to trigger the bug. Refine: bypass auth with a session token fixture, deep-link straight to the broken screen, replay the captured XHR. New loop: 3s.
1b. Handle Non-Deterministic Bugs
- IF: bug only reproduces sometimes → the goal is not a clean repro but a higher reproduction rate.
- THEN: loop the trigger 100×, parallelise, add stress, narrow timing windows, inject sleeps. A 50%-flake bug is debuggable; 1% is not — keep raising the rate until it's debuggable.
- Example: "Race condition in cache write, fails ~2% of the time" → wrap the trigger in a 200-iteration loop with concurrent workers; failure rate jumps to ~40%; now you can hypothesise.
1c. When You Genuinely Cannot Build a Loop
- Stop and say so explicitly. Do not proceed to hypothesise without a loop.
- List what you tried.
- Ask the user for one of:
- (a) access to whatever environment reproduces it
- (b) a captured artifact (HAR file, log dump, core dump, screen recording with timestamps)
- (c) permission to add temporary production instrumentation
- Example: "I can't reproduce locally — the bug needs the prod payment provider sandbox. Options: give me sandbox creds, share a HAR from a failing session, or approve temporary structured logging around
chargeCard() for 24h."
- Do not proceed to step 2 until you have a loop you believe in.
-
Reproduce
- Run the loop. Watch the bug appear.
- Confirm:
- The loop produces the failure mode the user described — not a different failure that happens to be nearby. Wrong bug = wrong fix.
- The failure is reproducible across multiple runs (or, for non-deterministic bugs, reproducible at a high enough rate to debug against).
- You have captured the exact symptom (error message, wrong output, slow timing) so later phases can verify the fix actually addresses it.
- Example: User said "Export throws TypeError". Your loop throws a
RangeError from a different code path. STOP — that's a different bug. Adjust the loop or ask the user to clarify which symptom is the real one.
- IF: loop produces a different symptom than reported → narrow the loop or re-confirm the symptom with the user before proceeding.
- Do not proceed until you reproduce the bug the user actually reported.
-
Hypothesise
- Generate 3–5 ranked hypotheses before testing any of them. Single-hypothesis generation anchors on the first plausible idea.
- Each hypothesis must be falsifiable: state the prediction it makes.
- Format: "If
<X> is the cause, then <changing Y> will make the bug disappear / <changing Z> will make it worse."
- IF: you cannot state the prediction → the hypothesis is a vibe. Discard or sharpen it.
- Show the ranked list to the user before testing. They often have domain knowledge that re-ranks instantly ("we just deployed a change to #3"), or know hypotheses they've already ruled out. Cheap checkpoint, big time saver. Don't block on it — proceed with your ranking if the user is AFK.
- Example: Export throws
TypeError: cannot read 'length' of undefined. Ranked hypotheses:
exportItems is empty when called from the keyboard-shortcut path → predict: triggering Export from the menu (which preloads items) will not throw.
- New row format from yesterday's migration has a nullable
tags field that the formatter assumes is an array → predict: rows created before the migration export fine; rows created after throw.
- Race: items are still loading when Export fires after a quick page switch → predict: forcing a 500ms delay before clicking Export removes the bug.
-
Instrument
- Each probe must map to a specific prediction from step 3.
Works well with
Optional collaborators — diagnose runs standalone and these degrade gracefully if absent.
grill-me — when the bug report is vague, grill first to nail the real failure mode before building the diagnosis loop.
tdd — once the root cause is found, capture it as a failing test and drive the fix red-green so the regression can't return.