Skip to content

A platform for community discussion. Free, open, simple.

FIX: Sync spam model with agent default changes (#43566)

Sam64e289422dd0
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb
index d45bc5c80b8..e493d597b79 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb
@@ -110,6 +110,7 @@ module DiscourseAi
mcp_server_ids = params.delete(:ai_mcp_server_ids)
mcp_server_tool_names = params.delete(:mcp_server_tool_names) || {}
initial_attributes = @ai_agent.attributes.dup
+ initial_spam_settings = AiModerationSetting.spam if params.key?(:default_llm_id)
if @ai_agent.update(params.except(:rag_uploads))
ensure_ai_agent_user(@ai_agent)
@@ -118,6 +119,7 @@ module DiscourseAi
end
RagDocumentFragment.update_target_uploads(@ai_agent, attached_upload_ids(@ai_agent))
log_ai_agent_update(@ai_agent, initial_attributes)
+ log_ai_spam_model_update(initial_spam_settings)
render_ai_agent_resource(@ai_agent)
else
@@ -739,6 +741,23 @@ module DiscourseAi
)
end
+ def log_ai_spam_model_update(settings)
+ return if settings.blank? || settings.ai_agent_id != @ai_agent.id
+
+ previous_model_id = settings.llm_model_id
+ settings.reload
+ return if settings.llm_model_id == previous_model_id
+
+ previous_model_name =
+ LlmModel.find_by(id: previous_model_id)&.display_name || previous_model_id
+ logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
+ logger.log_custom(
+ "update_ai_spam_settings",
+ subject: I18n.t("discourse_ai.spam_detection.logging_subject"),
+ llm_model_id: "#{previous_model_name} → #{settings.llm_model.display_name}",
+ )
+ end
+
def log_ai_agent_deletion(agent_details)
logger = DiscourseAi::Utils::AiStaffActionLogger.new(current_user)
agent_details[:subject] = agent_details[:name]
diff --git a/plugins/discourse-ai/app/models/ai_agent.rb b/plugins/discourse-ai/app/models/ai_agent.rb
index 8a152ac9278..dc85225bdf0 100644
--- a/plugins/discourse-ai/app/models/ai_agent.rb
+++ b/plugins/discourse-ai/app/models/ai_agent.rb
@@ -82,6 +82,7 @@ class AiAgent < ActiveRecord::Base
before_validation :normalize_subagent_ids
before_update :regenerate_rag_fragments
+ after_update :sync_spam_detection_model, if: :saved_change_to_default_llm_id?
before_destroy :ensure_not_system
after_destroy :remove_destroyed_agent_from_subagents
@@ -514,6 +515,15 @@ class AiAgent < ActiveRecord::Base
private
+ # Spam scans use an explicit model setting, so agent edits must update it too.
+ # Clearing the agent default preserves the model selected for spam detection.
+ def sync_spam_detection_model
+ return if default_llm_id.blank?
+
+ settings = AiModerationSetting.spam
+ settings.update!(llm_model_id: default_llm_id) if settings&.ai_agent_id == id
+ end
+
def normalize_subagent_ids
self[:subagent_ids] = Array(self[:subagent_ids])
.filter_map { |id| normalize_subagent_id(id) }
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb
index ada0be2195b..7a5de8ef4ec 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_moderation/spam_scanner_spec.rb
@@ -135,6 +135,18 @@ RSpec.describe DiscourseAi::AiModeration::SpamScanner do
end.to change { AiSpamLog.count }.by(1)
end
+ it "uses the configured agent's updated default model for spam scans" do
+ updated_model = Fabricate(:fake_model)
+ spam_setting.ai_agent.update!(default_llm_id: updated_model.id)
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [{ spam: false }],
+ llm: updated_model,
+ ) { described_class.perform_scan!(post) }
+
+ expect(AiSpamLog.find_by!(post: post).llm_model_id).to eq(updated_model.id)
+ end
+
it "does nothing when disabled" do
SiteSetting.ai_spam_detection_enabled = false
expect { described_class.perform_scan!(post) }.not_to change { AiSpamLog.count }
diff --git a/plugins/discourse-ai/spec/models/ai_agent_spec.rb b/plugins/discourse-ai/spec/models/ai_agent_spec.rb
index 9e98c4c23cb..3f2768129f3 100644
--- a/plugins/discourse-ai/spec/models/ai_agent_spec.rb
+++ b/plugins/discourse-ai/spec/models/ai_agent_spec.rb
@@ -59,6 +59,63 @@ RSpec.describe AiAgent do
expect(AiAgent.agent_users(user: user).map { |candidate| candidate[:id] }).to include(agent.id)
end
+ describe "#update!" do
+ fab!(:spam_agent, :ai_agent)
+ fab!(:spam_settings) do
+ AiModerationSetting.create!(
+ setting_type: :spam,
+ ai_agent: spam_agent,
+ llm_model: seeded_llm_model,
+ )
+ end
+
+ it "updates the spam model when the configured agent's default model changes" do
+ spam_agent.update!(default_llm_id: llm_model.id)
+
+ expect(spam_settings.reload.llm_model_id).to eq(llm_model.id)
+ end
+
+ it "preserves the spam model when another agent's default model changes" do
+ other_agent = Fabricate(:ai_agent, default_llm_id: seeded_llm_model.id)
+
+ other_agent.update!(default_llm_id: llm_model.id)
+
+ expect(spam_settings.reload.llm_model_id).to eq(seeded_llm_model.id)
+ end
+
+ it "preserves an explicit spam model when saving unrelated agent changes" do
+ spam_agent.update!(default_llm_id: llm_model.id)
+ spam_settings.update!(llm_model_id: seeded_llm_model.id)
+
+ spam_agent.update!(description: "Updated spam detector")
+
+ expect(spam_settings.reload.llm_model_id).to eq(seeded_llm_model.id)
+ end
+
+ it "preserves an explicit spam model when clearing the agent's default" do
+ spam_agent.update!(default_llm_id: llm_model.id)
+ spam_settings.update!(llm_model_id: seeded_llm_model.id)
+
+ spam_agent.update!(default_llm_id: nil)
+
+ expect(spam_settings.reload.llm_model_id).to eq(seeded_llm_model.id)
+ end
+
+ it "preserves the spam model when the agent update fails validation" do
+ expect(spam_agent.update(default_llm_id: llm_model.id, description: nil)).to eq(false)
+
+ expect(spam_settings.reload.llm_model_id).to eq(seeded_llm_model.id)
+ end
+
+ it "does not configure spam detection when updating an agent without spam settings" do
+ spam_settings.destroy!
+
+ spam_agent.update!(default_llm_id: llm_model.id)
+
+ expect(AiModerationSetting.spam).to be_nil
+ end
+ end
+
it "defaults subagent_ids to an empty array and exposes them on class instances" do
agent = Fabricate(:ai_agent)
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
index 9b3890ee895..59ef8b577d3 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
@@ -484,6 +484,49 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do
end
end
+ it "logs spam model changes made through the configured agent" do
+ original_model = Fabricate(:seeded_model)
+ AiModerationSetting.create!(
+ setting_type: :spam,
+ ai_agent: ai_agent,
+ llm_model: original_model,
+ )
+
+ expect do
+ put "/admin/plugins/discourse-ai/ai-agents/#{ai_agent.id}.json",
+ params: {
+ ai_agent: {
+ default_llm_id: llm_model.id,
+ },
+ }
+ end.to change { UserHistory.where(custom_type: "update_ai_spam_settings").count }.by(1)
+
+ expect(response).to have_http_status(:ok)
+ expect(AiModerationSetting.spam.llm_model_id).to eq(llm_model.id)
+ history = UserHistory.where(custom_type: "update_ai_spam_settings").last
+ expect(history.acting_user_id).to eq(admin.id)
+ expect(history.subject).to eq(I18n.t("discourse_ai.spam_detection.logging_subject"))
+ expect(history.details).to include(
+ "llm_model_id: #{original_model.display_name} → #{llm_model.display_name}",
+ )
+ end
+
+ it "does not log a spam model change when clearing the agent's default" do
+ ai_agent.update!(default_llm_id: llm_model.id)
+ AiModerationSetting.create!(setting_type: :spam, ai_agent: ai_agent, llm_model: llm_model)
+
+ expect do
+ put "/admin/plugins/discourse-ai/ai-agents/#{ai_agent.id}.json",
+ params: {
+ ai_agent: {
+ default_llm_id: nil,
+ },
+ }
+ end.not_to change { UserHistory.where(custom_type: "update_ai_spam_settings").count }
+
+ expect(response).to have_http_status(:ok)
+ end
+
it "creates a missing bot user when forcing the default LLM" do
agent = Fabricate(:ai_agent, name: "test_bot2", user_id: nil, default_llm_id: llm_model.id)
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb
index 10f9fbeae95..7086f1e95f6 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb
@@ -26,6 +26,43 @@ RSpec.describe DiscourseAi::Admin::AiFeaturesController do
)
end
+ it "reflects changes to the spam detector agent's default language model" do
+ hosted_model = Fabricate(:seeded_model)
+ agent_id = DiscourseAi::Agents::Agent.system_agents[DiscourseAi::Agents::SpamDetector]
+
+ put "/admin/plugins/discourse-ai/ai-spam.json",
+ params: {
+ is_enabled: true,
+ llm_model_id: hosted_model.id,
+ ai_agent_id: agent_id,
+ }
+
+ expect(response.status).to eq(200)
+ expect(SiteSetting.ai_spam_detection_enabled).to eq(true)
+
+ put "/admin/plugins/discourse-ai/ai-agents/#{agent_id}.json",
+ params: {
+ ai_agent: {
+ default_llm_id: llm_model.id,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(AiAgent.find(agent_id).default_llm_id).to eq(llm_model.id)
+
+ get "/admin/plugins/discourse-ai/ai-features.json"
+
+ expect(response.status).to eq(200)
+ spam_module =
+ response.parsed_body["ai_features"].find { |ai_module| ai_module["module_name"] == "spam" }
+ spam_feature = spam_module["features"].find { |feature| feature["name"] == "inspect_posts" }
+
+ expect(spam_feature["llm_models"]).to contain_exactly(
+ "id" => llm_model.id,
+ "name" => llm_model.display_name,
+ )
+ end
+
it "includes automation-related features" do
SiteSetting.discourse_automation_enabled = true