website/src/content/docs/guides/harden.md
title: Harden Python code description: Find silent failures, fixed waits, retries, and unreliable tests, then verify every fix with the xpd enhance harden determinism gate. sidebar: order: 8
Run xpd enhance harden <paths> to find silent failures, fixed waits, retries, and unreliable tests across a Python repository, and prove every fix against a recorded determinism gate.
| 1 | flowchart LR |
| 2 | Code["Python modules and tests"] --> Survey["Find silent fallbacks, waits, and flaky tests"] |
| 3 | Survey --> Loop["start -> plan -> gate (runs check twice) -> finish"] |
What xpd enhance harden checks
| Group | Finding kinds | What it catches |
|---|---|---|
| Silent failure and reliability errors | dual-key-fallback, fallback-on-exception, rule-issue | Dual-key fallbacks, silent fallback-on-exception paths, and error-severity lint findings |
| Fixed waits and retries | sleep-or-retry | time.sleep, asyncio.sleep, and retry wrappers hiding timing bugs |
| Disabled or unreliable tests | disabled-test, flaky-test | Skipped tests, test sleeps, reruns, suppressed health checks, and mock imports |
Every finding of this track is advice: a fix may name one, and no run or audit has to clear one. A kind blocks only after an oracle that can prove a finding wrong has labelled at least 30 of its findings and at least 90% were right, and no kind has reached that. Run xpd enhance harden --questions to inspect the six reliability questions. At every gate, xpd runs the first --check command twice and verifies that both exit status and stdout SHA-256 digest match.
Walkthrough on a failing module
Save a module that hides a parse error behind a second parser, start a recorded run, fix it, and close the ledger.
| 1 | def load_count(raw): |
| 2 | try: |
| 3 | return parse_count(raw) |
| 4 | except Exception: |
| 5 | return parse_count_v1(raw) |
| 6 | |
| 7 | |
| 8 | def parse_count(raw): |
| 9 | return int(raw) |
| 10 | |
| 11 | |
| 12 | def parse_count_v1(raw): |
| 13 | return int(raw.split("=")[1]) |
| 1 | $ git init -q && git add loader.py && git -c user.name=xpd -c user.email=xpd@example.com -c commit.gpgsign=false commit -qm start |
| 2 | $ xpd enhance harden start loader.py --check "python3 -m py_compile loader.py" --format json | jq -c '{findings: .run.was, next: .next.stage}' |
| 3 | {"findings":1,"next":"plan"} |
| 4 | $ xpd enhance harden loader.py --format json | jq -c '{findings: (.findings | length), advice: [.advice[].kind]}' |
| 5 | {"findings":0,"advice":["fallback-on-exception"]} |
| 6 | $ xpd enhance harden loader.py --format json | jq -c '{fixes: [{id: "propagate", title: "Let the parse error surface", findings: [.advice[].id]}]}' | xpd enhance harden plan - --format json | jq -c .next.stage |
| 7 | "fix" |
| 8 | $ sed -i -e '10,13d' -e '2,5c\ return parse_count(raw)' loader.py && git -c user.name=xpd -c user.email=xpd@example.com -c commit.gpgsign=false commit -qam "Let the parse error surface" |
| 9 | $ xpd enhance harden gate propagate --format json | jq -c '.run.rounds[0][0] | {outcome, fixed: (.fixed | length)}' |
| 10 | {"outcome":"kept","fixed":1} |
| 11 | $ xpd enhance harden finish --format json | jq -c .run.finished |
| 12 | xpd: 1 findings at the start, 0 now: 1 fixed, 0 added, 0 accepted, 0 left without a reason; lines 13 -> 6 |
| 13 | true |
Run stages
- Start from a clean commit with
xpd enhance harden start . --check "<project-check>". - List finding IDs with
xpd enhance harden . --format json. - Record the fixes, each naming the findings it removes, with
xpd enhance harden plan plan.json. - Approve interface changes with
xpd enhance harden approve <fix-id>, commit one fix, and runxpd enhance harden gate <fix-id>. - Restore an abandoned edit before
xpd enhance harden drop <fix-id> --reason "<why>", checkxpd enhance harden statusafter a break, and close the run withxpd enhance harden finish.
See the command reference for every flag and subcommand.