fix(core): Decimal arithmetic in ledger-core goes through the kernel (K-U11a)

Bar item 1: money arithmetic lives in kernel/ and nowhere else.
scripts/raw-decimal-arith.sh listed 210 places in ledger-core that did
not: raw +, -, * and unary minus (which panic past Decimal's range), and
checked_add / checked_sub / checked_mul / checked_div calls that
resolved to rust_decimal's inherent methods (which round silently past
28 digits) and not to the kernel's Amount. 199 of them now call the
kernel: Amount's methods by their full path, total / total_of for the
hand-rolled sums, normal_balance, and money.rs's sign and in-range
helpers.

The rewrite was done by a script from the compiler's own line and
column for each site, so no site was copied by hand; the few that
needed a decision are explicit:
- a balance sheet that cannot add liabilities and equity is not
  balanced (it panicked);
- a template's shares, a party's percents, a reconciliation's book and
  cleared balances and a reversal's document movements refuse with
  TooLargeToCompute or their own message where they panicked;
- tax_capital's split refuses where a product by the sign could not be
  taken, and collects its remainders as Options.
No figure moves: every site computes what it computed, by the same
operations in the same order.

Left, each for a named reason (the detector prints these 11):
money.rs 139 and 150 and periods.rs 149 and 202 (K-U10a rewrites them),
audit.rs 355 and 511 (V-D3 is in them), ledger.rs 4048 (a share times
the closed total: checked_mul_rounded, with K-U10a), and four rescale
calls (money.rs 302, reports.rs 637, schedules.rs 370 and 377) that
need a kernel function of their own.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 17:22:17 -07:00
1 parent e0c78a783b
commit 32725fb669
19 files changed
+401 -394

No files matched your search

+18 -26
View File
@@ -22,6 +22,7 @@
//! the §704(c) built-in gain or loss at contribution.
use crate::money::Steps;
use crate::money::neg;
use crate::{
AccountId, AccountType, ChartOfAccounts, EntryId, EntryStatus, Ledger, LedgerError, Money,
kernel,
@@ -684,11 +685,9 @@ fn schedule_months(asset: &Asset, through: NaiveDate) -> Result<Vec<MonthLine>,
}
let too_large =
|| LedgerError::TooLargeToCompute(format!("asset {}'s depreciation schedule", asset.id));
let depreciable = asset
.cost
.amount
.checked_sub(asset.salvage_value.amount)
.ok_or_else(too_large)?;
let depreciable =
crate::kernel::Amount::checked_sub(asset.cost.amount, asset.salvage_value.amount)
.ok_or_else(too_large)?;
if depreciable <= Decimal::ZERO {
return Ok(Vec::new());
}
@@ -721,10 +720,7 @@ fn schedule_months(asset: &Asset, through: NaiveDate) -> Result<Vec<MonthLine>,
period: format!("{:04}-{:02}", p.year, p.month),
amount: steps.decimal(through_k - charged).ok_or_else(too_large)?,
accumulated,
book_value: asset
.cost
.amount
.checked_sub(accumulated)
book_value: crate::kernel::Amount::checked_sub(asset.cost.amount, accumulated)
.ok_or_else(too_large)?,
},
});
@@ -760,11 +756,11 @@ pub fn accumulated_scheduled_through(
/// [`crate::Ledger::accumulated_posted`] (the register's and the tools'
/// `book_value`, BS-asset-3).
pub fn scheduled_book_value(asset: &Asset, as_of: NaiveDate) -> Result<Decimal, LedgerError> {
asset
.cost
.amount
.checked_sub(accumulated_scheduled_through(asset, as_of)?)
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("asset {}'s book value", asset.id)))
crate::kernel::Amount::checked_sub(
asset.cost.amount,
accumulated_scheduled_through(asset, as_of)?,
)
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("asset {}'s book value", asset.id)))
}
/// A change to what the books carry for an asset's depreciation, in the
@@ -839,7 +835,7 @@ pub(crate) fn run_chain_entries(
.get_entry(id)
.filter(|x| x.status() == EntryStatus::Posted)
{
out.push((e.date(), if adds { amount } else { -amount }));
out.push((e.date(), if adds { amount } else { neg(amount) }));
}
link = reversals.get(&id).copied();
adds = !adds;
@@ -929,8 +925,7 @@ pub fn run_amounts(
RunAmount {
amount,
scheduled: own,
over: g
.checked_sub(s)
over: crate::kernel::Amount::checked_sub(g, s)
.map_or(Decimal::ZERO, |o| o.max(Decimal::ZERO)),
},
)
@@ -981,7 +976,7 @@ fn cumulatives(
li += 1;
}
while let Some(r) = runs.get(ri).filter(|r| (r.year, r.month) <= (y, m)) {
carry = carry.checked_add(r.amount).ok_or_else(too_large)?;
carry = crate::kernel::Amount::checked_add(carry, r.amount).ok_or_else(too_large)?;
ri += 1;
}
months.push((y, m));
@@ -1096,8 +1091,7 @@ pub fn catch_up_room(
let too_large =
|| LedgerError::TooLargeToCompute(format!("asset {}'s depreciation run", asset.id));
let c = cumulatives(asset, (year, month), latest, &lines, posted)?;
let over = c.carried[0]
.checked_sub(c.scheduled[0])
let over = crate::kernel::Amount::checked_sub(c.carried[0], c.scheduled[0])
.ok_or_else(too_large)?
.max(Decimal::ZERO);
Ok(RunAmount {
@@ -1154,15 +1148,13 @@ pub fn disposal_amounts(
check_disposal(asset, proceeds)?;
let accumulated = accumulated_scheduled_through(asset, date)?;
let too_large = || LedgerError::TooLargeToCompute(format!("asset {}'s disposal", asset.id));
let book = asset
.cost
.amount
.checked_sub(accumulated)
.ok_or_else(too_large)?;
let book =
crate::kernel::Amount::checked_sub(asset.cost.amount, accumulated).ok_or_else(too_large)?;
Ok(DisposalAmounts {
book_value: book,
accumulated,
gain_loss: proceeds.amount.checked_sub(book).ok_or_else(too_large)?,
gain_loss: crate::kernel::Amount::checked_sub(proceeds.amount, book)
.ok_or_else(too_large)?,
})
}