fix(core): foreign amounts and rates take at most 10 places (D-75)

The K-U1 review: foreign 0.99999999999999999999999999 (26 places) x rate
0.005 into USD booked 0.01, though the exact product
0.00499999999999999999999999995 is under half a cent. Decimal::checked_mul
rounds a product finer than 28 places to 28 before fx_booked rounds it to
the cent: two roundings, one cent off. A ledger in one money books the
exact product rounded once (BS-ledger-1, BS-ledger-14).

Owner decision D-75 (2026-11-08): a new line's foreign amount and its rate
take at most MAX_LINE_SCALE (10) decimal places each, as a base amount does
(B-28), so their product has at most 20 places, under Decimal's 28.
Ledger::add_line_fx refuses past it with ValueOutOfRange ("foreign amount
..." / "FX rate ..."; the variant for an amount that is not a journal
line's), trailing zeros not counted, before computing, and never on replay
(is_replaying; replay_add_line_fx books a journal's older line verbatim).

Tests: 11-place foreign amount and rate refused; ten and ten accepted with
hand-checked books; the review's input refused, not mis-booked; trailing
zeros are not places; replay keeps a 26-place foreign line in core, and a
store journal holding one opens with its figures.

fx_booked's spec block states the precondition (scales within 10 each, and
the mantissas' product within Decimal's 96 bits) and that the result is
exact under it, and documents Decimal::checked_mul's behaviours outside it
(rounding past scale 28 and past 96 bits; 1e-28 x 1e-28 underflowing to
zero); invariant 1 now says None when T's checked_mul cannot hold the
product. The bridge covers every D-75 scale (0..=10 each, products to 20
places) against an independent exact reference, shows 26 places is where the
reference and Decimal part (and the engine refuses), and pins an open edge
this bridge found: ten places each still allow a 20-place product past
~7.9e8 that outgrows 96 bits (1050000010.0000000001 x 9.9999999999 books
10500000099.90, exact 10500000099.89). B-28 and B-3 claims record the
enforcement and checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-08 18:08:12 -07:00
1 parent bf2fe200c2
commit c5a4894985
7 files changed
+394 -33

No files matched your search

+28 -1
View File
@@ -1831,15 +1831,26 @@ impl Ledger {
/// GOAL: book a foreign-currency amount in the ledger's one money.
/// SOURCE: docs/spec/rules.md B-3 (Pacioli ch. 36: one money; ASC
/// 830-20-30-1: at the transaction-date rate); minor units: ISO
/// 4217; rounding: DECISION (owner, 2026-10-13).
/// 4217; rounding: DECISION (owner, 2026-10-13); the ten-place
/// bound (invariant 3): DECISION D-75 (owner, 2026-11-08),
/// B-28's bound, stated in bookspec BS-entry-4.
/// INVARIANTS:
/// 1. The base amount MUST be a whole number of the base currency's
/// smallest unit, within half a unit of foreign * rate.
/// 2. NEVER convert into the base currency itself.
/// 3. MUST refuse a foreign amount or a rate of more than ten
/// decimal places (ValueOutOfRange), so the product keeps its
/// scale (within Decimal's 28) and is rounded once; NEVER on
/// replay.
/// SCENARIOS:
/// 1. IF 100.00 EUR at 1.0834567 into USD: SHOULD book 108.35.
/// 2. IF 10.00 USD at 151.237 into JPY: SHOULD book 1512.
/// 3. IF the rounded amount is zero: SHOULD refuse.
/// 4. IF the foreign amount is 0.99999999999999999999999999 at
/// 0.005 into USD: SHOULD refuse (more than ten places)
/// BECAUSE Decimal rounds that product to 28 places before the
/// cent rounding, booking a cent the exact product does not
/// reach (found by the K-U1 review).
/// ```
pub fn add_line_fx(
&mut self,
@@ -1864,6 +1875,22 @@ impl Ledger {
found: foreign_currency.code().to_string(),
});
}
// D-75 (owner, 2026-11-08): a new line's foreign amount and its
// rate take at most MAX_LINE_SCALE places each, as a base amount
// does (B-28), so their product has at most twice that many (20,
// under Decimal's 28) and keeps its scale: the booked amount is
// rounded once, not after Decimal's own rounding of the product.
// (A 20-place product past about 7.9e8 still outgrows Decimal's 96
// bits; kernel::fx_booked's spec block names that open edge.)
// The command's bound, never replay's: a journal's older line with
// a finer foreign amount opens as it was booked (replay_add_line_fx).
if !crate::parties::is_replaying() {
for (what, value) in [("foreign amount", foreign_amount), ("FX rate", fx_rate)] {
if value.normalize().scale() > kernel::MAX_LINE_SCALE {
return Err(LedgerError::ValueOutOfRange(format!("{what} {value}")));
}
}
}
// Booked in the base currency's smallest unit (cents for USD), half
// away from zero: a ledger in one money holds no fractions of its
// smallest unit (rules.md B-3; DECISION, owner 2026-10-13, after