refactor(core): the control, aging and reconciliation identities in the kernel

Three identities were still hand-written chains at their call sites:
an aging row's five buckets summing to what is owed (BS-arap-1), the
control check's difference, balance less documents less unattached
lines (BS-arap-2), and the K-1 reconciliation total, beginning plus
built-in gain plus property without a basis less property distributed
(BS-partner-4). They move to kernel::aging_total, control_difference
and reconciliation_total, generic over Amount, returning None on
overflow, so each is a named law with a harness and a Lean theorem
rather than a sum.

Behaviour is identical: the same checked steps in the same order, so
figures, scales and TooLargeToCompute refusals are unchanged; every
golden passes untouched. The next commit's bridge compares the old
chains and the kernel as strings on 20,000 random Decimals of any scale.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-09 11:48:10 -07:00
1 parent e45c62348a
commit 0df463b703
4 files changed
+132 -16

No files matched your search

+8 -4
View File
@@ -1355,10 +1355,14 @@ pub struct AgingRow {
impl AgingRow {
/// Total outstanding; refused past exact arithmetic.
pub fn total(&self) -> Result<Decimal, LedgerError> {
[self.days_30, self.days_60, self.days_90, self.days_90_plus]
.into_iter()
.try_fold(self.current, Decimal::checked_add)
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("{}'s aging total", self.name)))
crate::kernel::aging_total(
self.current,
self.days_30,
self.days_60,
self.days_90,
self.days_90_plus,
)
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("{}'s aging total", self.name)))
}
/// Add `other`'s buckets to this row's; refused past exact arithmetic.
+115 -4
View File
@@ -1,5 +1,116 @@
//! U8: control and aging, what is due or overdue (kernel sweep).
//! U8: control, aging and reconciliation totals (kernel sweep).
//!
//! Empty in U0: the unit adds its arithmetic here, in this file alone, so
//! no two sweep units ever edit the same file. The module root re-exports
//! it, keeping every path `crate::kernel::foo`.
//! Three identities the books state by name, each a short chain of exact
//! additions and subtractions: an aging row's five buckets sum to what is
//! owed (BS-arap-1), a control account's balance less its documents and its
//! unattached lines is its difference, which is always zero (BS-arap-2), and
//! a member's book capital less tax capital is their reconciliation total
//! (BS-partner-4). Naming them here puts them under proof rather than
//! leaving each a hand-written sum at its call site. The sums reuse
//! [`super::total`]; the chains of differences are checked one step at a
//! time, so none of them can panic or round.
use super::{Amount, total};
/// What an aging row owes: its five buckets, current, 1-30, 31-60, 61-90
/// and over 90 days, added exactly.
///
/// ```text
/// GOAL: an aging row's total is exactly the sum of its five buckets, so
/// the buckets partition what is owed and no bucket is lost or
/// counted twice in the total.
/// SOURCE: BS-arap-1 (an aging puts each open balance in exactly one
/// bucket, so the buckets sum to the open balances); BS-entry-3,
/// BS-entry-4 (exact, no panic).
/// INVARIANTS:
/// 1. Some(t): t = current + b30 + b60 + b90 + b90p, exactly.
/// 2. The order of the buckets does not change a total that exists
/// for in-range figures; aging_total of five zeros is Some(0).
/// 3. None when a running sum cannot be represented; NEVER a rounded or
/// wrapped figure, never a panic.
/// SCENARIOS:
/// 1. IF a customer owes 100.00 current and 40.00 over 90 days past due:
/// - SHOULD total 140.00
/// BECAUSE every bucket counts once.
/// 2. IF the buckets are Decimal::MAX and 1 in any two places:
/// - SHOULD be None
/// BECAUSE the sum is not representable.
/// ```
pub fn aging_total<T: Amount>(current: T, b30: T, b60: T, b90: T, b90p: T) -> Option<T> {
total([current, b30, b60, b90, b90p])
}
/// A control account's difference: its balance less what its documents
/// still owe on it, less its unattached lines.
///
/// ```text
/// GOAL: the control check's difference is the balance less the documents
/// and the unattached lines, exactly; it is zero whenever the
/// balance is those two sums, which it always is, as both sides sum
/// the same lines.
/// SOURCE: BS-arap-2 (a control account's balance equals the sum of its
/// documents plus its unattached lines); BS-ledger-8; BS-entry-4.
/// INVARIANTS:
/// 1. Some(d): d = balance - documents - unattached, exactly.
/// 2. control_difference(documents + unattached, documents, unattached)
/// = Some(0), and conversely Some(0) means balance = documents +
/// unattached: the difference vanishes exactly when the account
/// agrees with its subledger.
/// 3. None when a step cannot be represented; NEVER a rounded or
/// wrapped figure, never a panic.
/// SCENARIOS:
/// 1. IF the balance is 150.00, the documents 100.00 and one unattached
/// line 50.00:
/// - SHOULD be 0
/// BECAUSE 150.00 = 100.00 + 50.00.
/// 2. IF the balance is 100.00 and the documents 90.00 with nothing
/// unattached:
/// - SHOULD be 10.00
/// BECAUSE the account holds 10.00 no document explains.
/// ```
pub fn control_difference<T: Amount>(balance: T, documents: T, unattached: T) -> Option<T> {
balance
.checked_sub(documents)
.and_then(|d| d.checked_sub(unattached))
}
/// A member's book capital less their tax capital, by cause: the
/// beginning difference, plus the built-in gain, plus property without a
/// basis, less property distributed (all at book).
///
/// ```text
/// GOAL: the reconciliation total is exactly book ending less tax ending
/// as far as the books know it: what contributing and distributing
/// property does to the two capitals is the only way they part.
/// SOURCE: BS-partner-4 (the book-to-tax capital reconciliation: book
/// ending less tax ending = beginning (book less tax) + built-in
/// gain share + property without a basis (at book) - property
/// distributed (at book)); BS-asset-4 (the section 704(c)
/// built-in gain); BS-entry-4.
/// INVARIANTS:
/// 1. Some(t): t = beginning + built_in_gain + without_basis -
/// distributed, exactly.
/// 2. For any book and tax capital rows that differ only by those
/// causes (tax contributions = book contributions less the gain and
/// the property without a basis; tax distributions = book
/// distributions less what was distributed), book ending less tax
/// ending = reconciliation_total (Lean: Bookspec.Reconcile).
/// 3. None when a step cannot be represented; NEVER a rounded or
/// wrapped figure, never a panic.
/// SCENARIOS:
/// 1. IF two members jointly contribute a laptop worth 1,440.00 whose
/// basis is 1,688.40 (no other activity):
/// - SHOULD give each a total of -124.20
/// BECAUSE book property is 720.00 and tax 844.20.
/// ```
pub fn reconciliation_total<T: Amount>(
beginning: T,
built_in_gain: T,
without_basis: T,
distributed: T,
) -> Option<T> {
beginning
.checked_add(built_in_gain)?
.checked_add(without_basis)?
.checked_sub(distributed)
}
+3 -4
View File
@@ -7494,10 +7494,9 @@ impl Ledger {
}
}
let documents_total = doc_totals.get(&account).copied().unwrap_or_default();
let difference = balance
.checked_sub(documents_total)
.and_then(|d| d.checked_sub(unattached_sum))
.ok_or_else(too_large)?;
let difference =
crate::kernel::control_difference(balance, documents_total, unattached_sum)
.ok_or_else(too_large)?;
out.push(arap::ControlCheck {
role: role.key.to_string(),
account,
+6 -4
View File
@@ -274,10 +274,12 @@ pub struct Reconciliation {
impl Reconciliation {
/// Book ending less tax ending (as far as known).
pub fn total(&self) -> Option<Decimal> {
self.beginning
.checked_add(self.built_in_gain_704c)?
.checked_add(self.property_without_basis)?
.checked_sub(self.property_distributed)
crate::kernel::reconciliation_total(
self.beginning,
self.built_in_gain_704c,
self.property_without_basis,
self.property_distributed,
)
}
}