feat(categorization): try naive Bayes before the LLM
Problem
Every automatic categorization run goes to an LLM provider, including for merchants the family has already categorized dozens of times. Two costs fall out of that:
- Recurring merchants are re-sent to the provider on every sync — token spend on a decision the family already made, repeatedly.
Family::AutoCategorizer#auto_categorizeraisesNo LLM provider for auto-categorizationwhen none is configured, so automatic categorization does nothing at all for anyone self-hosting without AI.
What changed
Family::BayesCategorizer — pure Ruby, no provider, no new dependency. It trains a multinomial naive Bayes model (Laplace add-1 smoothing) on the family's already-categorized transactions and classifies the uncategorized ones from the same signals AutoCategorizer sends the LLM: entry name, notes, merchant name.
Family#auto_categorize_transactions now runs it first and passes only what it could not confidently label on to AutoCategorizer. When Bayes covers the whole batch, the provider is never called.
It stays quiet until it has real signal:
- needs at least 20 categorized transactions across at least 2 categories, otherwise it is a no-op
- per-transaction confidence must clear 0.7, otherwise that transaction falls through to the LLM as before
- training set is capped at the 5,000 most recent categorized transactions, since it is held in memory
- writes are recorded with a new
bayesDataEnrichmentsource, so provenance stays distinguishable fromai
One deliberate asymmetry with the LLM path: this does not lock_attr!(:category_id). A local heuristic should not pin a category as firmly as an explicit AI decision. Bayes-labeled transactions simply stop being uncategorized, so neither categorizer picks them up again.
Resulting behavior
- Categorization keeps working with no AI provider configured, once a family has enough history.
- Fewer provider calls and smaller batches when one is configured.
- Unchanged error contract when Bayes cannot help: if it labels nothing and no provider exists,
Family::AutoCategorizer::Erroris raised exactly as it is today.
Related, not overlapping
#3069 also learns categories from past transactions, but on the manual entry form (name autocomplete with a category preview). This is the automatic path through auto_categorize_transactions. No shared files.
Validation
bin/rails test— 8472 runs, 34140 assertions, 0 failures, 0 errorsbin/rubocop -f github— cleanbundle exec erb_lint ./app/**/*.erb— no errorsbin/brakeman --no-pager— 0 errors, 0 security warnings- No system tests: no view or JS changes.
9 new tests across test/models/family/bayes_categorizer_test.rb and test/models/family/auto_categorize_transactions_test.rb, covering the training guard, a hand-worked confidence calculation on separable merchants, a novel merchant staying below threshold, and each of the four Bayes/LLM split cases.
Confirmed the tests fail without the change — reverting app/models/family.rb to main and re-running gives:
Failure: test_bayes_handles_some,_LLM_handles_the_rest:_modified_count_sums
Expected: 2
Actual: 1
unexpected invocation: Provider::Registry.preferred_llm_provider()
unsatisfied expectations:
- expected never, invoked once: Provider::Registry.preferred_llm_provider(any_parameters)
Summary by CodeRabbit
-
New Features
- Added automatic transaction categorization using a Bayesian model trained on existing categorized transactions.
- Transactions are categorized automatically when confidence is sufficient; remaining transactions are passed to the existing LLM-based categorizer.
- Added Bayesian categorization as a recorded enrichment source.
- Transactions with insufficient training data or unfamiliar information remain available for fallback categorization.
-
Tests
- Added coverage for Bayesian classification, confidence thresholds, mixed categorization flows, and LLM fallback behavior.