test(core): the Rust ledger and the Lean model agree on random command sequences

docs/verification.md proves the accounting laws of a model of the
ledger (bookspec's Bookspec.Step, run by Bookspec.Exec.step); that the
model is the engine was checked by review. This makes it a machine check.
tests/bookspec/lean_model.rs generates random command sequences (entries
balanced or not, with zero, negative, finer-than-a-cent and unknown-account
lines; invoices; reversals of anything posted, twice, of a closing entry,
backdated; closes to retained earnings over nested, disjoint, partly
overlapping, inverted, empty and out-of-order periods), runs each through
the Rust Ledger, writes it as a bookspec example whose expected values are
the engine's own answers, and has `lake exe bookspec-check` (the pinned
bookspec, built in a copy under target/) check every refusal, the trial
balance after each event, and at the end the trial balance, balances, P&L
and each posted entry's lines. All cases of a run go to one directory and
one lake call; a failure is shrunk (rounds of single-event removal, one
lake call each) and printed with both answers. `just lean-model [cases]`
runs it with WANDERBOOKS_REQUIRE_LEAN=1; without lake it skips unless that
is set.

Refusal words the checker does not list are compared through a probe (the
model's own word is read from its mismatch), so every refusal is checked,
not skipped; the one word the model does not have (the control guard) is
an explicit allow-listed skip. The D-73 gap (a backdated plain reversal the
engine still accepts and the model refuses) is rolled back and counted.

Findings, all of word only (both sides refuse, the books agree): the order
of checks when a command fails two. Allow-listed in KNOWN_WORD_DIFFERENCES,
pinned by a test that fails when one side changes, and by #[ignore]d d_*
tests that assert agreement: reversing a closing entry or an already
reversed entry on a closed day, an unknown account beside another fault, a
zero line beside a negative one, a close inside a closed period (the
model's own word closed_period_contains_end), a finer amount on a closed
day (the example reader's order). Nothing in the engine or the model is
changed. docs/verification.md's "Be precise about the claims" says what is
now checked and what stays by review.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-08 15:10:22 -07:00
1 parent 8874b603b4
commit 8775d38d0d
4 files changed
+1896 -3

No files matched your search

+24 -2
View File
@@ -812,9 +812,31 @@ Be precise about the claims:
ten minutes.
- **The state machine is a model of `Ledger`'s checks**, not a proof
about `ledger.rs` line by line. Its correspondence to the code is
checked by review and by the end-to-end property tests in
checked by a differential test and, for what the test cannot say, by
review. `tests/bookspec/lean_model.rs` (`just lean-model`, P14 L-G5)
generates random command sequences (entries, balanced or not, with
zero, negative, finer-than-a-cent and unknown-account lines; invoices;
reversals of anything posted, twice, of a closing entry, backdated;
closes to retained earnings over nested, disjoint, partly overlapping,
inverted, empty and out-of-order periods), runs each through the Rust
`Ledger`, writes it as a bookspec example whose expected values are the
engine's answers, and has the Lean model (`bookspec-check`, built from
the pinned bookspec, running `Bookspec.Exec.step`) check the same
sequence: every refusal, the trial balance after each event, and at the
end the trial balance, balances, profit and loss and each posted entry's
lines. Any mismatch fails the test with the shrunk sequence and both
answers. What it compares is bounded by what an example can say: a
standalone `openPeriod`, an unposted draft (`drafts_in_period`,
`discard`), `addAccount` after the first event, and a reversal of an
event that posted nothing are not reachable, and stay by review; so do
the model's records, and the checks it does not carry (the control
guard's refusal is skipped, by an allow-list). Where the engine and the
model refuse in different words (a command failing two checks: the
order of checks is unspecified), the difference is allow-listed and
pinned (`KNOWN_WORD_DIFFERENCES`, the `#[ignore]`d `d_*` tests) until the
order is decided. The older end-to-end property tests in
`tests/properties.rs` (trial balance, balance sheet, reversals,
immutability) and the golden files.
immutability) and the golden files remain.
- **Out of scope:** currency (the ledger is single-currency, enforced
structurally), payroll, the calendar walk around
the proration kernel (`calendar::month_parts`, `assets::depreciation_schedule`,