Skip to content

feat: expose import over the component boundary (WIT 3.5.0) - #1764

Merged
robcohen merged 3 commits into
mainfrom
feat/component-import-interface
Jul 12, 2026
Merged

robcohen merged 3 commits into
mainfrom
feat/component-import-interface

Conversation

@robcohen

Copy link
Copy Markdown
Member

Implements the top Now item from the revised importing roadmap (#1763): the import engine was reachable only through rledger extract — rustfava imports via Python beangulp and the component had no import surface, so no GUI user could touch the Rust machinery.

What

Adds an importer interface to the rustledger:ledger WIT world (3.4.0 → 3.5.0, additive):

  • identify(filename, content) — builtin recognition by extension, with an OFX/CSV content sniff so misnamed downloads (e.g. an OFX body served as .txt) still resolve.
  • infer(filename, content) — CSV auto-inference returning an importers.toml entry as a bare TOML table that round-trips into extract: the host shows it, the user tweaks it, it goes straight back.
  • extract(filename, content, config) — CSV/OFX extraction from bytes (no filesystem needed); extracted entries carry the statement filename as their source location.

Canonical-function discipline

The [[importers]] entry schema moves from the CLI into rustledger_importer::toml_entry (new toml-config feature: serde + toml, off by default so slim builds stay slim). The CLI and the component now share one config parser — including parse_amount_locale and the #1133 regression test, which moved with it. The .wasm plugin-importer runtime stays host-side (a component cannot host nested guests); WIT docs say so explicitly.

Testing

  • toml_entry: entry parsing, config building, bare-table shape, inference→TOML→config round-trip, locale Debug-form round-trip.
  • Component convert unit tests (native): identify by extension + sniff, infer→extract loop, OFX minimal config, malformed-config rejection.
  • Parity suite against the real wasip2 component: all 21 tests pass locally, including a new importer_extract_matches_native_engine drift guard (component result vs native engine on identical input) and the version-lockstep test at "3.5".
  • Full rustledger CLI suite green; clippy -D warnings (all features/targets), fmt, rustdoc clean.

Follow-up (separate PRs)

rustfava ingest backend consuming this interface (beangulp stays as the escape hatch); enrichment (dedup-against-session, ML suggestions) as a 3.6 addition once the UI flow exists.

🤖 Generated with Claude Code

The import engine (CSV auto-inference, OFX, categorization, dedup)
was reachable only through `rledger extract` — rustfava's import UI
runs Python beangulp and the WASI component had no import surface, so
every GUI user was cut off from the Rust machinery (import review,
roadmap "import over the component boundary"). This adds an
`importer` interface to the `rustledger:ledger` world (3.4.0 ->
3.5.0, additive):

- identify(filename, content) — builtin recognition by extension
  with an OFX/CSV content sniff for misnamed downloads;
- infer(filename, content) — CSV auto-inference returning an
  `importers.toml` entry (bare TOML table) that round-trips into
  extract, so a host can show it, let the user edit, and pass it
  back;
- extract(filename, content, config) — CSV/OFX extraction from
  bytes, entries carrying the statement filename as provenance.

The `[[importers]]` entry schema moves from the CLI into
`rustledger-importer::toml_entry` (feature `toml-config`) so both
surfaces share ONE canonical config parser instead of the component
re-deriving it (Phase-3 discipline); the CLI now consumes the moved
module, including `parse_amount_locale`. The `.wasm` plugin-importer
runtime stays host-side — a component cannot host nested guests.

Verified end-to-end: the wasip2 component builds and the parity
suite (21 tests) passes against it, including a new
importer_extract_matches_native_engine drift guard; API_VERSION
"3.5" stays in lockstep with the WIT package (parity-enforced).

Co-Authored-By: Claude Fable 5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner July 12, 2026 01:17
Copilot AI review requested due to automatic review settings July 12, 2026 01:17
@codecov

codecov Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.78135% with 38 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-importer/src/lib.rs 0.00% 21 Missing ⚠️
crates/rustledger-importer/src/toml_entry.rs 94.13% 17 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.2ms ± 3ms 23.6 MB
beancount 894.6ms ± 9.2ms 50.7 MB
Comparison 27.8x faster 2.1x less

vs main branch (2026-07-11)

Metric main this PR Change
Time 33.1ms 32.2ms -2.7% ✅
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 exposes the existing rledger extract import engine (CSV inference + CSV/OFX extraction) over the WASI Component Model boundary by adding a new importer interface to the rustledger:ledger WIT world and implementing it in rustledger-ffi-component. It also centralizes the [[importers]] TOML entry schema + config mapping in rustledger-importer so the CLI and component share one canonical parser.

Changes:

  • Bump WIT world version to rustledger:[email protected] and add an exported importer interface (identify, infer, extract).
  • Introduce rustledger_importer::toml_entry (feature-gated toml-config) as the canonical parser/mapping for a single importer entry, reused by CLI + component.
  • Add component/native parity and unit tests covering identify/infer/extract flows.

Reviewed changes

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

Show a summary per file
File Description
crates/rustledger/src/cmd/extract_cmd/mod.rs Switches CLI locale parsing/tests to use the canonical toml_entry module.
crates/rustledger/src/cmd/extract_cmd/config.rs Keeps CLI-side config discovery/wrapper while re-exporting canonical entry/schema helpers.
crates/rustledger/Cargo.toml Enables rustledger-importer’s toml-config feature for the CLI crate.
crates/rustledger-importer/src/toml_entry.rs Adds canonical importers.toml entry schema, parsing, config building, and inference→TOML rendering.
crates/rustledger-importer/src/lib.rs Exposes toml_entry behind the toml-config feature.
crates/rustledger-importer/Cargo.toml Adds the toml-config feature (optional serde + toml).
crates/rustledger-ffi-component/wit/world.wit Bumps package to 3.5.0 and defines the new importer interface.
crates/rustledger-ffi-component/src/lib.rs Implements and exports the new importer guest interface; bumps runtime API version to 3.5.
crates/rustledger-ffi-component/src/convert.rs Implements importer identify/infer/extract logic bridging bytes + canonical config parsing.
crates/rustledger-ffi-component/Cargo.toml Adds rustledger-importer dependency with toml-config for importer interface support.
crates/rustledger-ffi-component-tests/tests/parity.rs Adds a parity/drift-guard test comparing component extraction vs native importer engine.
crates/rustledger-ffi-component-tests/Cargo.toml Enables toml-config on rustledger-importer for the parity test.
Cargo.lock Records new dependency edges and adds toml as a resolved dependency.

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

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
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 and others added 2 commits July 12, 2026 02:17
Four confirmed correctness bugs and three test/CI-quality findings
from the multi-agent review of #1764:

- Format dispatch is now the canonical
  rustledger_importer::detect_format, with the extension
  AUTHORITATIVE when recognized: a .csv whose narration mentions
  "<OFX" stays on the CSV path (the sniff previously overrode the
  extension, and the WIT surface had no way to force CSV). The
  policy lives in the importer crate so it cannot drift per-surface.
- currency defaults to USD in the component's extract, matching the
  schema's documented default (the CLI injects it via --currency);
  an account-only entry no longer errors on OFX.
- Non-UTF-8 content is decoded lossily in extract, consistent with
  identify — a Latin-1 OFX 1.x download (CHARSET:1252) degrades to
  replacement characters instead of dead-ending a file identify just
  recognized.
- A mistyped column value (amount_column = 2.0) is now an ERROR
  naming the field instead of a silent fallback to the default
  column — on the CLI too, where it was previously ignored.
- The parity drift guard compares dates/narrations/postings/warnings
  field-by-field, not entry counts (drift-guard rule: assert the
  exact observable).
- New drift test pins the TOML round-trip path against the canonical
  InferredCsvConfig::to_csv_config mapping, plus a test pinning the
  inference-never-emits-Index-secondary-date invariant the TOML
  serializer relies on.
- The component-parity CI job now greps its output for the graceful
  skip marker and fails loudly if the suite skipped despite the
  component having been built (availability-gated-tests rule).

Co-Authored-By: Claude Fable 5 <[email protected]>
- OFX extraction now requires account in the config entry instead of
  silently importing into the builder's Expenses:Unknown default.
- entry_toml_from_inferred returns Result rather than swallowing a
  serialization error into an empty string.
- Grammar/spelling nits in the config.rs module comment.

Co-Authored-By: Claude Fable 5 <[email protected]>
@robcohen
robcohen merged commit 7e36e33 into main Jul 12, 2026
65 checks passed
@robcohen
robcohen deleted the feat/component-import-interface branch July 12, 2026 13:48
robcohen added a commit that referenced this pull request Jul 12, 2026
The version gate is right: 3.5.0 landed on main in #1764, so this
PR's interface additions need their own minor. API_VERSION tracks it
(parity-enforced).

Co-Authored-By: Claude Fable 5 <[email protected]>
robcohen added a commit that referenced this pull request Jul 12, 2026
* feat: dedup + format-loaded on the component import surface

Closes the two gaps a rustfava ingest backend would hit immediately
(walked the fava import flow end-to-end against 3.5.0 before cutting
the release):

- importer.dedup(candidates, existing) -> list<bool> flags duplicate
  entries via the canonical rustledger_ops::dedup::is_duplicate — the
  same fuzzy matcher 'rledger extract --existing' filters with, so
  the review UI and the CLI agree on what counts as a duplicate.
  Per-candidate conversion keeps flags aligned with input order even
  when a candidate fails conversion.
- format.format-loaded(entries) renders LOADED directives (extract
  output, session entries) to canonical beancount text, with the
  loaded->input down-conversion inside the component — previously
  only input-directive could be formatted, forcing hosts to reshape
  directives themselves to save extracted entries.

Both land in the still-unreleased 3.5.0. Parity suite (21 tests)
passes against the rebuilt wasip2 component, now exercising the full
extract -> dedup -> format-loaded loop.

Co-Authored-By: Claude Fable 5 <[email protected]>

* feat: bump WIT contract to 3.6.0 for dedup/format-loaded

The version gate is right: 3.5.0 landed on main in #1764, so this
PR's interface additions need their own minor. API_VERSION tracks it
(parity-enforced).

Co-Authored-By: Claude Fable 5 <[email protected]>

* feat: session-held dedup, structured import warnings (WIT deep dive)

Redesigns the 3.6.0 additions per the WIT design review before
anything ships:

- dedup moves from importer.dedup(candidates, existing) to
  session.dedup(candidates). One move fixes three findings: the host
  no longer re-marshals the full ledger per import (the exact pattern
  the session resource exists to kill); the held side's
  conversion-drop policy is the documented, info()-observable
  from-entries one instead of a silent comparison-set shrink; and the
  implementation uses the batch canonical find_fuzzy_duplicates,
  which precomputes held keys once instead of rebuilding every key
  per candidate.
- extract-result.warnings becomes list<error> (severity "warning",
  phase "extract") — the structured diagnostic record every other
  surface uses, so line/field attribution is additive later rather
  than a record-shape break.
- format-loaded errors now name the failing entry index, so a host
  can exclude one bad entry instead of losing the whole batch.
- WIT dedup doc corrected: the matcher requires the SAME date (plus
  amount and text similarity) — "date proximity" promised behavior
  the canonical does not have.
- format_loaded rustdoc version tag fixed (3.6.0, not 3.5.0).
- The parity dedup guard now checks a fuzzy near-miss (reworded
  narration) against the NATIVE matcher's verdict, not just
  degenerate identity/empty inputs; extract warnings assert message
  parity plus severity/phase.

Remaining design debt (options-less entries surfaces) is tracked in
issue #1766 rather than patched piecemeal here.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[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