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
+41 -22

No files matched your search

+3 -1
View File
@@ -1348,7 +1348,9 @@ fn preview_lines_for(
} else { } else {
-amount.amount -amount.amount
}; };
let after = before + delta; let after =
ledger_core::money::add_exact(before, delta, "the account's balance after the line")
.map_err(|e| e.to_string())?;
out.push(PreviewLine { out.push(PreviewLine {
account_id: *account_id, account_id: *account_id,
code: account.code.clone(), code: account.code.clone(),
+20 -6
View File
@@ -839,7 +839,15 @@ impl BooksServer {
description, description,
period: r.period, period: r.period,
amount: money(r.amount), amount: money(r.amount),
caught_up: money((r.amount - r.scheduled).max(Decimal::ZERO)), caught_up: money(
ledger_core::money::sub_exact(
r.amount,
r.scheduled,
"the catch-up depreciation",
)
.map_err(|e| e.to_string())?
.max(Decimal::ZERO),
),
entry_id, entry_id,
already_posted: r.already_posted, already_posted: r.already_posted,
}); });
@@ -916,14 +924,20 @@ impl BooksServer {
.map(code_of) .map(code_of)
.collect::<Result<_, _>>()?; .collect::<Result<_, _>>()?;
let code_refs: Vec<&str> = codes.iter().map(|s| s.as_str()).collect(); let code_refs: Vec<&str> = codes.iter().map(|s| s.as_str()).collect();
let debits = preview.accumulated let debits = ledger_core::money::sum_exact(
+ proceeds.amount [
+ preview.catch_up_depreciation preview.accumulated,
+ if preview.gain_loss < Decimal::ZERO { proceeds.amount,
preview.catch_up_depreciation,
if preview.gain_loss < Decimal::ZERO {
-preview.gain_loss -preview.gain_loss
} else { } else {
Decimal::ZERO Decimal::ZERO
}; },
],
"the disposal's debits",
)
.map_err(|e| e.to_string())?;
let receipt_required = self let receipt_required = self
.settings() .settings()
.policy .policy
+6 -6
View File
@@ -114,12 +114,12 @@ impl BooksServer {
return Err(party_required("pass agency: the tax agency paid")); return Err(party_required("pass agency: the tax agency paid"));
} }
// Total before remitting, for the response. // Total before remitting, for the response.
let total: Decimal = store let total = ledger_core::money::sum_exact_of(
.ledger() store.ledger().payroll_tax_payables(),
.payroll_tax_payables() |(_, b)| b,
.iter() "the payroll taxes payable",
.map(|(_, b)| *b) )
.sum(); .map_err(|e| e.to_string())?;
let entry_id = store let entry_id = store
.remit_payroll_taxes(parse_date(&p.date)?, cash) .remit_payroll_taxes(parse_date(&p.date)?, cash)
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
+12 -9
View File
@@ -31,21 +31,24 @@ fn movement_over(
ledger: &Ledger, ledger: &Ledger,
account: AccountId, account: AccountId,
entries: &[&ledger_core::JournalEntry], entries: &[&ledger_core::JournalEntry],
) -> Decimal { ) -> Result<Decimal, String> {
let movement: Decimal = entries let movement = ledger_core::money::sum_exact_of(
entries
.iter() .iter()
.flat_map(|e| e.lines()) .flat_map(|e| e.lines())
.filter(|l| l.account == account) .filter(|l| l.account == account),
.map(|l| match l.side { |l| match l.side {
Side::Debit => l.amount.amount, Side::Debit => l.amount.amount,
Side::Credit => -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"); 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::Debit => movement,
Side::Credit => -movement, Side::Credit => -movement,
} })
} }
fn lines_out(lines: &[ledger_core::StatementLine]) -> Vec<StatementLineOut> { fn lines_out(lines: &[ledger_core::StatementLine]) -> Vec<StatementLineOut> {
@@ -835,7 +838,7 @@ impl BooksServer {
if a.placeholder { if a.placeholder {
continue; continue;
} }
let m = movement_over(ledger, a.id, &in_range); let m = movement_over(ledger, a.id, &in_range)?;
if !m.is_zero() { if !m.is_zero() {
totals.push(DigestTotal { totals.push(DigestTotal {
code: a.code.clone(), code: a.code.clone(),