diff --git a/crates/ledger-core/src/kernel/fx.rs b/crates/ledger-core/src/kernel/fx.rs index 6681c51f..078238fc 100644 --- a/crates/ledger-core/src/kernel/fx.rs +++ b/crates/ledger-core/src/kernel/fx.rs @@ -16,10 +16,34 @@ use rust_decimal::Decimal; /// /// The rate's precision is carried by the value itself: a `Decimal` /// rate keeps its own scale (1.0834567 is exact), and the product is the -/// exact `Decimal` product before the single rounding, exactly as -/// `Ledger::add_line_fx` computed it before this kernel existed. At -/// `i64` (the Kani model) amounts and the rate are plain integers in -/// the smallest unit, and the same law is proved of the product. +/// `T::checked_mul` product before the single rounding. At `i64` (the +/// Kani model) amounts and the rate are plain integers in the smallest +/// unit, and the same law is proved of the product. +/// +/// **The precondition that makes the product exact (D-75).** On +/// `Decimal`, `checked_mul` is exact only while the product keeps its +/// full scale: that scale (the two scales added) must be within 28, and +/// the product's mantissa (the two mantissas multiplied) must fit +/// `Decimal`'s 96 bits, under 7.9e28. A foreign amount and a rate of at +/// most [`MAX_LINE_SCALE`](super::MAX_LINE_SCALE) (10) places each, +/// trailing zeros not counted, meet the first half: a product of at most +/// 20 places, under 28. `Ledger::add_line_fx` enforces that bound on +/// every new line (never on replay). The second half is not enforced: +/// a product of 20 places past about 7.9e8 needs more than 96 bits, and +/// `Decimal` then drops places (see below). Under both halves the +/// product is the exact one and the result is rounded exactly once. +/// Outside them `Decimal::checked_mul` keeps the behaviours it has +/// always had, which this function inherits and does not hide: a +/// product that does not fit is rounded by `Decimal` (to 28 places, or +/// to the places 96 bits hold) before this function rounds it (a second +/// rounding, so a result can sit a unit off the exact product's: +/// 0.99999999999999999999999999 x 0.005 is 0.00499999999999999999999999995 +/// exactly but 0.005 as held, and books 0.01; 1050000010.0000000001 x +/// 9.9999999999 is 10500000099.89 to the cent but 10500000099.895 as +/// held, and books 10500000099.90), and a product below 1e-28 (1e-28 x +/// 1e-28) underflows to zero. Invariants 2 and 3 are stated for +/// products that are exact; invariant 1 (a whole multiple of the step) +/// holds regardless. The bridge pins both behaviours. /// /// ```text /// GOAL: book a foreign amount in the ledger's one money: the exact @@ -36,15 +60,18 @@ use rust_decimal::Decimal; /// bookspec's Lean). /// INVARIANTS: /// 1. Some(r) is a whole multiple of step: r = n x step for an -/// integer n. None when step <= 0, when rate <= 0, or when the -/// exact product or the rounded result cannot be held. -/// 2. In HalfAwayFromZero (the mode add_line_fx books in) r is -/// within half a step of the exact product foreign x rate, an -/// exact half going to the multiple farther from zero. -/// 3. Positive foreign and rate give a non-negative r, and a -/// positive r whenever the exact product reaches half a step -/// (a smaller product rounds to zero, which the caller refuses -/// as it does today). The sign never crosses zero. +/// integer n. None when step <= 0, when rate <= 0, or when T's +/// checked_mul cannot hold the product (the exact product, under +/// the precondition above) or the rounded result cannot be held. +/// 2. Under the precondition (both halves: the product is exact), in +/// HalfAwayFromZero (the mode add_line_fx books in) r is within +/// half a step of the exact product foreign x rate, an exact half +/// going to the multiple farther from zero. +/// 3. Under the precondition, positive foreign and rate give a +/// non-negative r, and a positive r whenever the exact product +/// reaches half a step (a smaller product rounds to zero, which +/// the caller refuses as it does today). The sign never crosses +/// zero. /// 4. On Decimal the value equals the pre-kernel computation /// (checked_mul, then round_minor half away from zero) exactly; /// fx_booked_minor also keeps its scale. @@ -53,6 +80,16 @@ use rust_decimal::Decimal; /// 2. IF 10.00 at 151.237 to the yen (step 1): SHOULD book 1512. /// 3. IF the rate is zero or negative: SHOULD be None, never a guess. /// 4. IF foreign x rate overflows: SHOULD be None, never a panic. +/// 5. IF foreign is 0.99999999999999999999999999 (26 places) and +/// the rate 0.005 to the cent: outside the precondition, books +/// 0.01 where the exact product is under half a cent BECAUSE +/// Decimal rounded the product first; the engine refuses such an +/// input (D-75), and the bridge pins this behaviour. +/// 6. IF foreign 1050000010.0000000001 at 9.9999999999 to the cent +/// (ten places each, a product past 96 bits): outside the +/// precondition's second half, books 10500000099.90 where the +/// exact product rounds to 10500000099.89; the bridge pins this +/// open edge. /// ``` pub fn fx_booked(foreign: T, rate: T, step: T, mode: Rounding) -> Option { if rate <= T::ZERO { diff --git a/crates/ledger-core/src/ledger.rs b/crates/ledger-core/src/ledger.rs index 8fc03d64..58aae056 100644 --- a/crates/ledger-core/src/ledger.rs +++ b/crates/ledger-core/src/ledger.rs @@ -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 diff --git a/crates/ledger-core/tests/kernel_bridge/fx.rs b/crates/ledger-core/tests/kernel_bridge/fx.rs index 819bbcb0..80eb609e 100644 --- a/crates/ledger-core/tests/kernel_bridge/fx.rs +++ b/crates/ledger-core/tests/kernel_bridge/fx.rs @@ -5,13 +5,21 @@ //! check that both instantiations agree, that `Decimal` agrees with an //! independent exact rational reference (integer mantissas over powers //! of ten, sharing no code with the kernel) for foreign amounts and -//! rates of up to ten places into base currencies of 0, 2 and 3 minor -//! units (JPY, USD, KWD), and the edge table: `Decimal::MAX`, scale 28, +//! rates of every scale D-75 allows, 0 to 10 places each (so products of +//! up to 20 places), into base currencies of 0, 2 and 3 minor units +//! (JPY, USD, KWD), and the edge table: `Decimal::MAX`, scale 28, //! half-unit ties of both signs, and a product that overflows (None, //! never a panic). +//! +//! D-75 is what keeps the reference and the engine equal: past ten +//! places, `Decimal` rounds the product to 28 places before the kernel +//! rounds it to the minor unit, and the two disagree +//! (`past_ten_places_the_reference_and_decimal_part_and_the_engine_refuses`). +//! The scale is half of the exactness condition; the other half is that +//! the mantissas' product fits `Decimal`'s 96 bits (`the_mantissa_edge_*`). -use ledger_core::Currency; use ledger_core::kernel::{Rounding, fx_booked, fx_booked_minor}; +use ledger_core::{AccountType, Currency, Ledger, LedgerError, Side}; use proptest::prelude::*; use rust_decimal::Decimal; @@ -39,8 +47,24 @@ fn reference(fm: i128, fsc: u32, rm: i128, rsc: u32, places: u32) -> Option Currency { @@ -75,22 +99,26 @@ proptest! { ); } - /// The exact reference above, on foreign amounts and rates of up - /// to ten places, booked into 0, 2 and 3 minor units: the - /// `Decimal` kernel is the exact product rounded once, both - /// through a bare step and through the currency wrapper. + /// The exact reference above, on foreign amounts and rates of every + /// scale D-75 allows (0 to 10 places each, so products to 20 + /// places), booked into 0, 2 and 3 minor units: the `Decimal` + /// kernel is the exact product rounded once, both through a bare + /// step and through the currency wrapper. The mantissas stay under + /// 2^47 so their product fits `Decimal`'s 96 bits (the other half of + /// exactness, pinned below). #[test] fn decimal_matches_the_exact_reference( - fm in -(1i128 << 40)..=(1i128 << 40), + fm in -(1i128 << 47)..=(1i128 << 47), fsc in 0u32..=10, - rm in 1i128..=(1i128 << 40), + rm in 1i128..=(1i128 << 47), rsc in 0u32..=10, places in prop_oneof![Just(0u32), Just(2u32), Just(3u32)], ) { let foreign = Decimal::try_from_i128_with_scale(fm, fsc).expect("in range"); let rate = Decimal::try_from_i128_with_scale(rm, rsc).expect("in range"); let want = reference(fm, fsc, rm, rsc, places); - prop_assert!(want.is_some(), "the reference fits for these ranges"); + // A result `Decimal` cannot hold (a count past 96 bits) is None in + // both: never a panic, never a wrapped amount. let step = Decimal::try_from_i128_with_scale(1, places).unwrap(); prop_assert_eq!( fx_booked(foreign, rate, step, Rounding::HalfAwayFromZero), @@ -251,3 +279,130 @@ fn fx_booked_at_the_edges() { Some(parse("1.851851835")) ); } + +/// A fresh USD ledger with an entry to add lines to. +fn usd_books() -> (Ledger, ledger_core::AccountId, ledger_core::EntryId) { + let mut ledger = Ledger::new(Currency::USD); + let cash = ledger + .add_account("1000", "Cash", AccountType::Asset, None, false) + .unwrap(); + let entry = ledger.create_entry(chrono::NaiveDate::from_ymd_opt(2026, 1, 1).unwrap(), "fx"); + (ledger, cash, entry) +} + +/// The top of D-75's range: ten places each, a product of twenty places, +/// every digit of it kept: the exact product, rounded once. +#[test] +fn ten_places_each_is_exact_through_the_engine() { + let (mut ledger, cash, entry) = usd_books(); + // 0.9999999999 x 0.0050000001 = 0.00499999999500000001...: let the + // exact product be 0.00500000009...; a hand-checked row each side of + // half a cent. 0.0000000001 x 0.0000000001 = 1e-20 books nothing. + for (foreign, rate, want) in [ + // 0.4999999999 x 0.01 = 0.004999999999: under half a cent. + ("0.4999999999", "0.0100000000", None), + // 0.5000000001 x 0.01 = 0.005000000001: over half a cent. + ("0.5000000001", "0.0100000000", Some("0.01")), + // 0.9999999999 x 0.5000000001 = 0.50000000004999999999: 0.50. + ("0.9999999999", "0.5000000001", Some("0.50")), + // 99999.9999999999 x 99999.9999999999 = 9999999999.99998...: 10000000000.00. + ( + "99999.9999999999", + "99999.9999999999", + Some("10000000000.00"), + ), + ] { + let (f, r) = (parse(foreign), parse(rate)); + let booked = ledger.add_line_fx(entry, cash, Side::Debit, Currency::EUR, f, r); + match want { + None => assert!(booked.is_err(), "{foreign} x {rate} books nothing"), + Some(want) => { + booked.unwrap_or_else(|e| panic!("{foreign} x {rate}: {e}")); + let lines = ledger.get_entry(entry).unwrap().lines(); + assert_eq!(lines.last().unwrap().amount.amount, parse(want)); + } + } + let (fm, fsc) = (f.mantissa(), f.scale()); + let (rm, rsc) = (r.mantissa(), r.scale()); + assert_eq!( + fx_booked_minor(f, r, Currency::USD, Rounding::HalfAwayFromZero), + reference(fm, fsc, rm, rsc, 2), + "{foreign} x {rate}: kernel equals the exact reference" + ); + } +} + +/// D-75's bound is what keeps the reference and the engine equal: the +/// K-U1 review's input, 0.99999999999999999999999999 (26 places) x 0.005, +/// is 0.00499999999999999999999999995 exactly (under half a cent, so +/// 0.00), but `Decimal` rounds the product to 28 places first and the +/// kernel books 0.01. The engine refuses any foreign amount or rate of +/// eleven or more places before it computes. +#[test] +fn past_ten_places_the_reference_and_decimal_part_and_the_engine_refuses() { + let foreign = parse("0.99999999999999999999999999"); + let rate = parse("0.005"); + assert_eq!( + reference( + foreign.mantissa(), + foreign.scale(), + rate.mantissa(), + rate.scale(), + 2 + ), + Some(parse("0.00")), + "the exact product is under half a cent" + ); + assert_eq!( + fx_booked_minor(foreign, rate, Currency::USD, Rounding::HalfAwayFromZero), + Some(parse("0.01")), + "outside D-75 the kernel inherits Decimal's early rounding" + ); + let (mut ledger, cash, entry) = usd_books(); + for (foreign, rate) in [ + (foreign, rate), + (parse("1.00000000001"), Decimal::ONE), + (Decimal::ONE, parse("1.00000000001")), + ] { + assert!( + matches!( + ledger.add_line_fx(entry, cash, Side::Debit, Currency::EUR, foreign, rate), + Err(LedgerError::ValueOutOfRange(_)) + ), + "{foreign} x {rate} is refused" + ); + } + assert!(ledger.get_entry(entry).unwrap().lines().is_empty()); +} + +/// The other half of exactness (found by this bridge, reported with +/// D-75): ten places each keep the product's scale within 28, but +/// `Decimal`'s mantissa is 96 bits (7.9e28), and a product of twenty +/// places past about 7.9e8 does not fit. `Decimal` then drops places +/// from the product, and the kernel rounds the already rounded value. +/// 1050000010.0000000001 x 9.9999999999 is 10500000099.89499999999999999999 +/// exactly, and 10500000099.89 to the cent, but `Decimal` holds +/// 10500000099.895 and the kernel books 10500000099.90. Pinned as it is +/// today, outside the precondition of `fx_booked` (which says the +/// mantissas' product must fit); a unit to refuse it is the owner's call. +#[test] +fn the_mantissa_edge_is_outside_the_precondition_and_pinned() { + let foreign = parse("1050000010.0000000001"); + let rate = parse("9.9999999999"); + assert_eq!(foreign.scale(), 10); + assert_eq!(rate.scale(), 10); + assert_eq!( + reference( + foreign.mantissa(), + foreign.scale(), + rate.mantissa(), + rate.scale(), + 2 + ), + Some(parse("10500000099.89")) + ); + assert_eq!( + fx_booked_minor(foreign, rate, Currency::USD, Rounding::HalfAwayFromZero), + Some(parse("10500000099.90")) + ); +} diff --git a/crates/ledger-core/tests/spec_bounds.rs b/crates/ledger-core/tests/spec_bounds.rs index 88ae63db..f16a5a99 100644 --- a/crates/ledger-core/tests/spec_bounds.rs +++ b/crates/ledger-core/tests/spec_bounds.rs @@ -743,6 +743,96 @@ fn disposal_proceeds_and_dates_are_bounded() { .unwrap(); } +/// D-75 (owner, 2026-11-08): a new line's foreign amount and its rate take +/// at most ten decimal places each, as a base amount does (B-28), so the +/// product is exact (at most twenty places, under Decimal's 28) and the +/// booked amount is rounded once. Found by the K-U1 review: a 26-place +/// foreign amount times 0.005 books 0.01 though the exact product is under +/// half a cent, because Decimal rounded the product to 28 places first. +/// Replay is exempt (B-28): an older line keeps its figures. +#[test] +fn a_foreign_amount_and_a_rate_take_at_most_ten_places() { + let dec = |s: &str| s.parse::().unwrap(); + let mut b = books(); + let e = b.ledger.try_create_entry(d(2026, 1, 1), "fx").unwrap(); + let mut fx = |amount: &str, rate: &str| { + b.ledger.add_line_fx( + e, + b.cash, + Side::Debit, + Currency::EUR, + dec(amount), + dec(rate), + ) + }; + // Eleven places in the foreign amount, or in the rate: refused, naming + // which; ten and ten accepted. + assert_eq!( + fx("1.00000000001", "1"), + Err(LedgerError::ValueOutOfRange( + "foreign amount 1.00000000001".into() + )) + ); + assert_eq!( + fx("1", "1.00000000001"), + Err(LedgerError::ValueOutOfRange("FX rate 1.00000000001".into())) + ); + // The review's input: refused, not mis-booked as 0.01. + assert_eq!( + fx("0.99999999999999999999999999", "0.005"), + Err(LedgerError::ValueOutOfRange( + "foreign amount 0.99999999999999999999999999".into() + )) + ); + // Ten places each: the product (twenty places) is exact and booked + // once. 0.0000000001 x 5000000000.0000000001 = 0.50000000000000000001 + // books 0.50; 0.9999999999 x 0.5000000001 = 0.50000000004999999999 + // books 0.50. + assert_eq!(fx("0.0000000001", "5000000000.0000000001"), Ok(())); + assert_eq!(fx("0.9999999999", "0.5000000001"), Ok(())); + // Trailing zeros are not places: 1.50000000000 is 1.5. + assert_eq!(fx("1.50000000000", "2.0000000000000"), Ok(())); + let lines = b.ledger.get_entry(e).unwrap().lines(); + assert_eq!(lines.len(), 3, "the refused lines were not added"); + assert_eq!(lines[0].amount.amount, dec("0.50")); + assert_eq!(lines[0].foreign_amount, Some(dec("0.0000000001"))); + assert_eq!(lines[1].amount.amount, dec("0.50")); + assert_eq!(lines[2].amount.amount, dec("3.00")); + // The exact tie just under half a cent is not rounded up: with ten + // places each the product is held, so 0.4999999999 x 0.01 = 0.004999999999 + // books to zero and is refused as before. + assert_eq!( + b.ledger.add_line_fx( + e, + b.cash, + Side::Debit, + Currency::EUR, + dec("0.4999999999"), + dec("0.01") + ), + Err(LedgerError::InvalidFxRate(dec("0.01"))) + ); + // Replay keeps an older line with a finer foreign amount, exactly as + // booked. + b.ledger + .replay_add_line_fx( + e, + b.cash, + Side::Debit, + usd(dec("0.01")), + Currency::EUR, + dec("0.99999999999999999999999999"), + ) + .unwrap(); + let lines = b.ledger.get_entry(e).unwrap().lines(); + assert_eq!(lines.len(), 4); + assert_eq!( + lines[3].foreign_amount, + Some(dec("0.99999999999999999999999999")) + ); + assert_eq!(lines[3].amount.amount, dec("0.01")); +} + /// The bounds are the line limit exactly: a new draft line (base or /// foreign) and a recurring template's line may be as large as a line, and /// no larger; a replayed one past it is accepted as journaled. diff --git a/crates/ledger-store/tests/store.rs b/crates/ledger-store/tests/store.rs index 196a71a3..2563ee9d 100644 --- a/crates/ledger-store/tests/store.rs +++ b/crates/ledger-store/tests/store.rs @@ -1792,6 +1792,44 @@ mod refusals { } } +/// D-75: the ten-place bound on a foreign amount is the command's, never +/// replay's (B-28). A journal an earlier version wrote, holding a line with +/// a 26-place foreign amount, opens with its figures exactly as journaled. +#[test] +fn a_journaled_foreign_line_finer_than_ten_places_still_opens() { + use serde_json::json; + let dir = test_dir("fx-fine-foreign"); + let mut store = Store::create(&dir, Currency::USD).unwrap(); + let cash = store + .add_account("1000", "Cash", AccountType::Asset, None, false) + .unwrap(); + drop(store); + let mut w = JournalWriter::open(&dir.join(ledger_store::JOURNAL_FILE)).unwrap(); + w.append( + "entry.created", + json!({"id": 1, "date": "2026-03-01", "description": "old fx"}), + ) + .unwrap(); + w.append( + "entry.line_added", + json!({"id": 1, "account": cash, "side": "debit", "amount": "0.01", "currency": "USD", + "foreign_currency": "EUR", "foreign_amount": "0.99999999999999999999999999", + "fx_rate": "0.005"}), + ) + .unwrap(); + drop(w); + let _ = std::fs::remove_file(dir.join(ledger_store::db_file())); + let store = Store::open(&dir).expect("the older foreign line still opens"); + store.verify().unwrap(); + let lines = store.ledger().get_entry(1).unwrap().lines(); + assert_eq!(lines.len(), 1); + assert_eq!(lines[0].amount.amount, "0.01".parse::().unwrap()); + assert_eq!( + lines[0].foreign_amount, + Some("0.99999999999999999999999999".parse::().unwrap()) + ); +} + /// Contract (2026-10-13): rules added to the engine never lock anyone out /// of books written before them. A journal holding what the new rules /// refuse (a child account of another type, partially overlapping periods, diff --git a/docs/spec/claims/entry.md b/docs/spec/claims/entry.md index c5aa77df..1a065f0e 100644 --- a/docs/spec/claims/entry.md +++ b/docs/spec/claims/entry.md @@ -68,16 +68,23 @@ None. **Claims:** yes -**Pick:** the bounds are bookspec's (dates 0001-01-01 through 9999-12-31, an amount at most `kernel::MAX_LINE_AMOUNT` = 1,000,000,000,000,000, scale at most `MAX_LINE_SCALE` = 10), and one run of the recurring entries creates at most 1,000 drafts (`MAX_RECURRING_RUN`). Provenance: CHOICE (P0 review round 3, 2026-10-16, finding S1: the +**Pick:** the bounds are bookspec's (dates 0001-01-01 through 9999-12-31, an amount at most `kernel::MAX_LINE_AMOUNT` = 1,000,000,000,000,000, scale at most `MAX_LINE_SCALE` = 10; a new foreign line's foreign amount and its FX rate take at most `MAX_LINE_SCALE` places each, so their product keeps its scale), and one run of the recurring entries creates at most 1,000 drafts (`MAX_RECURRING_RUN`). Provenance: CHOICE (P0 review round 3, 2026-10-16, finding S1: the server stops for every session when a request panics, so no input may reach one); CHOICE (P0 review round 4, 2026-10-17: posted is forever, so the bounds are the commands', never replay's). A new asset's life past 9999 is refused at registration, the bound the life's last day held (P3 settled the plan's defect 22 question this way); ones an earlier version took open, and their months after 9999-12 show as scheduled and never post. +The ten-place bound on a foreign amount and a rate is D-75 (the owner, +2026-11-08, "Cap them at 10 places"): the K-U1 review found that with a +26-place foreign amount `Decimal` rounded the product to 28 places before +the engine rounded it to the cent, booking a cent the exact product does +not reach (0.99999999999999999999999999 x 0.005 into USD). A journal's +older, finer foreign line replays as booked (`replay_add_line_fx`). **Enforced:** `ledger_core::calendar`; `Ledger::try_create_entry`, -`add_line`, `add_line_fx`, `validate_new_asset`, `dispose_asset`, +`add_line`, `add_line_fx` (the foreign amount and the rate, `ValueOutOfRange`, +never on replay), `validate_new_asset`, `dispose_asset`, `schedules::validate_schedule`, `validate_recurring`, `periods::validate_period_range`, `arap::validate_lines`, `validate_due_date`, `payroll::check_payroll_ranges`, `payout_plan`, @@ -87,9 +94,12 @@ took open, and their months after 9999-12 show as scheduled and never post. `run_recurring`; `ledger-mcp` `parse_date`, `parse_period_mcp`, `last_day_of_month`. **Checked:** `ledger-core` `tests/spec_bounds.rs` (each bound and its -replay pair), `calendar.rs`, `assets.rs` +replay pair; `a_foreign_amount_and_a_rate_take_at_most_ten_places`), +`kernel_bridge/fx.rs` (`past_ten_places_the_reference_and_decimal_part_and_the_engine_refuses`, +`ten_places_each_is_exact_through_the_engine`), `calendar.rs`, `assets.rs` `registration_rejects_date_overflow`, `invariants.rs` -`out_of_range_amount_is_rejected`; `ledger-store` `tests/bounds.rs`; +`out_of_range_amount_is_rejected`; `ledger-store` `tests/bounds.rs`, +`tests/store.rs` `a_journaled_foreign_line_finer_than_ten_places_still_opens`; `ledger-cli` `server.rs` `periods_are_a_year_and_a_month`, `dates_are_four_digit_years_and_real_days`. diff --git a/docs/spec/claims/ledger.md b/docs/spec/claims/ledger.md index e5dc5053..0d9e9f78 100644 --- a/docs/spec/claims/ledger.md +++ b/docs/spec/claims/ledger.md @@ -18,7 +18,9 @@ Rules: B-3, B-4, B-5, B-8, B-9, B-10, B-20, B-23, B-29, B-31, B-32, B-44, B-45. **Enforced:** `Ledger::add_line` (base currency only, no fraction of the minor unit), `Ledger::add_line_fx` (base amount = foreign amount × rate, -rounded to the minor unit; original kept), `Ledger::post_entry` (every +rounded once to the minor unit by `kernel::fx_booked_minor`; original kept; +a foreign amount or a rate past ten places is refused, D-75, B-28), +`Ledger::post_entry` (every line, whatever made it: payroll, closes, disposals; `FinerThanMinorUnit`, since 2026-10-13 after a payroll run posted $65.27794). Journals from before replay as booked (`replay_post_entry`). A new asset's cost and @@ -29,8 +31,10 @@ replay as journaled. **Checked:** `an_asset_cost_finer_than_a_cent_is_refused`, `a_foreign_line_is_booked_in_whole_base_units`, `no_entry_posts_a_fraction_of_the_minor_unit`, -`foreign_currency_line_is_rejected`, `fx_overflow_is_an_error`; the -GnuCash oracle. +`foreign_currency_line_is_rejected`, `fx_overflow_is_an_error`; +`kernel_bridge/fx.rs` (the exact reference, ten places each); Kani +`proofs::fx::*`; Lean `Fx.booked_near`, `Fx.booked_whole`, `Fx.booked_pos`; +the GnuCash oracle. ### Product rules