Skip to content

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

FIX: Prevent duplicate PMs when multiple LLM triage rules flag same post (#35596)

Roman Rizzia12f425b7ecf
diff --git a/plugins/discourse-ai/lib/automation/llm_triage.rb b/plugins/discourse-ai/lib/automation/llm_triage.rb
index a0d77d87dda..3e13d992f53 100644
--- a/plugins/discourse-ai/lib/automation/llm_triage.rb
+++ b/plugins/discourse-ai/lib/automation/llm_triage.rb
@@ -3,6 +3,17 @@
module DiscourseAi
module Automation
module LlmTriage
+ def self.flagged_by_another_triage_rule?(post)
+ triage_score_types = [ReviewableScore.types[:spam], ReviewableScore.types[:needs_approval]]
+
+ ReviewableScore
+ .pending
+ .where(user: Discourse.system_user, reviewable_score_type: triage_score_types)
+ .joins(:reviewable)
+ .where(reviewables: { target: post })
+ .exists?
+ end
+
def self.handle(
post:,
triage_persona_id:,
@@ -106,7 +117,6 @@ module DiscourseAi
end
elsif canned_reply.present? && action != :edit
post_type = whisper ? Post.types[:whisper] : Post.types[:regular]
-
PostCreator.create!(
user,
topic_id: post.topic_id,
@@ -133,6 +143,11 @@ module DiscourseAi
post.topic.update!(visible: false) if hide_topic
if flag_post
+ # Check if another triage rule already created a reviewable for this post.
+ # We'll later use it to avoid sending multiple PMs to the user.
+ # We are doing this now before we create another flag.
+ already_flagged = flagged_by_another_triage_rule?(post)
+
score_reason =
I18n.t(
"discourse_automation.scriptables.llm_triage.flagged_post",
@@ -191,7 +206,7 @@ module DiscourseAi
end
end
- if notify_author_pm && action != :edit
+ if notify_author_pm && action != :edit && !already_flagged
begin
pm_sender =
if notify_author_pm_user.present?
diff --git a/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb b/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb
index bc56b011189..9dcbc652ed1 100644
--- a/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb
@@ -387,4 +387,41 @@ describe DiscourseAi::Automation::LlmTriage do
"<a href=\"#{Discourse.base_path}/admin/plugins/automation/",
)
end
+
+ it "only sends one PM when multiple rules flag the same post" do
+ # First rule flags the post and sends PM
+ DiscourseAi::Completions::Llm.with_prepared_responses(%w[bad bad]) do
+ triage(
+ post: post,
+ triage_persona_id: ai_persona.id,
+ search_for_text: "bad",
+ flag_post: true,
+ flag_type: :review,
+ automation: nil,
+ notify_author_pm: true,
+ )
+
+ triage(
+ post: post.reload,
+ triage_persona_id: ai_persona.id,
+ search_for_text: "bad",
+ flag_post: true,
+ flag_type: :review,
+ automation: nil,
+ notify_author_pm: true,
+ )
+ end
+
+ pm_topics =
+ Topic
+ .where(archetype: Archetype.private_message)
+ .joins(:topic_allowed_users)
+ .where(topic_allowed_users: { user: post.user })
+
+ expect(pm_topics.size).to eq(1)
+
+ reviewable_scores_count =
+ ReviewableScore.joins(:reviewable).where(reviewable: { target: post }).count
+ expect(reviewable_scores_count).to eq(2)
+ end
end