Skip to content

feat(importer): a transaction-id link convention for WASM importers - #2532

Merged
robcohen merged 4 commits into
mainfrom
feat/importer-wasm-transaction-ids
Oct 9, 2026
Merged

robcohen merged 4 commits into
mainfrom
feat/importer-wasm-transaction-ids

Conversation

@robcohen

@robcohen robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

What changed

WASM importers can now give each transaction the source format's own id, and extract --existing uses it as identity, as it does ^ofx-<FITID> and ^csv-<id>.

The convention: a link wasm-<importer>/<id>, built by the new guest-side helper rustledger_plugin_types::wasm_id_link(importer, raw_id):

if let Some(link) = wasm_id_link("MT940", &reference) {
    links.push(link); // ^wasm-MT940/<reference> in the ledger
}
  • The importer name is encoded injectively: letters (case kept), digits, - and . stay, and every other byte becomes _ plus two hex digits (My Bank → My_20Bank, a/b → a_2Fb). So two different importer names can never share a namespace. The id is sanitized by the same rule as CSV/OFX ids. None is returned when either has no letter or digit.
  • Dedup (rustledger_ops::dedup) now finds an id's namespace with id_namespace. The namespaces are ofx-, csv-, and one per WASM importer, wasm-<importer>/. A shared id with equal money is the same transaction on any date. Different ids within one namespace mean two transactions. Ids in different namespaces never contradict each other, so moving a bank from one importer (or format) to another falls back to text, as moving from OFX to CSV already does. A wasm-… link with no /<id> is not an id. Importers named My Bank, My-Bank, My_Bank, My/Bank and my-bank get five distinct namespaces; a test pins that one's id never matches another's. Behavior for ofx-/csv- is unchanged.
  • One sanitizer: id_link moved to rustledger_plugin_types::id_link, since a guest cannot depend on rustledger-ops. rustledger_ops::dedup::id_link delegates to it, and WASM_ID_LINK_PREFIX and wasm_id_link are re-exported from rustledger_ops::dedup.

Docs: the importing guide has a new "Transaction ids from a WASM importer" section. wasm_importer_main! has a "Transaction ids" section, the guest module docs have "Transaction ids for duplicate detection", and the plugin-types README, docs/commands/extract.md and the CHANGELOG are updated.

Why a link convention and not an explicit id field

The issue preferred an explicit id field the host turns into a link, if that fits without a breaking change. It doesn't fit:

  • Wire format: a trailing #[serde(default)] field on ImporterOutput would have been fine for already-built modules (serde's derived sequence decoding defaults a missing trailing #[serde(default)] field; not exercised here).
  • Rust source: this is the break. ImporterOutput and EnrichedImporterOutput have public fields and are built with struct literals in importer code. EnrichedImporterOutput has no constructor at all (see tests/guest_macro_compiles.rs, default_enriched_from). A new field would break every importer's source, and a per-directive id would also need a parallel vector that importers keep aligned by hand.

A link is a field every transaction already carries, and the helper makes the convention explicit rather than a magic string. The namespace includes the importer name so ids from different importers cannot contradict each other.

How tested

  • a_wasm_importer_id_link_is_an_id: a shared wasm-mt940/1 link is decisive across a date and narration change. mt940/1 vs mt940/2 are two transactions despite the same text. camt/2 vs mt940/1, or an ofx- id, fall back to text. Bare wasm-1/wasm-2 links are not ids.
  • id_namespaces: every namespace shape and edge case (wasm-mt940/, wasm-/1, wasm-1).
  • Doctests for id_link and wasm_id_link. All existing ofx-/csv- id tests pass unchanged.
  • Shown to fail without the fix: with wasm- links not recognized as ids, both new tests fail.
  • cargo test for plugin-types, ops, importer and rustledger (all features); workspace clippy (-D warnings); rustdoc (-D warnings) for the changed crates: clean.

Closes #2519

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

A WASM importer adds a `wasm-<importer>/<id>` link, built by the new
rustledger_plugin_types::wasm_id_link, and extract --existing treats it
as an id like ^ofx- and ^csv-, with one namespace per importer. The id
sanitizer moves to rustledger_plugin_types::id_link so guests share it;
rustledger_ops::dedup::id_link delegates. A link rather than a new
ImporterOutput field, which would break importers built with struct
literals.

Closes #2519

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 8, 2026 18:29
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 27.3ms ± 0.5ms 376.6ms ± 4.2ms 13.8x 51.3 MB / 50.0 MB
investment 41.5ms ± 0.2ms 1452.4ms ± 7.3ms 35.0x 57.1 MB / 58.1 MB
fifo 39.7ms ± 0.2ms 7780.0ms ± 90.2ms 195.8x 55.2 MB / 57.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 27.6ms ± 0.8ms 27.3ms ± 0.5ms -1.1% ✅ within noise 51.1 MB → 51.3 MB (+0.3%)
investment 41.7ms ± 0.2ms 41.5ms ± 0.2ms -0.5% ✅ within noise 57.3 MB → 57.1 MB (-0.2%)
fifo 40.0ms ± 0.2ms 39.7ms ± 0.2ms -0.7% ✅ within noise 55.5 MB → 55.2 MB (-0.6%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.4ms, investment 0.1ms, fifo 0.1ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Oct 8, 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 Oct 8, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

The importer name was lowercased and every other character mapped to
`-`, so `My Bank` and `my-bank` (or `a/b` and `a-b`) shared a namespace
and one importer's id could match the other's with equal money,
dropping a different transaction. Keep letters (case included), digits,
`-` and `.`, and write every other byte as `_` plus two hex digits.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

Review (adversarial pass)

Namespace collision: a real finding, fixed in 4e08a1e. The importer name was lowercased and every other character mapped to -, so My Bank, my-bank, My/Bank and My_Bank all became wasm-my-bank/. Two such importers' ids then shared a namespace, and an equal id with equal money on the same account would drop a different transaction. The name is now encoded injectively: letters (case kept), digits, - and . stay, and every other byte, _ and / included, becomes _XX hex. Since _ only ever appears as the escape prefix, two different names can never produce the same namespace. / can no longer appear in the name, so the first / still ends the namespace. The registry already keeps one importer per name, so namespaces are unique per run.

  • New assertions in a_wasm_importer_id_link_is_an_id: those five names give five distinct namespaces, and one importer's id does not match the other's. Fail-without-fix: with the old sanitizer, the test fails.
  • Doctest: wasm_id_link("My Bank", …) is wasm-My_20Bank/…, and MT940 keeps its case.
  • The id part can still collide after sanitizing (a b/a:b), the same as CSV/OFX. Dedup still requires the same account, commodity and amount.

Cross-PR integration (applies to #2524, #2526, #2527, #2531, #2532, #2533)

All six PRs were merged onto origin/main 25fc6be in two orders: 2524→2526→2527→2531→2532→2533, and the reverse.

Second review round (the injective encoding, as fresh code): no new defects

Added 311de54: crates/rustledger-ops/tests/wasm_id_link_properties.rs. Names and ids are generated over kept ASCII, _, /, ., spaces, literal _20/_5F/_2F runs, tab, %, ^, multi-byte é/€/日本/emoji, and the empty string.

  • Link grammar. Every emitted link uses only [A-Za-z0-9-_/.], which is both rledger's lexer rule and beancount's. A non-ASCII name becomes _XX runs, one per UTF-8 byte. Every link parses, and comes back byte-identical through parse → canonicalize_directives (what extract writes) → parse. Edge cases checked: an empty name (None), a 10,000-byte name (no limit; the link is len + 7), names like . and - that sit right after the fixed wasm- prefix, and ids like /x, x/, ..1.
  • Beancount oracle. 55 links built from names like MT940, My Bank, My_20Bank, a/b, ., Ünïcode Bänk, 日本, a 300-char name and _: beancount 3 loads the file with 0 errors and every link comes back intact. rledger check is clean, and rledger format leaves the links unchanged. One caveat: this file was produced by a Python copy of the encoder; the Rust encoder itself is covered by the proptest above.
  • Literal _XX names. _ is always escaped, so My_20Bank becomes My_5F20Bank and cannot collide with My Bank (My_20Bank). The proptest decodes every namespace back to the exact name bytes, which shows the encoding is injective, and checks that random distinct names never share a namespace.
  • Namespace split when the id has /. The encoded name can never contain /, so the first / always ends the namespace (wasm-a/b/c is name a, id b/c; the name a/b with id c is wasm-a_2Fb/c). A dedup property checks, for random names and ids containing /: the same link matches whatever the text; one importer with different ids gives two transactions; different importers never contradict each other, so text decides.
  • The tests can fail. With _ kept unescaped, 2 of 4 tests fail. With the namespace split at the last /, the dedup property fails. With the round-1 lossy sanitizer, 3 of 4 fail.

robcohen and others added 2 commits October 9, 2026 02:21
Every emitted link uses only the link charset, decodes back to the
importer name, and survives parse -> canonical format -> parse; different
names never share a namespace (a literal `_20` is escaped again); dedup
splits at the first `/` even when the id contains `/`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen merged commit 7d476e8 into main Oct 9, 2026
49 of 50 checks passed
@robcohen
robcohen deleted the feat/importer-wasm-transaction-ids branch October 9, 2026 04:51
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.

extract dedup: no transaction-id convention for WASM importers

1 participant