The exorcist for flaky tests

Catchthe flake.Then proveit's gone.

A 1-in-30 ghost becomes a 30/30 bug you can replay. A Kiro agent fixes it, and six deterministic seals decide whether the fix ships.

Fig. 1 · the subject

  1. 01 haunted5/30
  2. 02 possessed30/30
  3. 03 exorcised0/500
5/30 → 30/30runs that fail: in the scan, then under the possession seed
0/500chaos seeds that fail after the exorcism
6/6haunted tests possessed
0tests weakened, across 3 exorcisms

01Detect

Every test, run 30 times under chaos.

6 of 13 tests in haunted-demo failed at least once, each run under its own chaos vector. Hover or focus a cell to see the vector that run used.

Phase 1 shows the scan: scattered failures. Phase 2 shows the possession seed failing every run. Phase 3 shows the same runs passing after the exorcism.

catalog prices a vip seattests/order-dependence.test.ts
56.7%HAUNTED
shared cart starts emptytests/shared-state-leak.test.ts
43.3%HAUNTED
offers a valid next slottests/time-of-day.test.ts
33.3%HAUNTED
reads seat availabilitytests/network-latency.test.ts
26.7%HAUNTED
captures payment before sending the receipttests/timer-race.test.ts
16.7%HAUNTED
gives 20 bookings unique idstests/unseeded-random.test.ts
6.7%HAUNTED
stable controls counts seats in a fresh carttests/controls.test.ts
0.0%STABLE
stable controls formats booking idstests/controls.test.ts
0.0%STABLE
stable controls formats slot labelstests/controls.test.ts
0.0%STABLE
stable controls rejects unknown skus with a loaded catalogtests/controls.test.ts
0.0%STABLE
stable controls totals a fresh carttests/controls.test.ts
0.0%STABLE
catalog loads the catalogtests/order-dependence.test.ts
0.0%STABLE
shared cart adds a seattests/shared-state-leak.test.ts
0.0%STABLE

failedpassedskipped

02Possess

The ghost, caught in one seed.

A fast-check search over the chaos vector finds a failing run, then shrinks it to the perturbations that matter. What is left fails every time: the possession seed.

PS-3BD799E0 · CASE 01

captures payment before sending the receipt

≠·▒·▒≠░≠⟳●0
seed
0
randomSeed
0
timerJitter
0 to 27 ms
reproduces
30/30 runs fail
found in
6 probes, 2 shrink steps
tests/timer-race.test.ts

03Autopsy

Anatomy of the race.

Which perturbation was active, which line it reached, and which assertion broke. Read top to bottom.

captures payment before sending the receipt

  1. Perturbations active

    • test order pinned to the order in the file
    • timers and setImmediate delayed by up to 27 ms (seed 0)
    • Math.random seeded with 0
  2. Assertion failed

    tests/timer-race.test.tsline 6
    3
    4it("captures payment before sending the receipt", async () => {
    5 const { events } = await checkout();
    6 expect(events).toEqual(["payment-captured", "receipt-sent"]);
    7});

    expected [ 'payment-captured', 'receipt-sent' ], actual [ 'receipt-sent', 'payment-captured' ]

04Exorcise

Six seals, or no fix.

The exorcist agent proposes a fix. Deterministic code accepts or rejects it against the canon, E-1 to E-6. The exorcisms in this demo were recorded from real poltergeist exorcise runs with the headless Kiro agent, and replay here without a Kiro call.

captures payment before sending the receipt

1 revision · 419 s · branch exorcist/timer-race--captures-payment-before-sending-the-receipt

SealRuleEvidenceResult
E-1seed reproduces before the fixseed fails 30 of 30 runs before the fix (needs 29)✓ pass
E-2seed passes after the fixseed fails 0 of 30 runs after the fix (needs 0)✓ pass
E-3500 chaos seeds + full suite500 chaos vectors: 0 failed (sample seed 1189583782); full suite: 16 tests, 0 failed, 1 other known ghost ignored✓ pass
E-4test not weakenedno assertion removed or loosened, no skip, retry or longer timeout in 2 changed files✓ pass
E-5only autopsy files touchedchanged src/checkout.ts, tests/timer-race.possessed.test.ts; all inside the autopsy scope✓ pass
E-6regression test pins the seedtests/timer-race.possessed.test.ts pins no seed values and runs in 16 ms✓ pass
haunted-demo/src/checkout.ts+9 −9
@@ -1,12 +1,12 @@
1export interface Result { events: string[] }
1export interface Result { events: string[] }
2
2
3// Payment capture takes a variable time (simulated gateway). The receipt
3// Payment capture takes a variable time (simulated gateway). The receipt is
4// is sent after a fixed 10ms, on the assumption that payment is done by then.
4// only sent once the capture has finished, so the order never depends on timing.
5export function checkout(): Promise<Result> {
5export async function checkout(): Promise<Result> {
6 const events: string[] = [];
6 const events: string[] = [];
7 const capture = new Promise<void>((resolve) =>
7 await new Promise<void>((resolve) =>
8 setTimeout(() => { events.push("payment-captured"); resolve(); }, 5 + Math.random() * 6));
8 setTimeout(() => { events.push("payment-captured"); resolve(); }, 5 + Math.random() * 6));
9 const receipt = new Promise<void>((resolve) =>
9 await new Promise<void>((resolve) =>
10 setTimeout(() => { events.push("receipt-sent"); resolve(); }, 10));
10 setTimeout(() => { events.push("receipt-sent"); resolve(); }, 0));
11 return Promise.all([capture, receipt]).then(() => ({ events }));
11 return { events };
12}
12}
haunted-demo/tests/timer-race.possessed.test.ts (new)+22 −0
@@ -0,0 +1,22 @@
1import { afterEach, expect, it, vi } from "vitest";
2import { checkout } from "../src/checkout.js";
3
4// Possession seed: seed 0, randomSeed 0, timerJitterMs 27.
5// Math.random is pinned to 0 (seed 0) and every timer is delayed by the 27 ms jitter.
6const JITTER_MS = 27;
7
8afterEach(() => {
9 vi.useRealTimers();
10 vi.restoreAllMocks();
11});
12
13it.each([0, 0.5, 0.999])("captures payment before sending the receipt (random %s, jitter 27ms)", async (r) => {
14 vi.useFakeTimers();
15 vi.spyOn(Math, "random").mockReturnValue(r);
16 const realSetTimeout = globalThis.setTimeout;
17 vi.spyOn(globalThis, "setTimeout").mockImplementation(((fn: () => void, ms?: number) =>
18 realSetTimeout(fn, (ms ?? 0) + JITTER_MS)) as typeof setTimeout);
19 const pending = checkout();
20 await vi.runAllTimersAsync();
21 expect((await pending).events).toEqual(["payment-captured", "receipt-sent"]);
22});

05Proof

The ghost is gone.

Failure rate in the scan before the fix, and under 500 fresh chaos seeds after it.

captures payment before sending the receipt

16.7%→0.0%

500/500 runs pass after the fixseed: 30/30 fail → 0/30

→Try it

Try it in thirty seconds.

Runs on the bundled haunted-demo: scan, possess, and a replayed exorcism. No credits, nothing leaves your machine.

$ npx -y github:let-the-dreamers-rise/poltergeist demo
recorded output50 lines
$ npx -y github:let-the-dreamers-rise/poltergeist demo --runs 30┌──────────────────────────────────────────────┐│ POLTERGEIST · scan of haunted-demo · 30 runs │└──────────────────────────────────────────────┘ TEST                                            FLICKER                           RATE  VERDICT──────────────────────────────────────────────  ──────────────────────────────  ──────  ─────────catalog prices a vip seat                       ☠☠█☠███☠☠█☠☠☠☠☠☠☠█☠☠██☠█☠██☠██   56.7%  HAUNTEDshared cart starts empty                        ██☠█☠☠☠██☠███████☠██☠☠█☠█☠☠█☠☠   43.3%  HAUNTEDoffers a valid next slot                        █☠███☠█☠█☠██☠█☠██☠☠████☠█☠████   33.3%  HAUNTEDreads seat availability                         █☠██☠██☠███☠█☠██☠████☠☠███████   26.7%  HAUNTEDcaptures payment before sending the receipt     ████████☠█☠██████☠██☠████☠████   16.7%  HAUNTEDgives 20 bookings unique ids                    █████☠████████☠███████████████    6.7%  HAUNTEDstable controls counts seats in a fresh cart    ██████████████████████████████    0.0%  STABLEstable controls formats booking ids             ██████████████████████████████    0.0%  STABLEstable controls formats slot labels             ██████████████████████████████    0.0%  STABLEstable controls rejects unknown skus with a l…  ██████████████████████████████    0.0%  STABLEstable controls totals a fresh cart             ██████████████████████████████    0.0%  STABLEcatalog loads the catalog                       ██████████████████████████████    0.0%  STABLEshared cart adds a seat                         ██████████████████████████████    0.0%  STABLE ✗ 6 haunted, 0 possessed, 7 stable of 13 tests over 30 runs $ npx -y github:let-the-dreamers-rise/poltergeist possess --file tests/timer-race.test.ts -t "captures payment before sending the receipt"┌──────────────────────────────────────────────────────────────────────────────────┐│ POLTERGEIST · possess captures payment before sending the receipt · haunted-demo │└──────────────────────────────────────────────────────────────────────────────────┘ possession seed  seed=0 randomSeed=0 timerJitterMs=27✗ POSSESSED: the seed fails 30 of 30 runs (E-1 met) $ npx -y github:let-the-dreamers-rise/poltergeist exorcise timer-race--captures-payment-before-sending-the-receipt --dry-runCHECK  RULE                            RESULT─────  ──────────────────────────────  ──────E-1    seed reproduces before the fix  ✓ pass       seed fails 30 of 30 runs before the fix (needs 29)E-2    seed passes after the fix       ✓ pass       seed fails 0 of 30 runs after the fix (needs 0)E-3    500 chaos seeds + full suite    ✓ pass       500 chaos vectors: 0 failed (sample seed 1189583782); full suite: 16 tests, 0 failed, 1 other known ghost ignoredE-4    test not weakened               ✓ pass       no assertion removed or loosened, no skip, retry or longer timeout in 2 changed filesE-5    only autopsy files touched      ✓ pass       changed src/checkout.ts, tests/timer-race.possessed.test.ts; all inside the autopsy scopeE-6    regression test pins the seed   ✓ pass       tests/timer-race.possessed.test.ts pins no seed values and runs in 16 ms seed failure rate  30/30 (100.0%) → 0/30 (0.0%) ✓ EXORCISED: all six seals hold

⌘Built with Kiro

Five Kiro features, one exorcist.

Specs

Requirements written in EARS form. Each task in tasks.md traces back to one.

Steering

Product, tech, the exorcism canon and this design system, loaded into every agent call.

Hooks

Verify on save, canon sync, a fence that denies test-weakening writes, and report regeneration.

Headless exorcist

kiro-cli chat --no-interactive in a git worktree, up to four revisions per ghost.

Power

The agent, steering, canon and fence in one install: kiro-cli powers install ./power.