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

@@ -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);