Review AI category suggestions for the uncategorised backlog (11.4, #130)

Merged
JA
2mo ago
+1384-33

Summary

IP11 PR 11.4: a preview-gated page to review AI category suggestions for the uncategorised backlog, accept them all or one row at a time, and turn a suggestion into a rule.

Part of #130.

Before this, AI categorisation only existed as a rule action that wrote straight to transactions. There was no way to see what the AI would pick, accept some and not others, or reuse a good suggestion as a rule.

What it does:

  • Family::AutoCategorizer#suggest asks the provider about at most 25 transactions and returns [transaction, category] pairs. It enriches, locks and saves nothing, does not run the shadow comparison and logs nothing on success. It selects with the same scope as the write path. Answers naming a category the family does not have, naming none, for a transaction not sent, or withheld below the confidence threshold are dropped, as the write path skips them.
  • Family::CategorySuggestionReview is the backlog and the accept step. #suggest takes the 25 newest backlog transactions and reports how many remain. #accept applies only the posted rows. Each row is re-checked under a row lock, so a transaction categorised or locked since the suggestion is skipped, never overwritten. A transaction or category outside the family, or on an account the user may only read (Account.annotatable_by), is skipped. Applied rows use enrich_attribute(:category_id, id, source: "ai") then lock_attr!, as the AI write path does, so cache clearing and the ai_status filter keep working.
  • Transactions::CategorySuggestionsController (require_preview_features!): index (backlog, cost of one batch, Suggest button; message and no button when no provider; "no pricing" message when the cost is nil), create (one bounded batch on an explicit click, rendered into a form, nothing stored between requests), accept (all posted rows, or one via the per-row button).
  • Each row links to new_rule_path(name:, action_type: "set_transaction_category", action_value: category.id).
  • Family#auto_categorize_estimate is the cost quote moved out of RulesController so both pages quote the same way.
  • No JavaScript: results render into a Turbo Frame and the accept form posts out of it. Locale: config/locales/views/transactions/category_suggestions/en.yml (English only) and one breadcrumb key. Linked from the transactions menu for preview users.

Scope and dependencies

  • Issue: Part of #130 (IP11, row 11.4). Independent of 11.1 to 11.3; shared files with them are only config/routes.rb and locale files.
  • Parent PR/branch, if stacked: none, branched from fork main at af2c49c.
  • Fork pre-flight / upstream links: to be added when the upstream PR exists (this is UI, held on fork main behind the preview gate until its drop).
  • Intentionally out of scope: persisting suggestions between requests, auto-accept, assistant tool changes, provider changes, a migration (none needed), changes to the Quick Categorize wizard or the "Uncategorized" filter.

Blast radius, one sentence. Who else hits this line, and what do they pass? Family::AutoCategorizer is called by AutoCategorizeJob (via Family#auto_categorize_transactions) and Rule::ActionExecutor::AutoCategorize with explicit transaction id lists; the write path's behaviour is unchanged (transactions_input gained an optional argument defaulting to scope, and the no-categories report moved to a private method that the existing test still covers), pinned by a new characterisation test that passed before the refactor. RulesController#confirm/#confirm_all are the only callers of the moved cost estimate and pass the same arguments through the same private wrapper, pinned by a rule-confirm test that passed before the move.

What else reads what I changed the reading of?

  • Family::AutoCategorizer#transactions_input: also read by run_shadow (called with no argument, unchanged) and the write path (unchanged).
  • Family::AutoCategorizer#scope: read by the write path, transactions_input and now suggest. Not changed; suggest deliberately shares it so a locked or already-categorised transaction is not offered.
  • Entry.uncategorized_transactions: the review backlog is narrower than the wizard badge and the Transactions "Uncategorized" filter on purpose (locked-category rows can never be answered, and read-only shared accounts cannot be annotated). Those surfaces are unchanged. The wizard's own accessible_entries (rather than annotatable) scope is a separate existing question and is not touched here.
  • RulesController#new prefill: read, not changed; a controller test follows the rendered link and checks the form.

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. (Family::AutoCategorizerTest "write path hands the provider the documented inputs and stores the answer"; RulesControllerTest "confirm quotes the estimated cost, and says so when there is no pricing".)
  • Any gate test has an observed failing mutation transcript attached. Not applicable: no gate test. The mutation results are in the table below.
  • Fixtures used as evidence assert internal consistency. (Deltas measured before and after; counts compared with rows actually changed.)
  • Financial calculations use BigDecimal and documented rounding points. Not applicable: no financial calculation. The cost estimate reuses LlmUsage.estimate_auto_categorize_cost unchanged.
  • De-identification uses token-shape allowlists; no personal data is included.
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

Mutation results (each break restored; a named test fails every time):

BreakFailing test (first)
suggest enriches a transactionFamily::AutoCategorizerSuggestTest "returns transaction and category pairs and writes nothing" (also review and controller "writes nothing" tests)
suggest locks a transactionsame test
accept eligibility not scoped to the backlogCategorySuggestionReviewTest "accept rejects a transaction from another family"; controller test of the same name
accept category not family-scopedCategorySuggestionReviewTest "accept rejects a category from another family"
AutoCategorizer#scope not family-scopedAutoCategorizerSuggestTest "does not offer a transaction from another family"
backlog scoped by neither family nor account sharefive failures incl. "accept rejects a transaction from another family" (breaking only the family.entries layer survives: Account.annotatable_by already confines it to the family, so that layer is redundant defence)
lock-time recheck removedCategorySuggestionReviewTest "accept re-checks under the row lock when the eligibility query was stale"
backlog includes categorised rowsCategorySuggestionReviewTest "backlog counts only uncategorised, unlocked transactions..."
accept overwrites (ignore_locks, no recheck)the stale-query test above
SUGGEST_LIMIT 26"refuses 26 transactions without calling the provider" and three more
backlog batch not limited"suggest sends the 25 newest of 26 in one call..." and the controller 25-of-26 test
suggest does not enforce the cap"refuses 26 transactions without calling the provider"

Validation

Commands and results (Ruby 3.3.6 in this environment, repo pins 3.4.9; Postgres 16):

bundle exec rails test test/models/family/auto_categorizer_suggest_test.rb test/models/family/category_suggestion_review_test.rb \
  test/controllers/transactions/category_suggestions_controller_test.rb test/models/family/auto_categorizer_test.rb
  -> 72 runs, 0 failures
bundle exec rubocop <12 changed .rb files>      -> 12 files inspected, no offenses detected
bundle exec erb_lint <changed .erb files>       -> No errors were found in ERB files
bundle exec brakeman --no-pager -q              -> Security Warnings: 0
bundle exec rails test (full suite)             -> 11419 runs, 49425 assertions, 4 failures, 1 errors, 33 skips

The 5 full-suite failures are not from this change; all five fail identically on af2c49c (origin/main) in the same sandbox: Provider::OpenaiTest "extra_headers stringifies nested values Ruby-style" (Ruby 3.3 Hash#inspect format), and Provider::RegistryTest / Settings::HostingsControllerTest x3 (the sandbox sets ANTHROPIC_BASE_URL). No migration; db/schema.rb untouched. npm run lint not run: no JavaScript changed and node_modules is not installed here.

Checked against upstream AGENTS.md (fetched from we-promise/sure main on 2026-10-01; identical to this fork's copy): Current.user/Current.family, thin controller with logic in Family::*, DS components (DS::Table, DS::Button, DS::Link, DS::Alert, DS::EmptyState, DS::Pill), functional tokens only, icon helper, t() everywhere, preview gate per gating-a-preview-feature.md, provider failure goes to DebugLogEntry.

Risks, blockers, and follow-up

  • Each click spends tokens (one provider call, at most 25 transactions, shown with a cost estimate beforehand). Nothing runs without the click.
  • Rows the model cannot answer stay in the backlog, so the next click's newest-25 can include them again. There is no persistence by design; carrying an exclusion list in the form is a possible follow-up (decision for the owner).
  • suggest does not run the Bayesian categoriser that Family#auto_categorize_transactions tries first; it asks the configured LLM/Jev provider only.
  • The suggestion cap of 25 mirrors Provider::Anthropic; OpenAI and Jev would accept more, but one cap keeps the page provider-independent (decision for the owner).
  • No visual check in a browser was possible here; structure was checked through controller tests and rendered HTML. Screenshots to be added before ready.
  • Gatekeeper review fires automatically on this draft; it is not handled here.

Generated by Claude Code