Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Leave excluded transactions out of automatic categorization (#5104)

David Gilcc45998bd8ee
diff --git a/app/models/family.rb b/app/models/family.rb
index 7fc4374e51..d6fe91ebd5 100644
--- a/app/models/family.rb
+++ b/app/models/family.rb
@@ -436,11 +436,14 @@ class Family < ApplicationRecord
end
# Runs Bayes over the uncategorized transactions in scope the user hasn't
- # locked, transfer legs aside (they never belong in Uncategorized); those
- # it isn't sure about are tried again with a better model next time.
- # Returns the modified count.
+ # locked, transfer legs aside (they never belong in Uncategorized), and
+ # excluded transactions aside too, as the uncategorized badge and Quick
+ # Categorize already do (Entry.uncategorized_transactions); those it isn't
+ # sure about are tried again with a better model next time. Returns the
+ # modified count.
def bayes_categorize_uncategorized(scope = categorizable_transactions)
- scope = scope.where(category_id: nil).where.not(kind: Transaction::UNCATEGORIZED_EXCLUDED_KINDS).enrichable(:category_id)
+ scope = scope.with_entry.where(entries: { excluded: false }, category_id: nil)
+ .where.not(kind: Transaction::UNCATEGORIZED_EXCLUDED_KINDS).enrichable(:category_id)
requested = scope.count
return 0 if requested.zero?
diff --git a/app/models/family/auto_categorizer.rb b/app/models/family/auto_categorizer.rb
index 73d321e173..fa8c16f257 100644
--- a/app/models/family/auto_categorizer.rb
+++ b/app/models/family/auto_categorizer.rb
@@ -306,8 +306,11 @@ class Family::AutoCategorizer
)
end
+ # Excluded transactions are out of every report on purpose; a guessed
+ # category on them is noise, so the LLM is never asked about them.
def scope
- family.transactions.where(id: transaction_ids, category_id: nil)
+ family.transactions.with_entry
+ .where(id: transaction_ids, category_id: nil, entries: { excluded: false })
.enrichable(:category_id)
.includes(:category, :merchant, :entry)
end
diff --git a/test/models/family/auto_categorize_transactions_test.rb b/test/models/family/auto_categorize_transactions_test.rb
index be362d6621..f6a0d977c2 100644
--- a/test/models/family/auto_categorize_transactions_test.rb
+++ b/test/models/family/auto_categorize_transactions_test.rb
@@ -69,6 +69,37 @@ class Family::AutoCategorizeTransactionsTest < ActiveSupport::TestCase
assert_nil transfer.reload.category
end
+ # Excluding a transaction takes it out of every report on purpose (a house
+ # deposit, a loan drawdown). A guessed category on it is noise at best, and a
+ # large one-off reads like the nearest frequent label (rent).
+ test "Bayes leaves excluded transactions uncategorized" do
+ train_two_categories
+ excluded = create_transaction(account: @account, name: "Starbucks Coffee", excluded: true).transaction
+ included = create_transaction(account: @account, name: "Starbucks Coffee").transaction
+
+ assert_equal 1, @family.bayes_categorize_uncategorized
+ assert_nil excluded.reload.category
+ assert_equal @coffee, included.reload.category
+ end
+
+ test "Bayes skips excluded transactions in a rule's scope too" do
+ train_two_categories
+ excluded = create_transaction(account: @account, name: "Starbucks Coffee", excluded: true).transaction
+
+ assert_equal 0, @family.bayes_categorize_uncategorized(@family.transactions)
+ assert_nil excluded.reload.category
+ end
+
+ test "the LLM is not asked about excluded transactions" do
+ Provider::Registry.stubs(:preferred_llm_provider).returns(@llm_provider)
+ excluded = create_transaction(account: @account, name: "House deposit", excluded: true).transaction
+
+ @llm_provider.expects(:auto_categorize).never
+
+ assert_equal 0, @family.auto_categorize_transactions([ excluded.id ])
+ assert_nil excluded.reload.category
+ end
+
test "clearing Bayes locks unlocks only what Bayes locked and still has its pick" do
train_two_categories
@family.update_bayes_settings!(lock_results: true)