refactor(core): the last eleven Decimal sites go through the kernel (K-U11a)

The detector now reads zero for ledger-core. These eleven waited for
K-U10's exact operations, since each needed a decision the first 199 did
not:

- The frozen close rules (periods::try_allocate and
  try_allocate_last_absorbs) and Ledger::member_close_split multiply a
  share by a total. That product is a rounded figure by construction (a
  share of 27 places), and posted closes replay through the first two, so
  not a digit may move: they call Amount::checked_mul_rounded, the product
  they have always computed, by name.
- The audit's check of a close (check_split) computes its bound and each
  member's drift through Amount. A bound or a drift that exact arithmetic
  cannot hold is raised as a finding, never passed as clean.
- Four places changed how many decimal places a figure is written with
  (a contribution margin's one place, a schedule's months at the
  currency's places, Money's step count). Each called Decimal::rescale,
  which rounds half away from zero when asked for fewer places than the
  value has. None of them asked for that, and only a debug assertion said
  so. kernel::rescale_exact adds or drops trailing zeros and returns None
  for anything else, so a figure's written places can change and its
  value cannot.

rescale_exact has no Kani harness and no Lean theorem: whole-number money
has no places, so there is nothing for an i64 harness or a law over the
rationals to state. It is the sixteenth listed exception of the
traceability gate, and two bridge tests carry it instead: a property over
Decimal's whole range against whole-number arithmetic that shares no code
with it (None exactly when the amount is no whole multiple of the step;
a result is the same number with the same sign), and a table of the
figures the four call sites print.

No figure moves. The full ledger-core suite passes.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 23:27:23 -07:00
1 parent 3918151794
commit 7d525d392e
9 files changed
+158 -27

No files matched your search

+19 -9
View File
@@ -36,7 +36,7 @@ use serde::Serialize;
use crate::evidence::{Policy, Requirement};
use crate::settings::Settings;
use crate::{AccountType, DocStatus, EntryId, EntryStatus, Ledger, PeriodStatus, Side};
use crate::{AccountType, DocStatus, EntryId, EntryStatus, Ledger, PeriodStatus};
/// How much a finding matters.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
@@ -350,10 +350,7 @@ fn closed_net(ledger: &Ledger, through: NaiveDate) -> Option<Decimal> {
let Some(a) = ledger.chart().get(l.account) else {
continue;
};
let signed = match l.side {
Side::Debit => l.amount.amount,
Side::Credit => -l.amount.amount,
};
let signed = crate::money::signed_amount(l.side, l.amount.amount);
if matches!(a.account_type, AccountType::Revenue | AccountType::Expense) {
net = crate::kernel::Amount::checked_add(net, signed)?;
}
@@ -536,16 +533,29 @@ pub fn check_split(
trued_up: bool,
) -> Vec<Finding> {
// B(n) as derived above: (n-1)*u/2, floored at u. `unit` is a power
// of ten and n is small, so the product and the halving are exact.
// of ten and n is small, so the product and the halving are exact. A
// bound exact arithmetic could not hold (no real unit and member
// count reach it) falls to the floor, the stricter of the two: the
// audit then raises a drift it might have allowed, never hides one.
let legacy_bound = {
let others = Decimal::new(split.len().saturating_sub(1) as i64, 0);
let last = others * unit / Decimal::new(2, 0);
if last > unit { last } else { unit }
match crate::kernel::Amount::checked_mul(others, unit)
.and_then(|product| crate::kernel::Amount::checked_div_exact(product, Decimal::TWO))
{
Some(last) if last > unit => last,
_ => unit,
}
};
split
.iter()
.filter(|m| {
let drift = (m.running - m.exact).abs();
// A difference exact arithmetic cannot hold is no drift
// under a unit: raised.
let Some(drift) =
crate::kernel::Amount::checked_sub(m.running, m.exact).map(|d| d.abs())
else {
return true;
};
if trued_up {
drift >= unit
} else {