merge: every report and book total goes through the kernel's checked sum (K-U4; branch t3/kernel-checked-totals, bc81b849; reviewed)

periods.rs conflicted with K-U2: the frozen replay rules delegate to
K-U4's checked twins, whose rounding now goes through K-U2's
round_places (so every rounding stays in the kernel); no figure changes.
The K-U4 note gains the review's list of bounded raw sums left, and the
caveat that the twin test checks delegation (the review's independent
20,000-case comparison found the twins identical).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Opus 5.5 committed 2026-11-08 17:09:27 -07:00
commit 57d86b1920
25 files changed
+1184 -229

No files matched your search

+16 -14
View File
@@ -2566,12 +2566,13 @@ impl Store {
Some((e, d, if i % 2 == 0 { amount } else { -amount }))
})
.collect();
let at_disposal: Decimal = dated
.iter()
.filter(|(_, d, _)| *d <= month_end)
.map(|(_, _, a)| *a)
.sum();
let in_the_end: Decimal = dated.iter().map(|(_, _, a)| *a).sum();
let what = format!("{period}'s depreciation run");
let at_disposal = ledger_core::money::sum_exact_of(
dated.iter().filter(|(_, d, _)| *d <= month_end),
|(_, _, a)| *a,
&what,
)?;
let in_the_end = ledger_core::money::sum_exact_of(&dated, |(_, _, a)| *a, &what)?;
if at_disposal != in_the_end {
// The entries the difference is in: those dated after the
// month (or the run itself, dated after a reversal of it).
@@ -3446,7 +3447,7 @@ impl Store {
let ar = self.ensure_control_account(&ledger_core::RECEIVABLES)?;
let revenue = self.ensure_control_account(&ledger_core::REVENUE)?;
let invoice_id = self.ledger.next_invoice_id();
let total: Decimal = lines.iter().map(|l| l.amount).sum();
let total = arap::document_total(lines, "the invoice")?;
let description = format!("Invoice {invoice_id}");
let mut posting = vec![(ar, Side::Debit, total, None)];
posting.extend(arap::document_parts(lines, "the invoice")?.into_iter().map(
@@ -3741,7 +3742,7 @@ impl Store {
let expense = self.ensure_control_account(&ledger_core::GENERAL_EXPENSES)?;
let ap = self.ensure_control_account(&ledger_core::PAYABLES)?;
let bill_id = self.ledger.next_bill_id();
let total: Decimal = lines.iter().map(|l| l.amount).sum();
let total = arap::document_total(lines, "the bill")?;
let description = format!("Bill {bill_id}");
let mut posting: Vec<_> = arap::document_parts(lines, "the bill")?
.into_iter()
@@ -4449,7 +4450,8 @@ impl Store {
return Err(LedgerError::UnknownAccount(cash_account).into());
}
let payables = self.ledger.payroll_tax_payables();
let total: Decimal = payables.iter().map(|(_, b)| *b).sum();
let total =
ledger_core::money::sum_exact_of(&payables, |(_, b)| *b, "the payroll taxes payable")?;
if total.is_zero() {
return Err(LedgerError::NothingToRemit.into());
}
@@ -4496,11 +4498,11 @@ impl Store {
)?);
// Name the entry as a remittance, as payroll.run names a run's
// (readers like the viewer tell it from a hand-made entry by this).
let total: Decimal = plan
.iter()
.filter(|(_, s, _)| *s == Side::Debit)
.map(|(_, _, m)| m.amount)
.sum();
let total = ledger_core::money::sum_exact_of(
plan.iter().filter(|(_, s, _)| *s == Side::Debit),
|(_, _, m)| m.amount,
"the payroll tax remittance",
)?;
journaled.push(self.append_journal(
"payroll.taxes_remitted",
json!({"entry_id": entry_id, "date": date.to_string(), "total": total.to_string()}),
+71
View File
@@ -0,0 +1,71 @@
//! Documents whose lines' sum is past exact arithmetic are refused, never a
//! panic (kernel sweep unit K-U4; rules.md B-21, B-28).
//!
//! A posted entry's lines are bounded by the line cap at the moment it
//! posts, restored or new (`entry.posted` validates), so a report's sums
//! over posted lines cannot reach the edge; a document's lines are the
//! figures that can be restored "of any size", and are what this guards.
//!
//! ```text
//! GOAL: a document whose lines sum past exact arithmetic is refused with
//! a ledger error and journals nothing.
//! SOURCE: docs/spec/rules.md B-28 (a replayed line is accepted of any
//! size), B-21 (amounts are exact); bookspec BS-entry-3,
//! BS-entry-4.
//! ```
use chrono::NaiveDate;
use ledger_core::Currency;
use ledger_core::arap::DocumentLine;
use ledger_store::{Store, StoreError};
use rust_decimal::Decimal;
use std::path::PathBuf;
struct TestDir(PathBuf);
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn test_dir(name: &str) -> TestDir {
let dir = std::env::temp_dir().join(format!(
"wanderbooks-store-exact-totals-{name}-{}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
TestDir(dir)
}
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(y, m, day).unwrap()
}
/// Store::create_invoice / create_bill: lines whose sum is past exact
/// arithmetic are refused (the raw .sum() ran before the checked
/// document_total), and the books are untouched.
#[test]
fn a_document_of_enormous_lines_is_refused_not_a_panic() {
let dir = test_dir("documents");
let mut s = Store::create(&dir.0, Currency::USD).unwrap();
let customer = s.add_customer("Cedar").unwrap();
let vendor = s.add_vendor("Pine").unwrap();
let line = |amount: Decimal| DocumentLine {
description: "x".into(),
amount,
class_id: None,
account: None,
quantity: None,
unit_price: None,
};
let lines = || vec![line(Decimal::MAX), line(Decimal::MAX)];
let before = std::fs::read_to_string(dir.0.join(ledger_store::JOURNAL_FILE)).unwrap();
let invoice = s.create_invoice(customer, d(2026, 1, 2), d(2026, 2, 1), lines());
assert!(matches!(invoice, Err(StoreError::Ledger(_))), "{invoice:?}");
let bill = s.create_bill(vendor, d(2026, 1, 2), d(2026, 2, 1), lines());
assert!(matches!(bill, Err(StoreError::Ledger(_))), "{bill:?}");
let after = std::fs::read_to_string(dir.0.join(ledger_store::JOURNAL_FILE)).unwrap();
assert_eq!(before, after, "a refused document journals nothing");
}