Repository navigation
fix(booking): a cost matching only its own side adds to that side - #2391
Conversation
Whether a cost-bearing posting reduces was decided by the currency alone: any
opposite-sign lot made it a reduction, which then found no lot its cost named
on that side and failed. Holding a short at 101 and a long at 102,
`+3 X {102 USD}` was refused with `No matching lot`; beancount accepts it.
`Inventory::is_booking_reduction` now treats a spec that matches none of the
opposite-sign lots but does match an own-sign lot as an augmentation: a new
lot at the transaction date. A spec that matches nothing is still a reduction
and still fails, so a mistyped cost is still caught, as beancount catches it.
Gated on the cached sign counts, so an account holding one side never scans.
The two sides are matched differently. Against the opposite side the spec is
a reduction filter, date and label included. Against the own side it is an
augmentation, whose date is an acquisition date, so only the cost is compared.
The first version compared the date too: `book` booked `{102}` as `{102 USD,
<txn date>}`, `apply` asked again of that, the date matched no lot, and the
booked augmentation read as a failed reduction. Caught in review; a core test
and an agreement test now fail on that version.
`book` held two private copies of the rule (the classification and the
augmentation date fill); both now call the canonical predicate, like `apply`,
the validator and the query replay.
Deliberately not beancount's result. Its lot matching ignores sign, so it
merges the units into the same-sign lot and backdates them to that lot's
date, and refuses the posting when the quantity exceeds that lot. Against
beancount on 2000 generated same-transaction ledgers: 1985 agree, 3 differ
in lot date only, 12 are ledgers beancount refuses with `Not enough lots to
reduce`; none differ in units or cost.
Also: compat-booking-fuzz 300/300; the book/apply agreement properties 100
fresh runs clean; `check` and `BALANCES` byte-identical to main on seven
benchmark ledgers; callgrind on `check` +0.01% to +0.21%.
Closes #2384
Refs #2390
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 #2391. The own side is matched on cost alone, with the spec's date and label set aside. A spec with no cost, only a date or a label, then has nothing left to compare and matches every own-side lot. On an account holding a long at 100 and a short at 101, a sale written `-1 X {2020-01-09}`, naming a lot date nothing has, booked as a new short lot dated with the typo when the cash leg implied 101. Beancount refuses it, and so did main. The exception now needs a per-unit cost in the spec. That cost is what says "the own side"; without it the posting stays a reduction and still fails. Core and loader tests fail with the guard removed. The beancount differential, fuzz and agreement results are unchanged (the generator only writes numbered costs or `{}`). Refs #2384 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Deep review (head 4e7d044, four passes)Verdict: ready once CI is green. Two real defects found and fixed (e6c2771, dffb053), plus three test gaps closed. Found: a mistyped lot date could open a position (fixed)The own side is matched on cost alone, with date and label set aside, because on the own side the date is an acquisition date. A spec with no cost (only a date or label) then has nothing left to compare and matches every own-side lot: Beancount and main refuse this. At c905c0d it booked Found (second pass): a currency-less cost spec could make
|
Second review of #2391. A cost spec written without a currency, `{102}`, is booked with one inferred from the price or the other postings, and `apply` then asks its question of the booked `{102 USD}`. `book` asked of `{102}`, where a missing currency matches any: holding a long at 102 EUR and a short at 101 USD, it matched the EUR lot as the posting's own side, booked an augmentation, and `apply` refused it. The ledger was refused either way, as beancount refuses it, but by `apply` after `book` accepted. `book` now classifies with the currency the augmentation branch fills in, through one `inferred_cost_currency` shared by both; the reduction match itself still uses the spec as written. `cost_currency_inference_agreement` pins both directions and fails with the classification reverted: book accepts, apply refuses. Mutation testing on the PR's lines (`cargo mutants --in-diff`) also found three untested conditions in the own-side match: a label on the buy, a lot of another commodity at the same cost, and a lot netted to zero. The core test now covers each. The remaining survivors are equivalent or performance-only: the fused sign-count guards only decide whether the own-side scan runs, and the date-fill site's currency only decides when an augmentation's date is filled, not what it is. Refs #2384 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
…use it
Classifying with the inferred cost currency cost +0.13 to +0.17% on `check`:
the benchmark ledgers sell with `{}`, which has no currency, so every sale
ran the inference and cloned its spec, and the augmentation date fill cloned
the spec for every cost-bearing buy. A spec naming no per-unit cost never
reaches the own-side rule, and a spec that already has a currency needs no
filling, so both now skip it. Callgrind on `check` vs main: +0.01% to +0.27%.
Refs #2384
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Third review of #2391. The PR adds a deliberate difference from Python beancount (a new lot at the transaction date where beancount merges and backdates, and acceptance regardless of quantity where beancount refuses one larger than the matched lot), and the project keeps two lists of those. Adds it to both: a worked example with both engines' holdings in the compatibility reference, and a short entry in the migration guide's known differences. Also checked: no doc states the old per-currency reduction rule, and the TLA+ specs model reductions without the classification step, so neither is stale. Found in passing: the same migration list still describes #2118 as open (#2392). Refs #2384 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
A cost-bearing posting is now an augmentation when its cost spec matches none of the opposite-sign lots but does match a lot of its own sign. Otherwise the reduction rule is unchanged.
-2 {101}, long5 {102}+3 {102}8 {102, 01-01}(backdated)No matching lot3 {102, 01-05}5 {102}, short-2 {101}-1 {101}No matching lot-1 {101, 01-05}10 {100}-5 {101}(typo)No position matchesNo matching lotNo matching lot(unchanged)-2 {101}only+3 {102}No position matchesNo matching lotNo matching lot(unchanged)10 {100}, short-2 {101}-1 {2020-01-09}(date typo)No position matchesNo matching lotNo matching lot(unchanged)How
Inventory::is_booking_reduction, the one predicatebook,apply, the validator and the query replay share, gains the exception.applyasks the question again of the booked posting. The first version compared the date too and failed exactly there; review caught it, and two tests now pin it.checkis +0.01% to +0.27%.bookclassifies with the cost currency it will book with. A spec without one,{102}, is otherwise a wildcard at classification and a fixed currency by the timeapplyasks again (inferred_cost_currency, shared with the augmentation fill; review, dffb053).bookhad two private copies of the rule (the classification, and the augmentation date fill). Both now call the canonical predicate.Deliberately not beancount
Beancount's lot matching ignores sign. It merges the units into the same-sign lot, backdating them to that lot's date, and refuses the posting when the quantity exceeds that lot (
Not enough lots to reduce). Units and cost basis agree with beancount everywhere; the lot date and acceptance follow the posting instead.Against beancount 3.2.3 on 2000 generated same-transaction ledgers: 1985 agree, 3 differ in lot date only, and 12 are ledgers beancount refuses with
Not enough lots to reduce. None differ in units or cost.Evidence
a_cost_matching_only_its_own_side_is_an_augmentationcovers each shape, the booked form with its filled-in date, a spec matching both sides,{}, and NONE.mixed_sign_holdings_test.rscovers both ledgers with their holdings and dates, and a mistyped cost still refused.a_buy_at_a_long_side_cost_applies_as_it_booked_beside_a_shortchecks thatbookandapplyagree end to end. The property alone can't reach this, because it skips anythingbookrefuses.compat-booking-fuzz300/300; agreement properties clean over 100 fresh runs.checkandBALANCESoutput byte-identical to main on seven benchmark ledgers.Follow-up
This PR is the third time a rule re-asked of the booked posting has disagreed with
book, after #2368 and #2378. #2390 proposes recordingbook's decision on each posting instead of re-deriving it.Closes #2384
Refs #2390
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr