A double-refund bug that passed 100% of unit tests.
Below is an end-to-end walkthrough of what SafeState produces on a stateful billing module: the code under test, the shrunk 3-step failure, the emitted zero-dependency Vitest repro, the coding-agent fix prompt, and the mutation-gated verification run.
1. The module under test (100% green unit tests)
Single-call unit tests verify that requestRefund() refunds a charge and rejects an over-refund when called twice sequentially. What they miss is the async yield around the outbound gateway call:
export async function requestRefund(db, gateway, chargeId: string, cents: number) {
const charge = await db.getCharge(chargeId);
if (charge.refundedCents + cents > charge.amountCents) {
throw new Error("EXCEEDS_CHARGE");
}
// Yield point: while awaiting the external provider, a concurrent webhook can land
await gateway.createRefund(chargeId, cents);
await db.setRefundedCents(chargeId, charge.refundedCents + cents);
}
2. Shrunk 3-step counterexample (seed 42, 16 shrinks)
SafeState explored interleaved schedules with its deterministic scheduler and shrunk the failing trace to 3 minimal calls:
Step 1: CreateCharge({ chargeId: "ch_2", amountCents: 2500 })
Step 2: RequestRefund({ chargeId: "ch_2", cents: 2500 }) // yields at gateway.createRefund
Step 3: DeliverWebhook({ eventId: "evt_9", type: "refund.succeeded", chargeId: "ch_2", cents: 2500 })
-> webhook increments refundedCents: 0 -> 2500
-> Step 2 resumes and writes stale total: refundedCents = 5000
Invariant NO_OVER_REFUND failed:
charge "ch_2" has refundedCents (5000) > amountCents (2500)
3. Standalone Vitest repro (0 SafeState imports)
SafeState compiles the shrunk trace into an ordinary Vitest file you can run locally or commit directly into your repository without installing any SafeState package:
import { describe, expect, it } from "vitest";
import { createInMemoryBillingStore, handleWebhook, requestRefund } from "../src/billing/refunds";
describe("SafeState shrunk repro: NO_OVER_REFUND (seed 42)", () => {
it("never exceeds charge amount when a refund webhook lands mid-flight", async () => {
const store = createInMemoryBillingStore();
await store.createCharge("ch_2", 2500);
let resumeGateway!: () => void;
const gateway = {
createRefund: () => new Promise<void>((resolve) => { resumeGateway = resolve; }),
};
const inFlight = requestRefund(store, gateway, "ch_2", 2500);
await handleWebhook(store, { id: "evt_9", type: "refund.succeeded", chargeId: "ch_2", cents: 2500 });
resumeGateway();
await inFlight.catch(() => undefined);
const finalCharge = await store.getCharge("ch_2");
expect(finalCharge.refundedCents).toBeLessThanOrEqual(finalCharge.amountCents);
});
});
4. Copy-paste block for your coding agent
Paste this block straight into your coding agent alongside the emitted repro file:
## Fix stateful race: NO_OVER_REFUND (billing/refunds.ts:48)
Failing repro: vitest run tests/safestate/billing-double-refund.spec.ts
Shrunk trace (seed 42):
1. CreateCharge("ch_2", 2500)
2. RequestRefund("ch_2", 2500) yields at gateway.createRefund()
3. DeliverWebhook("refund.succeeded", "ch_2", 2500) commits before step 2 writes back
Fix requirement:
- Reserve refund capacity atomically before calling gateway.createRefund()
- Deduplicate refund.succeeded webhook events by eventId and refundId
- Re-run vitest run tests/safestate/billing-double-refund.spec.ts until green
5. Anti-tautology mutation gate & verified re-check
Before turning the pull request check green, SafeState injects a planted fault that weakens the refund cap guard and confirms the invariant fails on the broken mutant. Once the mutation gate passes, SafeState runs 10,000 deterministic traces at $0 LLM cost and posts a passing check.