fix(core): a legacy close's drift before its true-up is the rule's, not one unit

Before a count is trued up, check_split flagged any drift beyond one unit. BS-partner-1 statement 6 allows a legacy last-absorbs close more than that with four or more members, so the audit raised a false finding on legitimate legacy books.

The rule does not state an audit bound; this bound is DERIVED from statement 6's per-member facts, and the derivation is in check_split's spec block: with n members and unit u, each member but the last was rounded to within u/2, and the last absorbed the sum of those roundings, so |e_last| <= (n-1)*u/2. Floored at u (statement 6's allowance for each run break and each per-close close), the pre-true-up bound is B(n) = max(u, (n-1)*u/2): 1.5 units with four members, 2 with five. A drift of exactly B(n) is legitimate and not flagged; past it is. After the true-up, statement 5's strict bound still holds: a drift of a unit or more is flagged.

WB-ledger-16 invariant 8 now states B(n) and marks it derived.

Tests: a_legacy_last_absorbs_close_drifts_by_member_count_before_its_true_up (BS-partner-1 statement 6 before the true-up, statement 5 after it; WB-ledger-16 invariant 8): four- and five-member closes at their largest legitimate drift are not flagged, one unit past it they are, and after the true-up the strict >= unit bound holds.

Co-Authored-By: Muse Wonder 1.0
This commit is contained in:
swaitsandMuse Wonder 1.0 committed 2026-11-09 14:50:38 -07:00
1 parent 5b1596f977
commit 02087ef3fb
3 files changed
+117 -10

No files matched your search

+65 -3
View File
@@ -361,8 +361,13 @@ fn a_jpy_close_is_checked_at_the_allocations_unit() {
let e = l.create_entry(d(1, 5), "sale");
l.add_line(e, cash, Side::Debit, Money::new(dec("101"), Currency::JPY))
.unwrap();
l.add_line(e, revenue, Side::Credit, Money::new(dec("101"), Currency::JPY))
.unwrap();
l.add_line(
e,
revenue,
Side::Credit,
Money::new(dec("101"), Currency::JPY),
)
.unwrap();
l.post_entry(e).unwrap();
let jan = l.create_period("2026-01", d(1, 1), d(1, 31)).unwrap();
let members = caps
@@ -375,7 +380,10 @@ fn a_jpy_close_is_checked_at_the_allocations_unit() {
.collect();
l.close_period_to(jan, &CloseTo::Members(members)).unwrap();
// 101 yen at 50/50 splits 51 / 50 whole yen (exact 50.5 each).
let balances: Vec<Decimal> = caps.iter().map(|&a| l.account_balance(a).unwrap()).collect();
let balances: Vec<Decimal> = caps
.iter()
.map(|&a| l.account_balance(a).unwrap())
.collect();
assert_eq!(balances, [dec("51"), dec("50")]);
let f = run(&l, &Settings::default(), &all_receipts(&l), d(12, 31));
assert!(
@@ -385,6 +393,60 @@ fn a_jpy_close_is_checked_at_the_allocations_unit() {
);
}
/// BS-partner-1 statement 6 and WB-ledger-16 invariant 8: before the
/// true-up, a legacy last-absorbs close's largest legitimate drift is
/// derived (the rule states the per-member facts, not the bound): with
/// n members and unit u, each member but the last was rounded to within
/// u/2, and the last absorbed the sum of those roundings, up to
/// (n-1)*u/2 -- so 3u/2 with four members and 2u with five (floored at
/// u, which statement 6's per-close and run allowances give). At that
/// drift a close is not flagged; one unit past it, it is. After the
/// true-up, BS-partner-1 statement 5's strict bound holds instead: a
/// drift of a unit or more is flagged.
#[test]
fn a_legacy_last_absorbs_close_drifts_by_member_count_before_its_true_up() {
let unit = dec("0.01");
// A split of `n` members whose last-listed member carries `drift`
// (running against an exact 1.00) and whose others are exact.
let split = |n: usize, drift: &str| -> Vec<MemberSplit> {
(0..n)
.map(|i| {
let running = if i + 1 == n {
dec("1.00") + dec(drift)
} else {
dec("1.00")
};
MemberSplit {
capital: 3001 + i as u64,
share: dec("0.25"),
part: running,
running,
exact: dec("1.00"),
}
})
.collect()
};
// Four members: largest legitimate drift 3 * 0.005 = 0.015, either way.
assert!(audit::check_split(1, "Jan", &split(4, "0.015"), unit, false).is_empty());
assert!(audit::check_split(1, "Jan", &split(4, "-0.015"), unit, false).is_empty());
// One unit past it (0.025) is flagged, exactly one member.
let f = audit::check_split(1, "Jan", &split(4, "0.025"), unit, false);
assert_eq!(codes(&f), ["close_split_drifts"]);
// Five members: largest legitimate drift 4 * 0.005 = 0.02.
assert!(audit::check_split(1, "Jan", &split(5, "0.02"), unit, false).is_empty());
assert!(audit::check_split(1, "Jan", &split(5, "-0.02"), unit, false).is_empty());
let f = audit::check_split(1, "Jan", &split(5, "0.03"), unit, false);
assert_eq!(codes(&f), ["close_split_drifts"]);
// After the true-up the same splits answer to statement 5: the
// four-member split at its legacy bound is past one unit, so it is
// flagged, and one just under a unit is not.
let f = audit::check_split(1, "Jan", &split(4, "0.015"), unit, true);
assert_eq!(codes(&f), ["close_split_drifts"]);
assert!(audit::check_split(1, "Jan", &split(4, "0.009"), unit, true).is_empty());
let f = audit::check_split(1, "Jan", &split(4, "0.01"), unit, true);
assert_eq!(codes(&f), ["close_split_drifts"]);
}
/// BS-partner-1 statement 6 and 8 (WB-partner-1): a count is trued up by
/// the first close under the cumulative rule that posts an entry, not by
/// a close of an older rule or one that posts nothing; the books say so