Skip to content

1.07 In-Memory Reference Ledger (the Oracle): Exit Evidence

Evidence · Step: 1.07 In-Memory Reference Ledger (the Oracle)

Step 1.07 In-Memory Reference Ledger (the Oracle)
Date 2026-11-11
Result Passed
Deliverable File Contents
MemLedger crates/core/src/model/ledger.rs Open accounts, change status, post entries, reverse entries, read accounts, entries and per-account history. Single-threaded, plain collections, no optimisation. Every line stores the account’s totals after it (ADR-0012). A refused operation changes nothing.
Invariant checker crates/core/src/model/invariants.rs check_invariants(&MemLedger): INV-1, INV-2, INV-3, INV-5, INV-6, INV-7, INV-8, INV-12, INV-14, INV-24 (accounts exist), and “a closed account has a zero balance”
State-machine tests crates/core/src/model/tests.rs Random operation sequences with invariants checked after every step, the planted-bug test, and unit tests for every operation and every invariant check

A change to step 1.06 made along the way: the non-negative balance rule (INV-8) needs each account’s current totals, which validate already receives. It was added to validate as two new rules with new codes, 1011 (account total overflow) and 1012 (insufficient funds), so the oracle and the future PostgreSQL engine share one implementation of the rule. Existing codes are unchanged.

The worked story now replays through the oracle. Instead of validating each worked entry against empty accounts, crates/core/tests/worked_entries.rs posts OB and E01–E18 in order into a MemLedger. After every entry the invariants hold, and at the end every account’s balance equals the trial balance, row by row. Posting E02 against an empty wallet is refused with 1012.

Each test case is a sequence of operations on six chart accounts plus two more wallets: open an account, transfer, split payment (one debit, two credits), arbitrary lines (usually refused), reverse a random earlier entry, and change an account’s status. After every operation:

  1. if the operation was refused, the ledger must be exactly as before;
  2. check_invariants must pass.
Requirement Result
10,000 random sequences of 200 operations; invariants hold after every step Pass: PROPTEST_CASES=10000 cargo test -p mezgeb-core --release --lib invariants_hold_after_every_operation, 2,000,000 operations, 42 s
Shrinking produces minimal failing cases (verified by planting a bug) Pass, see below

Each check in check_invariants also has a unit test that corrupts a ledger in that one way and expects that invariant to be reported.

A planted bug is caught within 1,000 cases. The planted bug: the last line of every entry does not update its account’s totals. It exists only in test builds (#[cfg(test)]).

planted bug caught on case Some(1); shrunk to 3 operations:
[Open(3), Open(6), Transfer { debit: 3, credit: 3, amount: 1 }]
reason: step 2, after Transfer { debit: 3, credit: 3, amount: 1 }:
INV-6 violated: entry 00000000-0000-7000-8000-000000000003:
stored totals (1, 0) after a line, replay gives (1, 1)

Caught on the first case. Proptest shrank a sequence of up to 200 operations to 3. The true minimum is 2 (Open(6) is not needed).

Correction (after merge): the test first required “at most 3 operations”, but proptest picks a new random seed on every run, and on CI it shrank to a different case: open three accounts and post one split payment, 4 operations. That case is minimal too (a split needs three accounts), so the rule was wrong, and the test was flaky. It now:

  • uses a fixed seed, so every machine gets the same result: caught on the first case and shrunk to 2 operations, [Open(2), Transfer { debit: 2, credit: 2, amount: 1 }], the true minimum;
  • checks minimality directly: removing any one operation from the shrunk case must make the bug disappear. The 4-operation case from CI passes this check as well.

Pass.

Gate: the oracle and invariant checker are reviewed, and the planted-bug test is recorded.

  • Recorded: above, and the test runs on every cargo test.
  • Reviewed: the code is in this step’s pull request for review. The checker is independent of the ledger’s own bookkeeping: it recomputes every total from the stored lines alone.

Result: passed.

86 tests in mezgeb-core, all passing; fmt and clippy clean. The only code in model/ and validate.rs not reached by tests is defensive error handling that validate makes unreachable (for example, an account that disappears between validation and posting).