Repository navigation
fix(booking): read an AVERAGE sale's pool-average cost as a reduction beside a same-cost short - #2970
Merged
Merged
Conversation
… beside a same-cost short
On an AVERAGE account holding a short at 101 beside longs averaging 101, a
sale `-1 X {}` reduced the longs and booked as `-1 X {101 USD}`. Asked again
of the inventory (any consumer realizing a posting with no recorded booking
decision), the #2384 own-side exception read that cost lot by lot: it matched
the short and neither long, so the booked reduction became a second short.
Under AVERAGE a cost is a claim about the pool, so the exception now also
treats the other side as named when its pool accepts the cost as a claim.
The claim rule moves from rustledger-booking's check_average_spec into
rustledger-core (average_claim_mismatch, with written_allowance and its
helpers), and both booking and Inventory::is_booking_reduction call it.
A written `-1 X {101 USD}` in that shape now books like `-1 X {}`.
Commits the proptest seed and adds regression tests at core and validate
level. WASM cache bumped to 107 (106 is held by #2931); loader cache
unchanged.
Closes #2958
Co-Authored-By: Claude Opus 5.5 <[email protected]>
v107 is claimed by #2969; v106 is held by #2931. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…quences
The AVERAGE pool-claim rule applies to a buy that covers shorts as well as
to a sale. Say "a sale or a cover" in compatibility.md section 14 and the
v108 WASM cache note, and pin the cover with a test: holding a long at 101
beside shorts at 100 and 102, +1 X {101 USD} covers the shorts (1 long,
9 short left), where main opened a second long (2 long, 10 short).
The ADR 0007 amendment now states two consequences: a claim the pool
accepts only by rounding beats a lot at exactly that cost, and a ledger
that loaded can now fail with E4002 on an over-reduction of the pool.
Also update the adds_to_its_own_side doc comment to describe the AVERAGE
pool test.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 13, 2026
WASM cache: main holds v107 (#2969), #2970 holds 108 and #2971 holds 109, so this PR moves from 106 to 110. The v107 note now says v106 was held here and never released; loader cache stays 73. Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 13, 2026
…2971) * fix(wasm): report multi-file errors as rledger check and the FFI do The multi-file WASM surfaces (validateMultiFile, validateWithHost, parseMultiFile, Ledger, queryMultiFile, queryWithHost) diverged from rledger check, the FFI and the single-source surfaces three ways: - A parse error's message was prefixed with its file path, which is already in the error's file field (#2961). load_errors_to_errors now maps parse errors through the loader's parse_errors_to_ledger_errors and places them with the new ledger_error_to_wasm. - A loader error (validation finding, booking error, include LOAD error) had a null end and no hint, because From<LedgerError> cannot locate the end and dropped the hint (#2964). From now copies the hint, and ledger_error_to_wasm locates the end of source_span with SourceMap::locate; every surface holding the load's SourceMap converts through it. - queryMultiFile / queryWithHost refused on a parse error before process, so no Ledger existed and no option diagnostic was listed (#2960). They now take the parse errors out and process the rest, as Ledger.fromFiles does, and list Ledger::query_option_errors beside them. The parse_error_mapping_parity drift guard now compares every field, the end included, and adds two tests: validateSource, validateMultiFile and Ledger.fromFiles place every finding at the same start and end on the same ledgers, and the multi-file surfaces keep E8001's hint. The queryMultiFile skip and the prefix stripping in the wasm-pack tests are gone. WASM cache v109 (v108 is held by #2970). Closes #2960 Closes #2961 Closes #2964 Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(wasm)!: mark the parse-message prefix change breaking, and state hints as they are - CHANGELOG: the dropped file prefix on multi-file parse error messages moves under Breaking (rustfava unaffected; mcp-server formatErrors no longer names the file twice), with a Fixed bullet pointing to it. - CHANGELOG: multi-file hints match rledger check, a LOAD error carries none, and validateSource still differs on E2001 (#2972); in a parse-error refusal, queryMultiFile/queryWithHost now report other load errors as LOAD, where they were code-less parse-phase errors. - validate_with_filesystem stays pub: pub(crate) in the private api module trips clippy::redundant_pub_crate; say so in a comment. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
What was wrong
The committed CI seed (
cc 43c8647...) reproduces on main, but its shrunk failure is not the one the issue describes.apply, the replay and the validator all agree with booking when they read the recorded decision. The assertion that fails is property 2 inbooking_record_tests: with the recorded decision forgotten, the consumers askInventory::is_booking_reductionagain and get a different answer. The issue's three-leg case passes on its own. It only fails as the first of three transactions:Booking reads T3 as a reduction of the longs and books it as
-1 X {101 USD}, their average. When that booked posting is asked again, the #2384 own-side exception reads{101 USD}lot by lot. It matches the short and neither long, so the posting is treated as an augmentation and a second short is opened. Booking was right. The re-derivation was wrong, because booking had replaced the cost spec that the rule reads.The fix
Under AVERAGE, the cost on a reduction (a sale or a cover) is a claim about the pool's average (#2603), not a filter on individual lots. The own-side exception now also treats the other side as named when that side's pool accepts the cost as a claim. The cost must have no date or label, and the pool must be held in the claim's cost currency.
Canonical-function discipline: the claim rule used to live only in
check_average_specin rustledger-booking. It now lives in rustledger-core asaverage_claim_mismatch, along withwritten_allowance,fills_decimalandhalf_unit_in_last_place, which moved with it. Booking'scheck_average_specandInventory::is_booking_reductionboth call it, so the two cannot drift.Behavior change (AVERAGE only): on an account holding both a long and a short, a sale or a cover written with a cost that the facing pool accepts as its average (e.g.
-1 X {101 USD}in the shape above, or+1 X {101 USD}beside shorts at 100 and 102 and a long at 101) now books like{}: it reduces the pool. Before, it opened a new lot on its own side. Two consequences, stated in the ADR: a claim the pool accepts only by rounding now beats a lot at exactly that cost (longs 5@100 and [email protected] beside a short at 101:-1 X {101 USD}sells the longs), and a ledger that loaded can now fail with E4002 (-11 X {101 USD}against 10 longs averaging 101 beside a short at 101 reports "requested 11, available 10" instead of opening a short of 11). Python beancount has no AVERAGE booking (AVERAGE method is not supported), so there is no beancount answer to match. ADR 0007 has a new amendment and compatibility.md section 14 has a short note.Tests
crates/rustledger-validate/proptest-regressions/booking_record_tests.txt: the CI seed, committed.an_average_sale_priced_like_a_short_beside_it_agrees_everywhere_2958: the exact shrunk case under every method, and it checks that the pool ends at 9 long and 1 short.an_average_cover_priced_like_a_long_beside_it_agrees_everywhere_2958: the mirror for a cover, under every method. It checks that+1 X {101 USD}beside shorts at 100 and 102 and a long at 101 leaves 9 short and 1 long (main left 10 short and 2 long).sales_beside_a_short_priced_at_the_pool_average_agree_everywhere_2958: nearby shapes under every method, including several sales then a buy in one transaction, partial, whole and over-reductions, a sale stating the average, a sale stating neither cost, a cover, and a{*}sale. It also checks that a stated average and{}book the same.is_booking_reduction_average_*_2958covers the pool claim, unchanged FIFO/LIFO/HIFO/STRICT behavior, dated and labeled specs, mixed and foreign cost currencies, and a non-terminating rounded average.Gates
cargo fmt --all --checkcargo clippy --workspace --all-features --all-targets -- -D warningsRUSTDOCFLAGS=-Dwarnings cargo doc --workspace --all-features --no-depscargo test --all-featuresfor rustledger-core, -booking, -loader, -validate, -wasmevery_realization_agrees_with_the_recordatPROPTEST_CASES=20000, 3 debug runs and 1 release run, all greenCache
WASM
CACHE_VERSION105 -> 108, because booked directives change for the shape above. 106 is held by #2931 and 107 by #2969. Loader stays at v73 because parsed directives did not move.Closes #2958
🤖 Generated with Claude Code