Skip to content

fix(extract): match dedup text across Unicode normalization forms - #2533

Merged
robcohen merged 5 commits into
mainfrom
fix/extract-dedup-unicode-normalization
Oct 9, 2026
Merged

robcohen merged 5 commits into
mainfrom
fix/extract-dedup-unicode-normalization

Conversation

@robcohen

@robcohen robcohen commented Oct 8, 2026

Copy link
Copy Markdown
Member

What changed

extract --existing dedup now folds payee + narration to NFC before comparing, in rustledger_ops::dedup::txn_text. The text is decomposed, lowercased, then recomposed: Unicode's canonical caseless match (D145), with to_lowercase standing in for full case folding. So é stored as U+00E9 and é stored as e + U+0301 compare equal, in either case, whichever side uses which form.

NFC, deliberately not NFKC

Canonical equivalents are the same characters by definition. Compatibility equivalents are not always the same text: ²/2, ½/1⁄2, fi/fi, full-width/ASCII. The two failure modes are not symmetric:

  • a false text match silently drops a statement row;
  • a missed match leaves a visible duplicate the user can delete.

So only the folding that can never merge different text is done. Whitespace variants need no folding, because matching already splits on char::is_whitespace, which covers a no-break space. This reasoning is written at txn_text, in docs/commands/extract.md (Duplicate Detection) and in the CHANGELOG, and pinned by a test.

New dependency

unicode-normalization 0.1.25 (MIT/Apache-2.0; its one dependency, tinyvec, is already in the tree).

  • cargo vet: covered by Mozilla's existing wildcard audit for Manish Goregaokar's crates (audits.mozilla.wildcard-audits.unicode-normalization, safe-to-deploy). Importing it added 16 lines to supply-chain/imports.lock (the publisher row and the imported audit). No exemption was needed. cargo vet --locked passes.
  • cargo deny: cargo deny --locked check gives advisories ok, bans ok, licenses ok, sources ok.
  • Cargo.lock: only the new package and its edge from rustledger-ops (+10 lines). cargo wanted to re-resolve unrelated Windows-only deps (windows-sys, getrandom) while adding it, so the lock change was kept minimal by hand and verified with cargo metadata --locked.

How tested

  • nfc_and_nfd_spellings_of_the_same_text_match: NFD new vs NFC existing, NFC vs NFD, and upper-case NFD vs NFC are each recognized as one duplicate, by text and not by amount alone.
  • compatibility_forms_are_not_folded: fish/fish and Unit²/Unit2 are not matched.
  • Shown to fail without the fix: with plain to_lowercase, the NFC/NFD test fails. With NFKC folding instead of NFC, the compatibility test fails. So the second test pins the decision, and is not decoration.
  • cargo test -p rustledger-ops -p rustledger --all-features, workspace clippy (-D warnings) and rustdoc (-D warnings) are clean.

Closes #2507

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Fold payee and narration to NFC (decompose, lowercase, recompose) before
dedup compares them, so an NFC and an NFD spelling of the same text are
one transaction rather than a re-imported duplicate. Compatibility forms
(NFKC) are deliberately not folded: they are not always the same text,
and a false match drops a row silently. Adds unicode-normalization,
covered by Mozilla's imported cargo-vet audit.

Closes #2507

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:30
@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

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 45.4ms ± 1.8ms 821.2ms ± 4.5ms 18.1x 51.1 MB / 50.0 MB
investment 68.8ms ± 0.9ms 3094.9ms ± 28.0ms 45.0x 57.1 MB / 58.2 MB
fifo 68.8ms ± 1.7ms 15407.9ms ± 109.4ms 223.9x 55.1 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.5ms ± 0.5ms 45.4ms ± 1.8ms +1.9% ✅ within noise 51.2 MB → 51.1 MB (-0.3%)
investment 68.2ms ± 0.7ms 68.8ms ± 0.9ms +0.9% ✅ within noise 57.1 MB → 57.1 MB (-0.0%)
fifo 66.4ms ± 1.0ms 68.8ms ± 1.7ms +3.6% ✅ within noise 59.2 MB → 55.1 MB (-7.0%)

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

@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

@robcohen

robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

Review (adversarial pass): the lockfile

  • The +10-line lock change is stable. cargo build --locked, cargo metadata --locked and cargo vet --locked all pass. cargo update -w --dry-run locks 0 packages on this branch, as it does on main. CI has no lockfile-freshness job (no cargo update --locked step). Cargo Vet Auto ran on this PR and pushed nothing, because the imported Mozilla audit already covers the crate.
  • The windows-sys/getrandom shift is not caused by this PR. It appeared only when cargo did a fresh resolution to add a new registry crate; adding a crate already in the lock (tested with tinyvec) changed nothing. A full cargo update --dry-run, which is what Dependabot runs, proposes the same rustix 1.1.4→1.1.5 bump and adds windows-sys 0.59/0.60 on both main and this branch. So the next Dependabot PR will move those Windows-only entries whether or not this lands, and nothing here works against it.

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.

@robcohen
robcohen merged commit cf37f96 into main Oct 9, 2026
53 of 54 checks passed
@robcohen
robcohen deleted the fix/extract-dedup-unicode-normalization branch October 9, 2026 04:48
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: NFC and NFD forms of the same text don't match

1 participant