Skip to content

feat(ffi)!: load reports balance failures + balance diff on directives (#1663, WIT 3.1.0) - #1664

Merged
robcohen merged 1 commit into
mainfrom
feat/load-balance-errors
Jul 1, 2026
Merged

robcohen merged 1 commit into
mainfrom
feat/load-balance-errors

Conversation

@robcohen

@robcohen robcohen commented Jul 1, 2026

Copy link
Copy Markdown
Member

Closes #1663. Breaking WIT change: rustledger:ledger 3.0.0 → 3.1.0 — rustfava must regenerate bindings.

Part 1 — load/load-file now report balance-assertion failures (correctness)

load/load_file ran only parse + booking; semantic checks (balance assertions are a Late check) lived only in validate. So an embedder that loads via load (rustfava) got errors: [] for a failing balance — a silent correctness failure. Now load/load_file run the same ValidationSession as validate (shared semantic_validation_errors helper), matching beancount's loader.load_file. query/clamp stay validation-free (they operate on an already-loaded ledger).

Part 2 — balance-dir.diff (UX)

balance-dir gains diff: option<amount> = computed − asserted in the asserted currency (zero = matched exactly, non-zero = failed; none if not checked). UIs can render per-assertion pass/fail without re-deriving the balance.

Crucially, the value comes from the validator's own computed actual — rustledger-validate records BalanceActual during the Late balance check (exposed via ValidationSession::balance_actuals()), and convert stamps it. No re-derivation, so this can't drift from the check (the recurring lesson from #1648/#1656/@@).

Tests

  • Load-vs-validate parity: the repro (balance Assets:Cash 999 USD when it's really -5 USD) surfaces the failing balance in load's errors and the directive carries diff = -1004 USD.
  • 92 rustledger-validate + 18 FFI parity tests pass; clippy -D warnings + fmt clean. The WIT 3.1.0 record change broke none of the 16 existing parity tests.

Release note

This is a breaking FFI/WIT change (3.1.0) — it'll want a 0.18.0 release, and rustfava must regenerate its component bindings to pick up balance-dir.diff and the load-reports-balances behavior.

🤖 Generated with Claude Code

… directives (#1663)

WIT rustledger:ledger 3.0.0 -> 3.1.0 (adds balance-dir.diff); API_VERSION -> 3.1.

Part 1 (correctness): load/load-file did not run semantic validation, so
balance-assertion failures were reported only by validate. Embedders that load via
load (rustfava) saw a failing balance as errors: [] — a silent correctness
failure, the user told their books reconcile when they don't. load/load-file now
run the same ValidationSession as validate (shared semantic_validation_errors),
matching beancount's loader.load_file. query/clamp stay validation-free.

Part 2 (UX): balance-dir gains diff: option<amount> = computed - asserted in the
asserted currency (zero = exact match, non-zero = failed), so UIs render
per-assertion pass/fail without re-deriving. The Late balance validator records
its OWN computed actual (new BalanceActual + ValidationSession::balance_actuals in
rustledger-validate) — no re-derivation, so load/validate cannot diverge from the
check. convert stamps each balance directive's diff.

Tests: the repro's failing balance appears in load's errors AND its directive
carries diff = -1004 USD. 92 validate + 18 FFI parity tests pass; clippy -D
warnings clean.

Closes #1663

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

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-validate/src/lib.rs 0.00% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 31.8ms ± 1.1ms 23.6 MB
beancount 908.6ms ± 5.9ms 50.5 MB
Comparison 28.6x faster 2.1x less

vs main branch (2026-07-01)

Metric main this PR Change
Time 36.5ms 31.8ms -12.9% 🟢
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

This PR updates the Component-Model FFI load surface to match validate semantics for balance assertions, and exposes per-balance computed vs asserted deltas to improve downstream UX (e.g., rustfava) without re-summing the ledger.

Changes:

  • Run rustledger-validate::ValidationSession during FFI load/load-file to surface Late-phase semantic validation errors (including balance assertion failures).
  • Record Late-phase balance “actuals” (computed − asserted) in rustledger-validate and stamp them onto balance-dir.diff in the WIT output.
  • Bump WIT package version rustledger:ledger from 3.0.0 to 3.1.0 and add parity coverage for the new behavior.

Reviewed changes

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

Show a summary per file
File Description
crates/rustledger-validate/src/validators/balance.rs Records per-balance computed diff during Late balance validation.
crates/rustledger-validate/src/lib.rs Adds BalanceActual + ValidationSession::balance_actuals() and stores results in LedgerState.
crates/rustledger-ffi-component/wit/world.wit Bumps WIT package version and adds balance-dir.diff.
crates/rustledger-ffi-component/src/lib.rs Updates reported API version to 3.1 and documents the breaking change.
crates/rustledger-ffi-component/src/convert.rs Runs semantic validation during load and attaches computed balance diffs onto returned directives.
crates/rustledger-ffi-component-tests/tests/parity.rs Adds a regression/parity test ensuring load surfaces balance failures and emits diff.

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

Comment thread crates/rustledger-validate/src/lib.rs Resolved
@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 cf77f04 into main Jul 1, 2026
44 checks passed
@robcohen
robcohen deleted the feat/load-balance-errors branch July 1, 2026 04:41
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.

load/load-file do not report balance-assertion failures (only validate does)

2 participants