Skip to content

fix(booking,validate): lot-label on reduction + balance currency + dedup oversell (#1666, #1668) - #1669

Merged
robcohen merged 1 commit into
mainfrom
fix/booking-validation-diagnostics
Jul 1, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/booking-validation-diagnostics

Conversation

@robcohen

@robcohen robcohen commented Jul 1, 2026

Copy link
Copy Markdown
Member

Fixes surfaced by differential-testing rustfava against beancount.

#1666 — reduction drops the matched lot's label (holdings bug) — Closes #1666

Reducing a labeled cost lot emitted the reduction with label: null, so it didn't share lot identity with its augmenting lot and left a phantom unlabeled negative lot in the holdings view (aggregates stayed correct). Root cause: the single-lot reduction path in book.rs set label: None, while the multi-lot path already preserved it. Fix carries the matched lot's label (as the date already was).

Before: 10 X {lot-a} + 10 X {lot-b} + -5 X {no label} — After: 10 X {lot-a} + 5 X {lot-b} ✓ (matches beancount).

#1668 #2 — balance directive currency not validated

balance Assets:Cash 100 EUR on a USD account only gave a confusing Balance failed … got 0 EUR. Added a dedicated Invalid currency EUR for Balance directive on Assets:Cash (account holds USD) diagnostic in validate_balance_early, mirroring the transaction currency check — only when the account constrains its currencies (empty = any).

#1668 #1 — duplicate "Not enough units" on oversell (FFI)

An oversell reported the error twice via the FFI: booking (inside load_source) reports it with transaction context, and the #1663 validation session re-derives it context-free (its reduce-check is a standalone-validation safety net). Now the session's copy is dropped when a load error already conveys it. The CLI never doubled — it partitions booking-failed txns out before late validation; this was FFI-only.

#1668 #3 — duplicate open across includes — investigated, no change needed

Does not reproduce: the CLI already flags it (E1002 Account … already open, reproduced), and #1663 made the FFI load path validate (it now runs the full ValidationSession, including validate_open), so load catches it too. The report reflects the pre-#1663 non-validating load. Safe to close as resolved in v0.18.0.

Tests

  • Booking: a labeled reduction carries its lot's label and nets (test_reduction_carries_matched_lot_label).
  • Validate: balance wrong-currency flagged; unconstrained account not falsely flagged.
  • FFI parity: oversell reports "Not enough units" exactly once via load.
  • 94 validate + 106 booking + 19 FFI parity pass; clippy -D warnings + fmt clean.

🤖 Generated with Claude Code

…p oversell (#1666, #1668)

#1666 — reducing a labeled cost lot dropped the matched lot's label (single-lot
path set label: None while the multi-lot path preserved it), so the reduction
didn't net against its augmenting lot and left a phantom unlabeled negative lot in
the holdings view. Carry the matched lot's label onto the reduction posting (as
the date already was). rustledger-booking/src/book.rs.

#1668 #2 — a balance asserted in a currency the account doesn't allow only got a
confusing 'Balance failed ... got 0 EUR'. Add a dedicated 'Invalid currency ...
for Balance directive' diagnostic (E-code CurrencyNotAllowed) in
validate_balance_early, mirroring the transaction currency check; only when the
account constrains its currencies.

#1668 #1 — an oversell reported 'Not enough units' twice via the FFI: booking
(inside load_source) reports it WITH transaction context, and the #1663 validation
session re-derived it context-free (its reduce-check is a standalone-validation
safety net). Drop the session's copy when a load error already conveys it. The CLI
never doubled (it partitions booking-failed txns out before late validation).

#1668 #3 — investigated, no change: rustledger already flags duplicate open across
includes (E1002 on the CLI), and #1663 made the FFI load path validate too, so it
is caught there as well; the report reflects the pre-#1663 non-validating load.

Tests: booking reduction-label netting; balance wrong-currency (+ unconstrained
no-false-positive); FFI parity oversell-single-error. 94 validate + 106 booking +
19 FFI parity pass; clippy -D warnings + fmt clean.

Closes #1666

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

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 31.6ms ± 1.7ms 23.7 MB
beancount 627.4ms ± 2.7ms 50.5 MB
Comparison 19.9x faster 2.1x less

vs main branch (2026-07-01)

Metric main this PR Change
Time 36.5ms 31.6ms -13.4% 🟢
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 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.82609% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-validate/src/lib.rs 93.93% 2 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

Fixes several booking/validation parity issues discovered via differential testing against beancount, improving lot identity correctness, diagnostics, and FFI error reporting consistency.

Changes:

  • Booking: preserve matched lot label when generating single-lot reduction postings to avoid phantom unlabeled negative lots (#1666).
  • Validate: add an early diagnostic for balance directives asserting a currency not allowed by the account’s currency constraints (#1668).
  • FFI: de-duplicate oversell/reduction errors so load reports “Not enough units” exactly once, plus add parity coverage (#1668).

Reviewed changes

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

Show a summary per file
File Description
crates/rustledger-validate/src/validators/balance.rs Adds early balance-currency constraint check and emits CurrencyNotAllowed.
crates/rustledger-validate/src/lib.rs Adds unit tests covering wrong-currency balance and unconstrained accounts.
crates/rustledger-booking/src/book.rs Carries matched lot label on single-lot reductions; adds regression test.
crates/rustledger-ffi-component/src/convert.rs Filters out validation-session reduction errors that duplicate booking errors in load.
crates/rustledger-ffi-component-tests/tests/parity.rs Adds parity test ensuring oversell error is reported exactly once via load.
crates/rustledger-plugin/tests/fixtures/sample_stub/Cargo.lock Fixture lockfile bump for rustledger-plugin-types version.
crates/rustledger-importer/tests/fixtures/sample_stub/Cargo.lock Fixture lockfile bump for rustledger-plugin-types version.

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

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
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 e639eeb into main Jul 1, 2026
55 checks passed
@robcohen
robcohen deleted the fix/booking-validation-diagnostics branch July 1, 2026 20:15
@robcohen robcohen mentioned this pull request Jul 1, 2026
robcohen added a commit that referenced this pull request Jul 1, 2026
Minor (breaking-FFI) release: WIT contract rustledger:ledger 3.2.0 — the component
now imports host.decrypt so encrypted ledgers are decrypted by the embedder
(#1667/#1670); embedders must provide the import and regenerate bindings. Also
carries the booking/validate diagnostics fixes (#1666, #1668 via #1669): lot-label
preserved on reductions, dedicated balance-currency diagnostic, deduped oversell
error. Workspace 0.18.0 -> 0.19.0; 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.

Cost-lot reduction drops the matched lot's label (phantom negative lot in holdings)

2 participants