Drop the Investment Contributions category from plain transfer legs
Summary
A transfer leg whose kind is funds_movement or cc_payment is meant to be uncategorized (Transaction::UNCATEGORIZED_EXCLUDED_KINDS). Transfer#update_investment_contribution!(false) leaves it that way, but other paths changed the kind and kept the Investment Contributions category of the old kind, so those transfers still showed under that category:
Transfer::InvestmentKindReconcilerrewrites the kind withupdate_columns, which skips callbacks.Account::ProviderImportAdaptergave a leg labelled "Contribution" the category even when its matched transfer made it afunds_movementleg (a broker deposit matched to a bank transfer).- Any later save that moved a leg to a plain transfer kind (
Transfer#confirm!on the inflow, a re-sync deriving the kind from its transfer) kept the category.
Now:
Transaction.drop_stale_contribution_categories!(scope)clears the category from the plain-transfer legs of a scope that still carry it without a lock of the user's. A lock an automatic categorizer set itself (the AI, which locks every category it assigns, or Bayes withlock_results) counts as no lock while the category is still the value it set; one the user has changed since keeps its lock. Clearing such a category also removes that lock and the categorizer's claim (the AI's enrichment, Bayes'slocked_by_bayesflag), so the leg is left as if never categorized and a later choice of the same category is the user's. Built onTransaction.reassign_category!, it rechecks each row underFOR UPDATEand touches exactly the entries it cleared, so a category or lock a user sets meanwhile is not overwritten.- A save that moves a leg to a plain transfer kind runs it for that leg afterwards, unless the same save set the category. It checks the row in the database rather than the record's copy, which may predate a category a categorizer set after it was loaded. Any other category stays.
- The reconciler runs it with the kind it rewrites (
update_columnsskips callbacks). - The import adapter assigns it only while the leg's kind is
investment_contribution. Family::AutoCategorizerleaves plain transfer legs out, asFamily#bayes_categorize_uncategorizeddoes, so the AI cannot put back a category the cleanup removed.data_migration:clear_stale_contribution_categories(DRY_RUN,FAMILY_ID,BATCH_SIZE) runs it over rows already left that way, read and written in batches of 1,000 by default, each its own transaction, so a large cleanup neither loads every id nor holds every row's lock at once. It reports how many it cleared and is idempotent.- A category chosen in the transfer editor is saved under the row lock and locked as the user's, as one set on the transaction itself is, so neither the cleanup nor a sync treats it as stale. Choosing it also drops the automatic categorizers' claim on it, so it stays the user's even where it is the value the AI or Bayes had set. The task's notes say to run
reconcile_investment_transfer_kindsfirst: it gives a leg stored with a stale plain-transfer kind its contribution kind back, which takes it, and any older unlocked editor choice on it, out of the cleanup's scope.
Related: #3766 (the category left on an outflow after a confirmed investment transfer is rejected, whose legs go back to standard) is a different case and stays open. That one cannot tell an auto-assigned category from one the user chose, whereas here the leg's new kind is one that carries no category at all.
Flow
flowchart TD
R["Reconciler rewrites the kind<br/>(update_columns)"] --> D
V["A save changes the kind<br/>(confirm, re-sync)"] --> SS(["Same save set<br/>the category?"])
SS -- yes --> K0["Kept"]
SS -- no --> D
M["Cleanup task<br/>(rows already stale)"] --> D
D["drop_stale_contribution_categories!"] --> Q(["Investment<br/>Contributions?"])
Q -- "another category" --> K1["Kept"]
Q -- yes --> L(["Locked?"])
L -- "by the user" --> K2["Kept"]
L -- "no lock, or the AI's<br/>or Bayes' own lock" --> C["Cleared, with that lock<br/>and the categorizer's claim"]
Only legs of a plain transfer kind (funds_movement, cc_payment) are in scope; an automatic categorizer's lock counts only while the category is still the value it set.
What stops the category coming back:
flowchart LR
I["Provider import labels a leg<br/>Contribution"] --> IK{"Leg's kind is<br/>investment_contribution?"}
IK -- yes --> A1["Gets the category"]
IK -- "no (matched as a plain transfer)" --> A2["Left uncategorized"]
AC["AI auto-categorize"] --> A3["Skips plain transfer legs,<br/>as Bayes already does"]
TE["Category chosen in<br/>the transfer editor"] --> A4["Saved under the row lock, locked as the user's,<br/>automatic claims dropped, so the cleanup keeps it"]
Tests
TransactionTest: "a leg that becomes a plain transfer loses the Investment Contributions category"; "a leg that becomes a plain transfer keeps the Investment Contributions category a rule set" (a category a rule of the user's set and still holds is theirs, as rules don't lock; fails without the change); "a plain transfer leg keeps a category the user locked, set in the same save, or another category"; "with_stale_contribution_category finds unlocked plain transfer legs with the category"; "with_stale_contribution_category counts an automatic categorizer's lock as no lock"; "a leg that becomes a plain transfer loses an Investment Contributions category an automatic categorizer locked" (the AI's and Bayes' locks cleared, a later choice of the user's kept; both fail without the change); "clearing an automatic categorizer's category drops its lock and claim with it"; "a copy loaded before a categorizer set the category still has it cleared"; "a leg becoming a plain transfer keeps a category set after its copy was loaded" (fails with a before-save clear); "drop_stale_contribution_categories! clears unlocked stale legs and touches their entries".ClearStaleContributionCategoriesTaskTest: the dry run counts and clears nothing; the real run clears and reports the count;BATCH_SIZE=1makes one call per batch and clears both legs across the batches;FAMILY_IDclears only that family's legs.Family::AutoCategorizerTest: "leaves plain transfer legs uncategorized" (fails without the change); "leaves a leg that became a plain transfer after it was selected uncategorized": each AI result is applied under the row's lock and skipped if the row has meanwhile become a plain-transfer kind or gained a category (fails without the recheck).Family::BayesCategorizerTest: "a leg that became a plain transfer after it was selected stays uncategorized": Bayes results are applied under the row's lock with the same kind and category recheck as the AI's (fails without the recheck).DataEnrichmentTest: "drop_metadata_flag! removes only that key, and only from the scope". The helper now clears Bayes's lock flag for bothTransaction.drop_automatic_category_claims!andFamily#unlock_own_locks.Transfer::InvestmentKindReconcilerTest: "drops the Investment Contributions category of a leg it makes a plain transfer".TransfersControllerTest: "a category chosen in the transfer editor is locked as the user's"; "a category re-chosen in the transfer editor stays the user's where the AI had set it"; "a different category chosen in the transfer editor keeps the AI's superseded suggestion" (dropping a claim leaves a superseded AI suggestion in place, so it stays in the AI history filter). The four tests added last each fail without their change.Account::ProviderImportAdapterTest: "a broker deposit labelled as a contribution keeps no category once matched as a plain transfer leg".bin/rubocopon changed files: no offenses.bin/brakeman: no warnings.- Full suite, on main d3e3206d5: 15,836 runs, 0 failures, 0 errors, 9 skips.
bin/brakeman: no warnings. - Rebased onto main 6aa8cf218 (2026-11-21): the AI categorizer's scope keeps main's rule that excluded transactions are never sent, and still leaves plain transfer kinds out;
transaction_test.rbkeeps main's tag tests beside this PR's. Full suite (--all) 18,162 runs, 0 failures, 9 skips.
🤖 Generated with Claude Code
Summary by cubic
Stops plain transfer legs (funds_movement, cc_payment) from keeping the Investment Contributions category their old kind gave them, so they no longer show under that category. The category is now cleared whenever a leg becomes a plain transfer unless the user locked it, a rule set it, an automatic categorizer owns it, or the same save set it.
Clearing a category also drops the automatic categorizer's lock and claim (the AI's enrichment, Bayes's locked_by_bayes flag), leaving the leg as if never categorized. The cleanup rechecks each row under the row lock, so a category or lock a user sets meanwhile is never overwritten.
- A save moving a leg to a plain transfer rechecks the row in the database rather than the record's copy; the reconciler clears the category alongside the rewritten kind since
update_columnsskips callbacks. - The import adapter assigns the category only while the kind is
investment_contribution. Family::AutoCategorizerand Bayes recheck under the row lock and skip plain-transfer legs, so neither puts the category back.- A category chosen in the transfer editor is saved under the row lock and locked as the user's.
data_migration:clear_stale_contribution_categories(DRY_RUN,FAMILY_ID,BATCH_SIZE) clears stale rows in bounded, idempotent batches.- The rejected-investment case in #3766 stays open: it cannot distinguish an auto-assigned category from a user-chosen one.
Summary by CodeRabbit
- Bug Fixes
- Transactions identified as plain transfers no longer receive the Investment Contributions category by default.
- When a transaction changes from an investment contribution to a plain transfer, its unlocked Investment Contributions category is cleared. Locked categories and categories changed at the same time are preserved.
- Re-importing a broker transaction confirmed as a plain transfer no longer restores the Investment Contributions category.
- Corrected transfer categories remain consistent when an investment transfer is reconciled as a plain transfer.
- Categories selected in the transfer editor are now retained as locked choices.