2.13 Gaps, Conflicts, Expiry, Rejections and Disputes: Exit Evidence
Evidence · Step: 2.13 Gaps, Conflicts, Expiry, Rejections and Disputes
| Step | 2.13 Gaps, Conflicts, Expiry, Rejections and Disputes |
| Date | 2026-11-16 |
| Database | PostgreSQL 18.6 (postgres:18-alpine) in Docker Desktop on Windows 11; postgres:18-alpine on GitHub’s ubuntu-latest in CI |
| Decision | ADR-0019; protocol §7, §10–§14 |
| Result | Passed |
Deliverables
Section titled “Deliverables”| Deliverable | Where |
|---|---|
| Gap handling | settle_waiting and the gap timeout in offline_tick (crates/engine/src/offline_lifecycle.rs); the successor check in decide (offline_receipts.rs) |
| Conflict handling | The freeze, the evidence in offline_receipt_conflicts, the offline.freeze alert, and a dispute opened (step 2.12, extended here) |
| Expiry handling | offline_tick: expiry, grace, the return of escrow (E18), the late deadline; run by the service every minute |
| Late receipts | SETTLED_LATE by the loss policy (E28, E29), shortfalls (offline_shortfalls), the voucher block, repayment (E30) |
| The dispute workflow | offline_disputes; resolve_dispute (paid or refused); GET /v1/offline/disputes, POST …/resolve under the operator’s new offline:resolve scope |
| Templates | offline_voucher_return.v1 (E18, E25, E27), offline_receipt_pay.v1 (E26, E28, E29, E31), offline_shortfall_repay.v1 (E30) |
| Schema | Migration 0013 |
| Operator tools | mezgeb-cli offline deadline; the offline runbook: setup, the late deadline, disputes, shortfalls |
The work arrived in nine pull requests:
- the schema (#132), the templates (#133);
- the engine (#134), the API (#135), the ten scenarios (#136);
- the late-deadline setting (#137), the documentation (#138);
- the Postman collection (#139);
- this evidence (#140).
A roadmap task replaced by an earlier decision
Section titled “A roadmap task replaced by an earlier decision”The roadmap’s first draft said:
A late receipt after the return is booked to a suspense account plus a dispute case (
offline_late_receipt.v1).
ADR-0019 decided otherwise in step 2.10, with the maintainer. A valid late receipt is paid:
- by the payer’s wallet first;
- then by the loss account 5030, with the shortfall recorded against the payer, who gets no new voucher until it is repaid.
Suspense would hold the merchant’s money with no rule for releasing it.
The loss policy pays the merchant who acted in good faith, puts the cost on
the payer whose device signed, and bounds the platform’s share. This step
builds the ADR’s policy: offline_receipt_pay.v1, shortfalls and the block.
A dispute case is opened only by a conflict or an over-spend, where the
facts need a person.
Implementation tasks
Section titled “Implementation tasks”| Task | Done |
|---|---|
WAITING_FOR_GAP handling: settle a contiguous chain, hold later receipts, set the gap deadline |
Receipts settle in counter order; a later one waits. Settling one settles those waiting behind it (step 2.12). The deadline is 72 hours after the waiting receipt arrived (GAP_TIMEOUT_MICROS). At the deadline, or at the voucher’s return, the waiting receipts settle past the gap, marked gap_skipped. A missing receipt arriving later must match its successor’s prev_digest, or it is a CONFLICT |
| On a conflict: freeze the device; store both receipts; settle nothing beyond the last contiguous verified counter; raise an alert | The device and voucher freeze. The first receipt stays; the second is kept in offline_receipt_conflicts. Every later receipt is FROZEN, and the tick leaves a frozen voucher alone. An offline.freeze audit event is written, and a dispute opens |
| An expiry worker returns unspent escrow to the wallet | offline_tick, run by the service (MEZGEB_OFFLINE_TICK_SECONDS, 60 by default): ACTIVE → GRACE at expiry, then the return after 7 days (E18) → RETURNED, then ENDED after the late deadline (90 days by default, mezgeb-cli offline deadline) |
| A late receipt after the return is booked to suspense plus a dispute case | Replaced by ADR-0019’s loss policy (above). SETTLED_LATE: E28, or E29 with a shortfall. After the deadline, EXPIRED, with nothing posted |
disputes table and operator endpoints to resolve them |
offline_disputes and offline_dispute_payments; list, get, resolve. paid: every valid receipt in counter order, from the escrow left (E26), then the wallet, then 5030; a conflicting copy from the wallet and 5030 only (E31, INV-21). refused: none, marked REFUSED. Either way the escrow left returns (E27) and the device is revoked. Idempotent; the other resolution is refused |
Testing requirements
Section titled “Testing requirements”| Requirement | Test | Result |
|---|---|---|
| A clone (two receipts, one counter) → frozen device; neither conflicting receipt settles automatically | two_receipts_with_one_counter_are_a_conflict; scenario 1 |
The device freezes; the second receipt settles only when an operator resolves the dispute, and from the wallet, never escrow |
| Gap filled later → settles in order | any_upload_order_gives_the_same_final_state; scenarios 6 and 8 |
Settles in counter order, whatever the arrival order |
| A gap never filled → behaviour at the deadline matches the spec | a_gap_times_out_and_the_missing_receipt_must_match; scenario 8 |
Nothing at 71 hours; at 73, the waiting receipts settle past the gap. The missing one, arriving later, settles only if it is the one its successor named |
| Expiry racing a receipt ingestion: exactly one outcome, correct accounting | an_expiry_racing_a_receipt_has_one_outcome: an upload and a tick at the same moment, 3 times |
One outcome each time (SETTLED or SETTLED_LATE). The merchant paid once, escrow at zero, the wallet exactly 40 better off either way |
| Late receipt → suspense, and the case is opened | a_voucher_expires_returns_and_ends; scenarios 4, 9 and 10 |
By ADR-0019 instead: E28 when the wallet can pay; E29 with a shortfall when it cannot; new vouchers blocked until E30 repays it; EXPIRED after the deadline |
Also tested:
- Over-spend disputes resolved
paid(E26 in counter order) andrefused(REFUSED, escrow back):an_overspend_dispute_pays_in_order_or_refuses. - A client’s own late deadline:
a_client_sets_its_own_late_deadline. - Through the API: a clone’s dispute resolved by an operator, and a late receipt’s shortfall that blocks a voucher until repaid.
Acceptance criteria
Section titled “Acceptance criteria”INV-11 holds, and every scenario walkthrough from step 2.10 is an
automated test. crates/engine/tests/offline_scenarios.rs, one test per
scenario. Each one ends by checking:
- every escrow against its unspent voucher value;
- the hash chains (
verify_chains: INV-11); - the balances, as every database test does.
| # | Scenario | Outcomes | Accounting checked |
|---|---|---|---|
| 1 | Clone | SETTLED, CONFLICT, FROZEN |
E17; the dispute pays Stall from the wallet (E31) and the frozen receipt from escrow; the rest back (E27); 5030 untouched |
| 2 | Replay | SETTLED, 3 × DUPLICATE |
One E17: Coffee 29.40, fee 0.60 |
| 3 | Forged amount | BAD_SIGNATURE, then SETTLED |
Nothing for the forgery; 19.60 for the original |
| 4 | Clock rollback | SETTLED in grace; SETTLED_LATE after the return |
E17 or E28; the device’s day-2 time recorded |
| 5 | Payee double-upload | 2 × SETTLED, 4 × DUPLICATE |
Two E17 |
| 6 | Payer never syncs | WAITING_FOR_GAP, then SETTLED × 2 |
E17 × 2, E18 of 300; escrow 0 |
| 7 | Lost device | SETTLED |
The finder’s E17, E18 of the 40 left; no new voucher for a lost device |
| 8 | Partial sync | SETTLED, WAITING_FOR_GAP; past the gap at 72 h |
Six E17; escrow 10 left |
| 9 | Voucher expiry race | SETTLED_LATE; EXPIRED after the deadline |
E29: 25 from the wallet, 65 from 5030; a shortfall of 65; vouchers blocked |
| 10 | Colluding merchant | SETTLED_LATE, DUPLICATE, BAD_SIGNATURE |
E29: 100 from 5030; a shortfall of 100; vouchers blocked |
Exit gate
Section titled “Exit gate”All 10 adversarial scenarios pass as integration tests. On every PR in CI, and locally:
test scenario_1_clone ... oktest scenario_2_replay ... oktest scenario_3_forged_amount ... oktest scenario_4_clock_rollback ... oktest scenario_5_payee_double_upload ... oktest scenario_6_payer_never_syncs ... oktest scenario_7_lost_device ... oktest scenario_8_partial_sync ... oktest scenario_9_voucher_expiry_race ... oktest scenario_10_colluding_merchant ... oktest result: ok. 10 passed; 0 failedOne difference from the walkthroughs: every payer here is a tier A card, so a voucher expires after 7 days where the walkthroughs’ phone expires after 3. The dates shift; the outcomes and accounting do not.
Found and fixed during the step
Section titled “Found and fixed during the step”- A conflicting copy must not be paid from escrow. The first version
of
resolve_disputepaid every receipt from escrow first. E31 and scenario 1 say escrow pays one counter once (INV-21), so a clone’s second receipt is paid from the wallet and 5030 only. Fixed before the engine PR, and asserted ina_disputed_clone_is_paid_from_the_wallet. - A missing loss account. In the API’s contract test, a late receipt whose payer could not pay failed with a 500, because the test world had not opened 5030. That is correct (the engine never books into a missing account), but it is now in the runbook: open 4010 and 5030 before turning offline payments on.
- A required check that never ran. GitHub did not run the
conventional-titlecheck on #132 and #133. Editing the titles re-triggered it.
The whole suite. On the combined branch before the engine PR, cargo test --workspace against PostgreSQL ran 46 test binaries and 358 tests,
with 0 failures. On the final main (after #139): 46 binaries, 359 tests,
0 failures.
Not done in this step
Section titled “Not done in this step”- The early close (protocol §7.1, E25): the template exists (
closedreason) but the signed close message and its endpoint do not; a voucher returns only at expiry or after a dispute. - Payee acknowledgements (§8): optional dispute evidence, not built.
- Alerts beyond the audit log: a freeze and a resolution are audit events; paging someone is step 2.15 and phase 3 monitoring.
- Late-upload monitoring of merchants (scenario 10): step 2.15.
- The external security review and card attestation: still open, as recorded for step 2.11.