Make a category change end the AI's and Bayes' claim on it
Summary
The AI's or Bayes' category counts as theirs while their enrichment record still holds the transaction's category (Transaction.category_from_sql, Enrichable#clear_ai_cache, the "AI" search filter). A category the user changes away and then back again matches too, so:
clear_ai_cacheunlocks a category the user chose;Family#clear_bayes_locks!drops the user's lock;- the Bayes evaluation skips the user's label as one of Bayes' own;
- the "AI" filter lists the transaction as the AI's, not under "AI history".
This came up in review on #4799, which uses the existing rule as it stands.
How it goes wrong
sequenceDiagram
participant AI
participant T as Transaction
participant R as AI's enrichment record
actor U as User
AI->>T: category = Groceries
AI->>R: value = Groceries
U->>T: category = Dining
Note over R: Before: still Groceries, ignored while it differs<br/>After: marked superseded
U->>T: category = Groceries (back again)
Note over T,R: Before: the record matches again, so the user's choice counts as the AI's<br/>After: the superseded record is skipped, so it stays the user's
What each reader concludes about that transaction once the user has changed it back:
| Reader | Before | After |
|---|---|---|
| Transactions filter "AI" | listed under current | listed under history |
clear_ai_cache | unlocks the category | keeps the user's lock |
Family#clear_bayes_locks! (Bayes' record) | drops the user's lock | keeps it |
| Bayes evaluation | skips it as Bayes' own label | tests it as the user's label |
AutoCategorizer | counts it as an AI cache hit | counts it as protected |
If the AI (or Bayes) sets the category again itself, its record is rewritten and the claim is back. Only someone else's change ends it.
Change
- A category change marks the transaction's
aiandbayescategory enrichmentssupersededin their metadata. They are not deleted, so the "AI history" filter keeps them. - A change a source makes itself goes through
Enrichable#enrich_attributes, which rewrites that source's record (value and metadata) right after the save. Only someone else's change ends a claim, and a source setting its category again reclaims it. Transaction.enriched_from_sql, the search's AI status filter andEnrichable#clear_ai_cacheskip superseded records.AutoCategorizer#cached_transaction_idsnow usescategory_from_sqlinstead of its own copy of the rule.- Bulk category reassignment (
Transaction.reassign_category!, for category merges and deletions) skips callbacks, so it marks the reassigned rows' claims superseded itself, in the batches it touches their entries in. Otherwise a claim recorded before this change on the category merged into would become current again. The callback and it shareTransaction.supersede_automatic_category_claims!. Enrichable#clear_ai_cachenow locks the record before deleting its enrichments. That's the order a save followed bylog_enrichmentuses, and the callback too, so they can't deadlock. A record deleted since it was loaded clears nothing.
Tests
- New model tests cover:
- the round trip, for the AI's claim, the "AI history"/"current" filters,
clear_ai_cacheandclear_bayes_locks!; - a categorizer replacing another's guess;
- a categorizer reclaiming its category;
- a category merge ending a claim recorded on the category merged into;
clear_ai_cachelocking the transaction before deleting its enrichments;clear_ai_cacheon a transaction deleted since it was loaded clearing nothing.
- the round trip, for the AI's claim, the "AI history"/"current" filters,
- Mutation-checked: removing the callback, the superseded check from any one of the three readers, the reassignment's superseding or
clear_ai_cache's lock fails the matching tests. - Full suite: 15248 runs, 0 failures.
🤖 Generated with Claude Code
Summary by cubic
Changing a transaction's category now ends the AI's or Bayes' claim on it: a category changed away and back again no longer counts as theirs. Previously the claim survived so long as their enrichment record matched the current value, which let clear_ai_cache and clear_bayes_locks! unlock a category the user had chosen.
Bug Fixes
- Any category change marks the AI's and Bayes' enrichment records superseded rather than deleting them, so they stay in "AI history"; a source setting its category again reclaims it.
Transaction.enriched_from_sql, the search's AI status filter, andEnrichable#clear_ai_cacheskip superseded records, andAutoCategorizer#cached_transaction_idsnow reusescategory_from_sql.Enrichable#clear_ai_cachelocks the record before deleting its enrichments to match the save-then-log_enrichmentlock order; bulk reassignment (Transaction.reassign_category!) marks the reassigned rows' claims superseded itself.
Summary by CodeRabbit
- Bug Fixes
- Automatic category status now reflects the current assignment, while previous automatic assignments remain available in history.
- Changing a transaction’s category ends the matching automatic claim, even if the category is later restored. Automatic categorization can retain or reclaim its claim when appropriate.
- Clearing AI enrichment caches preserves user-established category locks and safely handles transactions that have been deleted.