Skip to content

2.14 Offline Deterministic Simulation: Exit Evidence

Evidence · Step: 2.14 Offline Deterministic Simulation

Step 2.14 Offline Deterministic Simulation
Date 2026-11-17
Database PostgreSQL 18.6 (postgres:18-alpine) in Docker Desktop on Windows 11; postgres:18-alpine on GitHub’s ubuntu-latest (Ubuntu 26.04) in CI
Decision ADR-0019; protocol §7, §11, §14
Result Built; acceptance pending. Every planted bug is caught and a failing seed reproduces on another machine. The 14 consecutive clean nights start on 2026-11-17
Deliverable Where
Simulator v2 mezgeb-sim --workload offline (crates/sim/src/offline/): the fleet (fleet.rs), the reference model (model.rs), the checker (check.rs), the run against PostgreSQL (pg.rs)
The nightly job .github/workflows/nightly.yml: 10 million operations against the model, 200,000 against PostgreSQL with crashes, the online workload, and the planted bugs
On every change ci.yml: 20,000 offline operations against the model and 2,000 against PostgreSQL with crashes; the unit tests pin the cross-machine digest and catch the planted bugs

The work arrived in nine pull requests:

  • three engine fixes the simulator found (#141, #142, #143);
  • the simulator (#144), its CI and nightly job (#146);
  • the OpenAPI document and Postman for the voucher’s last_counter (#145);
  • the documentation (#147);
  • a fix to the simulator’s PostgreSQL harness found by the first nightly run (#148);
  • this evidence (#149).

The point of the step is to find what tens of hand-written cases miss. In its first million operations the simulator found five problems, each now fixed and covered by an engine test:

# Problem Found by Fixed in
1 The gap timeout was measured from received_at, the database’s clock, while every other deadline uses ledger time. A simulated run could not time a gap out deterministically Building the simulator: every clock must be the simulated one #141
2 A device’s second voucher restarted at counter 1. devices.next_counter was never advanced, so the second voucher’s first receipt collided with the first voucher’s counter 1: CONFLICT, and an honest device frozen. No test spent on a second voucher Reading the engine for the model, before the first run. The maintainer chose the fix: the device reports its last counter (protocol §7) #142
3 A late receipt of an old voucher was judged a CONFLICT against the next voucher’s first receipt: “the successor’s link” was looked up by counter alone The test written for #2 #142
4 A dispute reopened an ended voucher. A clone’s copy arriving after the end opened a dispute; resolving it set the voucher back to RETURNED, with a new return time, so the late deadline restarted and late receipts were paid again The checker (voucher states only move forward), step 3,192 of the first run #143
5 Receipts stayed FROZEN forever. A conflict on a device’s new voucher froze the device; a late receipt of its old voucher was then kept FROZEN, and the dispute, about the new voucher, never decided it. Its payee was neither paid nor refused The end-of-epoch check (nothing left undecided), step 5,979 #143

A refinement of #4 came from the next run: skipping every payment once a voucher had ended stranded receipts that had arrived in time while the operator was slow. The rule is now by arrival: what reached the ledger before the voucher ended is paid, what reached it after is EXPIRED.

Task Done
Thousands of simulated devices with tiers 256 devices per lane, 16 lanes: 4,096 devices in each nightly run; tiers A, B and C, 40/40/20. Some lanes halve a tier’s limit or disable tier C, as a client may. Each lane is its own client with its own customers (two devices each) and merchants
Random payments Real receipts, signed with P-256 by each device’s key, to merchants (70%) or other customers (30%)
Random sync timing and order Each receipt is handed to its payer and its payee; either uploads it, usually soon, sometimes long after, in any order, and keeps a copy to upload again 20% of the time. Replays of old receipts
Duplicate and lost receipts Duplicates as above; lost receipts dropped from a holder; at most 4,000 held per lane, older ones lost
Cloned devices A device with something left on its voucher is copied; both copies spend. Also phones rolled back to a backup, and buggy phones that sign more than their voucher holds
Clock skew Each device’s clock is off: 70% by up to 5 minutes, 20% by up to a day, 10% by up to a year. The ledger never decides by device time; skew shows in each entry’s event_time, kept only between the voucher’s issue and now
Voucher expiry Time passes in steps of minutes to days: vouchers expire, return, end; late receipts settle late or expire. Lanes use the default 90-day late deadline or 1 to 30 days
Ledger crashes Against PostgreSQL, --faults per thousand attempts crash at one of the engine’s four fault points: before the lock, after the lines, before the commit (rolled back), or after it (the response lost). Each is retried as a client would, with a fresh entry ID and the same idempotency key
Runs against the in-memory model and against PostgreSQL (slower; nightly) run_reference (offline/mod.rs) and run_database (offline/pg.rs). Against PostgreSQL every answer must equal the model’s; each lane has its own database, because the offline tick moves every client’s vouchers
Prints the seed and step on failure FAIL (seed S): lane L epoch E step N: …, then replay: cargo run --release -p mezgeb-sim -- … with every option
Nightly CI job nightly.yml, 01:00 UTC; the seed is the date

Also beyond the roadmap:

  • Forgeries: tampered amounts, an attacker’s key in a real device’s name, a short signature, an unregistered device, another device’s voucher, a counter before the voucher’s first, a payee that is not one, and random bytes.
  • The rest of the life of a voucher: shortfalls and their repayment, lost devices, and disputes resolved paid or refused.
  • Epochs: each lane runs as epochs of --epoch operations, each a fresh client, so the reference ledger stays small. This resolves the 1.16 finding that the reference ledgers grew with the run, for the offline workload. The online workload still runs as one epoch per lane, as in 1.16.
Requirement Result
10M simulated operations per nightly run The nightly offline-model job: 10,000,000 operations, 4,096 devices. First run (by hand, run 44054696436, seed 20261117): passed, in 14 minutes on a GitHub runner
Invariants INV-1 to INV-12 after every step See below
Planted bugs (skip the counter check, skip the chain check) caught within one nightly run Caught in the first 2,000 operations of lane 0 (seed 1, 8 lanes of 64 devices): skip-counter-check at step 1,953 by INV-20; skip-chain-check at step 1,355 by the chain check. Nightly, both must fail twice with the same failure line

The invariants after every step. What is checked when, and why:

  • After every operation, the checker (check.rs) audits what changed in the model:
    • INV-19 to INV-23;
    • the chain links;
    • voucher states, which only move forward;
    • the loss account against the unpaid shortfalls.
  • Every 1,000 operations and at each epoch’s end, the reference ledger’s own checker runs: INV-1 to INV-8 and INV-13 to INV-25, in memory. It replays the whole ledger, so running it after every step would make a run quadratic.
  • INV-9 to INV-12 are properties of PostgreSQL’s storage: append-only journals, closed periods, the hash chain and the commit order. They are checked by a full verification and verify_chains at the end of every run against PostgreSQL.
  • Against PostgreSQL, every answer must equal the model’s at every step:
    • every account and device is compared every 5,000 operations;
    • every voucher, receipt, shortfall and the entry count at each epoch’s end.
  • At each epoch’s end, after every receipt is uploaded, every dispute resolved and every deadline passed, nothing may be left undecided.
Criterion Status
14 consecutive clean nightly runs Pending, counted from the first scheduled night, 2026-11-18. The first run, by hand on 2026-11-17 (run 44054696436, seed 20261117): offline-model and planted-bugs passed; offline-postgres failed at lane 0, step 1,905, on a bug in the harness, not the engine (below), fixed in #148. The second run, by hand after the fix (run 44064608511, the same seed): every job passed: offline-model in 14 minutes, offline-postgres (200,000 operations, 10% of attempts crashed, and the online workload) in 6, planted-bugs in 2
Every planted bug caught Met: both, in CI on every change and in the nightly job

Exit gate: a failing seed reproduces identically on another machine

Section titled “Exit gate: a failing seed reproduces identically on another machine”

Met:

  • Every run prints a digest: the SHA-256 of every operation and every answer, combined in lane order.
  • Pinned across machines: offline::tests::the_digest_is_the_same_on_every_machine pins the digest of a small run, computed on Windows 11 (x86-64). CI on Ubuntu 26.04 reproduced it on #144 and does again on every change.
  • The same digest against PostgreSQL: a run against PostgreSQL with 154 crashes injected gave the same digest as the same run against the model alone (seed 3, 3,000 operations). The faults have their own random stream.
  • The same failure twice: the nightly planted-bugs job fails each planted bug twice and requires the same failure line both times.
FAIL (seed 20261117): lane 0 epoch 0 step 1905: resolve: PostgreSQL answered Done(Err("DisputeNotFound")), the model Done(Ok(()))
  • What happened: a crash after the commit of a dispute’s resolution lost the response. The harness’s retry looked for the dispute among the open ones, and it was already resolved.
  • The fix: the harness now finds the dispute once and resolves that same one on every retry, as a client would.
  • Replayed with the fix: the same lane ran all 25,000 operations against PostgreSQL (683 attempts crashed, 197 after their commit): PASS.
Run Result
1,000,000 operations, 8 lanes of 64 devices, seed 20261117 (Windows 11, 8 cores) PASS in 104 s (11,443 steps a second; 1,195,026 steps with setups and drains; 56 epochs, 3,584 devices). Outcomes: 46,496 SETTLED, 46,806 SETTLED_LATE, 102,888 WAITING_FOR_GAP, 287,845 DUPLICATE, 9,814 CONFLICT, 60 OVER_LIMIT, 375 FROZEN, 47,526 EXPIRED; refused: 11,417 bad signatures, 7,571 bad formats, 7,520 unknown vouchers, 3,625 unknown devices; 23,006 vouchers issued. Digest b6ffb220…f8bf30b
P-256 on this machine 0.51 ms a signature, 0.47 ms a verification: the simulator’s floor. The model skips verifying a signature it has verified before (half of all uploads are duplicates); the engine never does
Against PostgreSQL with 10–20% of attempts crashed Every answer equal to the model’s; escrows, full verification and hash chains clean
  • The early close and payee acknowledgements (protocol §7.1, §8): not built yet, so not simulated.
  • Crashes against the model alone: a crash means something only for PostgreSQL. In the model, an operation either happens or it does not.
  • Concurrency inside one client: a lane’s operations run one at a time, so each has exactly one right answer. Races between a receipt and the expiry were tested in step 2.13 (an_expiry_racing_a_receipt_has_one_outcome).
  • Fleet size: thousands of devices, not millions. A lane is one client of 256 devices, and lanes do not share customers.