The personal finance app for everyone (by everyone)
Ruby
Improve async AI rule error reporting (#2271)
Sure Admin (bot)81c613f64a60
diff --git a/app/jobs/auto_categorize_job.rb b/app/jobs/auto_categorize_job.rbindex 5f82ff9557..e299b2c3d7 100644--- a/app/jobs/auto_categorize_job.rb+++ b/app/jobs/auto_categorize_job.rb@@ -7,8 +7,9 @@ class AutoCategorizeJob < ApplicationJob# If this job was part of a rule run, report back the modified countrule_run&.complete_job!(modified_count: modified_count)- rescue => e- rule_run&.fail_job!(error: e, source: self.class.name, transaction_ids: transaction_ids)+ rescue => error+ rule_run&.fail_job!(error: error, source: self.class.name, transaction_ids: transaction_ids)+raiseendenddiff --git a/app/jobs/auto_detect_merchants_job.rb b/app/jobs/auto_detect_merchants_job.rbindex cdb4ecb7c3..0f6d24f6be 100644--- a/app/jobs/auto_detect_merchants_job.rb+++ b/app/jobs/auto_detect_merchants_job.rb@@ -2,12 +2,14 @@ class AutoDetectMerchantsJob < ApplicationJobqueue_as :medium_prioritydef perform(family, transaction_ids: [], rule_run_id: nil)+ rule_run = RuleRun.find_by(id: rule_run_id) if rule_run_id.present?modified_count = family.auto_detect_transaction_merchants(transaction_ids)# If this job was part of a rule run, report back the modified count- if rule_run_id.present?- rule_run = RuleRun.find_by(id: rule_run_id)- rule_run&.complete_job!(modified_count: modified_count)- end+ rule_run&.complete_job!(modified_count: modified_count)+ rescue => error+ rule_run&.fail_job!(error: error, source: self.class.name, transaction_ids: transaction_ids)++ raiseendenddiff --git a/app/models/rule_run.rb b/app/models/rule_run.rbindex 935b790e8a..cb354ca749 100644--- a/app/models/rule_run.rb+++ b/app/models/rule_run.rb@@ -37,7 +37,7 @@ class RuleRun < ApplicationRecordself.transactions_modified += modified_countself.pending_jobs_count = [ pending_jobs_count - 1, 0 ].max- # If all jobs are done, mark as success+ # Preserve a previously recorded failure while still draining pending jobs.self.status = "success" if pending_jobs_count <= 0 && !failed?save!@@ -46,13 +46,15 @@ class RuleRun < ApplicationRecorddef fail_job!(error:, source:, transaction_ids: [])should_log = false+ current_error_message = "#{error.class}: #{error.message}"with_lock doshould_log = !failed?self.pending_jobs_count = [ pending_jobs_count - 1, 0 ].maxself.status = "failed"- self.error_message = "#{error.class}: #{error.message}"+ existing_error_messages = error_message.to_s.split("\n").reject(&:blank?)+ self.error_message = (existing_error_messages + [ current_error_message ]).uniq.join("\n")save!enddiff --git a/test/jobs/auto_detect_merchants_job_test.rb b/test/jobs/auto_detect_merchants_job_test.rbnew file mode 100644index 0000000000..1456a7c567--- /dev/null+++ b/test/jobs/auto_detect_merchants_job_test.rb@@ -0,0 +1,42 @@+require "test_helper"++class AutoDetectMerchantsJobTest < ActiveJob::TestCase+ setup do+ @family = families(:dylan_family)+ @transaction = transactions(:one)+ @rule_run = rules(:one).rule_runs.create!(+ rule_name: rules(:one).name,+ execution_type: "manual",+ status: "pending",+ transactions_queued: 1,+ transactions_processed: 0,+ transactions_modified: 0,+ pending_jobs_count: 1,+ executed_at: Time.current+ )+ end++ test "records rule run failure debug log before reraising merchant detection errors" do+ @family.stubs(:auto_detect_transaction_merchants).raises(StandardError, "Invalid JSON in provider response")++ assert_difference "DebugLogEntry.count", 1 do+ assert_raises(StandardError) do+ AutoDetectMerchantsJob.perform_now(@family, transaction_ids: [ @transaction.id ], rule_run_id: @rule_run.id)+ end+ end++ @rule_run.reload++ assert_equal "failed", @rule_run.status+ assert_equal 0, @rule_run.pending_jobs_count+ assert_equal "StandardError: Invalid JSON in provider response", @rule_run.error_message++ entry = DebugLogEntry.order(:created_at).last+ assert_equal "rule_run", entry.category+ assert_equal "error", entry.level+ assert_equal "AutoDetectMerchantsJob", entry.source+ assert_equal @family, entry.family+ assert_equal @rule_run.id, entry.metadata["rule_run_id"]+ assert_equal [ @transaction.id ], entry.metadata["transaction_ids"]+ end+enddiff --git a/test/models/rule_run_test.rb b/test/models/rule_run_test.rbindex fd0fb8e5a4..00f6892e1d 100644--- a/test/models/rule_run_test.rb+++ b/test/models/rule_run_test.rb@@ -58,6 +58,18 @@ class RuleRunTest < ActiveSupport::TestCaseassert_equal 0, rule_run.pending_jobs_countend+ test "fail_job deduplicates repeated error messages individually" do+ rule_run = create_rule_run(pending_jobs_count: 3)++ rule_run.fail_job!(error: RuntimeError.new("first failed batch"), source: "AutoCategorizeJob")+ rule_run.fail_job!(error: RuntimeError.new("second failed batch"), source: "AutoCategorizeJob")+ rule_run.fail_job!(error: RuntimeError.new("second failed batch"), source: "AutoCategorizeJob")++ rule_run.reload+ assert_equal "RuntimeError: first failed batch\nRuntimeError: second failed batch", rule_run.error_message+ assert_equal 0, rule_run.pending_jobs_count+ end+test "complete_job does not overwrite failed status" dorule_run = create_rule_run(status: "failed", pending_jobs_count: 0)@@ -69,6 +81,20 @@ class RuleRunTest < ActiveSupport::TestCaseassert_equal 3, rule_run.transactions_modifiedend+ test "complete_job preserves failure while remaining jobs finish" do+ rule_run = create_rule_run(pending_jobs_count: 2)+ error = RuntimeError.new("LLM provider returned HTTP 503")++ rule_run.fail_job!(error: error, source: "AutoCategorizeJob")+ rule_run.complete_job!(modified_count: 1)++ rule_run.reload+ assert_equal "failed", rule_run.status+ assert_equal "RuntimeError: LLM provider returned HTTP 503", rule_run.error_message+ assert_equal 0, rule_run.pending_jobs_count+ assert_equal 1, rule_run.transactions_modified+ end+privatedef create_rule_run(status: "pending", pending_jobs_count: 1)@rule.rule_runs.create!(