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

+14 -4
View File
@@ -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`.
+7 -3
View File
@@ -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