Skip to content

fix(ffi): run pre-booking synth pass through the FFI load surface - #1404

Merged
robcohen merged 2 commits into
mainfrom
fix/ffi-synth-pass
Jun 16, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/ffi-synth-pass

Conversation

@robcohen

Copy link
Copy Markdown
Member

Problem

The FFI load_file/load_source helpers hand-rolled a partial loader (parse + a manual booking loop) that ran in parallel to the canonical rustledger_loader::process::load pipeline. That fork skipped the pre-booking SYNTH pass entirely, so auto_accounts / document_discovery never generated directives through any FFI surface — JSON-RPC ledger.load/ledger.loadFile, or the WIT component (which delegates to the same helpers).

The native loader runs synth correctly (rustledger-loader's test_plugin_execution_auto_accounts proves it), but no rustledger-ffi-wasi or rustledger-ffi-component test asserted synth through the FFI, so the divergence shipped green.

This was surfaced while embedding the engine in rustfava: a ledger declaring plugin "auto_accounts" produced zero generated Open directives through the FFI.

Fix — collapse the duplicated pipeline

  • load_file now delegates to rustledger_loader::load (the canonical process::load: sort → synth → book → regular → finalize). The hand-rolled booking loop is removed. validate: false preserves this surface's load-only error contract; run_plugins (default true) brings the synth pass.
  • load_source (string surface) reuses the canonical run_plugins(.., PreBookingSynth) entry point rather than a fork, preserving its parse/book/error-phase and unresolved-include reporting so ledger.validate/query are unaffected.
  • The WIT component inherits the fix (it delegates to these helpers).

Synth-generated directives surface with lineno 0 / <unknown> file — the "generated entry" marker embedders rely on.

Close the coverage gap

  • New rustledger-ffi-wasi/tests/load_synth_plugins.rs — asserts synth generation through load_file, load_source, and the JSON-RPC loadFile/load handlers, plus a regression guard that plain ledgers gain no spurious generated directives. (Verified failing before / passing after.)
  • New parity cases in rustledger-ffi-component-tests — assert synth through the real wasm component's load and load-file.

Testing

  • cargo test -p rustledger-ffi-wasi — 63 unit + 11 integration ✅
  • cargo build -p rustledger-ffi-component --target wasm32-wasip2 && cargo test -p rustledger-ffi-component-tests — 13 ✅ (incl. the 2 new synth parity tests)
  • clippy + fmt clean; no snapshot churn

Out of scope (follow-ups)

  • Whether the FFI load path should also run validation (kept validate: false to scope this to the synth bug).
  • Cutting a release so embedders (rustfava) pick up the fixed wasm.

🤖 Generated with Claude Code

The FFI `load_file`/`load_source` helpers hand-rolled a partial loader
(parse + manual booking) that ran in parallel to the canonical
`rustledger_loader::process::load` pipeline. That fork skipped the
pre-booking SYNTH pass entirely, so `auto_accounts` / `document_discovery`
never generated directives through ANY FFI surface (JSON-RPC `ledger.load`
/`loadFile`, or the WIT component, which delegates to the same helpers) —
even though the native loader did. No ffi-wasi or ffi-component test
asserted synth through the FFI, so the divergence shipped green.

Collapse the duplication instead of patching it:

- `load_file` now delegates to `rustledger_loader::load` (the canonical
  `process::load`). `validate: false` preserves this surface's load-only
  error contract; `run_plugins` (default true) brings the synth pass.
- `load_source` reuses the canonical `run_plugins(.., PreBookingSynth)`
  entry point rather than a fork, keeping its parse/book/error-phase and
  include-reporting behavior so `ledger.validate`/`query` are unaffected.
- The WIT component inherits the fix (it delegates to these helpers).

Synth-generated directives surface with `lineno 0` / `<unknown>` file —
the "generated entry" marker embedders rely on.

Close the test-coverage gap that let this through:

- New `rustledger-ffi-wasi/tests/load_synth_plugins.rs`: asserts synth
  generation through `load_file`, `load_source`, and the JSON-RPC
  `loadFile`/`load` handlers, plus a guard that plain ledgers gain no
  spurious generated directives.
- New `rustledger-ffi-component-tests` parity cases: assert synth through
  the real wasm component's `load` and `load-file`.

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

codecov Bot commented Jun 16, 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 Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 36.6ms ± 5.4ms 25.3 MB
beancount 882.3ms ± 9.1ms 50.6 MB
Comparison 24.1x faster 2.0x less

vs main branch (2026-06-16)

Metric main this PR Change
Time 35.9ms 36.6ms +1.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 fixes a behavioral divergence in the FFI load surfaces by routing file loads through the canonical rustledger_loader processing pipeline (ensuring the pre-booking SYNTH pass runs), and adds regression tests to pin synth behavior across FFI JSON-RPC and WASM component surfaces.

Changes:

  • rustledger-ffi-wasi load_file now delegates to rustledger_loader::load(..) (canonical pipeline incl. synth), removing the hand-rolled booking loop.
  • rustledger-ffi-wasi load_source now runs the loader’s PreBookingSynth plugin pass on parsed directives.
  • New integration tests in ffi-wasi and new parity tests in ffi-component-tests assert synth-generated Open directives surface with the expected “generated entry” marker (lineno == 0).

Reviewed changes

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

File Description
crates/rustledger-ffi-wasi/tests/load_synth_plugins.rs Adds integration coverage asserting synth Open generation through load_file, load_source, and JSON-RPC load handlers.
crates/rustledger-ffi-wasi/src/helpers.rs Switches file loads to canonical loader pipeline; adds synth pass execution for string loads; introduces LedgerError -> FFI Error mapping.
crates/rustledger-ffi-component-tests/tests/parity.rs Adds parity assertions that the WASM component surfaces synth Open directives for both load and load-file.

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

@github-actions

github-actions Bot commented Jun 16, 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

Resolve Copilot review feedback on the load_source synth injection:

- Don't ignore `run_plugins`'s `Result`: a fatal `ProcessError` is now
  surfaced as an error instead of being silently dropped.
- Gate booking on parse success only (`no_parse_errors`), captured before
  the synth pass appends its diagnostics — so synth/plugin errors no longer
  suppress booking/interpolation (matches the canonical pipeline and prior
  load_source behavior, where the gate reflected only parse errors).
- Mirror the loader's `python:` plugin-prefix handling: strip the prefix
  and set `force_python` instead of hardcoding `false`.
- Fix the `ledger_error_to_ffi` doc comment to match the implementation
  (non-parse phases map to "validate", not "parse").

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen merged commit ddbbdc3 into main Jun 16, 2026
62 checks passed
@robcohen
robcohen deleted the fix/ffi-synth-pass branch June 16, 2026 18:57
@robcohen robcohen mentioned this pull request Jun 16, 2026
robcohen added a commit that referenced this pull request Jun 16, 2026
Patch release. Includes the FFI synth-pass parity fix (#1404): the
rustledger-ffi-wasi load surface now runs the pre-booking synth pass
(auto_accounts, document_discovery) through the JSON-RPC and WIT/component
surfaces, matching the native loader.

Co-authored-by: Claude Opus 4.8 <[email protected]>
robcohen added a commit that referenced this pull request Jun 20, 2026
The string-input FFI load surface (`load_source`, backing `ledger.load`,
`ledger.validate`, and `ledger.query`) was a hand-rolled second loader —
parse + a manual booking loop — that drifted from the canonical
`process::load` pipeline (`sort → synth → book → regular → finalize`). Two
phases were silently missing:

1. The post-booking REGULAR plugin pass. File-declared `plugin "..."`
   directives for regular plugins (`rename_accounts`, `split_expenses`,
   `currency_accounts`, `unrealized`, …) never ran when a ledger was loaded
   from source text. Confirmed: a ledger with `rename_accounts` returned the
   pre-rename account through `load_source`.
2. The date sort. Directives booked and surfaced in file order, not date
   order.

This was the same class of bug as the synth-pass gap fixed in #1404 — which
patched in one `run_plugins(PreBookingSynth)` call rather than removing the
duplicate. `load_file` was already collapsed onto the canonical loader; only
`load_source` remained hand-rolled because the string surface has no
filesystem to resolve `include`s against.

Collapse `load_source` onto the canonical pipeline via an in-memory VFS:
build a `VirtualFileSystem` holding the source as `<source>`, run
`Loader::with_filesystem(..).load()` + `process(.., validate:false)`, and
rebuild the wire DTO from the returned `Ledger`. `validate:false` preserves
this surface's load-only error contract (booking errors surface; semantic
validation stays the `ledger.validate` endpoint's job). Declared includes are
recovered from a parse for the DTO and stubbed as empty files in the VFS so
include resolution is a no-op — preserving the historical "list includes,
don't resolve, don't error" behavior (an unresolved include must not emit a
parse-phase error, which would suppress validation). Any future pipeline
phase now reaches the FFI for free.

Regression tests in `tests/load_regular_plugins.rs` cover regular-plugin
execution, date sorting, and include listing through `load_source`.

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