Skip to content

fix(booking): infer augmenting empty-{} cost from the residual (completes #1706) - #1709

Merged
robcohen merged 3 commits into
mainfrom
fix/empty-cost-inference
Jul 3, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/empty-cost-inference

Conversation

@robcohen

@robcohen robcohen commented Jul 3, 2026

Copy link
Copy Markdown
Member

Builds directly on @yurenju's #1706 — the solver core is their work (parent commit preserved; Co-Authored-By carried through the squash) — finishing the four review items so this ships now rather than round-tripping:

  1. fix(booking): reject empty-cost augmentations loudly (interim for #1705) #1708 guard removed — it fired in book() before interpolation ran, making the solver dead code post-rebase (all 11 augmentation fixtures verified dying in the guard on a trial merge).
  2. Price annotations don't block solving — bean-check 3.2.3: {} @ 0.90 with a 0.95 residual books {0.95}, silently (verified). The price.is_none() skip re-opened the silent-corruption path for annotated postings.
  3. Negative solved cost rejects (bean: "Cost is negative") instead of booking a {-0.90} lot.
  4. Ambiguous cost currency rejects (bean: "Failed to categorize") — candidates exclude currencies owning their own missing-amount unknown (disjoint groups stay legal, pinned by existing unit tests), and the check defers to the more specific unassigned+cost-unknown rejection.

Validation

17-fixture matrix (committed as tests/compatibility/files/interpolation/, every row cross-checked against real bean-check 3.2.3 in the compat container): 17/17 accept/reject parity, solved values match to the cent, the issue's original file passes end-to-end. Full methodology + ground-truth table in the #1705 plan comments. 108/108 booking suite · clippy -D warnings · fmt.

Closes #1705
Closes #1706 — @yurenju thank you for the report AND the fix; the maintainer preferred to land it immediately, so review items were applied directly rather than requested.

🤖 Generated with Claude Code

yurenju and others added 3 commits July 3, 2026 17:46
An augmenting posting with an empty cost spec `{}` was accepted by
bean-check but rejected by `rledger check` as "2 unknowns". Beancount
books, then interpolates the single remaining unknown, so `1000 USD {}`
against one `-900 EUR` leg is booked at the inferred 0.90 EUR/unit.

Root cause: the pipeline books before interpolating, but `interpolate`
only ever filled missing *units*, never a cost *number* — an augmenting
`{}` was counted as one unknown and then never solved. The lot entered
inventory with no cost basis, so a later `-100 USD {}` reducing it
could no longer match a cost-bearing lot and became a spurious second
unknown beside the elided PnL leg. Fixing the augmentation fixes the
reduction by cascade.

Fix (interpolate.rs only): for an empty, price-less `{}` that is the
sole unknown in its cost currency group, infer the per-unit cost from
the residual (stored as `PerUnitFromTotal` to preserve precision). A
priced `{}` still defers to booking-time lot matching (keeps #1026).
Compat-policy bucket 1 — not a breaking change.

Closes #1705

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Every fixture cross-checked against bean-check 3.2.3 (matrix in the
#1705 plan comment). Encodes: residual solve, currency/date-only specs,
2-unknown rejections, price-vs-residual precedence (residual wins),
negative-cost rejection, ambiguous-currency rejection, NONE method,
STRICT ambiguity, multi-lot FIFO reduce, missing-units sell-all.

For cherry-pick into #1706.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…gative and ambiguous-currency rejection

Finishes #1706 (the solver core is @yurenju's work, preserved as the
parent commit) with the four items from its review, each pinned against
bean-check 3.2.3 via the 17-fixture matrix (committed here as
tests/compatibility/files/interpolation/):

- Remove the #1708 interim guard: it fired in book() before
  interpolation could run, making the solver dead code on rebase. Its
  book.rs test returns to not-an-error with the full history documented.
- Solve regardless of price annotations: beancount infers from the
  residual even when a price is present AND when they disagree
  ({} @ 0.90 with a 0.95 residual books {0.95} — verified, silent).
- Reject a negative solved cost (bean: "Cost is negative") instead of
  booking a negative-cost lot.
- Reject an ambiguous cost currency: when the spec names none and more
  than one candidate currency exists among complete cost-less postings,
  bean fails to categorize. Candidates exclude currencies owning their
  own missing-amount unknown (disjoint groups stay legal), and the check
  defers to the more specific unassigned+cost-unknown rejection when a
  fully-unassigned posting exists.

Matrix: 17/17 accept/reject parity with bean-check; solved values match
to the cent (0.90 inferred; 0.95 residual-over-price). The issue's
original file passes end-to-end.

Closes #1705
Closes #1706

Co-Authored-By: yurenju <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings July 3, 2026 18:08
@robcohen
robcohen requested a review from a team as a code owner July 3, 2026 18:08
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 33.5ms ± 0.6ms 23.8 MB
beancount 906.9ms ± 8.4ms 50.5 MB
Comparison 27.1x faster 2.1x less

vs main branch (2026-07-03)

Metric main this PR Change
Time 34.9ms 33.5ms -4.0% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.02985% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-booking/src/interpolate.rs 91.57% 8 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the booking/interpolation pipeline to match Beancount’s behavior when encountering augmenting postings with an empty cost spec ({}): it now infers a per-unit cost from the transaction residual (including when a price annotation is present), and replaces the prior interim “guard” error with the actual solver behavior.

Changes:

  • Implement residual-based inference for empty-cost augmentations in interpolate.rs, including new rejection paths for negative inferred costs and ambiguous cost-currency inference.
  • Remove the interim booking-time guard that rejected empty-cost augmentations, allowing interpolation to solve them as intended.
  • Add a compatibility fixture matrix under tests/compatibility/files/interpolation/ and update the fixture .gitignore rules to include it.

Reviewed changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/rustledger-booking/src/interpolate.rs Adds residual-based inference of per-unit cost for empty cost specs, plus new error cases and unit test coverage.
crates/rustledger-booking/src/book.rs Removes the interim booking guard/error for empty-cost augmentations and updates tests accordingly.
tests/compatibility/files/.gitignore Allows the new interpolation/ compatibility fixture directory to be tracked.
tests/compatibility/files/interpolation/e01_augment_empty.beancount Adds compatibility fixture for basic empty-cost augmentation inference.
tests/compatibility/files/interpolation/e02_reduce_empty_auto.beancount Adds compatibility fixture for reduction with {} and auto-filled residual posting.
tests/compatibility/files/interpolation/e03_augment_currency_only.beancount Adds compatibility fixture for augmentation with cost currency-only spec ({EUR}).
tests/compatibility/files/interpolation/e04_augment_date_only.beancount Adds compatibility fixture for augmentation with cost date-only spec ({YYYY-MM-DD}).
tests/compatibility/files/interpolation/e05_two_empty_augments.beancount Adds compatibility fixture covering multiple empty-cost augmentations in one txn.
tests/compatibility/files/interpolation/e06_empty_plus_auto.beancount Adds compatibility fixture combining empty-cost augmentation with an auto posting.
tests/compatibility/files/interpolation/e07_empty_plus_price.beancount Adds compatibility fixture ensuring price annotations don’t block cost solving.
tests/compatibility/files/interpolation/e08_none_augment_empty.beancount Adds compatibility fixture for empty-cost augmentation under booking method NONE.
tests/compatibility/files/interpolation/e09_none_reduce_empty.beancount Adds compatibility fixture for {} reduction under booking method NONE.
tests/compatibility/files/interpolation/e10_strict_reduce_empty_ambiguous.beancount Adds compatibility fixture for STRICT booking method and {} reduction ambiguity.
tests/compatibility/files/interpolation/e11_fifo_reduce_empty_two_lots.beancount Adds compatibility fixture for FIFO reduction spanning multiple lots with {}.
tests/compatibility/files/interpolation/e12_double_broken.beancount Adds compatibility fixture for multiple empty-cost postings causing multiple-unknown behavior.
tests/compatibility/files/interpolation/e13_missing_units.beancount Adds compatibility fixture for missing-units interpolation in a later transaction.
tests/compatibility/files/interpolation/e14_negative_residual.beancount Adds compatibility fixture for rejecting negative inferred costs.
tests/compatibility/files/interpolation/e15_multicurrency_residual.beancount Adds compatibility fixture for ambiguous cost-currency inference in multicurrency residuals.
tests/compatibility/files/interpolation/e16_price_vs_residual.beancount Adds compatibility fixture for price-vs-residual conflict (residual wins for inferred cost).
tests/compatibility/files/interpolation/e17_price_plus_auto.beancount Adds compatibility fixture combining price annotation with an auto posting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 810
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen merged commit 9acb624 into main Jul 3, 2026
52 of 53 checks passed
@robcohen
robcohen deleted the fix/empty-cost-inference branch July 3, 2026 18:33
robcohen added a commit that referenced this pull request Jul 3, 2026
Patch release: augmenting empty-{} cost specs now infer the per-unit
cost from the transaction residual, matching beancount exactly (#1705,
reported and first implemented by @yurenju in #1706, completed in #1709
with bean-check-verified edge semantics: residual wins over a
disagreeing price annotation; negative and ambiguous-currency solves
are rejected). The #1708 interim guard is superseded. No wire or API
changes.

Workspace 0.20.0 -> 0.20.1; packages/mcp-server bumped to match.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen robcohen mentioned this pull request Jul 3, 2026
robcohen added a commit that referenced this pull request Jul 3, 2026
Patch release: augmenting empty-{} cost specs now infer the per-unit
cost from the transaction residual, matching beancount exactly (#1705,
reported and first implemented by @yurenju in #1706, completed in #1709
with bean-check-verified edge semantics: residual wins over a
disagreeing price annotation; negative and ambiguous-currency solves
are rejected). The #1708 interim guard is superseded. No wire or API
changes.

Workspace 0.20.0 -> 0.20.1; packages/mcp-server bumped to match.

Co-authored-by: Claude Opus 4.8 <[email protected]>
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.

[Compat]: augmenting posting with empty cost spec {} is not interpolated (rejected as "2 unknowns")

3 participants