Hardens research and scientific code for correctness, numerical stability, and regression safety. Establishes explicit verification criteria and targeted tests.
How this skill is triggered — by the user, by Claude, or both
Slash command
/ai-research-workflows:hardening-research-codeThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Make research and scientific code trustworthy: correct, regression-safe, and
Make research and scientific code trustworthy: correct, regression-safe, and numerically stable by establishing explicit criteria and backing them with targeted tests.
This skill leans Direct by default. For the full Collaborative-vs-Direct protocol and override rules, see the Interaction Modes reference in the ai-research-workflows:using-research-workflows skill.
Research code often starts as exploratory scripts. Hardening turns it into trustworthy software: outputs are verifiably correct, regressions are caught automatically, and numerical behavior is documented and bounded.
Pinning the current outputs proves only that behavior hasn't changed — not that it is right. Never label outputs as a verified or "golden" baseline until they are checked against an independent reference (analytic limit, reference implementation, published result, or invariant).
Before falling back to regression-pinning, you MUST confirm no analytic limit,
reference implementation, or invariant is reasonably derivable. If one is,
verify against it first — "the output looks plausible" is not verification. If
you pin outputs that have NOT been independently verified, label the baseline
UNVERIFIED so it is never mistaken for established-correct values, and say so
to whoever relies on it.
Before writing any test, decide what "correct" means for this code:
Document the chosen criterion explicitly before writing tests. A test without a stated criterion is uninterpretable when it fails.
Compute expected outputs from a trusted source — an analytic formula, a reference implementation, or a published dataset — and assert that the code under test matches those outputs within tolerance. Store expected values and any input data in a versioned file alongside the tests.
Never use exact float equality. Always use absolute or relative tolerances appropriate to the domain:
# strategy-level pseudocode — defer mechanics to python-testing skill
assert |result - expected| < atol + rtol * |expected|
Document the tolerance choice: what physical or numerical argument justifies it? If tolerance is tightened or loosened later, the justification must be updated.
Choosing the bound: start from the floating-point floor — roughly machine
epsilon times the number of accumulating operations (atol ≈ N · eps · scale)
— and widen only for a documented reason (iterative-solver tolerance, stochastic
variance). Use rtol for values spanning orders of magnitude, atol for values
near zero. Never tune the tolerance to make a failing test pass — a bound
chosen to fit the result is a silent correctness hole; derive it from the
numerics, not the output.
Use this only after confirming no analytic limit, reference implementation,
or invariant is reasonably derivable (see the Iron Law) — a regression baseline
is a fallback, not a substitute for verification. When it genuinely is the only
option: run the code on canonical inputs, check the outputs against whatever
partial criteria you can (ranges, signs, invariants, expected trends), and pin
them. Future runs must stay within tolerance of that baseline. If the pinned
values were not verified against an independent reference, label the baseline
UNVERIFIED so it is never mistaken for established-correct output. Update the
baseline deliberately, with a commit message explaining why the output changed.
Perturb inputs, random seeds, data types (e.g., float32 vs float64), or batch sizes, and verify that outputs vary by a bounded amount. A result that changes dramatically under small perturbations is not trustworthy, regardless of how close it is to a reference on the nominal input.
Assert structural properties that must hold regardless of the specific input values: symmetry, monotonicity, conservation, idempotence, or range bounds. These catch whole classes of bugs that value-comparison tests miss.
Defer pytest fixtures, parametrization, numpy.testing / torch.testing
assertion mechanics, and CI configuration to the
scientific-python-development:python-testing skill. This skill is the
research-specific strategy layer: what to validate and why. Apply the
same principles to R, Julia, Fortran, or any other runtime.
ai-research-workflows:ensuring-reproducibility skill for test-data provenance (content hashes,
retrieval dates, lockfile references).The deadline temptation is to pin whatever the script prints and call it hardened:
| Thought | Reality |
|---|---|
| "The output looks plausible, I'll pin it as the baseline" | Plausible ≠ correct. Check a derivable reference/invariant first; otherwise label it UNVERIFIED. |
| "There's an analytic case but it'd take a while to derive" | That derivation is the correctness anchor. Do it before pinning, or mark the baseline UNVERIFIED. |
"float == is fine here, it's the same machine" | It won't be the same machine in CI or for the next person. Use tolerances. |
| "I'll widen the tolerance so the test passes" | Tuning the bound to the result hides the bug. Derive the bound from the numerics. |
| "It passed on the first run, so it's correct" | A test never seen to fail proves nothing. Make it go red, then green. |
== on floating-point results fails on trivially different hardware or library versions; always use absolute or relative tolerances appropriate to the domain.Before marking hardening complete:
UNVERIFIEDIntegrates with the ai-research-workflows:validating-implementations skill (confirming the
hardened code still satisfies its plan's success criteria) and the
ai-research-workflows:ensuring-reproducibility skill (provenance for reference data and pinned
baselines).
npx claudepluginhub uw-ssec/rse-plugins --plugin ai-research-workflowsVerifies math-heavy code for algorithmic correctness and numerical stability. Use when reviewing scientific algorithms, ML models, or numerical code.
Audits paper-vs-code implementation fidelity across five dimensions: formula matching, hyperparameter parity, eval protocol, notation consistency, and citation chain. Reads paper from PDF, arXiv URL, or pasted text.
Verifies reproducibility claims for experiments: requires exact commands, config, seed, commit hash, dataset version, and metric tables before accepting performance improvements.