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

+44 -9
View File
@@ -124,9 +124,25 @@ pub fn allocate(
amount: rust_decimal::Decimal,
shares: &[rust_decimal::Decimal],
) -> Vec<rust_decimal::Decimal> {
try_allocate(amount, shares)
.expect("shares sum to 1, so the shortfall is under one cent per member")
}
/// [`allocate`], refusing with `TooLargeToCompute` instead of panicking
/// when a share of `amount` is beyond exact arithmetic (a replayed close
/// carries a journaled amount of any size, rules.md B-28). The same split,
/// to the cent, for every amount [`allocate`] accepts.
pub fn try_allocate(
amount: rust_decimal::Decimal,
shares: &[rust_decimal::Decimal],
) -> Result<Vec<rust_decimal::Decimal>, LedgerError> {
use rust_decimal::Decimal;
let too_large = || LedgerError::TooLargeToCompute(format!("the members' shares of {amount}"));
let magnitude = amount.abs();
let exact: Vec<Decimal> = shares.iter().map(|s| magnitude * s).collect();
let exact: Vec<Decimal> = shares
.iter()
.map(|s| magnitude.checked_mul(*s).ok_or_else(too_large))
.collect::<Result<_, _>>()?;
let mut parts: Vec<Decimal> = exact
.iter()
.map(|e| {
@@ -134,15 +150,19 @@ pub fn allocate(
.expect("a cent place of a representable amount is representable")
})
.collect();
let remainders: Vec<Decimal> = exact.iter().zip(&parts).map(|(e, p)| e - p).collect();
let remainders: Vec<Decimal> = exact
.iter()
.zip(&parts)
.map(|(e, p)| e.checked_sub(*p).ok_or_else(too_large))
.collect::<Result<_, _>>()?;
crate::kernel::apportion(magnitude, &mut parts, &remainders, Decimal::new(1, 2))
.expect("shares sum to 1, so the shortfall is under one cent per member");
.ok_or_else(too_large)?;
if amount.is_sign_negative() {
for p in &mut parts {
*p = -*p;
}
}
parts
Ok(parts)
}
/// The rule closes used before 2026-10-13: each share rounded half away
@@ -152,16 +172,31 @@ pub fn allocate_last_absorbs(
amount: rust_decimal::Decimal,
shares: &[rust_decimal::Decimal],
) -> Vec<rust_decimal::Decimal> {
try_allocate_last_absorbs(amount, shares).expect("allocated amounts are in range")
}
/// [`allocate_last_absorbs`], refusing with `TooLargeToCompute` instead of
/// panicking when `amount × share` or the plug is beyond exact arithmetic
/// (replay of a legacy close with a huge journaled amount, rules.md B-28).
/// The same split, to the cent, for every amount [`allocate_last_absorbs`]
/// accepts: this is the frozen rule, only its overflow now an error.
pub fn try_allocate_last_absorbs(
amount: rust_decimal::Decimal,
shares: &[rust_decimal::Decimal],
) -> Result<Vec<rust_decimal::Decimal>, LedgerError> {
use crate::kernel::{Rounding, round_places};
let too_large = || LedgerError::TooLargeToCompute(format!("the members' shares of {amount}"));
let mut parts: Vec<_> = shares
.iter()
.map(|s| {
round_places(amount * s, 2, Rounding::HalfAwayFromZero)
.expect("a cent place of a representable amount is representable")
amount
.checked_mul(*s)
.and_then(|p| round_places(p, 2, Rounding::HalfAwayFromZero))
.ok_or_else(too_large)
})
.collect();
crate::kernel::plug_last(amount, &mut parts).expect("allocated amounts are in range");
parts
.collect::<Result<_, _>>()?;
crate::kernel::plug_last(amount, &mut parts).ok_or_else(too_large)?;
Ok(parts)
}
/// What one members close gives each member: [`allocate`]'s split of the