Skip to content

fix(booking): reject empty-cost augmentations loudly (interim for #1705) - #1708

Merged
robcohen merged 1 commit into
mainfrom
fix/empty-cost-augmentation-guard
Jul 3, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/empty-cost-augmentation-guard

Conversation

@robcohen

@robcohen robcohen commented Jul 3, 2026

Copy link
Copy Markdown
Member

The interim guard from the #1705 review: converts the SILENT half of the bug (an augmenting {} posting booking an uncosted lot, corrupting every downstream cost read) into a loud, actionable diagnostic:

error[BOOK]: cost spec on augmenting posting to Assets:Broker has no cost amount;
write the cost explicitly (e.g. {0.90 EUR}) — inferring it from the transaction
balance is not supported yet (#1705)

Scoping (the subtle part):

  • positive units only — a negative-units {} posting that didn't classify as a reduction (e.g. empty inventory) is reduction intent, which the Late validator's independent lot-matching pass already reports; the two-phase engine-lenient/validator-diagnoses design is preserved (test_book_no_inventory_for_account still passes unchanged).
  • Reductions with {} (wildcard lot-match syntax) and {*} merge specs unaffected.
  • test_book_augmentation_not_reduction previously pinned the silent booking — flipped with history documented.

The real fix (book-first-interpolate-after, per the #1705 analysis) is planned separately; this guard makes the interim safe. E2E: the issue's file now fails on the buy with the message above instead of silently mis-booking and failing one transaction later.

Refs #1705

🤖 Generated with Claude Code

An augmenting posting with a number-less cost spec ({}, {USD},
{2024-01-02}) booked an UNCOSTED lot (CostSpec::resolve() -> None),
silently corrupting every downstream cost read — gains, STRICT matching,
cost reports (#1705's worse, unreported half; the reported 2-unknowns
error only fired one transaction LATER, at the reduction).

Interim guard until cost-from-residual interpolation lands: booking an
augmentation whose cost spec has no number is now
BookingError::EmptyCostAugmentation, with the workaround in the message.
Scoped precisely:
- positive units only — a negative-units {} posting that didn't classify
  as a reduction (empty inventory) is reduction INTENT, which the Late
  validator's independent lot-matching pass already reports (two-phase
  design preserved; test_book_no_inventory_for_account still passes);
- reductions with {} (wildcard lot match) and {*} merge specs unaffected.

test_book_augmentation_not_reduction previously pinned the silent
booking ('should not error - just skip lot matching') — flipped with the
history documented.

Refs #1705

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

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 33.8ms ± 5ms 23.6 MB
beancount 865ms ± 4ms 50.7 MB
Comparison 25.6x faster 2.1x less

vs main branch (2026-07-03)

Metric main this PR Change
Time 34.9ms 33.8ms -3.2% ✅
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

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

Adds an interim safety guard in the booking engine to reject augmenting postings with an empty/numberless cost spec (e.g. {} / {USD} / {2024-01-02}) that would otherwise book an uncosted lot and corrupt downstream cost reads, pending the fuller book-first/interpolate-after fix planned for #1705.

Changes:

  • Introduces BookingError::EmptyCostAugmentation with an actionable error message.
  • Adds a booking-time guard to error on positive-unit augmentations whose cost spec lacks a cost number (excluding merge specs).
  • Updates fixtures’ stub Cargo.lock files to reflect rustledger-plugin-types v0.20.0 and adjusts the relevant booking test expectation.

Reviewed changes

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

File Description
crates/rustledger-booking/src/book.rs Adds new error variant + guard for empty-cost augmentations; updates a test to expect the loud failure.
crates/rustledger-plugin/tests/fixtures/sample_stub/Cargo.lock Updates stub lockfile to rustledger-plugin-types 0.20.0.
crates/rustledger-importer/tests/fixtures/sample_stub/Cargo.lock Updates stub lockfile to rustledger-plugin-types 0.20.0.

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

Comment thread crates/rustledger-booking/src/book.rs Resolved
Comment thread crates/rustledger-booking/src/book.rs Resolved
@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 793
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 3eaa4bd into main Jul 3, 2026
55 of 56 checks passed
@robcohen
robcohen deleted the fix/empty-cost-augmentation-guard branch July 3, 2026 16:57
robcohen added a commit that referenced this pull request Jul 3, 2026
…etes #1706) (#1709)

* fix(booking): infer augmenting empty-{} cost from the residual

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]>

* test: 17-fixture differential edge matrix for #1705 ({} cost solving)

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]>

* fix(booking): complete empty-{} cost inference — price precedence, negative 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]>

---------

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

2 participants