e431791bbcc6be131a89e0f525d84022ceefab41
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
e431791bbc |
build(lean): verify/lean requires bookspec's Lean; the laws live once
D-69 puts the accounting laws in one place: bookspec's Lean, namespace `Bookspec`. L-F1 landed there every file of `verify/lean/Wanderbooks/`. Before deleting the copies, each was checked: comments stripped, namespace normalised, blank lines dropped, and diffed against its bookspec peer -- all ten are identical (Basic, Entry, Statements, Closing, Ledger, Apportion, Parties, Prorate, Deductible, Documents). There is nothing wanderbooks-only left to keep. So `verify/lean/lakefile.toml` now `[[require]]`s bookspec as a git dependency at 77e046334eea29e082b53bbe907017d9ffef5a0a -- the same rev `crates/ledger-core/Cargo.toml` pins the Rust checker to -- with `subDir = "lean"`. The generated `lake-manifest.json` is committed, because Lake builds from it and it must not float. `Wanderbooks.lean` is now a thin local target that imports `Bookspec`: `lake build` builds and checks the whole law set, and the file says where a law is edited and how wanderbooks picks it up (move the pin). The duplicated modules and the `Wanderbooks/` directory are gone. `Audit.lean` imports `Bookspec` and audits all 151 theorems (the 118 headline laws plus the 33 named laws in `Laws.lean` that cite them) -- the whole set bookspec itself audits. `just prove` still checks standard axioms only; its summary now says "theorems", since the set is more than headline ones. The new `tests/bookspec/pin.rs` reads the rev out of the lakefile, the manifest, and both crates' Cargo.toml, and fails unless all four name the same repository at the same commit, and bookspec's `lean/` package. Without it the Lean laws and the Rust checker could silently be different spec versions. Verified: `lake build` from a clean `.lake` (fetches and builds in 7 s); `just prove` 871 s (Kani 53 harnesses, then 151 theorems, standard axioms only); `cargo test -p ledger-core --test bookspec pin`; `cargo fmt --check`; `cargo clippy -p ledger-core --tests -D warnings`. Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai> |
||
|
|
2d36fb05a4 |
feat(core): prove the accounting laws (Kani + Lean); fix three engine gaps
The money arithmetic every invariant rests on now lives in one kernel,
ledger-core/src/kernel.rs: signing a line, totals, net, per-account
movements, reversal, normal balances, trial-balance columns, closing
lines, and the rounding plug for member splits. It is generic over a
checked `Amount` trait. The engine runs it on Decimal (validate,
movements, reverse_entry, account_balance, close_period_to, allocate,
trial_balance): there is one copy of the arithmetic, and it is the one
under proof.
Three layers, docs/verification.md maps every law to its proofs:
- Kani (src/proofs.rs): 17 harnesses prove the kernel at i64 for every
input up to 4 lines over 3 accounts. An entry is accepted exactly
when it is two or more positive, in-range lines whose debits equal
credits; accepted entries net to zero; grouping by account loses
nothing; a reversal mirrors and cancels its original; trial-balance
columns balance iff the books net to zero; closing lines zero their
accounts and a close balances however the net is split; the plug
makes parts sum to the whole. No panics, no overflow. kani::cover!
shows the proofs are not vacuous.
- Lean 4 (verify/lean): 24 headline theorems for books of any size. The
ledger is a state machine (add account, draft, post, reverse, open,
close), and in every reachable state the books net to zero, the trial
balance balances (as of any date), and the accounting equation holds.
Posting only appends ("posted is forever"), closed periods are
frozen, a close zeroes revenue and expenses and credits exactly the net
income, and period statements add up. Audit.lean shows standard axioms
only, no sorry.
- Bridge (tests/kernel_bridge.rs): the Decimal and i64 kernels agree.
What the proofs found (each one missed by every test, fuzzer, and
mutant):
- The engine accepted negative line amounts; only MCP refused them. Now
NegativeAmountLine: direction is the side's job.
- Summing lines near Decimal::MAX panicked. Lines are now capped at one
quadrillion and ten decimal places (AmountOutOfRange), which keeps
every realistic sum exact, and the kernel's sums are checked.
- FX conversion (foreign_amount * fx_rate) could panic the same way; it
is now a checked multiply.
- Kani itself: 0.68 mis-models symbolic-length copy_from_slice
(model-checking/kani#4904) and reported a reversal that doesn't net to
zero. The counterexample, replayed natively, passes, and a 15-line
standalone crate reproduces the bug. Harnesses avoid bulk copies, and
a should_panic canary will flag the upstream fix.
`just prove` runs it all (Kani memory- and time-capped, fails on an
unsatisfiable cover or a non-standard Lean axiom). AGENTS.md now makes
formal coverage part of every core change.
Also, docs: GnuCash is *an* oracle, never *the* oracle (the owner). The
spec is accounting principles, proved. Golden files are recomputed from
principles. External implementations (GnuCash, the old Sheet, QBO/Xero)
are cross-checks whose disagreements get adjudicated, never obeyed.
docs/oracle.md gains "Who is right"; AGENTS, README, architecture,
roadmap, parity, and STATUS are reworded to match.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GWVo7x3UHw5kwCUaWxiejX
|