Debugging
A disciplined root-cause loop. The goal is a stated causal mechanism with
proof, then a fix verified by the exact check that first exposed the problem.
Never fix by guessing, and never make a symptom disappear without knowing why.
The loop
- Reproduce. Run the failing thing yourself and capture the exact error,
command, and environment. If you cannot reproduce, stop and gather the
reporter's exact conditions; do not fix blind. Side observations found on
the way get noted and set aside, not chased.
- Trace the intended path. Before reading logs, read the code/docs to
state what SHOULD happen, step by step. You cannot recognize the broken
step without knowing the intended sequence.
- Check live state. What is actually on disk, in the database, running as
a process, deployed? A grep that finds no trace of a migration is not proof
it never ran; completed one-off scripts are routinely deleted. Absence in
code is not absence in reality.
- Find the smoking gun. Logs, CI runs, git history. Cheapest checks
first: installed version vs latest, the artifact's own declared type or
config, the library's supported list. Only conclude "unsupported" or
"impossible" after checking the declared capability directly.
- Prove the mechanism. Confirm with a live probe, not log trust (example:
the blob that returned 403 in the failing run returns 200 now, so it was a
permission-timing issue, not missing data). A differential test beats an
assumption: when two configurations should differ, run both and compare.
- Only now read source for the WHY, and state the cause in one sentence
with the evidence that proves it, plus what changes AND what deliberately
does not change because it was not the cause.
- Fix, minimally. Fix the upstream cause, not the symptom (a service that
dies when the machine sleeps needs the sleep addressed, not a restart
loop).
- Verify with the original failing check, unchanged. Then run the wider
gates (tests, lint, typecheck). Add a regression test that would have
caught this bug. If recovery of live state was part of the fix, its success
doubles as diagnosis confirmation.
Isolation techniques (when the cause is entangled)
- Single-variable toggles: flip one factor at a time (feature flag, config
bit, environment) and record the result per state. Never attempt a compound
fix while two candidate causes are still entangled.
- Necessary-but-not-sufficient check: after any partial fix, re-run the
original failing check. The first improving number is not proof of a
complete fix; say "necessary but not sufficient" and keep going.
- Ranked compounding causes: many incidents have several contributing
causes (stale binary + lost race + shared timeout). Enumerate and rank them;
do not stop at the first plausible one.
- Confound check: before concluding "the fix did not work" or "the test is
wrong", look for an independent confound (a second process racing the test,
a hook rewriting the file). Re-run clean.
- Instrumentation-first for weird results: a flat or extreme result that
fails to discriminate across conditions that should matter is an
instrumentation bug until a targeted, isolated reproduction says otherwise.
Do not report it as a finding, and do not let it block unrelated work while
you wait to reproduce it.
- Doc-vs-code conflicts: when documentation (or an Architecture Decision
Record) and code disagree, determine the actual source of truth
empirically (test the code), then fix whichever artifact is wrong, making
the fix more precise about where the invariant is enforced. Never silently
pick a side.
Anti-shortcut gates (hard rules)
- Never delete, skip, or loosen a failing test to make a suite pass. If a test
is genuinely wrong, prove it (git history: did the tested thing move or
disappear?) and say so explicitly.
- Never add a broad catch, silent fallback, or retry loop to make a symptom
disappear. That converts a loud bug into a silent one.
- Never weaken a designated safety mechanism (rate limit, halt, permission
gate) because it fired. When a guardrail fires during a bad outcome, the
default hypothesis is that the new code is incompatible with a correct
guardrail. Changing the guardrail requires separate, explicit user sign-off.
- Never read or print secrets (.env contents, tokens) while debugging
credential or environment issues; test credentials differentially instead
(call the API with each and compare behavior).
- Retry a flaky operation at most once under changed conditions. If it fails
again under conditions you believe clean, stop retrying and surface a
structural hypothesis instead.
Verification is layered
An automated check passing covers only what that check was built to detect.
Before declaring a generated or visual artifact done, do one perceptual/manual
pass for defect classes the validator cannot see (label collisions,
off-canvas content, semantic errors). If the ideal verification is
structurally unavailable, run the nearest safe non-mutating substitute and
state exactly what it does and does not cover; never report "verified"
against a silently downgraded check.
Numerical and scientific code
For parity work against a reference implementation, tolerance judgment, and
trajectory comparison, read references/numerical-debugging.md before
starting. Summary of its hard rules: compare from identical initialization,
know the float noise floor before calling a residual a bug, reserve
"bit-exact" for residuals at machine precision, and test whether the metric
itself is well-posed before chasing an unmet target.
When to stop
- Root cause stated in one sentence, with cited evidence.
- Fix verified by the original failing check plus the standard gates.
- Regression test added (or an explicit reason why not).
- If the investigation dead-ends: document it (goal, issue, root cause if
known, lesson, alternative) in the project's scratch/history notes and the
issue tracker; a documented dead end is a valid outcome, a silent one is
not.
- If you are deep in a long session and the remaining fix is risky, prefer a
validated, independently re-runnable handoff artifact (script plus ordered
notes, verified to run) over a rushed multi-file edit.
1---2name: debugging3description: Use this skill when the user asks to "debug", "find the root cause", "figure out why X fails", "investigate this bug", "this test is flaky", "the build broke", "production incident", "why is the output wrong", or when any observed behavior contradicts expected behavior. Covers the reproduce-isolate-prove-fix-verify loop, anti-shortcut gates, and numerical/scientific debugging.4---56# Debugging78A disciplined root-cause loop. The goal is a stated causal mechanism with9proof, then a fix verified by the exact check that first exposed the problem.10Never fix by guessing, and never make a symptom disappear without knowing why.1112## The loop13141. **Reproduce.** Run the failing thing yourself and capture the exact error,15 command, and environment. If you cannot reproduce, stop and gather the16 reporter's exact conditions; do not fix blind. Side observations found on17 the way get noted and set aside, not chased.182. **Trace the intended path.** Before reading logs, read the code/docs to19 state what SHOULD happen, step by step. You cannot recognize the broken20 step without knowing the intended sequence.213. **Check live state.** What is actually on disk, in the database, running as22 a process, deployed? A grep that finds no trace of a migration is not proof23 it never ran; completed one-off scripts are routinely deleted. Absence in24 code is not absence in reality.254. **Find the smoking gun.** Logs, CI runs, git history. Cheapest checks26 first: installed version vs latest, the artifact's own declared type or27 config, the library's supported list. Only conclude "unsupported" or28 "impossible" after checking the declared capability directly.295. **Prove the mechanism.** Confirm with a live probe, not log trust (example:30 the blob that returned 403 in the failing run returns 200 now, so it was a31 permission-timing issue, not missing data). A differential test beats an32 assumption: when two configurations should differ, run both and compare.336. **Only now read source for the WHY**, and state the cause in one sentence34 with the evidence that proves it, plus what changes AND what deliberately35 does not change because it was not the cause.367. **Fix**, minimally. Fix the upstream cause, not the symptom (a service that37 dies when the machine sleeps needs the sleep addressed, not a restart38 loop).398. **Verify with the original failing check**, unchanged. Then run the wider40 gates (tests, lint, typecheck). Add a regression test that would have41 caught this bug. If recovery of live state was part of the fix, its success42 doubles as diagnosis confirmation.4344## Isolation techniques (when the cause is entangled)4546- **Single-variable toggles**: flip one factor at a time (feature flag, config47 bit, environment) and record the result per state. Never attempt a compound48 fix while two candidate causes are still entangled.49- **Necessary-but-not-sufficient check**: after any partial fix, re-run the50 original failing check. The first improving number is not proof of a51 complete fix; say "necessary but not sufficient" and keep going.52- **Ranked compounding causes**: many incidents have several contributing53 causes (stale binary + lost race + shared timeout). Enumerate and rank them;54 do not stop at the first plausible one.55- **Confound check**: before concluding "the fix did not work" or "the test is56 wrong", look for an independent confound (a second process racing the test,57 a hook rewriting the file). Re-run clean.58- **Instrumentation-first for weird results**: a flat or extreme result that59 fails to discriminate across conditions that should matter is an60 instrumentation bug until a targeted, isolated reproduction says otherwise.61 Do not report it as a finding, and do not let it block unrelated work while62 you wait to reproduce it.63- **Doc-vs-code conflicts**: when documentation (or an Architecture Decision64 Record) and code disagree, determine the actual source of truth65 empirically (test the code), then fix whichever artifact is wrong, making66 the fix more precise about where the invariant is enforced. Never silently67 pick a side.6869## Anti-shortcut gates (hard rules)7071- Never delete, skip, or loosen a failing test to make a suite pass. If a test72 is genuinely wrong, prove it (git history: did the tested thing move or73 disappear?) and say so explicitly.74- Never add a broad catch, silent fallback, or retry loop to make a symptom75 disappear. That converts a loud bug into a silent one.76- Never weaken a designated safety mechanism (rate limit, halt, permission77 gate) because it fired. When a guardrail fires during a bad outcome, the78 default hypothesis is that the new code is incompatible with a correct79 guardrail. Changing the guardrail requires separate, explicit user sign-off.80- Never read or print secrets (.env contents, tokens) while debugging81 credential or environment issues; test credentials differentially instead82 (call the API with each and compare behavior).83- Retry a flaky operation at most once under changed conditions. If it fails84 again under conditions you believe clean, stop retrying and surface a85 structural hypothesis instead.8687## Verification is layered8889An automated check passing covers only what that check was built to detect.90Before declaring a generated or visual artifact done, do one perceptual/manual91pass for defect classes the validator cannot see (label collisions,92off-canvas content, semantic errors). If the ideal verification is93structurally unavailable, run the nearest safe non-mutating substitute and94state exactly what it does and does not cover; never report "verified"95against a silently downgraded check.9697## Numerical and scientific code9899For parity work against a reference implementation, tolerance judgment, and100trajectory comparison, read `references/numerical-debugging.md` before101starting. Summary of its hard rules: compare from identical initialization,102know the float noise floor before calling a residual a bug, reserve103"bit-exact" for residuals at machine precision, and test whether the metric104itself is well-posed before chasing an unmet target.105106## When to stop107108- Root cause stated in one sentence, with cited evidence.109- Fix verified by the original failing check plus the standard gates.110- Regression test added (or an explicit reason why not).111- If the investigation dead-ends: document it (goal, issue, root cause if112 known, lesson, alternative) in the project's scratch/history notes and the113 issue tracker; a documented dead end is a valid outcome, a silent one is114 not.115- If you are deep in a long session and the remaining fix is risky, prefer a116 validated, independently re-runnable handoff artifact (script plus ordered117 notes, verified to run) over a rushed multi-file edit.