fix(core): due-today is Current; odd cents split by largest remainder
Max-testing pass, step 2: two spec decisions the owner confirmed (2026-10-13), each test-first. Aging (arap::aging_bucket): a document due today was "1-30 days past due"; it is Current, since past due starts the day after (QuickBooks' convention). Spec in the doc comment. Boundary examples at every edge (due today, 1, 30, 31, 60, 61, 90, 91 days) fail on the old code. A property: the balance lands in exactly one bucket, and time only ages it. Member splits (periods::allocate): the last member absorbed every odd cent, so the same partner always got or lost it. Now largest remainder: each share rounded toward zero to the cent, leftover cents to the largest remainders, ties to the earlier member, a loss the negation of the gain. The core is a new kernel function, kernel::apportion, allocation-free so Kani proves it (n <= 3): apportion_is_exact (it succeeds exactly when the shortfall is under a unit per part; then the parts sum to the whole, each gains 0 or 1 unit, one may take the sub-unit fraction) and apportion_is_fair (never a unit to a part while a larger remainder goes without). Kani found an off-by-one in the first version: a shortfall of n units plus a fraction was accepted. Native regression apportion_refuses_a_shortfall_of_a_unit_per_part_or_more. Properties at Decimal (2,000 cases, up to 5 members): conservation, within a cent of each exact share, fairness, sign symmetry; they fail on the old rule. Bridge: apportion agrees on i64 and Decimal. Contract: closes journaled before this change replay exactly. New member closes journal "rounding": "largest_remainder"; journal lines without it replay as CloseTo::MembersLastAbsorbs, the old rule (periods::allocate_last_absorbs, pinned by closes_journaled_under_the_old_rule_replay_with_it). The members' equity statement isn't journaled and uses the new rule. Open gap: Lean proof of apportion's sum law for any n (needs a permutation lemma on the ranking); Kani covers n <= 3, properties n <= 5. The Lean closing theorems already hold for any split summing to the net. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GWVo7x3UHw5kwCUaWxiejX
This commit is contained in:
1 parent
847dd4388d
commit
e0b6d6988f
11 files changed
+502
-36
No files matched your search
@@ -460,6 +460,89 @@ fn plug_last_sums_exactly() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Most parts the apportionment harnesses explore (its ranking is
|
||||
/// quadratic, so the bound is smaller than `N`).
|
||||
const AP: usize = 3;
|
||||
|
||||
/// Inputs for `kernel::apportion`: rounded-down parts, remainders, a unit,
|
||||
/// and a whole some shortfall above the parts' sum.
|
||||
fn any_apportionment() -> (usize, [i64; AP], [i64; AP], i64, i64, i64) {
|
||||
let n: usize = kani::any();
|
||||
kani::assume(n <= AP);
|
||||
let before: [i64; AP] = core::array::from_fn(|_| any_amount());
|
||||
let rems: [i64; AP] = core::array::from_fn(|_| any_amount());
|
||||
let unit: i64 = kani::any();
|
||||
kani::assume(unit > 0 && unit <= 1000);
|
||||
let shortfall: i64 = kani::any();
|
||||
kani::assume(shortfall.unsigned_abs() <= 4000);
|
||||
let mut whole = shortfall;
|
||||
for &p in &before[..n] {
|
||||
whole += p;
|
||||
}
|
||||
(n, before, rems, unit, shortfall, whole)
|
||||
}
|
||||
|
||||
/// `kernel::apportion` succeeds exactly when the parts are short of the
|
||||
/// whole by less than one unit per part; then the parts sum to the whole,
|
||||
/// and each gains nothing or one unit, except that one part may also take
|
||||
/// the sub-unit fraction.
|
||||
#[kani::proof]
|
||||
#[kani::unwind(5)]
|
||||
fn apportion_is_exact() {
|
||||
let (n, before, rems, unit, shortfall, whole) = any_apportionment();
|
||||
let mut parts = before;
|
||||
let ok = kernel::apportion(whole, &mut parts[..n], &rems[..n], unit);
|
||||
let in_range = if n == 0 {
|
||||
whole == 0
|
||||
} else {
|
||||
shortfall >= 0 && shortfall < n as i64 * unit
|
||||
};
|
||||
assert_eq!(ok.is_some(), in_range);
|
||||
if ok.is_none() {
|
||||
return;
|
||||
}
|
||||
let mut sum = 0i64;
|
||||
let mut fraction_takers = 0;
|
||||
for i in 0..n {
|
||||
sum += parts[i];
|
||||
let gain = parts[i] - before[i];
|
||||
assert!(gain >= 0 && gain < 2 * unit);
|
||||
if gain != 0 && gain != unit {
|
||||
fraction_takers += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(sum, whole);
|
||||
assert!(fraction_takers <= 1);
|
||||
kani::cover!(
|
||||
n == AP && shortfall >= 2 * unit,
|
||||
"three parts, two units short"
|
||||
);
|
||||
}
|
||||
|
||||
/// `kernel::apportion` never gives a unit to a part while a part with a
|
||||
/// strictly larger remainder goes without (owner, 2026-10-13: no member
|
||||
/// favored by position).
|
||||
#[kani::proof]
|
||||
#[kani::unwind(5)]
|
||||
fn apportion_is_fair() {
|
||||
let (n, before, rems, unit, _, whole) = any_apportionment();
|
||||
let mut parts = before;
|
||||
if kernel::apportion(whole, &mut parts[..n], &rems[..n], unit).is_none() {
|
||||
return;
|
||||
}
|
||||
for i in 0..n {
|
||||
for j in 0..n {
|
||||
if rems[i] > rems[j] {
|
||||
assert!(parts[i] - before[i] >= unit || parts[j] - before[j] < unit);
|
||||
}
|
||||
}
|
||||
}
|
||||
kani::cover!(
|
||||
n == AP && rems[0] < rems[2],
|
||||
"a later part with a larger remainder"
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// The checker itself
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user