Skip to content

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

FIX: Prevent duplicate reviews and incorrect flag approvals (#44804)

Krzysztof Kotlarek9f191e4f8a62
diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb
index 594c740152c..463c31c157d 100644
--- a/lib/post_destroyer.rb
+++ b/lib/post_destroyer.rb
@@ -411,6 +411,8 @@ class PostDestroyer
end
def handle_reviewable_after_deletion
+ return if @user.id == @post.user_id
+
if @opts[:reviewable_id]
handle_explicit_reviewable
elsif @post.reviewable_flag
diff --git a/plugins/discourse-ai/lib/automation/llm_triage.rb b/plugins/discourse-ai/lib/automation/llm_triage.rb
index f4713f2c7cb..fd9beba0904 100644
--- a/plugins/discourse-ai/lib/automation/llm_triage.rb
+++ b/plugins/discourse-ai/lib/automation/llm_triage.rb
@@ -226,86 +226,89 @@ module DiscourseAi
post.topic.update!(visible: false) if hide_topic
+ already_flagged = false
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 =
- DiscourseAi::Automation.flag_post_reason(
- reason: result,
- automation_id: automation&.id.to_s,
- automation_name: automation&.name,
- )
+ DistributedMutex.synchronize("llm_triage_reviewable_#{post.id}") do
+ # 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 =
+ DiscourseAi::Automation.flag_post_reason(
+ reason: result,
+ automation_id: automation&.id.to_s,
+ automation_name: automation&.name,
+ )
- automation_score_context =
- DiscourseAi::Automation.triage_automation_score_context(automation&.id)
+ automation_score_context =
+ DiscourseAi::Automation.triage_automation_score_context(automation&.id)
- if !flagged_by_tool
- if flag_type == :spam || flag_type == :spam_silence
- spam_score_reason =
- DiscourseAi::Automation.spam_score_reason(
- automation_id: automation&.id.to_s,
- automation_name: automation&.name,
- )
+ if !flagged_by_tool
+ if flag_type == :spam || flag_type == :spam_silence
+ spam_score_reason =
+ DiscourseAi::Automation.spam_score_reason(
+ automation_id: automation&.id.to_s,
+ automation_name: automation&.name,
+ )
- spam_post_action_message =
- DiscourseAi::Automation.spam_post_action_message(
- reason: result,
- automation_id: automation&.id.to_s,
- automation_name: automation&.name,
- )
+ spam_post_action_message =
+ DiscourseAi::Automation.spam_post_action_message(
+ reason: result,
+ automation_id: automation&.id.to_s,
+ automation_name: automation&.name,
+ )
- promote_post_reviewable_to_flagged!(post)
+ promote_post_reviewable_to_flagged!(post)
- result =
- PostActionCreator.new(
+ result =
+ PostActionCreator.new(
+ Discourse.system_user,
+ post,
+ PostActionType.types[:spam],
+ message: spam_post_action_message,
+ reason: spam_score_reason,
+ context: automation_score_context,
+ queue_for_review: true,
+ ).perform
+
+ if flag_type == :spam_silence
+ if result.success?
+ SpamRule::AutoSilence.new(post.user, post).silence_user
+ else
+ Rails.logger.warn(
+ "llm_triage: unable to flag post as spam, post action failed for #{post.id} with error: '#{result.errors.full_messages.join(",").truncate(3000)}'",
+ )
+ end
+ end
+ else
+ reviewable = review_reviewable_for(post)
+
+ reviewable.add_score(
Discourse.system_user,
- post,
- PostActionType.types[:spam],
- message: spam_post_action_message,
- reason: spam_score_reason,
+ ReviewableScore.types[:needs_approval],
+ reason: score_reason,
context: automation_score_context,
- queue_for_review: true,
- ).perform
-
- if flag_type == :spam_silence
- if result.success?
- SpamRule::AutoSilence.new(post.user, post).silence_user
- else
- Rails.logger.warn(
- "llm_triage: unable to flag post as spam, post action failed for #{post.id} with error: '#{result.errors.full_messages.join(",").truncate(3000)}'",
- )
- end
- end
- else
- reviewable = review_reviewable_for(post)
-
- reviewable.add_score(
- Discourse.system_user,
- ReviewableScore.types[:needs_approval],
- reason: score_reason,
- context: automation_score_context,
- force_review: true,
- )
+ force_review: true,
+ )
- # We cannot do this through the PostActionCreator because hiding a post is reserved for auto action flags.
- # Those flags are off_topic, inappropriate, and spam. We want a more generic type for triage, so none of those
- # fit here.
- if flag_type == :review_hide
- post.hide!(PostActionType.types[:notify_moderators])
- elsif flag_type == :review_delete || flag_type == :review_delete_silence
- # Soft-delete the post so it is hidden from users until a moderator handles it in review.
- PostDestroyer.new(Discourse.system_user, post, context: "llm_triage").destroy
-
- if flag_type == :review_delete_silence
- UserSilencer.silence(
- post.user,
- Discourse.system_user,
- message: :silenced_by_staff,
- post_id: post.id,
- )
+ # We cannot do this through the PostActionCreator because hiding a post is reserved for auto action flags.
+ # Those flags are off_topic, inappropriate, and spam. We want a more generic type for triage, so none of those
+ # fit here.
+ if flag_type == :review_hide
+ post.hide!(PostActionType.types[:notify_moderators])
+ elsif flag_type == :review_delete || flag_type == :review_delete_silence
+ # Soft-delete the post so it is hidden from users until a moderator handles it in review.
+ PostDestroyer.new(Discourse.system_user, post, context: "llm_triage").destroy
+
+ if flag_type == :review_delete_silence
+ UserSilencer.silence(
+ post.user,
+ Discourse.system_user,
+ message: :silenced_by_staff,
+ post_id: post.id,
+ )
+ end
end
end
end
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 8555f4dd209..3c9eb4c8483 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
@@ -279,6 +279,64 @@ describe DiscourseAi::Automation::LlmTriage do
end
end
+ it "keeps one reviewable when review and spam rules flag the post concurrently" do
+ review_started = Queue.new
+ release_review = Queue.new
+ review_thread = nil
+ spam_thread = nil
+
+ allow(ReviewablePost).to receive(:needs_review!).and_wrap_original do |original, **args|
+ review_started << true
+ release_review.pop
+ original.call(**args)
+ end
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(%w[bad bad]) do
+ review_thread =
+ Thread.new do
+ triage(
+ post: Post.find(post.id),
+ triage_agent_id: ai_agent.id,
+ search_for_text: "bad",
+ flag_post: true,
+ flag_type: :review,
+ )
+ end
+
+ Timeout.timeout(5) { review_started.pop }
+ spam_thread =
+ Thread.new do
+ triage(
+ post: Post.find(post.id),
+ triage_agent_id: ai_agent.id,
+ search_for_text: "bad",
+ flag_post: true,
+ flag_type: :spam,
+ )
+ end
+
+ spam_thread.join(1)
+ release_review << true
+ Timeout.timeout(5) do
+ review_thread.value
+ spam_thread.value
+ end
+ end
+
+ reviewable = Reviewable.where(target: post).sole
+ expect(reviewable).to be_a(ReviewableFlaggedPost)
+ expect(reviewable.reviewable_scores.map(&:reviewable_score_type)).to contain_exactly(
+ ReviewableScore.types[:needs_approval],
+ ReviewableScore.types[:spam],
+ )
+ ensure
+ release_review << true
+ [review_thread, spam_thread].compact.each do |thread|
+ thread.kill if thread.alive?
+ thread.join
+ end
+ end
+
it "can handle spam+silence flags" do
DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do
triage(
diff --git a/spec/lib/post_destroyer_spec.rb b/spec/lib/post_destroyer_spec.rb
index a107d60a3e7..9bf819ec919 100644
--- a/spec/lib/post_destroyer_spec.rb
+++ b/spec/lib/post_destroyer_spec.rb
@@ -585,6 +585,43 @@ RSpec.describe PostDestroyer do
expect(reviewable.reload).to be_ignored
end
+ it "ignores all pending reviewables when the author immediately deletes an unpenalized post" do
+ SiteSetting.delete_removed_posts_after = 0
+ reply = create_post(topic: post.topic)
+ flagged_reviewable = PostActionCreator.spam(coding_horror, reply).reviewable
+ post_reviewable = ReviewablePost.queue_for_review(reply)
+
+ PostDestroyer.new(reply.user, reply).destroy
+
+ expect(reply.reload).to be_trashed
+ [flagged_reviewable, post_reviewable].each do |reviewable|
+ expect(reviewable.reload).to be_ignored
+ expect(reviewable.reviewable_scores).to all(
+ have_attributes(status: "ignored", reviewed_by_id: Discourse.system_user.id),
+ )
+ end
+ end
+
+ it "keeps all pending reviewables when a silenced author immediately deletes the post" do
+ SiteSetting.delete_removed_posts_after = 0
+ reply = create_post(topic: post.topic)
+ flagged_reviewable = PostActionCreator.spam(coding_horror, reply).reviewable
+ post_reviewable = ReviewablePost.queue_for_review(reply)
+ UserSilencer.silence(reply.user, moderator, post_id: reply.id)
+
+ PostDestroyer.new(reply.user, reply).destroy
+
+ expect(reply.reload).to be_trashed
+ [flagged_reviewable, post_reviewable].each do |reviewable|
+ expect(reviewable.reload).to be_pending
+ expect(reviewable.reviewable_scores).to all(be_pending)
+ expect(reviewable.reviewable_notes.last).to have_attributes(
+ user: Discourse.system_user,
+ content: I18n.t("reviewables.post_deleted_by_author_after_penalty"),
+ )
+ end
+ end
+
it "does not auto-ignore reviewable when author was silenced for the post" do
reply = create_post(topic: post.topic)
reviewable = PostActionCreator.spam(coding_horror, reply).reviewable