1.11 Idempotency and Exactly-Once Effect: Exit Evidence
Evidence · Step: 1.11 Idempotency and Exactly-Once Effect
| Step | 1.11 Idempotency and Exactly-Once Effect |
| Date | 2026-11-12 |
| Database | PostgreSQL 18.6 (postgres:18-alpine) |
| Result | Passed |
Deliverables
Section titled “Deliverables”| Deliverable | File |
|---|---|
| Idempotency records | migrations/0002_idempotency_records.{up,down}.sql |
| Request fingerprint | crates/core/src/fingerprint.rs (EntryDraft::fingerprint, reversal_fingerprint) |
| Claiming keys, returning the original, lookup | crates/engine/src/post.rs (claim_key, load_entry, lookup_by_idempotency_key) |
| New error | LedgerError::IdempotencyKeyReused |
| Failure-injection hook | PostOptions::lose_response_after_commit, only with the engine’s test-hooks feature (enabled for its own tests, never in a normal build) |
| Tests | crates/engine/tests/idempotency.rs |
| Crash demonstration | crates/engine/examples/lost_response.rs |
| Design and retention | db-schema.md, ADR-0008 follow-up |
Implementation tasks
Section titled “Implementation tasks”| Task | Done |
|---|---|
| Idempotency record in the same transaction as the entry (client, key, request hash, entry, response, created_at) | Yes. The response is the entry itself, rebuilt on a repeat: entries are immutable (INV-9), so it is always identical and no copy can drift. |
| Canonical request serialisation for the hash | Yes: a versioned, length-prefixed encoding of every field the client controls; SHA-256. The entry ID is excluded, because a retry may carry a new one. |
| Concurrent same-key requests: the second waits on the unique index, then returns the stored result | Yes: the key is claimed first with INSERT … ON CONFLICT DO NOTHING, before any account is locked |
Same key, different hash: IDEMPOTENCY_KEY_REUSED |
Yes: LedgerError::IdempotencyKeyReused, and nothing is written |
lookup_by_idempotency_key |
Yes |
| Retention policy, documented | Records are kept as long as their entry (forever); see db-schema.md |
A refused posting rolls back its claim, so it does not use up its key: once the wallet is funded, the same request succeeds. Reversals are idempotent the same way.
Testing requirements
Section titled “Testing requirements”| Requirement | Test | Result |
|---|---|---|
| The same request 10 times sequentially gives 1 entry | ten_sequential_repeats_post_once |
Pass: 1 entry, 10 identical responses |
| The same request 50 times concurrently gives 1 entry and 50 identical responses | fifty_concurrent_repeats_post_once |
Pass |
| The same key with a different amount gives an error and no entry | a_reused_key_with_a_different_request_is_refused |
Pass |
| Abort after commit, before the response; retry returns the original, no new entry | a_retry_after_a_lost_response_returns_the_original |
Pass: the connection the response was lost on is closed, the retry on a new connection returns the original entry, entries added: 1 |
Also: keys are scoped to their client; reversals are idempotent; a refused
posting does not use up its key; records are append-only (42501 for the
service, MZ001 even for the owner). Each test sends every attempt with a
new entry ID, as a real retry would. The fingerprint has unit tests showing
that every field changes it and that metadata boundaries are unambiguous.
Acceptance criteria
Section titled “Acceptance criteria”None of the tests can produce a duplicate entry. Every test counts the entries before and after; every repeat, concurrent or not, added exactly one. Pass.
Exit gate
Section titled “Exit gate”Gate: a crash-after-commit retry is demonstrated in a recorded test run.
Demonstrated twice: with the test hook above, and with a real process
crash. lost_response post-and-hang posts a deposit, commits, and then
waits forever before answering; it is killed with SIGKILL. Two new
processes then retry the same request with the same key:
--- process 1: post-and-hang (SIGKILL after 5 s) ---Killed timeout -s KILL 5 $L post-and-hang "$URL" "$KEY"exit status: 137--- process 2: retry with the same key ---entries with key "demo-1794485840" before the retry: 1this attempt's new entry ID: 00000000-0000-7000-8000-60134b754a02entry returned by the retry: 00000000-0000-7000-8000-60134b26aeb2entries with key "demo-1794485840" after the retry: 1--- process 3: retry again ---entries with key "demo-1794485840" before the retry: 1this attempt's new entry ID: 00000000-0000-7000-8000-60134b77cc8eentry returned by the retry: 00000000-0000-7000-8000-60134b26aeb2entries with key "demo-1794485840" after the retry: 1The first process committed and died without answering. Each retry came with a new entry ID, got back the original entry, and posted nothing. Result: passed.