Repository navigation
fix(booking)!: check a {*} merge spec's cost, currency, date and label - #2416
Conversation
`{*}` computes its cost, and everything else a merge spec wrote was
dropped without a word: `-5 X {*, 100.00 USD}` against a 110 pool booked
at 110, and a wrong currency, a date or a label passed the same way.
Each is now a claim checked against the merged pool. A cost must be the
pool's at the precision written (half a unit in the last place; a
compound cost sums both parts' allowances), a total or compound cost is
checked as what the units sold cost, a currency must be the pool's, and
a date or label is refused since the merged lot has neither. An accepted
claim still books the pool's own unrounded cost. The check runs in
`book` only: apply, the validator and the query replay see booked
postings, and a filtered replay builds a different pool on purpose.
New `BookingError::MergeSpecMismatch` (E4001) with `MergeSpecMismatch`
detail; wasm cache v29.
Closes #2398
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Mutation testing the #2398 diff left two survivors: `>` to `>=` in the precision check (no test stood exactly half a unit from the pool) and an empty `MergeSpecMismatch` Display (only the loader test read the text). A pool of 100.5 now holds for 100 and 101, and each detail variant's message is pinned in core. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
A `{*}` cost written as an expression (`{*, 500/3 USD}`) has 28 decimal
places, where half a unit in the last place cannot be written, so the
allowance fell to zero and the claim was compared exactly. It and the
pool are both quotients rounded to 28 places, each up to half a unit
from the true value, so an exact comparison would reject a correct claim
whenever the two roundings landed on neighboring digits. Eight probed
pools happened to round identically; the allowance should not rely on
that.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Deep review (three passes)Fixed: correct claims written as expressions were refused (80fd42f, ebae0b1). A total claim is checked against For example, The first pass's fix, a floor of one unit in the 28th place, didn't reach these:
Fixed in pass 3: claims that are sums of rounded parts (ab46b6c). The pool-rounding allowance assumed the stated number was rounded once, which holds for Sweep, with a check that it can fail:
Checked, no change needed
Pre-existing bugs this review found (not in this PR)
|
A total claim is checked against `units x pool`, and a pool that fills
Decimal's digits was rounded, so its error is carried once per unit. A
correct total written as an expression (`{{*, 1604500/66 USD}}` for 50
units of a 32090/66 pool) rounds once and landed 1e-24 away, outside
half a unit in the last place written: 32 of 150 generated correct total
claims were refused. The 28-place floor added before did not reach them,
since Decimal holds 28 significant digits, not 28 decimal places.
A result that fills the digits (28 or more) now counts as rounded, and
the allowance adds half a unit in its last place, per unit for the pool
and once for the product. Shorter results are exact and add nothing, so
an exact 110.00 pool still refuses 110.01. Swept: 0 of 300 generated
correct claims refused; all 300 refused when shifted by 0.01 or by 1e-20.
Also moves the two unit tests below the test module's imports.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Mutation testing left `l + 1` -> `l * 1` in the digit count alive: the only rounded fixture had 29 digits, which clears the threshold either way. 1/3 has 28 and must still count as rounded. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
A stated cost that fills Decimal's digits was not typed but computed, and how often the expression rounded depends on how it was written: `r*S/N` rounds once, `r*a/N + r*b/N` three times. Half a unit in its last place assumed once, and one of 200 generated correct totals summed from parts landed two units out and was refused. Such a number is now held to one unit in the place before its last; typed numbers keep half a unit in the last place written. Swept five expression forms (1,100 correct claims): none refused, and all refused when shifted by 0.01 or by 1e-20. Mutation testing on the booking diff: 17 caught, 2 unviable, none missed. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Problem
{*}merges an account's lots into one pool at their weighted-average cost. Anything else the spec wrote was dropped without a word. Holding 10 X at 100 and 10 X at 120 (a pool of 20 at 110):-5 X {*}-5 X {*, 110.00 USD}-5 X {*, 100.00 USD}/{100.00 USD, *}the merged pool costs 110.00 USD per unit, not the 100.00 USD the cost spec states-5 X {*, 110.00 EUR}-5 X {*, 2024-01-01}/{*, "lot"}-5 X {{*, 600.00 USD}}-5 X {*, 100 # 50 USD}The rule
Beancount has no merge to compare with (
Cost merging is not supported yet). The issue proposed reading the number as an assertion of the pool's cost, and this PR applies that to every part of the spec:Decimal's ~28 significant digits was rounded, so half a unit in its last place is added, once per unit sold for a total. This matters for a cost written as an expression ({{*, 1604500/66 USD}}): without it, 32 of 150 generated correct total claims were refused. An exact pool adds nothing, so a pool of exactly 110.00 still refuses a stated 110.01. A stated number that fillsDecimal's digits comes from an expression, and how many times it was rounded depends on how the expression was written, so it is held to one unit in the place before its last digit. Typed numbers keep half a unit in the last place written. The pool is a quotient and seldom has a finite decimal: for a pool of 106.666… USD,{*, 106.67 USD}and{*, 107 USD}hold, and{*, 106.66 USD}does not. This is deliberately not the ledger's balance tolerance, which bounds how far two sides of a transaction may disagree; this compares one number with itself.An accepted claim still books the pool's own unrounded cost, not the number written.
The error message gives the pool's cost to the places the spec writes, which is the number the author should have written. A mismatch is more than half a unit away, so it never rounds to the stated number.
Where the check lives
The check runs only in
BookingEngine::book, on the spec as written, before a compound cost is folded into one total so each part keeps its precision.apply, the validator and the query replay see booked postings, whose cost is the pool's by then. A filtered query replay builds a different pool on purpose (#1985), so checking there would reject correct answers.Breaking
rustledger_core::BookingErrorgainsMergeSpecMismatch, reported asE4001like any cost spec that names no lot. Its detail type is the newrustledger_core::MergeSpecMismatch.BookingErroris not#[non_exhaustive].Ledgerskips booking and serves its cached errors. The loader cache holds only parsed directives, and I confirmed the error still appears from a warm loader cache.Tests
crates/rustledger-booking/tests/merge_spec_claims_test.rscovers the precision rule at both edges, total and compound costs, currency, date and label, and an accepted cost booking the exact pool and applying. It also checks that a-Decimal::MAXstated cost is reported rather than panicking.wildcard_merge_test([Compat]: a trailing*cost component does not set the merge flag #2329) pinned{100.00 USD, *}booking at 110, which is the drop this fixes. It now expects the error.r*S/N,S/N*r, sums of rounded parts, scaled fractions): none refused. Every claim shifted by 0.01, and by 1E-20: all refused.100and101hold. The message for each detail variant is pinned in core.cargo mutants --in-diff, 17 mutants): 13 caught and 3 didn't compile. The one survivor is the message formatter. It lives in core, and the test that catches it is in booking, which a core-only mutation run doesn't execute; the core message test added after that run covers it.cargo clippy --workspace --all-features --all-targets -D warningsandcargo doc -D warningsare clean.Closes #2398
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr