Fold the six proof notes of plan P14's kernel sweep into the one place the proofs are described. verification.md gains: - the harness table's thirty-four new rows, each with its note's law number and measured time (all run in `prove`; none is in `kani_slow`); - four new law sections in the existing house style: law 40 rounding to a step (K-U2), law 41 FX booking at the transaction-date rate (K-U1), law 42 shares by weights (K-U3), and law 43 equal monthly parts (K-U5); - law 4's collection-total harnesses (K-U4), law 23's Direction::of and law 27's part_weight harnesses (the entries note), with their bridges and the Lean theorems each cites; - the sweep's Kani bounds (fixed steps, rates, month counts, weights totals, the `unwind(4)` and MAX_CORRECTION_STEPS bounds) and the symbolic-divisor note that carries them to Lean and the bridge; - "What the proofs found" and "What is and isn't proved": K-U1's double rounding and D-75 precondition with its open 96-bit edge, K-U4's checked_add dust at Decimal::MAX, K-U3's quotient-crossing-a-step and non-terminating correction loop, the pinned tie ranking, the preserved printed scale, and K-U10's MAX and scale-28 edges, each with the test that pins it; - the overview table's scope, the sweep's bridge reference, and the harness/audit counts (97 harnesses, 87 in `prove`; 188 Lean theorems). Each unit note now opens with the section it was folded into; the notes stay as the units' records. No code changed. Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
11 KiB
K-U4: collection totals (kernel/total.rs)
Folded into docs/verification.md, section "Entries" (law 4), the
sweep's bounds, and "What the proofs found".
Kernel sweep unit U4 (plan/kernel-sweep-audit.md sections 2 and 3). The
authority is BS-entry-3 (amounts are exact; rounding only on returns) and
BS-entry-4 (no input may reach a panic), bookspec BS-entry-3 and BS-entry-4.
The functions
| Function | Meaning |
|---|---|
kernel::total(values) |
exact sum of any figures; None the moment a running sum is not representable |
kernel::total_of(items, f) |
total of f(item): the shape of iter().map(f).sum() |
kernel::total_try(items, f) |
like total_of for a mapping that can itself fail (c - d) |
The signed sum of (Side, amount) lines is not new: it is
kernel::net (and kernel::totals for the two columns), which stay the one
copy of that shape. The audit's proposed signed_total is exactly net;
none was added. The engine-side wrappers (money::sum_exact,
sum_exact_of, sum_exact_try, add_exact, sub_exact) only map None
to LedgerError::TooLargeToCompute; they call the kernel and add no
arithmetic.
Kani harnesses (src/proofs/total.rs)
All on i64, fixed arrays (no symbolic-length Vec, no copy_from_slice,
model-checking/kani#4904), each with kani::cover!.
| Harness | Law | Bounds | Time |
|---|---|---|---|
total_empty_is_zero |
total([]), total_of([]), total_try([]) are Some(0) |
none | 0.1 s |
total_append |
total(a ++ b) = total(a) + total(b), all Some, for in-range runs |
two runs of 0..=3 amounts, each at most 2^40 | 60 s |
total_is_sound |
Some(s) means s is the true sum, for any i64 figures (edge included) |
0..=4 arbitrary i64 |
1.9 s |
total_of_is_map_sum |
total_of with the signed amount as the map is the signed sum, and equals net |
0..=4 lines, in-range | 7.4 s |
total_try_is_map_sum |
total_try of differences is the sum of the differences; a failing map fails the total |
0..=4 pairs, in-range | 5.3 s |
total_overflow_is_none |
for two arbitrary i64s, total is Some exactly when the true sum fits; both overflow directions covered |
all i64 pairs |
0.5 s |
The bookspec law each harness proves (machine-readable for the
traceability gate, crates/ledger-core/tests/bookspec/traceability.rs):
| Harness | Law |
|---|---|
total_empty_is_zero |
4 |
total_append |
4 |
total_is_sound |
4 |
total_of_is_map_sum |
4 |
total_try_is_map_sum |
4 |
total_overflow_is_none |
4 |
Times: alone, cargo kani --exact --harness, the shared dev machine under
nice -n 10, 2026-11-08. total_append was 115 s at runs of up to 4 and
was reduced to up to 3 (the same law; 60 s) to keep just prove fast. A
version with one array split at a symbolic point (split_at) did not
finish in 10 minutes and was dropped.
Why the bounds are enough: the law is about appending, so zero, one and
several figures on each side exercise every shape; total_is_sound covers
the overflow edge with unconstrained i64s. Beyond the bounds the Lean
statement carries it for any length (below).
Order dependence at the edge
total is a left-to-right checked fold: every running sum must be
representable. So total([MAX, 1, -1]) is None while
total([MAX, -1, 1]) is Some(MAX). total_append is stated for runs that
never leave the range (always true for line-capped amounts); total_is_sound
is the unconditional statement (a Some is never wrong). The bridge edge
table pins both orders.
Lean
No new law (the audit said so; confirmed against
~/Code/bookspec/lean/Bookspec/Basic.lean, which verify/lean's Basic
mirrors):
Basic.net_append(net (xs ++ ys) = net xs + net ys) is the direct counterpart oftotal_appendfor the signed sum, i.e.total_ofwith the line's signed amount;net_cons/net_nilgive the empty case.Basic.sumOver_add,sumOver_neg,sumOver_zerocover sums of a mapped field. There is nosumOver_appendover index lists; for a plain list of integers the statement is core Lean'sList.sum_append. If an explicit named theorem is wanted it is a three-line induction likenet_append; I did not write Lean in this unit.- Overflow (
None) has no Lean counterpart: Lean'sIntis unbounded. That half is carried bytotal_overflow_is_none,total_is_soundand the bridge edge table.
Bridge (tests/kernel_bridge/total.rs)
total_agrees,total_of_agrees,total_try_agrees,total_append_agrees: i64 vsDecimalon random runs (2,000 cases each).decimal_edge_table: empty,Decimal::MAX/MINalone, one unit past either edge (None),MAX + MAX,MAX + MINandMAX + -MAXcancel exactly, mixed signs cancelling in the middle, the order-dependent edge,total_of/total_tryat the edge.finding_decimal_add_rounds_dust_at_the_edge: see finding 1.
Findings
Amount::checked_addforDecimalis not exact within one unit ofDecimal::MAX.Decimal::MAX.checked_add(0.4)isSome(MAX):rust_decimalrounds sub-unit dust when the sum would need a 97th bit (MAX + 0.5isNone). The kernel's "exact orNone" contract therefore fails for figures in the top unit ofDecimal's range (~7.9 x 10^28, 10^13 times a line's cap), for every kernel function that adds, not onlytotal. Not reachable by posted lines (capped at 10^15). Not fixed here: it lives inimpl Amount for Decimal(kernel/entries.rs), shared by every unit. A canary test pins the present behaviour. Suggested fix: refuse anyDecimalsum whose result scale is below the larger operand scale (rounding happened).- The audit's section 2.1 overstated reachability.
money.rschecked_add/checked_subwere already checked;ledger-gui/books.rsshare_of_what_forwas already checked. A posted entry's lines are validated against the line cap when it posts, restored or new, so sums over posted lines (the reports) cannot reach the edge; the store'screate_invoice/create_billraw.sum()ran aftervalidate_lines(cap). What genuinely panicked with a public caller's arguments:audit::check_articulation(current_earnings + closed),periods::allocate_last_absorbs/allocate(amount * share), and the kernel-less helpers withDecimalarguments. All now refuse. Documents, drafts and recurring entries are the figures BS-entry-4 lets restore oversized.
Sites replaced
(file:line at this commit; each sum_exact* / add_exact / sub_exact /
kernel::total* use)
- ledger-core (about 90 sites):
reports.rs271-272 (P&L by class), 315-324 (balance sheet), 416 (statement lines), 521-522 (P&L statement totals), 824-834 (balance sheet as of), 961-1020 (general ledger begin and running balances and column totals), 1200 (members' shares), 1235-1427 (members' equity: capital, flows, distributions, income, totals), 1797-1798 (asset register);k1.rs183, 207, 227, 249, 277, 293, 304, 310;tax_capital.rs591, 823, and 759/rest(checked-);form1099.rs707 (sum3was a copy of the kernel's sum, nowkernel::total) and thesplit_shareloop;payouts.rs169-189;audit.rs528 (andclosed_net,check_articulation);ledger.rs3824-3853 (close splits), 4061 (member shares), 6414, 8434;periods.rs127-180 (try_allocate,try_allocate_last_absorbs, the infallible rules delegate to them);arap.rs1066 (document_totalis nowkernel::total_of). - ledger-store
store.rs2570, 2575, 3450, 3745, 4454, 4501 (6). - ledger-package
package.rs295, 297, 301, 318, 533, 542;vendor_1099.rs196, 201;text.rs35 (total_text) (8). - ledger-mcp
server.rs1352;tools/assets.rs843, 927;tools/payroll.rs117;tools/reports.rs35 (5). - ledger-gui
pages/cash.rs186 (1). - ledger-import: none (see below).
Not replaced (infallible public signatures)
Also left raw, found by the review (2026-11-08), each bounded by the
posted-line cap or a validated input: ledger.rs ~2305 (a document's
mirror moves), ~4000 (member_close_run), ~8106 (reconciliation_report,
a fallible function: a candidate for unit K-U8, control and
reconciliation totals); ledger-config lib.rs ~362 (the members'
shares, each validated to (0, 1]); ledger-import xlsx.rs ~390; the
viewer's pages/invoices.rs ~203, pages/reconciliation.rs ~171,
pages/reports.rs ~468, books.rs ~142 (display, unit K-U9).
The regression test comparing try_allocate with allocate checks the
delegation only (allocate calls it). The review compared both frozen
rules, reimplemented from before this unit, over 20,000 random cases
and the edges: identical. A property test with that independent
reference is worth adding with unit V-D2.
A raw sum remains where the function returns a bare figure and changing it
to Result would change many callers and existing tests. Each is unreachable
with posted-line-capped figures; each is a signature decision, not a
refactor:
reports.rstrial_balance*(movements, column totals),project_entries,ProfitAndLossStatement::net_incomeandBalanceSheet::balanced.tax.rsTaxSummary::book_net/tax_net(sumhelper).cashflow.rskinds();ledger.rsRegime(closed,given);payroll.rspayroll_remit_plan(its caller now refuses first);analysis.rs,conventions.rs,shares.rs,parties.rsrunning sums.ledger-import/report.rs(statementbuilders return a bareStatement),templates.rsequal_shares(n owners, tiny).ledger-gui/books.rsview-model sums (display only).tax_capital.rs485-502: multiplication by +/-1 and a value less its own truncation cannot overflow.- Rounding calls: left for K-U2.
Regression tests
ledger-core/tests/exact_totals.rs: the helpers refuse atDecimal::MAX;document_total;check_articulation(MAX, MAX, MAX)(panicked before);try_allocate*refuse whereallocate*panicked and equal them for ordinary input; ten lines at the cap still sum.ledger-store/tests/exact_totals.rs: an invoice and a bill ofDecimal::MAXlines are refused and journal nothing.- No test for the mcp previews or the gui cash page: their inputs are posted balances, which the line cap keeps far from the edge (finding 2), and the public tool API cannot construct an oversized one.
just prove
nice -n 10 just prove on the shared dev machine, 2026-11-08: 59 Kani
harnesses verified (53 before, plus these 6; every cover satisfied), then
Lean (118 headline theorems, standard axioms only); 616 s wall clock for
the whole recipe under load from other workers' builds (Kani's own
reported time for the heaviest harness of the run was 235 s).