refactor(mcp): previews and totals refuse oversized sums instead of panicking

preview_lines_for (balance before + line), the disposal preview's debits,
the depreciation run's catch-up difference, the digest's account movement
and the payroll remittance total used raw + / - / .sum() on stored or
replayed figures; they now go through ledger_core::money's checked
helpers and return the tool's string error (TooLargeToCompute). Unchanged
for ordinary input.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-08 12:23:11 -07:00
1 parent afc7c545da
commit e6104b49d6
4 files changed
+46 -27

No files matched your search

+14 -11
View File
@@ -31,21 +31,24 @@ fn movement_over(
ledger: &Ledger,
account: AccountId,
entries: &[&ledger_core::JournalEntry],
) -> Decimal {
let movement: Decimal = entries
.iter()
.flat_map(|e| e.lines())
.filter(|l| l.account == account)
.map(|l| match l.side {
) -> Result<Decimal, String> {
let movement = ledger_core::money::sum_exact_of(
entries
.iter()
.flat_map(|e| e.lines())
.filter(|l| l.account == account),
|l| match l.side {
Side::Debit => l.amount.amount,
Side::Credit => -l.amount.amount,
})
.sum();
},
"an account's movement",
)
.map_err(|e| e.to_string())?;
let account = ledger.chart().get(account).expect("account exists");
match account.account_type.normal_side() {
Ok(match account.account_type.normal_side() {
Side::Debit => movement,
Side::Credit => -movement,
}
})
}
fn lines_out(lines: &[ledger_core::StatementLine]) -> Vec<StatementLineOut> {
@@ -835,7 +838,7 @@ impl BooksServer {
if a.placeholder {
continue;
}
let m = movement_over(ledger, a.id, &in_range);
let m = movement_over(ledger, a.id, &in_range)?;
if !m.is_zero() {
totals.push(DigestTotal {
code: a.code.clone(),