A platform for community discussion. Free, open, simple.
FIX: Scope ReviewableAiToolAction to target post's topic and category (#39716)
Rafael dos Santos Silva0bc1216009f1
diff --git a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rbindex 0ed194dbaf2..d135a619c4f 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb@@ -3,6 +3,20 @@require_dependency "reviewable"class ReviewableAiToolAction < Reviewable+ def created_new!+ super++ self.topic ||= target_post&.topic+ self.category_id ||= topic&.category_id+ end++ def target_post+ return @target_post if defined?(@target_post)++ post_id = target&.post_id+ @target_post = post_id ? Post.find_by(id: post_id) : nil+ end+def build_actions(actions, guardian, args)return actions if !pending?diff --git a/plugins/discourse-ai/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope.rb b/plugins/discourse-ai/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope.rbnew file mode 100644index 00000000000..f076547e0da--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope.rb@@ -0,0 +1,23 @@+# frozen_string_literal: true++class BackfillReviewableAiToolActionScope < ActiveRecord::Migration[8.0]+ def up+ execute <<~SQL+ UPDATE reviewables r+ SET topic_id = p.topic_id,+ category_id = t.category_id+ FROM ai_tool_actions a+ JOIN posts p ON p.id = a.post_id+ JOIN topics t ON t.id = p.topic_id+ WHERE r.type = 'ReviewableAiToolAction'+ AND r.target_type = 'AiToolAction'+ AND r.target_id = a.id+ AND r.topic_id IS NULL+ AND a.post_id IS NOT NULL+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/plugins/discourse-ai/spec/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope_spec.rb b/plugins/discourse-ai/spec/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope_spec.rbnew file mode 100644index 00000000000..dfab35af809--- /dev/null+++ b/plugins/discourse-ai/spec/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope_spec.rb@@ -0,0 +1,75 @@+# frozen_string_literal: true++require Rails.root.join(+ "plugins/discourse-ai/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope",+ )++RSpec.describe BackfillReviewableAiToolActionScope do+ fab!(:private_group, :group)+ fab!(:private_category) { Fabricate(:private_category, group: private_group) }+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }+ fab!(:private_post) { Fabricate(:post, topic: private_topic) }+ fab!(:ai_agent)++ around { |example| ActiveRecord::Migration.suppress_messages { example.run } }++ before { enable_current_plugin }++ def create_unscoped_reviewable(post_id:)+ tool_action =+ AiToolAction.create!(+ tool_name: "close_topic",+ tool_parameters: {+ topic_id: private_topic.id,+ },+ ai_agent: ai_agent,+ bot_user_id: Discourse.system_user.id,+ post_id: post_id,+ )++ reviewable =+ ReviewableAiToolAction.needs_review!(+ target: tool_action,+ created_by: Discourse.system_user,+ reviewable_by_moderator: true,+ payload: {+ agent_name: "Test Agent",+ },+ )++ reviewable.update_columns(topic_id: nil, category_id: nil)+ reviewable+ end++ it "backfills topic and category for reviewables whose target action has a post" do+ reviewable = create_unscoped_reviewable(post_id: private_post.id)++ described_class.new.up++ reviewable.reload+ expect(reviewable.topic_id).to eq(private_topic.id)+ expect(reviewable.category_id).to eq(private_category.id)+ end++ it "leaves topic and category nil when the target action has no post" do+ reviewable = create_unscoped_reviewable(post_id: nil)++ described_class.new.up++ reviewable.reload+ expect(reviewable.topic_id).to be_nil+ expect(reviewable.category_id).to be_nil+ end++ it "does not overwrite reviewables that are already scoped" do+ reviewable = create_unscoped_reviewable(post_id: private_post.id)+ other_topic = Fabricate(:topic)+ reviewable.update_columns(topic_id: other_topic.id, category_id: other_topic.category_id)++ described_class.new.up++ reviewable.reload+ expect(reviewable.topic_id).to eq(other_topic.id)+ expect(reviewable.category_id).to eq(other_topic.category_id)+ end+enddiff --git a/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb b/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rbindex d1d59179225..5d78d4ebcca 100644--- a/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb+++ b/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb@@ -12,13 +12,14 @@ RSpec.describe ReviewableAiToolAction doSiteSetting.ai_bot_enabled = trueend- def create_tool_action(tool_name: "close_topic", params: nil)+ def create_tool_action(tool_name: "close_topic", params: nil, post_id: nil)params ||= { topic_id: topic.id, closed: true, reason: "Off-topic" }AiToolAction.create!(tool_name: tool_name,tool_parameters: params,ai_agent: ai_agent,bot_user_id: bot_user.id,+ post_id: post_id,)end@@ -41,6 +42,29 @@ RSpec.describe ReviewableAiToolAction doreviewableend+ describe "#created_new!" do+ fab!(:private_category_group, :group)+ fab!(:private_category) { Fabricate(:private_category, group: private_category_group) }+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }+ fab!(:private_post) { Fabricate(:post, topic: private_topic) }++ it "scopes the reviewable to the target post's topic and category" do+ tool_action = create_tool_action(post_id: private_post.id)+ reviewable = create_reviewable(tool_action)++ expect(reviewable.topic).to eq(private_topic)+ expect(reviewable.category).to eq(private_category)+ end++ it "leaves topic and category blank when the target action has no post" do+ tool_action = create_tool_action(post_id: nil)+ reviewable = create_reviewable(tool_action)++ expect(reviewable.topic).to be_nil+ expect(reviewable.category).to be_nil+ end+ end+describe "#build_actions" doit "has approve and reject actions when pending" dotool_action = create_tool_action