fix(core): payroll_remit_plan refuses past exact arithmetic instead of panicking

The remittance plan totalled the payables with a raw `total +=`, which
panics on Decimal overflow, on a pub fn returning a bare plan. Its
callers refused first through sum_exact_of, but that was an unenforced
precondition (BS-entry-4: every derived figure is checked arithmetic).
It now returns Result<_, LedgerError>, totalling through
kernel::total_of and refusing TooLargeToCompute; Ledger and the store
propagate it and keep their pre-checks. A test shows payables past the
range, up and down, are refused, not a panic.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-09 13:50:54 -07:00
1 parent 90d34585fd
commit 46dccd5d76
4 files changed
+36 -8

No files matched your search

+1 -1
View File
@@ -8533,7 +8533,7 @@ impl Ledger {
}
let entry = self.try_create_entry(date, &format!("Payroll tax remittance {date}"))?;
for (account, side, amount) in
payroll_remit_plan(&payables, cash_account, self.base_currency)
payroll_remit_plan(&payables, cash_account, self.base_currency)?
{
self.add_line(entry, account, side, amount)?;
}
+8 -4
View File
@@ -213,24 +213,28 @@ pub fn payroll_entry_plan(
/// The remittance entry's lines: Dr each payable with a non-zero balance,
/// Cr cash for the total. Balances are normal-side (positive = owed), so
/// the plan balances by construction.
///
/// Refused (TooLargeToCompute) when the payables' total is past exact
/// arithmetic, never a panic: the total is the kernel's checked fold, so
/// this function does not depend on its callers having refused first.
pub fn payroll_remit_plan(
payables: &[(AccountId, Decimal)],
cash_account: AccountId,
base: Currency,
) -> Vec<(AccountId, Side, Money)> {
) -> Result<Vec<(AccountId, Side, Money)>, LedgerError> {
let total = crate::kernel::total_of(payables.iter(), |(_, b)| *b)
.ok_or_else(|| LedgerError::TooLargeToCompute("the payroll taxes payable".into()))?;
let mut plan = Vec::new();
let mut total = Decimal::ZERO;
for (account, balance) in payables {
if balance.is_zero() {
continue;
}
plan.push((*account, Side::Debit, Money::new(*balance, base)));
total += *balance;
}
if !total.is_zero() {
plan.push((cash_account, Side::Credit, Money::new(total, base)));
}
plan
Ok(plan)
}
#[cfg(test)]
@@ -1029,7 +1029,8 @@ fn remit_plan_hand_worked() {
&[(3, d("600.00")), (4, d("0")), (6, d("145.00"))],
PLAN_CASH,
Currency::USD,
);
)
.unwrap();
let got: Vec<(AccountId, Side, Decimal)> =
plan.iter().map(|(a, s, m)| (*a, *s, m.amount)).collect();
assert_eq!(
@@ -1042,6 +1043,29 @@ fn remit_plan_hand_worked() {
);
}
/// Oracle: BS-entry-4 (every derived figure computed with checked
/// arithmetic): payables whose total is past exact arithmetic are refused
/// as TooLargeToCompute, never a panic, even when a caller did not
/// pre-check.
#[test]
fn remit_plan_past_exact_arithmetic_is_refused() {
let err = payroll_remit_plan(
&[(3, Decimal::MAX), (6, Decimal::ONE)],
PLAN_CASH,
Currency::USD,
)
.unwrap_err();
assert!(matches!(err, LedgerError::TooLargeToCompute(_)), "{err:?}");
// Past the range downward, too.
let err = payroll_remit_plan(
&[(3, Decimal::MIN), (6, -Decimal::ONE)],
PLAN_CASH,
Currency::USD,
)
.unwrap_err();
assert!(matches!(err, LedgerError::TooLargeToCompute(_)), "{err:?}");
}
/// Oracle: run_payroll INVARIANTS 1–2 and doc (auto-create 6100/6110/
/// 2110–2150), hand-worked Alice entry; balances after: cash 10000 − 3567.50
/// = 6432.50; payables as in the plan.
@@ -1478,7 +1502,7 @@ proptest! {
.enumerate()
.map(|(i, c)| (i as AccountId + 1, Decimal::new(*c, 2)))
.collect();
let plan = payroll_remit_plan(&payables, PLAN_CASH, Currency::USD);
let plan = payroll_remit_plan(&payables, PLAN_CASH, Currency::USD).unwrap();
let total: Decimal = payables.iter().map(|(_, b)| *b).sum();
let (dr, cr) = plan_totals(&plan);
prop_assert_eq!(dr, cr);
+1 -1
View File
@@ -4437,7 +4437,7 @@ impl Store {
}
let entry_id = self.ledger.next_entry_id();
let base = self.base_currency;
let plan = payroll_remit_plan(&payables, cash_account, base);
let plan = payroll_remit_plan(&payables, cash_account, base)?;
let description = format!("Payroll tax remittance {date}");
let mut journaled = Vec::with_capacity(plan.len() + 2);
journaled.push(self.append_journal(