refactor(core): proportional shares in the kernel, one copy

The K-1/1065 worksheet, the 1099 worksheet, the parties' shares, the
conventions' template split and the cash-flow government split each
computed "whole x weight / total, round, hand out the leftover" for
themselves. Six copies meant six places for a rounding or sign change to
reach a filed figure.

Put the shape in kernel::allocate: share_of (the engine's division,
rounded at Decimal's 28 significant digits, truncating on a machine
integer), share_floor_with (the exact floor to a step by multiplication,
the 1099 correction loop's arithmetic), and split_by_weights_with (the
split, with the leftover policy named: largest remainder, first absorbs,
last absorbs; the running policy stays apportion_cumulative). Each site
keeps its own mode, step, sign and leftover policy, so every figure is
the same to the digit; the frozen periods rules only gain a doc line
naming the kernel policy they equal.

Amount gains checked_div, the engine's division as distinct from
checked_div_exact.

All goldens and existing tests pass unchanged.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
swaitsandDeepSeek V4.1 Flash committed 2026-11-09 00:14:26 -07:00
1 parent 127d33fb0b
commit 24b59e7b85
9 files changed
+394 -84

No files matched your search

+12 -13
View File
@@ -284,23 +284,22 @@ fn checked_sum(values: impl IntoIterator<Item = Decimal>) -> Option<Decimal> {
/// part rounded toward zero to the cent, the cents left over to the first.
/// None when a product or sum is past exact arithmetic.
fn split_first(magnitude: Decimal, weights: &[Decimal]) -> Option<Vec<Decimal>> {
use crate::kernel::{Rounding, round_places};
use crate::kernel::{Leftover, Rounding, round_places, split_by_weights_with};
let total = checked_sum(weights.iter().copied())?;
if weights.is_empty() || total.is_zero() {
return Some(vec![Decimal::ZERO; weights.len()]);
}
let mut parts: Vec<Decimal> = weights
.iter()
.map(|w| {
magnitude
.checked_mul(*w)
.and_then(|x| x.checked_div(total))
.and_then(|x| round_places(x, 2, Rounding::TowardZero))
})
.collect::<Option<_>>()?;
let left = magnitude.checked_sub(checked_sum(parts.iter().copied())?)?;
parts[0] = parts[0].checked_add(left)?;
Some(parts)
// magnitude × weight ÷ total, each part truncated toward zero to the
// cent, the cents left over to the first (the legacy policy this
// split keeps). The kernel is the one copy of the shape.
split_by_weights_with(
magnitude,
weights,
total,
Decimal::new(1, 2),
|x| round_places(x, 2, Rounding::TowardZero),
Leftover::FirstAbsorbs,
)
}
/// What an entry's shares split, and what of it counts (the module's