0021-commit-safety-map.md 4.4 KB

The commit-safety invariant and where each part is enforced

  • Status: accepted (amended by ADR-0023: the double-spend, floor, and freeze/close guards now live in the store's atomic commit_envelope, not the saga's count-contract)
  • Authors: Cesar Rodas
  • Date: 2026-07-28
  • Targeted modules: kuatia-core (validate), kuatia (ledger::commit, saga)
  • Associated tickets/PRs: N/A

Context and Problem Statement

"What makes a commit safe" had no single place to audit. The invariant was split across three modules, and no anchor held them together:

  • value / conservation / floor / freeze / snapshot / book-policy checks in validate_and_plan (kuatia-core),
  • the double-spend / reservation-ownership check inlined inside the ~110-line finalize_envelope (kuatia/ledger/commit.rs),
  • the affected-row count contract in apply_and_verify / verify_postings (kuatia/saga.rs).

A reader who audits validate_and_plan on its own could conclude that it prevents a double-spend. That conclusion is reasonable and wrong. Its lifecycle check even carried a comment that said the real enforcement happens "elsewhere", and the comment named no destination.

The double-spend guard that carries the load had no name. Only an end-to-end commit reached it, so its unit-test surface covered the easy pure half alone.

Can one pure function hold the whole decision, as in "given this envelope and this current state, can it commit"? No, because a snapshot cannot decide the double-spend property. In a concurrent ledger there is one way to know that a posting can be spent. Try to spend it atomically, then read the result.

The CAS is the decision (ADR-0003, simple storage). It must not move next to validate_and_plan, which is pure, sync and IO-free by contract.

Decision Drivers

  • One audit surface for the ledger's core safety property. It must not merge the pure value checks and the stateful CAS into one function.
  • Keep the boundary between pure and async (ADR-0002, ADR-0003). Validation stays pure and IO-free, and the authority over concurrency stays in the saga.
  • Name the double-spend guard and unit-test it. Do not leave it inline, where only an end-to-end test reaches it.

Decision Outcome

Keep the two halves in their correct layers, but make the map explicit and give the runtime guard a name. Commit safety is the conjunction of three checks, each with a single home:

Invariant Home Kind
Value / conservation / floor / freeze / close / snapshot / book policy validate_and_plan (kuatia-core::validate) Pure, snapshot-in-time, best-effort under concurrency
Double-spend / reservation ownership consume_reserved (kuatia::saga), called by finalize_envelope Runtime CAS, authoritative under contention
Affected-row count contract after each dumb write apply_and_verify / verify_postings (kuatia::saga) Interpretation of storage counts

Concretely:

  • The double-spend guard moved out of finalize_envelope and into consume_reserved, beside the count-contract helpers it depends on. It consumes only the rows that this saga reserved, through deactivate_postings(_, Some(rid)). It then asserts that every consumed id is Spent. That assertion passes only when no id stayed active and no other saga held one. Three direct unit tests cover it. It spends our own reservation. It refuses a posting with no reservation. It refuses a posting that another saga holds.
  • validate_and_plan's lifecycle comment now points at consume_reserved and this ADR instead of a vague "elsewhere".

Positive Consequences

  • One documented map of the commit-safety invariant; each part is a named, independently testable seam.
  • The authoritative double-spend guard is unit-testable without an end-to-end commit.

Negative Consequences

  • The invariant still sits physically in two crates, kuatia-core and kuatia. That split is deliberate, because the pure value checks and the runtime CAS belong in different layers. This record is the anchor that makes the split navigable. It is not one code seam.

Links

  • Builds on ADR-0003, which covers simple storage and the note on the best-effort floor and freeze, and on ADR-0002.
  • Relates to ADR-0006, which gives the reservation protocol and the posting lifecycle.