feat(transactions): show auto-categorization provenance

Merged
JO
8d ago
+291-0

Branch: feat/ai-category-provenance-ui, merged with upstream main at c57975dc5.
Closes: #3597
Companion to the AI / AI history filter (#3603, merged): both key off Transaction::AUTO_CATEGORY_SOURCES and use the same rule (current = a non-superseded automatic enrichment whose value equals the live category_id), so the drawer and the filter always agree. The transaction list relies on that filter; provenance details appear only in the drawer.

Problem

Automatic category assignments are already recorded as DataEnrichment rows (attribute_name: "category_id", source: "ai" from the LLM categorizer / rule action, source: "bayes" from the naive-Bayes pre-pass added in #3465), but nothing in the UI shows which categories were chosen for the user or which of those they have since overridden.

Change

  • Scoped has_many :auto_category_enrichments, and Transaction#category_provenance (memoised with a defined? guard so nil is cached) backed by a small PORO Transaction::CategoryProvenance:
    • :current: a non-superseded automatic enrichment's value equals the live category_id. Since #4814 a category change marks earlier AI/Bayes claims superseded, so changing a category away and back again stays history; a later automatic re-assignment reclaims the row and makes it current again. The check is the same one Enrichable#clear_ai_cache uses.
    • :history: automatic enrichment(s) exist but none is current (changed by user/rule/import, or cleared).
    • nil: no automatic enrichment, so nothing is rendered.
  • Drawer (Overview section): compact "Auto-categorization" block with a neutral DS::Pill labelled by source ("AI" / "AI history", or "Bayes" / "Bayes history"), Assigned "Groceries" by AI 2 hours ago. (or "by the local Bayes classifier"), plus "The category was changed afterwards." for history. Deleted categories degrade to "a category that no longer exists". The block renders inside a stable dom_id(entry, :category_provenance) wrapper that TransactionsController#update turbo-replaces, so an in-drawer category change never leaves a stale block.
  • No per-row indicator in the transaction list; the existing AI filter covers that, and the list controllers and row template are unchanged from main.
  • All strings localised (English); functional tokens only.

Validation

  • test/models/transaction/category_provenance_test.rb (11): current via ai, current via bayes, history after change, history after clearing, latest-wins with ai+bayes rows, nil with no rows, rule-sourced enrichment alone → nil, nil result memoised, AI claim superseded then category set back → history, AI re-assignment after supersede → current, Bayes claim superseded then set back → history.
  • test/controllers/transactions_controller_test.rb (+4): drawer block text for AI, drawer labels Bayes provenance as "Bayes" with the Bayes source text, drawer shows "AI history" when the category returns to a superseded claim, turbo-stream update replaces the provenance frame.
  • CI on b20a2c984: all jobs green (test_unit 1/2 and 2/2, test_system 1/3 to 3/3, lint, lint_js, scan_ruby, scan_js, test_api_docs, security-scan).

Screenshots

Drawer, current assignment:

Drawer Auto-categorization block, current

Drawer, history (category changed after the automatic assignment):

Drawer Auto-categorization block, history