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:
1 parent
8874b603b4
commit
8775d38d0d
4 files changed
+1896
-3
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -36,7 +36,9 @@
|
|||||||
//! `events` (applying an event), `queries/` (each query family's answers
|
//! `events` (applying an event), `queries/` (each query family's answers
|
||||||
//! and checks), `engine` (the `Engine` impl), `check` and `run` (the
|
//! and checks), `engine` (the `Engine` impl), `check` and `run` (the
|
||||||
//! per-expectation and per-example drivers), and the tests by area
|
//! per-expectation and per-example drivers), and the tests by area
|
||||||
//! (`examples`, `loader_tests`, `sources`, `rules_md`).
|
//! (`examples`, `loader_tests`, `sources`, `rules_md`). `lean_model` is
|
||||||
|
//! apart: random command sequences, checked against the Lean model of the
|
||||||
|
//! ledger (`just lean-model`).
|
||||||
|
|
||||||
pub(crate) use std::borrow::Borrow;
|
pub(crate) use std::borrow::Borrow;
|
||||||
pub(crate) use std::collections::{BTreeMap, BTreeSet};
|
pub(crate) use std::collections::{BTreeMap, BTreeSet};
|
||||||
@@ -85,6 +87,7 @@ mod common;
|
|||||||
mod engine;
|
mod engine;
|
||||||
mod events;
|
mod events;
|
||||||
mod examples;
|
mod examples;
|
||||||
|
mod lean_model;
|
||||||
mod loader_tests;
|
mod loader_tests;
|
||||||
mod pin;
|
mod pin;
|
||||||
mod queries;
|
mod queries;
|
||||||
|
|||||||
+24
-2
@@ -812,9 +812,31 @@ Be precise about the claims:
|
|||||||
ten minutes.
|
ten minutes.
|
||||||
- **The state machine is a model of `Ledger`'s checks**, not a proof
|
- **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
|
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,
|
`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
|
- **Out of scope:** currency (the ledger is single-currency, enforced
|
||||||
structurally), payroll, the calendar walk around
|
structurally), payroll, the calendar walk around
|
||||||
the proration kernel (`calendar::month_parts`, `assets::depreciation_schedule`,
|
the proration kernel (`calendar::month_parts`, `assets::depreciation_schedule`,
|
||||||
|
|||||||
@@ -200,6 +200,15 @@ oracle:
|
|||||||
docker build -q -t bookspec-gnucash-oracle "$root/gnucash/oracle"
|
docker build -q -t bookspec-gnucash-oracle "$root/gnucash/oracle"
|
||||||
cargo test -p ledger-core --test oracle_gnucash -- --ignored --nocapture
|
cargo test -p ledger-core --test oracle_gnucash -- --ignored --nocapture
|
||||||
|
|
||||||
|
# The Rust ledger against the Lean model of it, on random command sequences
|
||||||
|
# Each sequence is run through the engine, written as a bookspec example whose
|
||||||
|
# expected values are the engine's answers, and checked by the Lean model
|
||||||
|
# (`lake exe bookspec-check`, built from the pinned bookspec): a mismatch is the
|
||||||
|
# engine and the model disagreeing. `cases` sequences, one `lake` call for all of
|
||||||
|
# them (crates/ledger-core/tests/bookspec/lean_model.rs); fails without lake.
|
||||||
|
lean-model cases="64":
|
||||||
|
WANDERBOOKS_REQUIRE_LEAN=1 PROPTEST_CASES={{cases}} cargo test -p ledger-core --test bookspec lean_model -- --nocapture
|
||||||
|
|
||||||
# Fuzz one target for some seconds (nightly; corpus in fuzz/corpus/<target>)
|
# Fuzz one target for some seconds (nightly; corpus in fuzz/corpus/<target>)
|
||||||
fuzz target="fuzz_op_sequence" seconds="300":
|
fuzz target="fuzz_op_sequence" seconds="300":
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|||||||
Reference in new issue
Block a user