Repository navigation
Description
Found by extending the query_report_realization_parity_test guard past the two booking shapes it covered (#1902 Phase 3).
Repro
2024-01-01 open Assets:Broker "AVERAGE"
2024-01-01 open Assets:Cash
2024-01-01 open Income:PnL
2024-01-05 * "buy lot1"
Assets:Broker 10 AAPL {150.00 USD}
Assets:Cash -1500.00 USD
2024-02-05 * "buy lot2"
Assets:Broker 10 AAPL {200.00 USD}
Assets:Cash -2000.00 USD
2024-06-10 * "sell 5"
Assets:Broker -5 AAPL {} @ 180.00 USD
Assets:Cash 900.00 USD
Income:PnL
$ rledger report x.beancount balances --no-pager
Assets:Broker
15 AAPL { 175.00 USD} <-- correct
$ rledger query x.beancount BALANCES
Assets:Broker 10 AAPL { 200.00 USD} 10 AAPL { 150.00 USD} -5 AAPL { 175.00 USD}
Three rows, including a negative holding, for an account that holds 15 units. SELECT SUM(number) agrees with the report (15), so the units are right — it is the lot-keyed presentation that is wrong.
Why
The parity guard's own docstring already names the mechanism, and it turns out to state an invariant that does not hold:
The query executor re-aggregates already-booked postings with naive
Inventory::add(lot-key netting), while reports realize through the booking engine's lot-matchingapply. They agree today ONLY because both consume post-booking directives — the loader's book phase has already matched every reduction against its lot, so naive re-aggregation lands on the same keys.
For AVERAGE that last step fails. Booking merges lots, so the reduction is booked at the merged average cost (175.00) while the two augmentations keep their original costs (150.00 and 200.00). Naive re-aggregation by lot key therefore sees three distinct keys where the booking engine sees one, and the reduction has no key to net against — so it survives as a negative row.
This is the same shape as #1726 (separate augmentation/reduction rows). It survived because the guard only covered FIFO-via-empty-{} and an explicit-cost reduction; AVERAGE is the one method where the booked cost of a reduction is not the cost of any augmentation.
Scope
Checked the whole matrix on the same fixture:
| method | query vs report |
|---|---|
| FIFO, LIFO, HIFO | agree |
| NONE | agree (same multiset; the report additionally shows lot dates) |
| STRICT | both error identically — ambiguous lot match |
| AVERAGE | diverge |
So this is specific to AVERAGE, not general.
Suggested fix
Either the executor's BALANCES aggregation honours the account's booking method rather than always key-netting, or it realizes through the same path reports use. The second is what Canonical-Function Discipline would say — the divergence exists because there are two realizations, which is the registry's realization family.
The parity guard is being extended to cover the full booking matrix; this case will be pinned as a characterization test naming this issue, so whichever way it is fixed, the guard notices.
Activity
test: extend the green/red and realization parity guards (#1986)
fix(query): one doc, one directive pass, and a docstring that had gon…
fix(query): one doc, one directive pass, and a docstring that had gon…
refactor(booking): separate realizing a ledger from replaying a filte…
refactor(booking): separate realizing a ledger from replaying a filte…
fix(query): replay transactions in the order booking read them (#2382)
refactor(booking): record booking's reduction decision on each posting (