Repository navigation
fix(booking): check a cost written on an AVERAGE sale against the pool - #2611
Conversation
An AVERAGE sale books at the pool's average cost, and the cost number in
its spec was dropped: with the pool at 11.00 EUR, `-1 X {99.00 EUR}`
booked at 11.00 without a word. Under every other method a written cost
is a filter, and #2589 already checked the spec's currency.
The number is now a claim about the average. It holds within the
transaction's tolerance for the cost currency of the pool's exact
average (the balance check's tolerance, rounded down, so never looser);
a transaction with no tolerance in that currency holds it to the places
written, as a `{*}` claim is. A total states the total over the units
sold, compared against the pool's exact total, and a compound cost
`a + b / units`. A claim the pool is not is the new
BookingError::AverageSpecMismatch (E4001), naming the pool's cost.
Beancount has no AVERAGE booking, so this is rledger's definition,
documented under E4001 and in ADR 0007. WASM cache version 39.
Closes #2603
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
📊 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?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Review rounds (#2603)First head: transaction-tolerance rule, since replaced
Decided rule (robcohen): a correct rounding at the precision writtenChange: 489876d. The claim must be within half a unit in its last written digit of the pool's exact average, inclusive (the #2398
The required checks are green on the final head. Non-required: Merge note: #2616 also bumps WASM |
…sion robcohen's decision on #2603: a cost written on an AVERAGE sale holds when it is a correct rounding of the pool's exact average at the precision written, half a unit in its last written digit, inclusive, the rule a `{*}` claim follows (#2398). This replaces the transaction-tolerance rule, whose verdict depended on the cash leg (`{106.67 USD}` for 106.666... failed beside cash written `106.667`), which `infer_tolerance_from_cost` widened by the claim's own value, and which gave AVERAGE and `{*}` different rules. A total cost is compared as a total, |T - average x units| <= w(T), not divided by the units first; a compound `{a # b}` states the total `a x units + b` and holds within units x w(a) + w(b), each part being rounded to its own places. Booking no longer needs the transaction's tolerances, so their threading into `book_with` is reverted. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
The variant doc still described the transaction-tolerance rule that #2603's decision replaced. Review round 2. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
A code span with a trailing space (`units × `) read badly in E4001 and the CHANGELOG. Review round 3. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
…er-spec # Conflicts: # CHANGELOG.md
…er-spec # Conflicts: # CHANGELOG.md
What
An AVERAGE sale's cost spec number is now checked against the pool instead of being dropped.
On
main, with an AVERAGE pool of 20 X at 11.00 EUR, this books at 11.00 and says nothing:Under every other booking method a written cost is a filter, and a spec that no lot matches is an error. #2589 already checked the spec's currency against the pool. This PR checks the number too. A number the pool is not is an E4001 error that names the pool's cost:
The rule (robcohen's decision)
Beancount has no AVERAGE booking (bean-check 3.2.3:
AVERAGE method is not supported), so this is rledger's own definition. It is documented under E4001 indocs/reference/errors.mdand in an amendment to ADR 0007.A claim holds when it is a correct rounding of the pool's exact average at the precision written:
|claim − average| ≤ w(claim), wherewis half a unit in the claim's last written digit, inclusive. This is the rule{*, N}claims already follow (#2398).{106.67 USD}and{107 USD}hold, and{106.66 USD}and{160.67 USD}do not.infer_tolerance_from_cost.{106.67 USD}holds beside cash written106.667 USD, beside106.67, beside107, and with the cash elided.{{T}}, compared as a total. The check is|T − average × units sold| ≤ w(T), without dividing T by the units first. For 3 units of the 500/3 pool,{{500.00 USD}}holds and{{500.01 USD}}does not; divided first, 500.01 would pass at 0.0033 per unit.{a # b}. It states the totala × units + b, written in two parts, each rounded to its own places. A correctly roundedacontributes at mostunits × w(a)to the total's error, andbat mostw(b). So it holds withinunits × w(a) + w(b), which is the reading{*, a # b}already uses. Neither part alone names a pool quantity, because how a total splits between the parts is the author's choice.Inventory::reduction_pool_exactsums the pool's units and its cost (each lot's kept exact total from Selling a whole {{T}} lot whose total doesn't divide evenly fails to balance: a lot keeps only its rounded per-unit cost #2425/fix(booking)!: a lot keeps its exact total, and its sales add up to it #2427, elseunits × per-unit) inBigDecimal. The comparison is|claim × units − total × denominator| ≤ allowance × |units|.{},{EUR}) books at the pool as before. A wrong currency keeps fix(booking): refuse a sale priced or paid in another currency than its cost #2589's error. A{*}sale keeps its own A number in a {*} merge spec is silently ignored: {*, 100 USD} books at the pool's 110 #2398 check, and the two now give the same verdicts (see How tested).The first head of this PR held the claim to the transaction's tolerance instead. That rule was replaced because it:
infer_tolerance_from_costwiden the check by the claim's own value;{*}.Validator and BQL
The check runs in booking only, the same place as the
{*}check. Here is why the other consumers can't accept what booking refuses:apply, the validator'supdate_inventoriesand BQL'sreplay_postingsee only the booked posting, whose cost is already the pool's.queryandreportrefuse to derive figures from a ledger with an unbooked transaction. Checked on the CLI: both print1 transaction(s) could not be booked; refusing to derive figures …naming this error.A filtered BQL replay builds a different pool on purpose (#1985), so the replay must not re-check the claim.
Breaking
rustledger_core::BookingErrorgainedAverageSpecMismatch. The enum is not#[non_exhaustive], so an exhaustive match on it needs the new arm.Inventory::reduction_pool_exactis new public API.WASM
CACHE_VERSIONgoes from 38 to 39: a sale that booked before now fails, andLedgerarchives booked directives and their errors. The loader cache is unchanged, because parsed directives did not move.How tested
rustledger-booking/tests/average_cost_claim_2603.rs(12 tests). Every test runs throughbook,book_and_interpolateandbook_interpolate_apply, which must agree. It covers:106.67,106.7,107and106.666667hold, while160.67,106.66,106and106.6are refused, and the message shows the pool to the places written;{106.67}and{107}beside cash106.667,106.67,107or elided;500.01refused,333.33vs333.34);units × w(a) + w(b);{},{EUR}, a wrong currency, and{*}.rustledger-loader/tests/average_cost_claim_test.rs: the full pipeline reports the mismatch exactly once, andinfer_tolerance_from_costdoes not widen the check (-1.5 X {11.30 EUR}against 11.00 is refused with the option on or off).main, 8 of the booking tests fail and the loader mismatch test fails; the other 4 pin behavior that must not change. Against this PR's first head (the transaction-tolerance rule), the 4 rule tests fail (correct rounding, the other-postings regression, totals as totals, compound), and so does theinfer_tolerance_from_costloader test.Fractions, from the decided rule as stated above. Against this branch: 662 accepted, 838 refused, 0 mismatches. Self-check: the first head's binary has 49 mismatches in 300 ledgers.{*}consistency. The same claim against the same generated pool, as AVERAGE{N}and as FIFO{*, N}, gave 0 disagreements in 800 ledgers with the cash elided and 600 with the cash written to 3 places. Self-check: the first head disagrees in 101 of 300 of the latter.main:checkplusSELECT date, account, position, balanceover the repo's 329 ledgers, cache off, gave 0 differences.cargo test --all-featuresforrustledger-core,rustledger-booking,rustledger-validate,rustledger-loaderandrustledger-wasm: 1,544 passed, 0 failed.clippy -D warningsand the release-config clippy.cargo doc -D warningsfor the changed crates.Closes #2603
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr