Repository navigation
fix(query): replay transactions in the order booking read them - #2382
Conversation
The BQL executor replayed each transaction one posting at a time through `replay_posting`, against live state: the walk #2368 fixed in `apply` and the validator. So `BALANCES` refused ledgers `check` accepts. `+10 X {100}` then `-20 X {100}` in one transaction failed with `Not enough units` where bean-query answers `-10 X {100.00 USD}`. On an AVERAGE account, cost-less units written ahead of a sale joined that sale's pool, and `BALANCES` showed `16 X {50}` where `report balances` shows `6 X {100}` beside `10 X`. The walk decision is now one private `BookingEngine::walk_step`, used by `apply` and by a new public replay: `replay_transaction` for the state after a transaction, and `begin_replay`, a `TransactionReplay` cursor stepped once per posting, for the executor's per-posting `account_balance`. Like `replay_posting` it runs no `{*}` precondition and no rollback (#1985). `account_balance` after a posting is the state the walk reaches over the postings up to and including it: reductions as `book` classified them, plus the augmentations seen so far. That is unchanged for every transaction without a sale after a buy; the cursor shares the engine's copy unless an augmentation is held back on that account. Tests: - The BALANCES-vs-report drift guard gains three shapes (both #2368 ledgers with bean-query's holdings, and the AVERAGE case). Each fails on main on its own: two refusals and `16 @ 50`. - A query test pins the per-posting `account_balance` values of the #2368 ledger; main fails it with `No matching lot`. `check`, `BALANCES` and an `account_balance` query are byte-identical to main on seven benchmark ledgers, and callgrind is flat (-0.58% to +0.22%). Closes #2380 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report❌ Patch coverage is
📢 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 #2382 on whether the `account_balance` change is acceptable. Over 3000 generated ledgers, `BALANCES` changed on 116 (all AVERAGE) and in every one the new result matches `report balances` and the old did not; the 17 the query used to refuse now match too; FIFO, LIFO and STRICT ledgers it could already answer are unchanged. A `{*}` sale after a same-transaction buy shows the same pattern: main merged the buy into the pool (21 @ 114) while the journal books the sale at 110. - docs/reference/bql.md states the within-transaction meaning. - The drift guard gains the `{*}` case; main fails it with 21 @ 114. - The CHANGELOG entry says where values change instead of a looser bound. Refs #2380 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Deep review (head 2c17e46)Verdict: the behavior change is OK, and the PR is ready once CI is green. No code defects found. Two gaps were closed in 2c17e46: the docs didn't say what Is the
|
| outcome | count | who matches report balances |
|---|---|---|
BALANCES changed (all AVERAGE) |
116 | PR 116/116, main 0/116 |
main refused (Not enough units / No matching lot), PR answers |
17 | PR 17/17 |
only mid-transaction account_balance rows changed (all AVERAGE) |
4 | PR's value is the pool the next sale is booked from |
| unchanged | 443 | every FIFO/LIFO/STRICT ledger main could answer |
The comparator does report mismatches: it flagged main in all 116 changed cases. By hand, a {*} sale after a same-transaction buy shows the same pattern: main merged the buy into the pool (21 @ 114) while the journal books the sale at 110; the PR and the report hold 16 @ 110 and 5 @ 130.
Alternatives considered:
- End-of-transaction value on every row: would change ordinary ledgers. Two buys on one account would show
8, 8instead of5, 8. - Keep live replay but stop erroring: it has no defined answer for the AVERAGE and
{*}cases above.
The prefix-walk definition matches main wherever main agreed with booking, and its last row always equals report balances.
Compatibility: account_balance is a rustledger extension; beanquery has no such column (column "account_balance" not found). balance, the beanquery column, is untouched and still adds positions without booking. On the two #2368 ledgers, BALANCES now equals bean-query's.
Code
walk_step:apply's three arms are preserved exactly.AddNowskipstouchedand the precondition, as before;Reducechecks the precondition before the account's first mutation;Deferis unchanged.checkoutput is byte-identical to main on seven ledgers.TransactionReplay:account_snapshotcounts held-back augmentations, using the samePosition::from_posting+Inventory::addthatfinishuses, so the preview equals whatfinishadds.- It shares the engine's
Arcwhen nothing is held back, so the BQL account_balance copies the account inventory twice per row (quadratic) #2086 copy-avoidance holds. #[must_use]on the struct guards against dropping it beforefinish.
- Executor:
- No replay step runs between the pre-
WHEREsnapshot and the post-WHEREfill, so the deferred-fill comment stays true. - The replay is created only when
account_balanceis needed, the same gatereplay_postinghad. - A
WHERErejection still advances the replay, soaccount_balancestays independent ofWHERE.
- No replay step runs between the pre-
replay_posting: now unused in the workspace but public. It is kept, with docs pointing to the transaction-level replays.- Known duplicate: the validator's
update_inventoriesstill carries its own copy of the walk (different state type). Both sites cross-reference each other, per the canonical-function rules.
Evidence at this head
- The drift guard has four same-transaction shapes, each failing on main by itself: two refusals,
16 @ 50,21 @ 114. - A query test pins the per-row
account_balancevalues; main fails it withNo matching lot. - Callgrind against main: -0.58% to +0.22% across
check/BALANCES/account_balance. - The fifo
account_balanceOOM is pre-existing on main and is account_balance output is quadratic in lots held: OOM on a 20k-transaction FIFO ledger #2383.
What
The BQL executor now replays each transaction the way
bookread it, through the same walkBookingEngine::applyuses since #2379. It used to go one posting at a time against live state.BALANCES+10 X {100},-20 X {100}in one txn-10 X {100.00 USD}Not enough units-10 X {100.00 USD}+9 {101},-2 {100},-2 {100}9 {101} -2 {100}No matching lot9 {101} -2 {100}+10 X(no cost),-4 X {}16 {50}6 {100}and10(=report balances)+5 {130},-4 {*}{*})21 {114}16 {110}and5 {130}(=report balances)How
BookingEngine::walk_step(private) is the one per-posting decision: reduce now, defer, or add now.applyis rewritten on top of it with no change in behavior.replay_transactionreplays a whole transaction; the executor'sFROM ... OPEN ONcarry-in uses it.begin_replay→TransactionReplayis a cursor stepped once per posting.account_snapshotreturns the state the walk reaches over the postings so far, counting augmentations still held back. It shares the engine'sArcunless something is held back for that account, so the BQL account_balance copies the account inventory twice per row (quadratic) #2086 copy-avoidance still holds.replay_posting, both are replays: no{*}precondition and no rollback, because a filtered stream legitimately meets a different pool ([Bug]: BQL BALANCES shows unmerged lots and a dangling negative for AVERAGE-booked accounts #1985).replay_postingis kept for single-posting callers; nothing in the workspace uses it now, and its docs point to the new API.account_balancesemantics: a row shows the account after its transaction's postings up to and including that row, read the way booking read them: a sale never reduces a lot bought earlier in the same transaction. Documented indocs/reference/bql.md.Is the behavior change OK? Yes: it is a correction, not a new interpretation. Over 3000 generated ledgers that
checkaccepts (FIFO/LIFO/STRICT/AVERAGE, cost-less legs, several buys and sales per transaction):report balancesBALANCESchanged (all AVERAGE)account_balancechanged,BALANCESsame (all AVERAGE)Values change only where main's disagreed with the journal's own booked costs.
Evidence
query_report_realization_parity_test): four new shapes. Each fails on main independently: two refusals,16 @ 50, and21 @ 114.account_balanceper-row values pinned for the [Booking]: book accepts transactions apply refuses (intermittent book_apply_agreement failure; deterministic seed) #2368 ledger; main fails withNo matching lot.cargo test --all-featuresgreen; clippy andcargo doc -D warningsclean.check,BALANCESand anaccount_balancequery are byte-identical to main on seven benchmark ledgers.check/BALANCES/account_balance:Pre-existing, not addressed:
SELECT account, account_balance WHERE account ~ '^Assets'on the 20k-transaction FIFO bench ledger reaches 28 GB of RSS and is OOM-killed on main as well: #2383.Closes #2380
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr