Keep a bill's old name as a match alias when it is renamed

Merged
JA
1mo ago
+322-22

Closes #503

Before: renaming a bill that has no merchant silently stopped its payments auto-linking. An example is a detected "FNB App Payment To Golf Cart J Kaiser" renamed to "Golf Cart". The picker's ranked list dropped those payments too. Detection then offered the old name as a new bill, and the stale check could retire the renamed one.

After: the old name is kept as a match alias. The bill keeps matching the same payments, keeps being claimed by detection, and shows the old name under "Also matches" on its page. This holds even when a rename only changes case, such as "CITY WATER" to "City Water". Matching is still exact: renaming to "Golf Cart" does not make it match "Golf Cart Hire".

How: a before_update callback on RecurringTransaction appends name_in_database to matcher_hints["name_aliases"] when a merchant-less series is renamed. The old name is stored exactly as it was, an exact duplicate is skipped, and the current name is never kept as its own alias. A manual link teaches an alias by the same exact-spelling rule. The callback skips data-import restores. RecurringTransaction#matcher_names (raw) and #known_names (normalised) are now the one source every reader uses.

Summary

Completes #503, following its triage plan as revised after the Gatekeeper review and after CodeRabbit's case-only finding.

Scope and dependencies

  • Issue: #503
  • Parent PR/branch, if stacked: none
  • Intentionally out of scope:
    • Loosening identity to substring matching (rejected in #503).
    • A UI to remove an alias.
    • Bills already renamed before this change. They have no alias, so the owner's Golf Cart bill still needs one manual link, or a rename back and forth, to recover.

Siblings. I grepped series.name, recurring.name and where(name: under app/models/recurring_transaction* and in the bills, recurring occurrences and recurring transactions controllers.

HitOutcome
Matcher#known_namesdelegates to the model
Allocator#learn_from_manual_attach!learns an alias unless series.matcher_names already holds the exact spelling (adversarial review, item 2)
RecurringOccurrencesController#matcher_name_patternsuses raw matcher_names (Gatekeeper note 3)
Identifier#identity_key → identity_keys / series_by_identity (candidate_patterns, identify_recurring_patterns)a series is indexed under every name it matches
Identifier#claimed_income_source?reads matcher_names
Identifier#manual_recurring_matches_entry?reads matcher_names
RecurringTransaction#matching_transactionswhere(name: matcher_names)
DeclaredBill duplicate check, sibling_scopeunchanged: these ask about display names
duplicate_keyunchanged: a deliberately strict audit rule
find_matching_transaction_entriesunchanged: takes an explicit name at creation
BillsHelper#bills_matcher_hintsunchanged: display-only, and picks up the new alias by itself (Gatekeeper note 1)

Case-only renames (CodeRabbit 4326121572). The identity index, matching_transactions, claimed_income_source? and manual_recurring_matches_entry? compare stored names exactly. So the old spelling is kept even when a rename only changes case or spacing. Matcher normalises through known_names, so an extra spelling there is harmless.

Writers of name. The bill edit form, the assistant update_bill tool, Api::V1::RecurringTransactionsController (permit(:name), :252 and :272) and the data importer update existing rows. The callback covers the first three, and the importer opts out. Smart fills, new-from-transaction and detection create rows, which the callback skips.

Declaring a bill with an alias's name. DeclaredBill's duplicate check still compares display names only, so a user can declare a new bill whose name is another bill's alias. Both then index under one identity key, and claiming falls back to amount nearness. Manual-link aliases already allowed this on main.

Behaviour change beyond renames. Aliases learned from a manual link were already read by the matcher. They are now also read by detection, income-source claiming, the manual-bill variance refresh and the stale check. This is intended: an alias is a name the user has told the app belongs to the bill. Before, these paths disagreed with the matcher about what the bill was.

Importer decision (Gatekeeper note 2). The importer sets restoring_matcher_hints, so a restore writes the exported matcher_hints exactly. A local name that differs is not treated as a rename.

Evidence checklist

  • Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
  • Contract rows touched by this change name existing tests.
  • Any gate test has an observed failing mutation transcript attached.
  • Fixtures used as evidence assert internal consistency.
  • Financial calculations use BigDecimal and documented rounding points. (No financial calculations.)
  • De-identification uses token-shape allowlists; no personal data is included.
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

Validation

The tests were written first. Five failed on main for the reason they name:

  • no alias was stored;
  • matching_transactions returned 0 of 3;
  • detection created a second bill (expected 1, got 2);
  • the matcher wrote no allocation.

The two case-only tests failed on fd138018: no alias was stored, and detection made a duplicate (expected 1, got 2).

TestGuards
renaming a name-keyed bill keeps the old name as a match alias (with assert_no_difference on occurrences and entries)callback
every earlier name survives repeated renames, and none is stored twicealias dedupe
renaming a bill back to an earlier name drops it from its aliasescurrent name kept out of "Also matches"
a rename that only changes case or spacing keeps the old spellingexact-value comparison
a case-only rename keeps the bill claimed and its payments foundcase-only rename end to end (detection, candidate_patterns, matching_transactions)
renaming a merchant-keyed bill adds no aliasmerchant guard
creating a bill and editing other fields add no aliasupdate-only, name-change-only
matching_transactions still finds a renamed bill's payments under the old namestale check
detection keeps claiming a renamed bill instead of suggesting a duplicate (also asserts candidate_patterns is empty)identity_keys
a renamed name-keyed bill still auto-links a payment under its old namematcher end to end
manually attaching an entry that differs only in case keeps that spelling (also asserts matching_transactions finds it)allocator exact-spelling rule
manually attaching an entry under the bill's own name teaches no aliasallocator skips the bill's own name
a rename does not widen what the bill matches (Golf Cart Hire; old name on another account)exact identity kept
income_source_candidates still skips a declared income source after it is renamedclaimed_income_source?
a renamed manual bill keeps tracking the payments under its old namemanual_recurring_matches_entry?
re-importing a bill renamed locally restores its matcher hints exactlyimporter flag

Mutations, each reverted after its run. Each was caught only by the test named:

MutantCaught by
remove the before_update callbackall rename tests and the importer test
drop restoring_matcher_hints = true in the importerre-import test
drop the alias deduperepeated-renames test
drop - [ name ] in the rename callbackrename-back test and repeated-renames test
allocator back to known_names.include?(normalize_name(entry.name))manual case-only attach test
restore the normalised-equality skipboth case-only tests
replace the merchant guard with truemerchant-keyed test
matching_transactions back to where(name: name)stale-check test
identity_keys back to the name onlydetection test
claimed_income_source? back to recurring.name ==income test
manual_recurring_matches_entry? back to entry.name == recurring.namemanual-bill test

Commands and results on cdaa16f1. The commits are re-authored to the owner, and the first two have the same trees as fd138018 and 06ff46cd.

  • The rename-back test and the two new allocator tests were written first. Before the fix:
    • the rename-back test failed (expected ["Cart lease"], got ["Golf Cart", "Cart lease"]);
    • the case-only attach test failed (expected ["WATSON PROPERTY"], got nil);
    • the own-name attach test passed, because it is a negative guard that holds both before and after.
  • bin/rails test, full suite: 12460 runs. Redis was down for that run, so every file with a failure was re-run with Redis up: 1288 runs, 1 failure, 1 error, 1 skip. All of these are sandbox-only:
    • Provider::OpenaiTest#test_extra_headers_stringifies_nested_values_Ruby-style expects Ruby 3.4's Hash#inspect, and the sandbox runs Ruby 3.3.
    • BillsControllerTest#test_a_suggestion_against_a_skipped_occurrence_from_last_month_stays_out_of_the_payment_review_queue errors identically on main (bb3c14a5) today. It is date-dependent and not this PR's.
    • The Anthropic hosting and registry tests read the sandbox's ANTHROPIC_BASE_URL, so they ran with it unset.
  • RuboCop: no offences. Brakeman: 0 warnings. No ERB changes.
  • Checked against upstream AGENTS.md: logic is in models, Minitest with fixtures, no UI or API change, and commit subjects are under 72 characters.

Risks, blockers, and follow-up

  • Detection, income claiming, the manual variance refresh and the stale check now treat learned aliases as the bill's own names (see above). A wrong manual link was already a wrong alias for the matcher; it now has the same effect on these paths.
  • "Also matches" can now list two spellings of one name after a case-only rename. That is honest about what is stored, and no UI change is made.
  • Upstream: #503 records that this is we-promise/sure#3591, which another contributor's open PR we-promise/sure#4222 mostly fixes. The gaps that PR leaves were posted there on 2026-10-21. No upstream PR from this fork is planned.
  • Aliases grow without a cap and cannot be removed from the UI. A follow-up issue can add a "forget" action if it is wanted.