refactor(core): every report and book total goes through the kernel's checked sum

A raw Decimal + / += / .sum() panics at Decimal::MAX, and a replayed
journal line is accepted of any size (rules.md B-28), so a total of
restored figures must be exact or refused (B-21), never a crash. The
reports (P&L, balance sheet, general ledger, members' equity, asset
register), K-1 lines, tax-basis wholes, payouts, the 1099 split, the
audit's articulation check, the members close split and arap's
document_total now sum with kernel::total / total_of / total_try and
add or subtract with checked arithmetic, refusing with TooLargeToCompute.
money::sum_exact* and add_exact/sub_exact are the engine's thin wrappers
over the kernel (no second copy of the sum). form1099::sum3 was a copy of
the kernel's sum and is now kernel::total.

periods::allocate and allocate_last_absorbs (frozen replay rules) keep
their signatures and results to the cent; each gains a try_ twin that
returns TooLargeToCompute where the raw amount * share used to panic, and
the ledger's close split calls the twins.

Behaviour for every ordinary input is unchanged: no figure moves, no test
or golden edited. Infallible public signatures (trial_balance,
ProfitAndLossStatement::net_income, tax book_net, project_entries,
cash-flow kinds, the Regime) keep their raw sums; making them fallible
would change ~100 callers and is a decision, not a refactor.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-08 12:17:58 -07:00
1 parent b3d504067f
commit 3376c3d549
10 files changed
+368 -162

No files matched your search

+10 -6
View File
@@ -333,7 +333,9 @@ pub fn check_equation(bs: &crate::reports::BalanceSheetStatement) -> Option<Find
/// Net income moved to equity by the closes dated through `through`:
/// revenue closed less expenses closed, read off the closing entries'
/// lines.
fn closed_net(ledger: &Ledger, through: NaiveDate) -> Decimal {
/// `None` when the sum is past exact arithmetic (the check is skipped, as
/// `articulation` skips a statement it cannot make).
fn closed_net(ledger: &Ledger, through: NaiveDate) -> Option<Decimal> {
let mut net = Decimal::ZERO;
for id in ledger.closing_entry_ids() {
let Some(e) = ledger.get_entry(id) else {
@@ -351,11 +353,11 @@ fn closed_net(ledger: &Ledger, through: NaiveDate) -> Decimal {
Side::Credit => -l.amount.amount,
};
if matches!(a.account_type, AccountType::Revenue | AccountType::Expense) {
net += signed;
net = crate::kernel::Amount::checked_add(net, signed)?;
}
}
}
net
Some(net)
}
fn articulation(ledger: &Ledger, through: NaiveDate) -> Option<Finding> {
@@ -369,7 +371,7 @@ fn articulation(ledger: &Ledger, through: NaiveDate) -> Option<Finding> {
check_articulation(
pnl.net_income(),
bs.current_earnings,
closed_net(ledger, through),
closed_net(ledger, through)?,
)
}
@@ -380,7 +382,8 @@ pub fn check_articulation(
current_earnings: Decimal,
closed: Decimal,
) -> Option<Finding> {
(net_income != current_earnings + closed).then(|| {
let explained = crate::kernel::Amount::checked_add(current_earnings, closed)?;
(net_income != explained).then(|| {
Finding::new(
"pnl_not_articulated",
Subject::Books,
@@ -522,7 +525,8 @@ fn schedules(ledger: &Ledger, through: NaiveDate) -> Vec<Finding> {
}
};
if let Some((first, _)) = months.first() {
let total: Decimal = months.iter().map(|(_, a)| *a).sum();
let total = crate::kernel::total_of(&months, |&(_, a)| a)
.map_or_else(|| "a sum too large to compute".to_string(), |t| t.to_string());
out.push(Finding::new(
"schedule_behind",
Subject::Schedule(s.id),