Repository navigation
fix(booking): drop lots netted to zero, and sell an undated pool last - #2385
Conversation
Two ways `book` and `apply` still disagreed after #2368, both found by the small-lot agreement property committed ignored in #2379. 1. A short and a buy-back of the same lot in one transaction netted the lot to zero units, and the lot stayed. The sign counts put zero in the positive bucket, so it still counted as a holding: the next sale read as reducing it and failed with `No matching lot`. This was not AVERAGE-only; plain STRICT and FIFO ledgers that beancount accepts failed (`-5 X {100}`, `+5 X {100}`, then `-1 X {100}`, where beancount holds -1 X {100}), and `report balances` listed an empty `0 X {100 USD}` lot. A zero-unit lot is now in no sign bucket, in the counts and in the scan they are checked against. `add` also removes a cost-bearing lot it nets to zero, as beancount does, on the owned backing only: a cost-less position has always stayed at zero and been hidden by consumers, and BQL's shared running `balance` nets a lot to zero on every full-lot sale, where removal would shift every later slot. Two core tests that pinned "left at zero rather than removed" now expect removal. 2. A `{*}` merge leaves its pool undated, which `report capgains` documents as an unknown holding period. When STRICT later sells every lot at that cost, `book` splits the sale per lot, and the pool's spec carries no date, which as a filter matches any date, so `apply` saw two candidates and refused with `AmbiguousMatch`. STRICT expansions now put an undated lot's posting last; by then the dated lots at that cost are gone. The other methods re-select with the ordering `book` used and keep their order. The property now runs un-ignored, and a regression test replays both cases; each fails with its own fix reverted. The BALANCES-vs-report drift guard gains the net-to-zero ledger. `check`, `BALANCES` and `report balances` output is byte-identical to main on seven benchmark ledgers; `compat-booking-fuzz` agrees 300/300; callgrind on `check` is +0.01% to +0.31%. A third divergence surfaced by the same differential run is pre-existing and separate: #2384. Closes #2378 Refs #2384 Co-Authored-By: Claude Opus 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 #2385: the one mutant the core gate missed in this PR's lines was `PositionStore::is_owned -> true`, i.e. nothing noticed if a zero-netted lot were also removed from a `Shared` store. That exclusion is load-bearing: removing from an imbl `Vector` shifts every later slot while `cost_index` still names the old ones, so the next buy of the lot that moved misses it and opens a second lot. The new test fails under that mutant. Refs #2378 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Deep review (head b5cfe53, five passes)Verdict: ready once CI is green. Three findings, all fixed: a core test gap (bcd0f13), an over-broad reorder (d2ce904), and an untested method gate (b5cfe53). Zero-lot rule (core)
Undated pool (booking)
Other consumers of
|
Second review of #2385. Putting every undated lot last in a STRICT expansion also reordered sales that never collided: with a `{*}` pool at 110 and a lot at 130, a STRICT `{}` sale of both applied on main, and the PR moved the pool's posting after the 130 lot, changing the booked posting order (and so `print` output and any pinned `meta.hash`) of a ledger that was already valid. The undated lot now moves only when a dated lot shares its cost, and only to just after the last one: the only lots its wildcard date can collide with. Every expansion main could apply keeps its exact order. Unit test covers the three shapes; the #2378 regression still fails with the call disabled. Also checked in this pass: 12 report subcommands on 7 ledgers are identical to main except the ledger main refuses; `CapitalGain` carries no posting index, so posting order cannot attach a gain to the wrong posting. Refs #2378 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Third review of #2385. The CI mutation gate covers rustledger-core only, so the new booking helper was mutation-tested locally: 10 of 11 mutants caught, 1 timeout (an infinite loop, caught), and the one miss, `i + 1` -> `i * 1`, is equivalent (the search then includes `i`, which the undated check excludes). The STRICT gate at the call site had no test: widening it to every method passed the suite, although it reorders FIFO's booked postings (FIFO sells the pool first). The new test fails under that change. Refs #2378 Co-Authored-By: Claude Opus 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
Fixes the two
book/applydisagreements in #2378, and turns on the small-lot agreement property that found them (committed#[ignore]d in #2379).1. A lot netted to zero kept counting as a holding
A short and a buy-back of the same lot in one transaction netted the lot to 0 units, and it stayed in the inventory. The sign counts put zero in the positive bucket, so the next sale read as a reduction of the empty lot and failed. Not AVERAGE-only: plain STRICT and FIFO ledgers beancount accepts were refused.
-5 X {100},+5 X {100}(one txn), then-1 X {100}-1 X {100 USD}No matching lot-1 X {100 USD}report balances0 X {100}listed-1 X {100}SignCountsand inis_reduced_by_scan(the definition the counts are debug-asserted against).addremoves a cost-bearing lot it nets to zero, as beancount does, but only on the owned backing:balancenets a lot to zero on every full-lot sale. Removing there shifts every later slot, and forcing an owned copy would undo perf(query): JOURNAL clones growing cumulative_balance per matching posting (same shape as #1080, separate path) #1086.2. An undated
{*}pool beside a dated lot:AmbiguousMatchA
{*}merge leaves its pool undated. That is deliberate:report capgainsdocuments a pooled lot's holding period as unknown, so dating the pool was ruled out. When STRICT later sells every lot at that cost,booksplits the sale per lot. The pool's posting carries no date, which as a filter means "any date", soapplysaw two candidates and refused.In a STRICT expansion, an undated lot's posting now moves to just after the last dated lot at its cost, the only lots its wildcard date can collide with. By then those lots are gone, so it is the only candidate. This holds because a STRICT multi-lot match consumes every lot its spec matches, and any spec that matches the undated lot matches every lot at its cost. When no dated lot shares its cost, nothing moves, so every sale main could apply keeps its exact posting order (and any pinned
meta.hash). FIFO, LIFO and HIFO re-select with the same orderingbookused and are untouched.On main this reaches
rledger check: a{*}merge, a later buy at the pool's cost, then a STRICT sale of everything fails withAmbiguous lot match.Evidence
the_engine_agrees_when_a_transaction_exhausts_a_lotis un-ignored (2048 cases): 30/30 fresh runs pass, where main failed 1 in 20.the_disagreements_found_after_2368_apply_as_bookedreplays both [Booking]: two more book/apply disagreements behind #2368: an undated merged lot, and AVERAGE zero/cost-less lots #2378 cases. Each fails with only its own fix reverted:AmbiguousMatch, andjournal -100 engine 0.query_report_realization_parity_test) gains the net-to-zero ledger. Main refuses it.cargo test --all-featuresgreen; clippy andcargo doc -D warningsclean.reportsubcommands on 7 ledgers are identical to main, except the net-to-zero ledger main refuses.check,BALANCESandreport balancesare byte-identical to main on seven benchmark ledgers.compat-booking-fuzz.py: 300/300 agree with beancount.check(mimalloc, cache off): simple +0.31%, money +0.12%, fifo +0.12%, investment +0.01%.Not in this PR
A same-transaction differential against beancount (2000 STRICT/FIFO/LIFO ledgers) still disagrees on 3, where main disagrees on 4. All 3 are a separate, pre-existing class: a posting is classed as a reduction by an opposite-sign lot its cost spec cannot match. rledger refuses those ledgers, and beancount accepts them but backdates the new units. Filed as #2384.
Closes #2378
Refs #2384
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr