Skip to content

[Compat]: implicit price is wrong #992

Closed

Description

@alensiljak

Description

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!

Beancount File

2025-01-23 txn "xyz insurance" "insurance matured"
  Assets:Fixed-Assets:Insurance  -27,204.53 BAM @@ 15,152.07 EUR
  Assets:Fixed-Assets:Insurance  15,152.07 EUR

Python Beancount Output

| 2020-09-16 | BAM      | 0.511286 EUR |
| 2020-09-21 | BAM      | 0.511316 EUR |
| 2025-01-23 | BAM      | 0.556969 EUR |

Rustledger Output

│ 2020-09-16 ┆ BAM       ┆              0.511285714285714 EUR │
│ 2020-09-21 ┆ BAM       ┆              0.511315789473684 EUR │
│ 2020-09-21 ┆ BAM       ┆              0.511315789473684 EUR │
│ 2025-01-23 ┆ BAM       ┆ 0.5569686372085825412164812257 EUR │
│ 2025-01-23 ┆ BAM       ┆                       15152.07 EUR │

Python Beancount Version

No response

Rustledger Version

0.14.1+

Activity

  1. added
    compatibility
    on May 4, 2026
  2. self-assigned this
    on May 5, 2026
  3. robcohen commented on May 5, 2026

    @robcohen
    Member

    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):

    if let Some(ref price_amount) = price.amount {
        let price_wrapper = DirectiveWrapper {
            // ...
            data: crate::types::DirectiveData::Price(crate::types::PriceData {
                currency: units.currency.clone(),
                amount: price_amount.clone(),  // ← uses total amount as-is
                // ...
            }),
        };
        generated_prices.push(price_wrapper);
    }

    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:

    Assets:Fixed-Assets:Insurance  -27,204.53 BAM @@ 15,152.07 EUR
    

    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.

    Reproducer (compact)

    2020-01-01 open Assets:Insurance
    2020-01-01 commodity BAM
    2020-01-01 commodity EUR
    
    2025-01-23 * "insurance matured"
      Assets:Insurance  -27204.53 BAM @@ 15152.07 EUR
      Assets:Insurance   15152.07 EUR
    

    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).

  4. 1 remaining item

  5. added a commit that references this issue on May 5, 2026
    4006151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

compatibility

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions