Repository navigation
fix(booking): sell a whole AVERAGE or {*} pool at its exact cost - #2424
Merged
Merged
Conversation
1 X at 100 and 2 X at 200 average 166.66..., which Decimal rounds, and a
sale of all three was booked at `3 x 166.6666666666666666666666667`,
500.0000000000000000000000001. Sold for `500 USD` (whole-dollar amounts
infer no tolerance) it failed with `residual -1E-26 USD`, an imbalance
booking created and the author could not remove.
A reduction that takes the whole pool now takes what the pool's lots
cost (`pool_total_cost`, 500 exactly) in `reduce_average` and
`reduce_merge`. Booking carries it as `PerUnitFromTotal`, as a `{{T}}`
cost already is, so the balance weighs the exact basis, and the realized
gain uses the same basis. A partial sale keeps the average, the only
figure there is. Wasm cache v31, since the booked cost changes shape.
Closes #2417
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! |
Contributor
📊 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?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Member
Author
Deep review (three passes)No changes to the fix itself. Pass 3 added a generated regression test (9656a2c). Probed, correct
Pass 2
Pass 3: generated sweep These are 240 generated pools: two to four lots of 1–30 units at 1–999 USD, long and short, AVERAGE and
Limitation, recorded in #2425 and the PR description
Pre-existing, unrelated to this PR
|
…and basis
The single-ledger test covers one pool whose average doesn't terminate.
The review sweep found such pools in about one in twenty generated
ones (13 of 240 failed before the fix). This replays 200 of them, long
and short, AVERAGE and `{*}`: every sale must balance, and every
realized lot value must be the pool's exact total. For a covered short
that is the gain's proceeds, since the roles swap. A fixed LCG keeps
the ledger identical on every run; reverting the core half of the fix
fails it.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
1 X at 100 and 2 X at 200 average 166.66…, which
Decimalrounds. Booking priced a sale of all three at3 × 166.6666666666666666666666667, which is 500.0000000000000000000000001:Sold for
500 USD, the transaction failed. Its amounts are whole dollars, so they infer no tolerance (the same rule as beancount), and any residual at all is an error. The lots cost exactly 500, so booking created an imbalance the ledger's author could not remove. It happened for AVERAGE{}and for{*}.Fix
Σ |units| × cost(pool_total_cost), in bothreduce_averageandreduce_merge. A partial sale is still priced at the average, the only figure there is.|units| × per-unit, booking writesPerUnitFromTotal(per_unit, total)instead ofPerUnit, the shape{{T}}costs already use for exact residual math (validate: rledger accepts sale of non-existent inventory + unsolvable interpolation #1026). The balance weighs the total. The realized gain for a single pooled match uses the same basis.apply, the validator and the BQL replay pass AVERAGE and{*}reductions toreduce_averageandreduce_merge, which ignore the spec's number, so they cannot disagree with booking. The [Bug]:{*}cost-merge reduction books but cannot apply — E4001 on a lot the account holds #2068 merge check readsper_unit(), which is still the pool's.{}, sell all 3 for500{*}, sell all 3 for500333.33Out of scope: a whole single
{{T}}lotA single lot bought as
3 X {{500 USD}}and sold whole with{}fails the same way. The lot stores only its rounded per-unit cost, so the exact total is lost when the lot is added. Beancount 3.2.0 does the same: it refuses that sale by-0.0000000000000000000000001 USD, and it books the purchase's cash at-500.0000000000000000000000001.Fixing it means a lot remembering its total, which changes the
Costtype, the cache format and themeta.hashcontract. That's tracked in #2425. So is the related limitation here: a pool sold in more than one posting, even within one transaction, leaves a remainder lot at the rounded average, so only a sale of the whole pool in one posting is exact.Visible changes
per_unit_from_total) where they sawper_unit. That changes those postings'meta.hash.Tests
a_whole_pool_sale_takes_the_lots_exact_cost: a whole-pool sale is priced at exactly 500, and a partial sale at 2 × the average, under both AVERAGE and{*}.whole_pool_basis_test: the issue's ledger, run through the full pipeline. There are no errors, the booked cost's total is 500, and the realized basis is 500. It fails with either the core half or the booking half of the fix reverted.cargo doc -D warningsare clean.Closes #2417
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr