Skip to content

feat(extract): rewrite payees on import with [payees] tables - #2585

Merged
robcohen merged 2 commits into
mainfrom
feat/extract-payee-rewrite
Oct 10, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/extract-payee-rewrite

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

Implements the option-A decision on #2550: rledger extract rewrites a bank's payee text to the name the ledger uses.

  • Config: a shared top-level [payees] table in importers.toml, plus an optional per-entry [importers.payees], tried first.
  • Matching: a key is a case-insensitive substring, as in [importers.mappings], or a case-insensitive regex written between slashes ("/^AMZN Mktp/"). The first match in file order wins. File order is kept with the same key-span technique the equal-length mappings fix (importers.mappings: equal-length overlapping keys produce nondeterministic accounts #2423) used. That code is now shared by Mappings and the new PayeeTable. Matching goes through rustledger_ops::categorize::RulesEngine, the engine mappings already use, so the two can't drift apart.
  • Input: the raw payee, or the narration when the row has none (a blank payee counts as none).
  • Where: rustledger_importer::payees::PayeeRules::apply runs on the importer's output in run_with_ledgers, right after importer.extract and before order::chronological and --existing dedup. So it covers CSV, OFX and WASM, and dedup compares canonical names. The WASI component's importer.extract applies the entry's own table too.
  • Account mappings are unchanged. They run inside the importer, so they still match the raw bank text.
  • Only the payee changes. The narration keeps the bank's description, and no metadata copy is made.
  • Errors: a regex that won't compile, an empty pattern or an empty payee is a config error naming its table, e.g. importer 'bank': [importers.payees]: "/(/" is not a valid regular expression: ... or the top-level [payees] table: .... load_importers_config validates the tables, so --list-importers (and ag-rledger) report it too. build_config_from_entry validates the entry's table for the component.

Decision made where the issue left it open: the shared table applies whenever an importers.toml is found, --auto and raw column flags included, the same way wasm_importer_dir does. On a run that would not otherwise read the config (--auto), a file that fails to load is skipped with a warning, not an error. That keeps test_auto_is_not_failed_by_an_unparsable_config_it_never_reads true.

Docs: a new "Payee Rewriting" section in docs/commands/extract.md, plus a note in Duplicate Detection. CHANGELOG has an entry under Importing & Ingestion.

Tests

  • CSV: payee rewritten, narration kept, and an account mapping on "HOFER DANKT" still matches the raw text.
  • Narration matched when there is no payee column.
  • OFX output rewritten by the shared table, with no [[importers]] entry.
  • WASM: plugin_conformance::wasm_importer_output_gets_payee_rewrites, through the real binary.
  • Component: extract_applies_the_entry_payee_table (CSV + OFX, plus the bad-regex error).
  • --existing dedup matches a ledger that uses the canonical name (AMZN Mktp DE*4Z81T → Amazon).
  • Per-entry table beats the shared one, and the shared one still applies to rows the entry's table misses.
  • First match in file order, including a case where the sorted key order would pick the other rule.
  • Invalid regex in an entry table and in the shared table, each naming its table, on extract and on --list-importers.
  • ag-rledger extract applies both tables, and ag-rledger extract --list-importers accepts them and rejects a bad pattern.
  • --auto applies a [payees] table from an importers.toml it discovered.
  • Unit tests for PayeeRules.

Every new test was shown to fail with the fix reverted: the apply calls removed in the CLI and the component, validate_payees removed, the key-order sort removed, and the blank-payee filter removed. The one exception is payees_tables_are_accepted_by_list_importers, an acceptance check: ImportersFile has no deny_unknown_fields, so it passes either way.

Local: cargo test -p rustledger-importer (265), -p rustledger-ffi-component (40) and -p rustledger --all-features (1001, including ag_rledger_test and plugin_conformance with the WASM guests built), all passing. Also ran cargo clippy --workspace --all-features --all-targets -D warnings, cargo doc -D warnings for the three crates, and the release-config clippy. The only warning there is the existing unused wasm_plugins_dir in cli_commands_test.rs, which is also on main.

Closes #2550

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

A bank writes "Hofer Dankt" where the ledger says "Hofer", and nothing
could change it: [importers.mappings] only picks the contra account.

importers.toml now takes a shared top-level [payees] table and a
per-entry [importers.payees] (tried first). A key is a case-insensitive
substring, or a case-insensitive regex between slashes; the first match
in file order wins. The raw payee is matched, or the narration when the
row has none. The rewrite runs on every importer's output (CSV, OFX,
WASM, and the component's importer.extract for an entry's own table),
after account mappings matched the raw text and before ordering and
--existing dedup. Only the payee changes. A bad pattern is a config
error naming its table, reported by --list-importers too.

Closes #2550

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 10, 2026 21:08
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 46.8ms ± 1.4ms 868.7ms ± 9.5ms 18.6x 51.2 MB / 50.2 MB
investment 74.8ms ± 0.7ms 3024.3ms ± 58.9ms 40.4x 57.3 MB / 57.9 MB
fifo 68.4ms ± 0.4ms 15302.7ms ± 242.4ms 223.7x 57.0 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 46.8ms ± 0.9ms 46.8ms ± 1.4ms +0.0% ✅ within noise 51.2 MB → 51.2 MB (-0.0%)
investment 75.5ms ± 1.5ms 74.8ms ± 0.7ms -0.9% ✅ within noise 57.2 MB → 57.3 MB (+0.3%)
fifo 68.7ms ± 0.6ms 68.4ms ± 0.4ms -0.5% ✅ within noise 55.1 MB → 57.0 MB (+3.5%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.7ms, investment 0.7ms, fifo 0.3ms), 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 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.81395% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-importer/src/toml_entry.rs 75.00% 12 Missing ⚠️
crates/rustledger-importer/src/payees.rs 98.21% 3 Missing ⚠️
crates/rustledger/src/cmd/extract_cmd/mod.rs 98.46% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

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

@robcohen

Copy link
Copy Markdown
Member Author

Self-review rounds

Round Scope Findings Action
1 Whole diff, adversarial: the precedence of resolve_payee_rules across the CSV, OFX/WASM, --auto, --ledger profile and --config paths; blank WASM payees; key-order restoration; validation on --list-importers, ag-rledger and the component; docs (a) A blank payee from a WASM importer would have been matched as the input instead of the narration. Fixed before the first push, with a unit test shown to fail without the fix. (b) docs/guides/importing.md didn't mention the feature. (a) fixed in 90eac22. (b) pointer added in 415db2d. Required checks green on 415db2d
2 Re-read of toml_entry.rs (shared ordered-pairs deserializer, PayeeTable, entry validation) and the CLI wiring. Full suites already ran on this code: importer 265, component 40, rustledger 1001 including ag_rledger_test and plugin_conformance with the WASM guests built None Converged

Fail-without-fix: with the apply calls removed (CLI and component), validate_payees removed, the key-span sort removed, and the blank-payee filter removed, every new test failed. The one exception is the acceptance check payees_tables_are_accepted_by_list_importers, which passes either way because ImportersFile does not deny unknown keys.

Behavior note: with --config naming a file that fails to parse, an OFX/WASM run in a slim build (no python-plugin-wasm) now stops with the parse error. Before, it warned and carried on. The default build already stopped there, in resolve_scan_dirs_explicit.

Non-required rustfava tests against PR component: 8 failed, 671 passed, the known baseline.

@robcohen
robcohen merged commit e6d624a into main Oct 10, 2026
49 of 50 checks passed
@robcohen
robcohen deleted the feat/extract-payee-rewrite branch October 10, 2026 22:20
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: rewrite payees from the source file ("Hofer Dankt" → "Hofer")

1 participant