legendkuatia-storage, kuatia-storage-sql,
kuatia (ledger, saga)ADR-0003 made storage a set of simple single-update primitives. It moved
crash-safety into a write-ahead record with tracked phases, which is
PendingSaga or PendingTransition, plus a roll-forward recovery in
Ledger::recover.
A commit was a sequence of primitives,
reserve → deactivate → insert → store_transfer → append_event, and they shared
no transaction. So the write-ahead log was the only protection against a
half-applied commit.
That design also left the overdraft floor and the freeze and close guards best-effort under concurrency. It re-checked them just before the writes, and not atomically with them.
Two things brought that design back up for review.
First, the legend saga machine had already shrunk to two thin adapters that
only a test used. The path for one commit was a linear method and not a saga.
Second, the whole write-ahead machinery existed because the store could not commit a transfer atomically. If it can, then the two-step reservation, the write-ahead record and the recovery are all unnecessary.
So must the store apply a whole transfer atomically, and reverse the per-primitive model of ADR-0003?
Pros:
Cons:
Add one CommitStore sub-trait, with commit_envelope and commit_transition.
Each one applies in one store transaction. On SQL that is one BEGIN..COMMIT.
In memory it holds every relevant lock across the apply.
The pure core still validates first. The transaction then re-checks the three stateful guards inside itself:
Pros:
PendingSaga,
PendingTransition, Ledger::recover and the SagaStore keyspace. A crash
cannot half-apply a commit, so there is nothing to recover.legend and the count-contract helpers go. A composition of
several transfers is then a plain runner, run_movements, over commit and
reverse, which unwinds in last-in-first-out order.Cons:
pool.begin() on SQLite is a deferred transaction. The
delete-affected-count stays the backstop against a double-spend, which is the
same property the earlier design depended on.Chosen: Option 2. The store owns the atomic commit boundary. The reservation
index, the write-ahead recovery, SagaStore and legend all go.
This supersedes ADR-0003. It refines ADR-0002, because there is no saga pipeline
for one commit any more. The atomic commit_transition takes over the
transition recovery of ADR-0020. The commit-safety map of ADR-0021 moves the
double-spend guard, the floor and the freeze and close guards into
commit_envelope.
reserve_postings and
release_postings primitives. They stay as code that the conformance suite
tests, and a later change can remove them.The removal of the saga is not a verdict against the pattern. A saga earns its keep when a workflow spans separate pieces that cannot share one transaction. Several services are such pieces, and so are several databases, and so is a sharded store where one commit cannot reach every partition.
There a saga sequences the local steps, and on a failure it compensates the steps that finished. It buys atomicity that the resources underneath cannot give on their own. It pays for that with a write-ahead record, tracked phases and compensation logic.
Kuatia was not that case. Every primitive that the commit saga drove lived in
one store, behind one transactional boundary, and those primitives were
reserve, deactivate, insert, store_transfer and append_event.
So the saga coordinated pieces that were coupled already. It re-implemented in application code the atomicity that the database gives for free. Where one ACID transaction is available, that transaction is the simpler and the stricter primitive, and a saga around it is ceremony.
The escape route stays open, and it belongs one layer down. A storage backend
can one day shard its data, so that the postings of a transfer span partitions
that no single transaction covers. That backend then implements
CommitStore::commit_envelope with a saga inside itself. A two-phase commit is
one way, and a local transaction per shard plus compensation is another.
The CommitStore contract says to apply the whole envelope atomically or to
reject it. It says nothing about how a backend reaches that atomicity.
So the saga then lives where those separate pieces are. The ledger core knows
nothing of it, and still calls commit_envelope and maps the outcome. The
pattern moves to the layer that has the distribution problem, and it leaves the
core, which does not have one.