# K-U11: no Decimal arithmetic outside the kernel (ledger-core) Bar item 1 of `AGENTS.md` says money arithmetic lives in `kernel/` and nowhere else. Until this unit nothing checked it, and it was not true. ## What was measured `scripts/raw-decimal-arith.sh ` asks the compiler. It runs clippy twice over the package's own source (tests and proof harnesses are not compiled; `src/kernel/` is where arithmetic belongs), once flagging every arithmetic operator and once with `Decimal` allowed, and prints the places that drop out: those are the `Decimal` ones. It also prints every call to one of rust_decimal's own arithmetic or rounding methods (`checked_add`, `checked_mul`, `round_dp`, `rescale`, ...) and every `Iterator::sum` or `product`. A call such as `a.checked_add(b)` on a `Decimal` is rust_decimal's method even with the kernel's `Amount` in scope, since an inherent method wins over a trait's; reading the source does not show which one runs, and the compiler does. At 8ad55e82a it listed **210 places in ledger-core**. Two kinds of fault sat behind them: - a raw operator (`+`, `-`, `*`, unary `-`) panics when the result does not fit; - rust_decimal's operators and its `checked_*` methods both return a rounded answer when the exact one needs more than 96 bits, and say nothing (K-U10, `docs/proofs/k-u10.md`). ## What was done | Sites | Now | |---|---| | 199 | the kernel's `Amount` methods by their full path, `kernel::total` and `total_of` for hand-rolled sums, `kernel::normal_balance`, and `money.rs`'s helpers over the kernel: `neg`, `signed_amount`, `on_side` (a line's sign, said once), `add_in_range` and `sub_in_range` (sums of posted lines, exact by BS-entry-3 and BS-entry-4), `add_exact` and `sub_exact` (anything a caller can make too large: a `TooLargeToCompute`) | | 2 | `Money::checked_add` and `checked_sub`: exact or `TooLargeToCompute` (K-U10a) | | 3 | the frozen close rules (`periods::try_allocate`, `try_allocate_last_absorbs`) and `Ledger::member_close_split`: `Amount::checked_mul_rounded`, the product they have always computed, by name. Posted closes replay with the first two, so not a digit of them may move | | 2 | the audit's check of a close (`audit::check_split`): the bound and the drift through `Amount`. A bound or a drift exact arithmetic cannot hold is raised as a finding, never passed | | 4 | `kernel::rescale_exact`, new: see below | Two sites among the 199 were then moved again, on evidence (see "What exact arithmetic refused"): the K-1 worksheet's running total adds and subtracts through `Amount::checked_add_rounded` and `checked_sub_rounded`. The first 199 were rewritten by a script from the compiler's own line and column for each site, so no site was copied by hand. The ones that needed a decision are in that commit's message (a balance sheet that cannot add liabilities and equity is not balanced; a template's shares, a party's percents and a reconciliation's balances refuse where they panicked). No figure moves: every site computes what it computed. What changes is the edge: a result past exact arithmetic is refused where it panicked or was rounded. ## What exact arithmetic refused The script ran before K-U10 landed, when `Amount::checked_add` on a `Decimal` was rust_decimal's rounding sum. K-U10 made it exact or `None`, and after the merge one test failed: `tax_capital::tests::thirds_summed_are_the_cents_they_are`, with `TooLargeToCompute("a member's tax-basis capital")`. The cause is not a wrong figure. A member's "exact" running total in the K-1 worksheet (`tax_capital::RunningSplit`) is a sum of shares, each `whole x weight / sum`, a quotient of 28 significant digits. Two thirds of 28 digits each do not add to a number 96 bits hold, so the exact sum refuses every split by thirds. rust_decimal's sum rounded it at the last digit, and INVARIANT 4 of that type was written for exactly that: the total is snapped to ten places before a cent is taken from it. A sum of rounded quotients is a rounded figure by construction, as a share's product is (K-U10, `checked_mul_rounded`). So the kernel names that sum too: `Amount::checked_add_rounded` and `checked_sub_rounded`, rust_decimal's own for `Decimal` and the plain operation for a machine integer, called by `RunningSplit::split` and `unsplit` and by nothing else. The bridge property `the_rounding_sum_is_the_exact_one_wherever_that_exists` (`tests/kernel_bridge/decimal_exact.rs`) checks, over `Decimal`'s whole range: it is rust_decimal's figure to the digit and the scale, it is the exact figure wherever exact arithmetic gives one, and where it rounds it is within half a unit of the last place it keeps. Every other place that makes a figure of more than ten places outside the kernel was then read for the same fault, since a test only catches the ones it reaches: | Place | What happens to the long figure next | |---|---| | `form1099::vendor_part` (`share_of`) | rounded to the cent on the next line | | `form1099::split_share` (`share_floor_with`) | floors and remainders stay inside the kernel (`apportion`) | | `settings::parse_share` (a percent over 100) | summed by `kernel::total_of`; shares that sum to one never pass 10^28 at 28 places, so a valid set is never refused, and one that does not sum to one was refused before | | `reports::contribution_margin_pct` (`percent_of`) | already rounded to one place by the kernel | | `Ledger::member_close_split` (`checked_mul_rounded`) | read only by the audit, whose difference is raised as a finding when exact arithmetic cannot hold it | | `tax_capital::RunningSplit` (`share_of`) | the case above | ## `kernel::rescale_exact` Four places changed how many decimal places a figure is written with: a contribution margin always prints one place ("100.0"), a schedule's months print at the currency's places (the kernel returns 0.250 as it came, the plan has always shown 0.25, and a posted month is compared as text on replay), and `Money`'s step count prints the minor places. Each called `Decimal::rescale`, which rounds half away from zero when asked for fewer places than the value has. None of the four ever asked for that, and a debug assertion said so; nothing stopped the next one. `rescale_exact(amount, places)` adds or drops trailing zeros and returns `None` for anything else. It is the only place outside the kernel's own rounding that writes a scale. It has no Kani harness and no Lean theorem, and is the sixteenth entry in the traceability gate's exception list, for this reason: whole-number money has no places, so there is nothing for an `i64` harness or a law over the rationals to state. What carries it instead: | Bridge property (`tests/kernel_bridge/round.rs`) | Checks | |---|---| | `rescale_exact_never_changes_the_value` | over Decimal's whole range (96-bit mantissas, every scale and every target from 0 to 28), against whole-number arithmetic that shares no code with it: `None` exactly when the amount is no whole multiple of 10^-places; a result is the same number with the same sign, at `places` places or, where 96 bits cannot hold it that wide, as many as they can | | `rescale_exact_at_the_edges` | the figures the four call sites print, a negative zero (it keeps its sign and takes the places), the refusals, and the largest mantissa | ## The gate `just kernel-only` runs the detector over the crates named in the justfile's `kernel_only_crates`, and the lint part of `just check` runs it, so a new raw operator or rust_decimal method on a `Decimal` outside the kernel fails the build. The list is ledger-core and the four outer crates that were already at zero (ledger-cli, ledger-host, ledger-reader, ledger-vault); the other six join it as K-U12 brings each to zero, and a crate never leaves it. A waiver is an `#[allow(clippy::arithmetic_side_effects)]` or `#[allow(clippy::disallowed_methods)]` with a `reason`, and the detector counts them on its last line so one cannot slip in unseen. There are none in ledger-core. ## What this does not show - That each site calls the *right* kernel function. The detector shows that arithmetic goes through the kernel, not that a sum or product which should round was not made exact (a refusal where there was a figure) or the reverse. The engine's tests and the properties check the figures, and they found one (above). Every multiplication and division outside the kernel was read for this unit (eleven, listed by `grep -n "Amount::checked_mul\|Amount::checked_div"`), and every place a long figure is made (the table above). A sum of figures a caller supplies with more than ten places each (settings, a template's shares) is refused where exact arithmetic cannot hold it; that is K-U10's rule, not a regression, and no such sum is rounded later. - The K-1 worksheet's running split is still a prelude outside the kernel (`plan/2026-11-09_TRIAGE.md`, P2.7): its rounding sum has a bridge property, and the split built on it has no Kani harness or Lean theorem yet. - `Decimal::abs`, `normalize` and comparisons are not arithmetic and are not flagged: none can round or overflow. - Integer arithmetic on counts, indexes and step counts (`i128` steps in `Steps`, day counts) is outside this detector.