Repository navigation
fix(core): AVERAGE pools only the side a reduction takes from - #2395
Conversation
`reduce_average` pooled every non-empty lot of the currency, long and short
together, and then removed every lot of the currency. On an account holding
a long of 5 at 102 and a short of 2 at 101, selling 2 booked at 102.67 (the
short pulled the basis toward its own cost), the holdings became
`1 X {102.67}` with the short gone, and selling 4 was refused as "available
3" from a long of 5. `report balances` and `BALANCES` disagreed with each
other too (102.67 vs 103).
The pool is now the side opposite the reduction's sign, the rule every other
method and `plan_merge` (`{*}`) already apply, and only that side is removed
and replaced by its remainder. A cover takes from the short side the same
way.
`merge_average`, which realizes a SUM of an AVERAGE account's postings, is
unchanged: there a sale is a negative lot at the pool's cost and must be
netted in, so sign cannot separate sales from shorts. A "leave mixed lots
unmerged" rule was tried and broke the single-sided case; #2394 tracks the
sum view of a mixed account.
`zero_cost_basis_2351`'s mixed-sign precision test reached the #2353
escalation through the pooling this removes. It now reaches it through
`merge_average`, where a mixed-sign average is legitimate, and also pins that
the reduction prices from the long side.
Tests: core `reduce_average_pools_only_the_side_it_reduces` (sale, larger
sale, cover, refusal against the side's size) and a drift-guard case in
`query_report_realization_parity_test`; both fail with the sign filter
removed. Agreement properties clean over 100 fresh runs; `check` and
`BALANCES` byte-identical to main on seven benchmark ledgers; callgrind on
`check` -0.004% to +0.12%.
Closes #2393
Refs #2394
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 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 |
Review of #2395. The one mutant `cargo mutants --in-diff` left alive was `==` -> `!=` in the retain that also drops emptied positions of the reduced currency: nothing checked that the tidying stays within that currency. The core test now holds a zero STK position and a zero USD one and asserts the first goes and the second stays. All 9 viable mutants on the PR's lines are now caught. Refs #2393 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Deep review (head eedb407, three passes)Verdict: ready once CI is green. No code defects. Two test gaps closed, and ADR-0007 amended. One implementation, every consumer
The rule matches the other methodsThe pool is the non-empty lots whose sign is opposite the reduction's: the same test Mutation testing (
|
…ADR-0007 Second review of #2395. The tax-visible consequence of #2393 was untested: on an AVERAGE account holding a long at 102 and a short at 101, covering 1 of the short at 95 is a short-term gain of 6 on proceeds of 101. Main priced the cover from the net pool and reported a loss of 8 with an unknown holding period, leaving `4 X {102.67}`. `average_mixed_sides_test` pins the disposal (short sale, units, cost basis, proceeds) and fails with the sign filter removed. ADR-0007 decided "one weighted-average pool" per commodity. This amends it to one pool per side, explains why the surveyed systems are silent on it, and records that `merge_average` is unchanged and why (#2394). Also checked: `check_average_cost` only applies to NONE-booked accounts (as in beancount), so it does not re-derive AVERAGE pools; rollback stays sound, the narrowed retain still tombstones through the undo log. Refs #2393 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Third review of #2395. The cover's gain was pinned, the sale's was not: on the mixed account, selling 2 from the long side at 110 realizes 16 against a basis of 204; main realized 15 against 205.33. Also measured in this pass, since no benchmark ledger is AVERAGE: the FIFO bench shape opened AVERAGE at 2k and 20k transactions gives byte-identical `check`, `BALANCES`, `report balances` and `report capgains` (6685 lines) to main, and callgrind on `check` is +0.086%. Refs #2393 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
…#2399) * fix(parser): a `*` that begins any cost component is the merge marker Beancount's grammar takes a cost as a comma-separated list of components (number, currency, date, label, `*`), and `*` may be any of them: `{100.00 USD, *}`, `{2020-01-01, *}` and `{*, 100.00 USD}` all set merge (checked against beancount 3.2.3's parser, which reaches its "Cost merging is not supported yet" action for each). `MergeFlag` decided the flag from the first token after the opener only, so a trailing `*` parsed as a plain per-unit spec, with no diagnostic: `-5 X {110.00 USD, *}` failed with `No matching lot`, and `-5 X {100.00 USD, *}` silently sold from the lot at 100 instead of the merged pool. The rule is now: a `*` is the merge marker when the previous non-whitespace token is the opener or a comma, i.e. when it begins a component. A `*` after a number or `)` is still multiplication (`{500 * 2 USD}`). `MergeFlag` is the one machine both the green and red walkers feed, so both change together; the test that pinned `{2 USD, *}` as not-merge now pins it as a merge, with rows for the other positions, and the red-tree accessor test gains the trailing form. `rledger format` round-trips every form unchanged. Caches: parser output changes, so loader CACHE_VERSION goes to 36 and the wasm cache to 24 (with its loader pin). The wasm cache archives BOOKED directives and errors, and the booking fixes merged since v23 (#2379, #2385, #2391, #2395, #2397) each changed those without a bump, so a v23 blob could serve their pre-fix answers; v24 covers them too. The CostNumber and MetaValue byte-layout tripwires still compare their bytes; only their FIXTURE_VERSION moves. Not changed: a number in a merge spec is ignored, for the leading form as it always was and now for the trailing one (#2398). Tests: the MergeFlag row test and the ast accessor test; a loader test that `{*}`, `{110.00 USD, *}` and `{100.00 USD, *}` all book from the merged pool at 110, which fails on main's parser. Workspace suite green (169 binaries); `check` byte-identical to main on four benchmarks; callgrind -0.03% to +0.004%. Closes #2329 Refs #2398 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr * test(parser): pin that MergeFlag ignores a comma before, or a brace after, the opener Review of #2399. `cargo mutants --in-diff` (the CI mutation gate covers rustledger-core only, not the parser) left the two `past_opener` guards alive: flipping either let a comma before the opener, or a second `{` on malformed input, arm a component start, so `, * {...` or `{2 USD {*` would read as a merge. Both are malformed-input paths the doc comment promises to ignore; two direct-feed assertions now pin them, and all 11 in-diff mutants are caught. Also checked in review: - Only `merge` changes. Parsing 18 shapes with main's and this branch's parser gives identical number, currency, date and label for every one (compound `#` and total `{{}}` included); a missing comma (`{100 USD *}`) is still a parse error, as in beancount. - No other code re-derives the merge flag; `Amount::is_arithmetic` sees the posting's amount node, never a cost spec's tokens. - A trailing `*` on a buy books the same lot as before, as the leading form always has. Refs #2329 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
What
An AVERAGE reduction now pools only the side it takes from: the lots whose sign is opposite the reduction's. Every other booking method, and
{*}(plan_merge), already matched that way. Only that side is removed and replaced by its remainder; the other side is left as it was.Why
reduce_averagepooled every lot of the commodity, long and short together:-2 X {101},3 X {102}report balances1 X {102.67}(short gone)BALANCES1 X {103}-2 X {101},3 X {102}Selling 4 of the long 5 was refused as "available 3", and covering 1 of the short at 95 reported a loss of 8 with an unknown holding period instead of a short-term gain of 6. A silent wrong number on any AVERAGE account holding both a long and a short. Beancount rejects AVERAGE, so this is a correctness fix with no oracle.
Not changed:
merge_averagemerge_averagerealizes a sum of postings forsum(position) GROUP BYan AVERAGE account. There a sale is a negative lot at the pool's cost and must be netted in, so sign cannot separate sales from shorts. I tried a "leave mixed-sign lots unmerged" rule and reverted it, because it broke the ordinary single-sided case (two core tests caught it). On a mixed account that view is still wrong, on main and here: filed as #2394, with replaying the group through the engine as the direction.A test that pinned the bug
zero_cost_basis_2351::a_mixed_sign_pool_is_correctly_rounded_through_the_escalationguards #2353's exact-escalation precision on a difference of products, and it got there through the long-plus-short AVERAGE reduction this PR removes. It now reaches the same escalation throughmerge_average, where a mixed-sign average is legitimate; the premise assertion still holds. It also pins that the reduction prices from the long side.Evidence
reduce_average_pools_only_the_side_it_reducescovers a sale, a sale larger than the net position, a cover, and a refusal against the side's own size.query_report_realization_parity_testgains the mixed AVERAGE ledger.average_mixed_sides_testpins the cover's realized gain (short sale, cost basis 95, proceeds 101).1 {103}vs1 {102.67}).checkandBALANCESbyte-identical to main on seven benchmark ledgers.check: −0.004% to +0.12%.Closes #2393
Refs #2394
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr