Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Scope AI-detected merchant creation to the family (#3843)

GFR7c33bdd81c22
diff --git a/app/models/family/auto_merchant_detector.rb b/app/models/family/auto_merchant_detector.rb
index 45ebabcbbb..d74e47a4b6 100644
--- a/app/models/family/auto_merchant_detector.rb
+++ b/app/models/family/auto_merchant_detector.rb
@@ -105,32 +105,48 @@ class Family::AutoMerchantDetector
end
def find_or_create_ai_merchant(auto_detection)
- # Strategy 1: Find existing merchant by website_url (most reliable for deduplication)
+ # Strategy 1: Find an existing merchant by website_url (most reliable for
+ # deduplication). Provider-sourced merchants (Plaid etc.) are vetted
+ # shared data and stay reusable across families. AI-sourced ones were
+ # created from some family's own transaction text (before #3842), so
+ # they're only reused when already assigned to this family — otherwise
+ # another family's crafted name/logo would surface here.
if auto_detection.business_url.present?
- existing = ProviderMerchant.find_by(website_url: auto_detection.business_url)
+ existing = reusable_provider_merchants.find_by(website_url: auto_detection.business_url)
return existing if existing
end
- # Strategy 2: Find by exact name match
- existing = ProviderMerchant.find_by(source: "ai", name: auto_detection.business_name)
+ # Strategy 2: Find an AI-sourced merchant by exact name match, limited
+ # to the ones this family already uses (see Strategy 1).
+ existing = family_ai_provider_merchants.find_by(name: auto_detection.business_name)
return existing if existing
- # Strategy 3: Create new merchant
- ProviderMerchant.create!(
- source: "ai",
- name: auto_detection.business_name,
- website_url: auto_detection.business_url,
- logo_url: build_logo_url(auto_detection.business_url)
+ # Strategy 3: no shared merchant to reuse. Create a merchant scoped to
+ # this family rather than a globally-shared ProviderMerchant, so a
+ # family can't use LLM-extracted data derived from its own transaction
+ # description/notes to create a record visible to every other family
+ # (issue #3842).
+ merchant, _created = FamilyMerchant.find_or_create_with_name(
+ family,
+ auto_detection.business_name,
+ website_url: auto_detection.business_url
)
- rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
- # Race condition: another process created the merchant between our find and create
- ProviderMerchant.find_by(source: "ai", name: auto_detection.business_name)
+ merchant
+ rescue ActiveRecord::RecordInvalid => e
+ # A name the model rejects (e.g. the reserved Merchant::NO_MERCHANT_FILTER_VALUE)
+ # leaves this transaction without a merchant, as before, instead of
+ # failing the whole detection batch. The name itself isn't logged since
+ # it's derived from the family's transaction text.
+ Rails.logger.warn("Skipping invalid AI-detected merchant for family #{family.id}: #{e.record.errors.attribute_names.join(', ')}")
+ nil
+ end
+
+ def family_ai_provider_merchants
+ ProviderMerchant.where(source: "ai", id: family.transactions.select(:merchant_id))
end
- def build_logo_url(business_url)
- return nil unless Setting.brand_fetch_client_id.present? && business_url.present?
- size = Setting.brand_fetch_logo_size
- "#{default_logo_provider_url}/#{business_url}/icon/fallback/lettermark/w/#{size}/h/#{size}?c=#{Setting.brand_fetch_client_id}"
+ def reusable_provider_merchants
+ ProviderMerchant.where.not(source: "ai").or(family_ai_provider_merchants)
end
def enhance_provider_merchant(merchant, auto_detection)
diff --git a/test/models/family/auto_merchant_detector_test.rb b/test/models/family/auto_merchant_detector_test.rb
index 78277da473..cfa0da7d37 100644
--- a/test/models/family/auto_merchant_detector_test.rb
+++ b/test/models/family/auto_merchant_detector_test.rb
@@ -40,6 +40,143 @@ class Family::AutoMerchantDetectorTest < ActiveSupport::TestCase
assert_equal 1, @account.transactions.reload.enrichable(:merchant_id).count
end
+ test "an invalid AI merchant name leaves that transaction unassigned without failing the batch" do
+ invalid_txn = create_transaction(account: @account, name: "Weird").transaction
+ valid_txn = create_transaction(account: @account, name: "Chipotle").transaction
+
+ provider_response = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: invalid_txn.id, business_name: Merchant::NO_MERCHANT_FILTER_VALUE, business_url: nil),
+ AutoDetectedMerchant.new(transaction_id: valid_txn.id, business_name: "Chipotle", business_url: "chipotle.com")
+ ])
+ @llm_provider.expects(:auto_detect_merchants).returns(provider_response).once
+
+ Family::AutoMerchantDetector.new(@family, transaction_ids: [ invalid_txn.id, valid_txn.id ]).auto_detect
+
+ assert_nil invalid_txn.reload.merchant
+ assert_equal "Chipotle", valid_txn.reload.merchant.name
+ end
+
+ # Regression: issue #3842. A family could previously get a globally-shared
+ # ProviderMerchant created from LLM-extracted data derived from its own
+ # transaction name/notes, which every other family could then match/reuse.
+ test "creates a family-scoped merchant instead of a shared ProviderMerchant" do
+ txn = create_transaction(account: @account, name: "Sushi Place").transaction
+
+ provider_response = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: txn.id, business_name: "Sushi Place", business_url: "sushiplace.example")
+ ])
+ @llm_provider.expects(:auto_detect_merchants).returns(provider_response).once
+
+ assert_no_difference "ProviderMerchant.count" do
+ Family::AutoMerchantDetector.new(@family, transaction_ids: [ txn.id ]).auto_detect
+ end
+
+ merchant = txn.reload.merchant
+ assert_instance_of FamilyMerchant, merchant
+ assert_equal @family, merchant.family
+ assert_equal "Sushi Place", merchant.name
+ end
+
+ test "creates the family merchant through FamilyMerchant.find_or_create_with_name" do
+ txn = create_transaction(account: @account, name: "Sushi Place").transaction
+ merchant = @family.merchants.create!(name: "Sushi Place")
+
+ provider_response = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: txn.id, business_name: "Sushi Place", business_url: "sushiplace.example")
+ ])
+ @llm_provider.expects(:auto_detect_merchants).returns(provider_response).once
+ Family::AutoMerchantDetector.any_instance.stubs(:find_matching_user_merchant).returns(nil)
+ FamilyMerchant.expects(:find_or_create_with_name)
+ .with(@family, "Sushi Place", website_url: "sushiplace.example")
+ .returns([ merchant, false ])
+ .once
+
+ Family::AutoMerchantDetector.new(@family, transaction_ids: [ txn.id ]).auto_detect
+
+ assert_equal merchant, txn.reload.merchant
+ end
+
+ test "does not let one family's transaction text create or reuse another family's AI merchant" do
+ other_family = families(:empty)
+ other_account = other_family.accounts.create!(name: "Other", balance: 100, currency: "USD", accountable: Depository.new)
+
+ txn1 = create_transaction(account: @account, name: "Shared Name Co").transaction
+ txn2 = create_transaction(account: other_account, name: "Shared Name Co").transaction
+
+ response1 = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: txn1.id, business_name: "Shared Name Co", business_url: "sharedname.example")
+ ])
+ response2 = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: txn2.id, business_name: "Shared Name Co", business_url: "sharedname.example")
+ ])
+ @llm_provider.expects(:auto_detect_merchants).returns(response1).once
+ Family::AutoMerchantDetector.new(@family, transaction_ids: [ txn1.id ]).auto_detect
+
+ @llm_provider.expects(:auto_detect_merchants).returns(response2).once
+ Family::AutoMerchantDetector.new(other_family, transaction_ids: [ txn2.id ]).auto_detect
+
+ merchant1 = txn1.reload.merchant
+ merchant2 = txn2.reload.merchant
+
+ assert_instance_of FamilyMerchant, merchant1
+ assert_instance_of FamilyMerchant, merchant2
+ assert_not_equal merchant1.id, merchant2.id
+ assert_equal @family, merchant1.family
+ assert_equal other_family, merchant2.family
+ end
+
+ # Legacy AI-sourced ProviderMerchants were created from some family's own
+ # transaction text, so another family must not pick them up by url or name.
+ test "does not reuse another family's legacy AI provider merchant" do
+ legacy = ProviderMerchant.create!(name: "Crafted Co", source: "ai", website_url: "crafted.example")
+ txn = create_transaction(account: @account, name: "Crafted Co purchase").transaction
+
+ provider_response = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: txn.id, business_name: "Crafted Co", business_url: "crafted.example")
+ ])
+ @llm_provider.expects(:auto_detect_merchants).returns(provider_response).once
+
+ Family::AutoMerchantDetector.new(@family, transaction_ids: [ txn.id ]).auto_detect
+
+ merchant = txn.reload.merchant
+ assert_instance_of FamilyMerchant, merchant
+ assert_not_equal legacy.id, merchant.id
+ end
+
+ test "reuses an AI provider merchant this family already uses" do
+ mine = ProviderMerchant.create!(name: "Mine Co", source: "ai", website_url: "mine.example")
+ used = create_transaction(account: @account, name: "Earlier Mine Co").transaction
+ used.update!(merchant: mine)
+ txn = create_transaction(account: @account, name: "Mine Co purchase").transaction
+
+ provider_response = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: txn.id, business_name: "Mine Co", business_url: "mine.example")
+ ])
+ @llm_provider.expects(:auto_detect_merchants).returns(provider_response).once
+
+ assert_no_difference [ "ProviderMerchant.count", "FamilyMerchant.count" ] do
+ Family::AutoMerchantDetector.new(@family, transaction_ids: [ txn.id ]).auto_detect
+ end
+
+ assert_equal mine.id, txn.reload.merchant.id
+ end
+
+ test "still reuses an existing shared provider merchant by website" do
+ known = ProviderMerchant.create!(name: "Known Co", source: "plaid", website_url: "known.example")
+ txn = create_transaction(account: @account, name: "Known Co purchase").transaction
+
+ provider_response = provider_success_response([
+ AutoDetectedMerchant.new(transaction_id: txn.id, business_name: "Known Co", business_url: "known.example")
+ ])
+ @llm_provider.expects(:auto_detect_merchants).returns(provider_response).once
+
+ assert_no_difference [ "ProviderMerchant.count", "FamilyMerchant.count" ] do
+ Family::AutoMerchantDetector.new(@family, transaction_ids: [ txn.id ]).auto_detect
+ end
+
+ assert_equal known.id, txn.reload.merchant.id
+ end
+
private
AutoDetectedMerchant = Provider::LlmConcept::AutoDetectedMerchant
end