You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a case where the life term insurance matured and the amount was converted for bookkeeping due to administrative issues.
In Beancount, I've used @@ for simple conversion of the total amount to match the insurance final report.
Later, when running some reports, I got some very high numbers on balances. The root problem was that the @@ syntax generated implied price with the full 15xxx amount!
Confirmed and diagnosed. There are actually two bugs in the same plugin (crates/rustledger-plugin/src/native/plugins/implicit_prices.rs), plus a third independent issue (excessive precision).
Bug 1: @@ (total) emitted as per-unit price (the root cause of 15152.07 EUR)
The implicit_prices plugin (implicit_prices.rs:38-57) generates a Price directive from posting.price.amount directly, without checking whether the annotation is @ (per-unit) or @@ (total):
The PriceAnnotationData type has an is_total: bool field (crates/rustledger-plugin-types/src/lib.rs:397) — the plugin just doesn't read it. For your transaction:
The plugin emits 1 BAM = 15,152.07 EUR instead of the correct 1 BAM = 15152.07 / 27204.53 = 0.5569... EUR.
Fix: when is_total is true, divide the total amount by units.number.abs() before emitting:
let per_unit = if price.is_total && !units.number.is_zero(){&price_amount.number / units.number.abs()}else{
price_amount.number.clone()};
Note: the BQL query path (crates/rustledger-query/src/price.rs::add_implicit_prices_from_transaction) already handles @@ correctly. The bug is plugin-only — which is why the query side and the plugin side disagree. The fix needs to mirror the query path's logic.
Bug 2: Plugin double-emits prices when a posting has both @ and {cost}
Same file, lines 60-79. The plugin emits a price from the price annotation AND another from the cost spec — back-to-back, no else. So a posting like 100 BAM {0.51 EUR} @ 0.51 EUR produces two identical Price directives. This is the source of your duplicate 2020-09-21 row.
The query path handles this correctly: price annotation takes priority, cost is a fallback (price.rs:106-144 uses continue after extracting from the annotation). The plugin should do the same — change the if let Some(ref cost) block to be an else if chained off the annotation block.
Issue 3 (cosmetic, separate): excessive precision
0.5569686372085825412164812257 EUR — 28 decimal places, the rust_decimal maximum. Python beancount rounds price annotations to 6 dp by default. Probably add_implicit_price in either path should round_dp(6) (or follow the per-currency display_precision for the quote currency). Tracked separately from the @@ bug — fixing the @@ logic would still leave you with a long-tail precision string.
Acceptance criteria for the primary fix
implicit_prices plugin honors is_total and divides total → per-unit before emitting.
implicit_prices plugin emits at most one price per posting (annotation OR cost, not both).
New test: posting with @@ TOTAL CURRENCY produces one price equal to total / units.abs().
New test: posting with @ PRICE CURRENCY and {COST CURRENCY} together produces exactly one price (from the annotation).
Differential test against bean-check on the reproducer to confirm no spurious extra rows.
After fix: queries / reports should show one price for BAM on 2025-01-23 ≈ 0.557 EUR.
Before fix: shows two prices, one of which is 15152.07 EUR (wrong by a factor of ~27,204).
Activity
robcohen commentedon May 5, 2026
Confirmed and diagnosed. There are actually two bugs in the same plugin (
crates/rustledger-plugin/src/native/plugins/implicit_prices.rs), plus a third independent issue (excessive precision).Bug 1:
@@(total) emitted as per-unit price (the root cause of15152.07 EUR)The
implicit_pricesplugin (implicit_prices.rs:38-57) generates aPricedirective fromposting.price.amountdirectly, without checking whether the annotation is@(per-unit) or@@(total):The
PriceAnnotationDatatype has anis_total: boolfield (crates/rustledger-plugin-types/src/lib.rs:397) — the plugin just doesn't read it. For your transaction:The plugin emits
1 BAM = 15,152.07 EURinstead of the correct1 BAM = 15152.07 / 27204.53 = 0.5569... EUR.Fix: when
is_totalis true, divide the total amount byunits.number.abs()before emitting:Note: the BQL query path (
crates/rustledger-query/src/price.rs::add_implicit_prices_from_transaction) already handles@@correctly. The bug is plugin-only — which is why the query side and the plugin side disagree. The fix needs to mirror the query path's logic.Bug 2: Plugin double-emits prices when a posting has both
@and{cost}Same file, lines 60-79. The plugin emits a price from the price annotation AND another from the cost spec — back-to-back, no
else. So a posting like100 BAM {0.51 EUR} @ 0.51 EURproduces two identical Price directives. This is the source of your duplicate2020-09-21row.The query path handles this correctly: price annotation takes priority, cost is a fallback (
price.rs:106-144usescontinueafter extracting from the annotation). The plugin should do the same — change theif let Some(ref cost)block to be anelse ifchained off the annotation block.Issue 3 (cosmetic, separate): excessive precision
0.5569686372085825412164812257 EUR— 28 decimal places, therust_decimalmaximum. Python beancount rounds price annotations to 6 dp by default. Probablyadd_implicit_pricein either path shouldround_dp(6)(or follow the per-currencydisplay_precisionfor the quote currency). Tracked separately from the @@ bug — fixing the @@ logic would still leave you with a long-tail precision string.Acceptance criteria for the primary fix
implicit_pricesplugin honorsis_totaland divides total → per-unit before emitting.implicit_pricesplugin emits at most one price per posting (annotation OR cost, not both).@@ TOTAL CURRENCYproduces one price equal tototal / units.abs().@ PRICE CURRENCYand{COST CURRENCY}together produces exactly one price (from the annotation).bean-checkon the reproducer to confirm no spurious extra rows.Reproducer (compact)
After fix: queries / reports should show one price for BAM on 2025-01-23 ≈ 0.557 EUR.
Before fix: shows two prices, one of which is
15152.07 EUR(wrong by a factor of ~27,204).test: add plugin differential fixtures (followup to 39ff54d)
refactor(plugin-types): add typed PriceAnnotationView for exhaustive …
ci(plugin-tests): codify plugin-test-quality policies + lint script (…
fix(ci): address Copilot review on plugin-test-quality (#1001)
ci(plugin-tests): codify plugin-test-quality policies + lint script (…
fix(ci): address Copilot review on plugin-test-quality (#1001)
refactor(plugin-types): add typed PriceAnnotationView for exhaustive …
fix(ci): address all items from second deep review of #1001
ci(plugin-tests): policy + lint script for plugin-test quality (Phase…
feat(compat): plugin differential harness via BQL fixtures (Phase 2 of …
test: add plugin differential fixtures (followup to 39ff54d)
1 remaining item
feat(compat): plugin differential harness via BQL fixtures (Phase 2 of …
fix(plugin): correct @@ total-price handling and double-emit bug (closes
test(plugin): full test matrices for unrealized, sell_gains, capital_…
test(plugin): test matrices for transaction-emitter plugins
test(plugin): tighten amount assertions + add multi-buy proptest
test(plugin): full test matrices for unrealized, sell_gains, capital_…
test(plugin): test matrices for forecast, rx_txn, effective_date, box…
test(plugin): test matrices for generate_base_ccy_prices, split_expen…
test(plugin): test matrices for generate_base_ccy_prices, split_expen…
ci(lint): add ML_PAT_A multi-line variant for weak `>= N` count asser…
fix(query): two-pass price-database build to eliminate implicit-price…
fix(query): two-pass price-database build to eliminate implicit-price…
feat(ci): per-plugin mutation survival floor (closes #1003) (#1041)
test(plugin): address PR #1042 review (both annotation forms, accurat…
test(plugin): proptests for implicit_prices, sell_gains, coherent_cos…