A platform for community discussion. Free, open, simple.
FIX: Don’t create AI Problem check trackers without a target LLM. (#35447)
Roman Rizzi2fd6d69bf17a
diff --git a/app/models/problem_check.rb b/app/models/problem_check.rbindex 3ee3bbacfd4..7ca64a4eaf7 100644--- a/app/models/problem_check.rb+++ b/app/models/problem_check.rb@@ -194,7 +194,7 @@ class ProblemCheck[NO_TARGET]end- def problem(target = nil, override_key: nil, override_data: {})+ def problem(target = nil, override_key: nil, override_data: {}, details: {})problem =Problem.new(I18n.t(@@ -207,6 +207,7 @@ class ProblemCheckpriority: self.config.priority,identifier:,target: target&.id,+ details:,)target.present? ? problem : [problem]diff --git a/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rb b/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rbindex 55d12d97131..bf7f902fb0c 100644--- a/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rb+++ b/plugins/discourse-ai/app/services/problem_check/ai_credit_hard_limit.rb@@ -26,23 +26,19 @@ class ProblemCheck::AiCreditHardLimit < ProblemCheckprivate+ def targets+ LlmModel.joins(:llm_credit_allocation).where("llm_models.id < 0").pluck("llm_models.id")+ end+def hard_limit_problem(model, allocation)- details = {+ override_data = {model_id: model.id,model_name: model.display_name,reset_date: format_reset_date(allocation.next_reset_at),url: "#{Discourse.base_path}/admin/plugins/discourse-ai/ai-llms",}- message = I18n.t("dashboard.problem.ai_credit_hard_limit", details)-- Problem.new(- message,- priority: "high",- identifier: "ai_credit_hard_limit",- target: model.id,- details:,- )+ problem(model, override_data:)enddef format_reset_date(date)diff --git a/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rb b/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rbindex bac275841c5..8966d509b16 100644--- a/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rb+++ b/plugins/discourse-ai/app/services/problem_check/ai_credit_soft_limit.rb@@ -28,8 +28,12 @@ class ProblemCheck::AiCreditSoftLimit < ProblemCheckprivate+ def targets+ LlmModel.joins(:llm_credit_allocation).where("llm_models.id < 0").pluck("llm_models.id")+ end+def soft_limit_problem(model, allocation)- details = {+ override_data = {model_id: model.id,model_name: model.display_name,percentage_remaining: allocation.percentage_remaining.round,@@ -37,15 +41,7 @@ class ProblemCheck::AiCreditSoftLimit < ProblemCheckurl: "#{Discourse.base_path}/admin/plugins/discourse-ai/ai-llms",}- message = I18n.t("dashboard.problem.ai_credit_soft_limit", details)-- Problem.new(- message,- priority: "low",- identifier: "ai_credit_soft_limit",- target: model.id,- details:,- )+ problem(model, override_data:)enddef format_reset_date(date)diff --git a/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rb b/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rbindex 8e6c0d94abb..e1297b24571 100644--- a/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rb+++ b/plugins/discourse-ai/app/services/problem_check/ai_llm_status.rb@@ -8,11 +8,17 @@ class ProblemCheck::AiLlmStatus < ProblemCheckself.max_blips = 2def call+ return [] if !SiteSetting.discourse_ai_enabled+llm_errorsendprivate+ def targets+ LlmModel.in_use.pluck(:id)+ end+def llm_errorsreturn [] if !SiteSetting.discourse_ai_enabledLlmModel.in_use.find_each.filter_map do |model|@@ -43,22 +49,13 @@ class ProblemCheck::AiLlmStatus < ProblemCheck)end- details = {+ override_data = {model_id: model.id,model_name: model.display_name,- error: parse_error_message(e.message),url: "#{Discourse.base_path}/admin/plugins/discourse-ai/ai-llms/#{model.id}/edit",}- message = I18n.t("dashboard.problem.ai_llm_status", details)-- Problem.new(- message,- priority: "high",- identifier: "ai_llm_status",- target: model.id,- details:,- )+ problem(model, override_data:, details: { error: parse_error_message(e.message) })endenddiff --git a/plugins/discourse-ai/db/post_migrate/20251016143343_remove_ai_problem_check_trackers_without_targets.rb b/plugins/discourse-ai/db/post_migrate/20251016143343_remove_ai_problem_check_trackers_without_targets.rbnew file mode 100644index 00000000000..f6b0c9c2f0f--- /dev/null+++ b/plugins/discourse-ai/db/post_migrate/20251016143343_remove_ai_problem_check_trackers_without_targets.rb@@ -0,0 +1,16 @@+# frozen_string_literal: true+class RemoveAiProblemCheckTrackersWithoutTargets < ActiveRecord::Migration[8.0]+ def up+ tracker_identifiers = %w[ai_llm_status ai_credit_soft_limit ai_credit_hard_limit]+ no_target = "__NULL__"++ DB.exec(<<~SQL, tracker_identifiers: tracker_identifiers, no_target: no_target)+ DELETE FROM problem_check_trackers+ WHERE identifier IN (:tracker_identifiers) AND target = :no_target+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/plugins/discourse-ai/spec/jobs/regular/run_problem_check_spec.rb b/plugins/discourse-ai/spec/jobs/regular/run_problem_check_spec.rbnew file mode 100644index 00000000000..38651a69884--- /dev/null+++ b/plugins/discourse-ai/spec/jobs/regular/run_problem_check_spec.rb@@ -0,0 +1,80 @@+# frozen_string_literal: true++RSpec.describe Jobs::RunProblemCheck do+ subject(:run_check_job) { described_class.new }++ describe "integration specs for AI-based problem checks" do+ before { enable_current_plugin }++ context "when running AI LLM status checks" do+ let(:identifier) { :ai_llm_status }++ let!(:llm_model) { assign_fake_provider_to(:ai_default_llm_model) }++ before { SiteSetting.ai_summarization_enabled = true }++ context "when everything is OK" do+ it "creates a problem check tracker that is targeting the tested model" do+ DiscourseAi::Completions::Llm.with_prepared_responses(["OK"]) do+ run_check_job.execute(check_identifier: identifier)+ end++ created_trackers = ProblemCheckTracker.where(identifier: identifier)++ expect(created_trackers.size).to eq(1)+ expect(created_trackers.last.target).to eq(llm_model.id.to_s)+ end+ end+ end++ context "when running AI Credit soft limits checks" do+ let(:identifier) { :ai_credit_soft_limit }++ fab!(:llm_model) { Fabricate(:llm_model, id: -1) }++ context "when we haven't reach the soft limit yet" do+ it "creates a problem check tracker that is targeting the tested model" do+ Fabricate(+ :llm_credit_allocation,+ llm_model: llm_model,+ monthly_credits: 1000,+ monthly_used: 700,+ soft_limit_percentage: 80,+ )++ run_check_job.execute(check_identifier: identifier)++ created_trackers = ProblemCheckTracker.where(identifier: identifier)++ expect(created_trackers.size).to eq(1)+ expect(created_trackers.last.target).to eq(llm_model.id.to_s)+ end+ end+ end++ context "when running AI Credit hard limits checks" do+ let(:identifier) { :ai_credit_hard_limit }++ fab!(:llm_model) { Fabricate(:llm_model, id: -1) }++ context "when we haven't reach the soft limit yet" do+ it "creates a problem check tracker that is targeting the tested model" do+ Fabricate(+ :llm_credit_allocation,+ llm_model: llm_model,+ monthly_credits: 1000,+ monthly_used: 850,+ soft_limit_percentage: 80,+ )++ run_check_job.execute(check_identifier: identifier)++ created_trackers = ProblemCheckTracker.where(identifier: identifier)++ expect(created_trackers.size).to eq(1)+ expect(created_trackers.last.target).to eq(llm_model.id.to_s)+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rbindex ff4c14a771d..bb7e995bd89 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_credit_hard_limit_spec.rb@@ -38,7 +38,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit doproblems = described_class.new.callexpect(problems.size).to eq(1)- expect(problems.first.identifier).to eq("ai_credit_hard_limit")+ expect(problems.first.identifier).to eq(:ai_credit_hard_limit)expect(problems.first.priority).to eq("high")expect(problems.first.target).to eq(llm_model.id)end@@ -55,7 +55,7 @@ RSpec.describe ProblemCheck::AiCreditHardLimit doproblems = described_class.new.callexpect(problems.size).to eq(1)- expect(problems.first.identifier).to eq("ai_credit_hard_limit")+ expect(problems.first.identifier).to eq(:ai_credit_hard_limit)endit "resets credits before checking if needed" dodiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rbindex b38ad2be55e..564ebf38705 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_credit_soft_limit_spec.rb@@ -38,7 +38,7 @@ RSpec.describe ProblemCheck::AiCreditSoftLimit doproblems = described_class.new.callexpect(problems.size).to eq(1)- expect(problems.first.identifier).to eq("ai_credit_soft_limit")+ expect(problems.first.identifier).to eq(:ai_credit_soft_limit)expect(problems.first.priority).to eq("low")expect(problems.first.target).to eq(llm_model.id)enddiff --git a/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rb b/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rbindex bb19aec80f0..65f7cf5d6ce 100644--- a/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rb+++ b/plugins/discourse-ai/spec/services/problem_check/ai_llm_status_spec.rb@@ -50,19 +50,14 @@ RSpec.describe ProblemCheck::AiLlmStatus do},)- expect(described_class.new.call).to contain_exactly(- have_attributes(- identifier: "ai_llm_status",- target: llm_model.id,- priority: "high",- message: message,- details: {- model_id: llm_model.id,- model_name: llm_model.display_name,- url: "/admin/plugins/discourse-ai/ai-llms/#{llm_model.id}/edit",- error: JSON.parse(error_response)["message"],- },- ),+ expect(described_class.new.call.first).to have_attributes(+ identifier: :ai_llm_status,+ target: llm_model.id,+ priority: "high",+ message: message,+ details: {+ error: JSON.parse(error_response)["message"],+ },)end@@ -113,7 +108,7 @@ RSpec.describe ProblemCheck::AiLlmStatus doproblems = described_class.new.callexpect(problems.length).to eq(1)expect(problems.first).to have_attributes(- identifier: "ai_llm_status",+ identifier: :ai_llm_status,target: llm_model.id,priority: "high",)@@ -125,7 +120,7 @@ RSpec.describe ProblemCheck::AiLlmStatus doproblems = described_class.new.callexpect(problems.length).to eq(1)expect(problems.first).to have_attributes(- identifier: "ai_llm_status",+ identifier: :ai_llm_status,target: llm_model.id,priority: "high",)