Settlement Recovery
settlement_runs (migration 020) makes settlement a resumable state machine — no orphan order claims are possible.
State machine
CREATED → CLAIMING → CLAIMED → VALIDATING → COMMITTED
↘ RELEASING → RELEASED (claim release is itself resumable)
↘ FAILED / NEEDS_REVIEW (recovery_action='release' → sweeper retries)1
2
3
2
3
- Claimed-but-uncommitted orders carry
orders.settlement_id='run:<run_id>'— always resolvable to a run row.run:*tags with no run row are zombie claims the sweeper releases. lease_owner/lease_expires_at/fencefence all transitions and order updates — a stale writer cannot release or commit.- Fee snapshot (
pg_fee_bp/platform_fee_bp) is fixed at run creation; commit re-checks0 ≤ fee ≤ gross,net ≥ 0— never commits abnormal settlement.
Operator actions
http
POST /api/v1/settlements # seller-scoped; resumes stale run inline or 409 SETTLEMENT_RUN_IN_PROGRESS
POST /api/v1/admin/settlements/sweep # admin — finish pending releases + zombie 'run:*' claims
POST /api/v1/admin/settlements/:id/mark-paid {payout_ref} # payout confirmation, CAS + ref conflict 4091
2
3
2
3
Recovery op RELEASE_STALE_SETTLEMENT_CLAIM (approval required) targets settlement_run or a single order when the sweeper is insufficient.
Evidence & drift
settlement.commitevidence is chain-sealed at record-batch commit (audit-evidence).- Post-commit re-check compares
SUM(amount-refunded)and order count against the run snapshot — drift →NEEDS_REVIEW(money is committed; investigate, don't re-run blindly). fee_rate.changeevidence records every fee write, so aNEEDS_REVIEWrun's snapshot can be audited against the rates in effect atrun.created_at.
S1 hardening notes (main, 85f113b / 45ffde5 / 6d8bb72 — after 45e270c)
- Transient DB failure mid-drive —
settle_implwas converted to fdb and gainedDriveOutcome::DbError(45ffde5): a transient D1 error returns HTTP 500 with the run left non-terminal and order claims retained. Resume viaPOST /api/v1/settlements(inline resume) or the admin sweep — a transient error never orphans claims. - Live-run guards — recovery ops (including
RELEASE_STALE_SETTLEMENT_CLAIM) no longer execute/verify against a live run target (45ffde5). - Strict reads —
committed_krwisResult-checked andscalar_i64parse failures are 500 (85f113b); a read error is never treated as a clean/zero state. - Suite status —
settle-recovery.test.mjsis 27 checks after the s1-02 fixture-pollution fix (6d8bb72): SREC-01's expected set is now derived independently — the suite had read 23/25 in CI because of integrity-suite fixture leftovers (test-isolation defect; implementation unchanged). 27/27 locally at45ffde5, plus injected-failure legs in the newfailclosedsuite (20/20). - S1 residuals — closed in S2 —
NOUGAT_FIprobe classessettle.claimed/settle.chk/settle.counterare now asserted per-leg viax-fi-probesrequest arming (s2-03c5cffd7; fault-injection 77 legs, incl.settle.claim_batch→ 500SETTLEMENT_RUN_FAILEDand a SIGKILL-mid-claim stale-resume leg). The counter row-missing →seq=1residual is fixed bynext_counterseed-then-increment (s2-10940fd75) — a missingsettlementcounter row heals; a missingcounterstable or a real DB error fails closed (500TX), no silentseq=1.
S2 assurance notes (main e6a05cf / c5cffd7 / 940fd75)
- New recon finding types over settlements (s2-02
e6a05cf).settlement_membership(order_ids_json↔orders.settlement_iddrift, dup/broken JSON),settlement_fee_mismatch(fees re-derived from the stored bp snapshot),settlement_run_mismatch(settlement ↔ committing run's gross/net/bp/order_count), andpost_settlement_refund(member-order refunds after commit — surfaced as a distinctneeds_reviewfinding, not folded into gross). The oldog<>gfalse positive on settlements containingpartially_refundedorders is fixed — recon now uses the sameΣ(amount−refunded)gross authority as the pipeline. - Settle-drive FI probes (s2-03).
settle.claimed,settle.counter,settle.chk,settle.snapshot,settle.live_check,settle.claim_batch— armed per-request viax-fi-probesunder the same dev/staging gate; production config is inert. - Cron-side settlement sweep unchanged in shape — the s2-10 cron change touches recon-run lifecycle (resume/reap), not the settlement sweeper;
sweep_stale_settlement_runsstill runs each tick before the recon step.