Skip to content

fix(booking): a STRICT sale of every matching lot books lots in purchase order - #3074

Merged
robcohen merged 3 commits into
mainfrom
fix/strict-total-match-order
Nov 28, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/strict-total-match-order

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

When a STRICT or STRICT_WITH_SIZE reduction matches several lots and takes them all (the total-match exception), its booked postings now list the lots in the order they entered the inventory (new LotOrder::Slot: a new lot takes the next slot, removal leaves a tombstone), where they were listed by lot date.

Why

beancount's inventory is an insertion-ordered map, and it books the total match in that order. Black-box, beancount 3.2.3:

2024-01-02 open Assets:B "STRICT"
2024-01-02 *
  Assets:B  14 VTI {{2898.00 USD}}
  Assets:B  4 VTI {75.14 USD, 2023-09-08}
  Assets:Cash
2024-01-12 *
  Assets:B  -18 VTI {} @ 177.23 USD
  Assets:Cash  3190.14 USD
  Income:Gains

beancount books -14 VTI {207.00 USD, 2024-01-02} then -4 VTI {75.14 USD, 2023-09-08}; rledger booked them the other way round (FIFO/LIFO/HIFO already agree). Units, costs and gains are unchanged; print, #postings and the journal show the order. Found by the semantic differential (#3071), category entries|postings.order|...|reduce.

WASM cache v156 (the open parser PRs hold v151 to v155): a Ledger archives its booked directives. The loader cache holds parsed directives and is unchanged.

How to test

cargo test -p rustledger-core --lib strict_total_match_lists_lots_in_the_order_they_were_added
cargo test -p rustledger-loader --test loader_test test_strict_total_sale_books_lots_in_purchase_order

The core, booking, loader, query, validate, CLI and WASM suites pass.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 52.7ms ± 1.4ms 768.4ms ± 4.3ms 14.6x 45.2 MB / 51.1 MB
investment 77.6ms ± 0.7ms 2850.5ms ± 5.5ms 36.7x 46.4 MB / 59.2 MB
fifo 76.4ms ± 1.1ms 14427.5ms ± 37.0ms 188.8x 49.1 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 51.9ms ± 0.3ms 52.7ms ± 1.4ms +1.4% ✅ within noise 45.0 MB → 45.2 MB (+0.3%)
investment 77.5ms ± 0.6ms 77.6ms ± 0.7ms +0.1% ✅ within noise 47.1 MB → 46.4 MB (-1.5%)
fifo 75.2ms ± 0.4ms 76.4ms ± 1.1ms +1.7% ✅ within noise 48.4 MB → 49.1 MB (+1.4%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.7ms, investment 0.4ms, fifo 0.5ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 828
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen force-pushed the fix/strict-total-match-order branch from bf54def to 6539f91 Compare November 27, 2026 17:01
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@robcohen
robcohen force-pushed the fix/strict-total-match-order branch from 6539f91 to 535d69b Compare November 28, 2026 01:51
@robcohen
robcohen changed the base branch from main to fix/auto-accounts-declared-order November 28, 2026 01:51
robcohen added a commit that referenced this pull request Nov 28, 2026
Restacked on #3074 (v158). VitePress compiles a {{ wrapped across lines as
a Vue interpolation, which broke validate-docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Base automatically changed from fix/auto-accounts-declared-order to main November 28, 2026 02:04
robcohen and others added 3 commits November 27, 2026 21:13
…ase order

When a STRICT or STRICT_WITH_SIZE reduction matches several lots and takes
them all (the total-match exception), its postings came out by lot date,
so a lot bought first but dated later came second. beancount lists the
positions of its insertion-ordered inventory, and so now does rledger
(LotOrder::Slot). Units, costs and gains are unchanged; print, #postings
and the journal show bean-check order.

WASM cache v156 (the parser PRs hold v151 to v155): a Ledger archives its
booked directives. Found by the semantic differential.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Restacked on #3075 (v157).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Restacked on #3075: the STRICT total-match posting-order mismatches now match, no regression.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen force-pushed the fix/strict-total-match-order branch from 535d69b to 04ac7f1 Compare November 28, 2026 03:13
robcohen added a commit that referenced this pull request Nov 28, 2026
Restacked on #3074 (v158). VitePress compiles a {{ wrapped across lines as
a Vue interpolation, which broke validate-docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen merged commit e54ce24 into main Nov 28, 2026
68 of 74 checks passed
@robcohen
robcohen deleted the fix/strict-total-match-order branch November 28, 2026 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant