commit_envelope, not the
saga's count-contract)kuatia-core (validate), kuatia (ledger::commit,
saga)"What makes a commit safe" had no single place to audit. The invariant was split across three modules, and no anchor held them together:
validate_and_plan (kuatia-core),finalize_envelope (kuatia/ledger/commit.rs),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.
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:
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".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.