A platform for community discussion. Free, open, simple.
FIX: Add workflow agents to review queue reasons (#44415)
Gabriel Grubbae2aec2ef493a
diff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.mdindex 543e8977eb2..290f92eb227 100644--- a/plugins/discourse-workflows/USE_CASES.md+++ b/plugins/discourse-workflows/USE_CASES.md@@ -79,7 +79,7 @@ For each run, record:- Connections leaving `condition:filter` or `condition:if` use `connection_type: "main"`; select the passing branch with `output_index: 0` and the rejected branch with `output_index: 1`.- Group membership checks should use `action:group` with `operation: check_membership` and the resolved `group_id` instead of Code nodes. Branch with `condition:if` on `$json.group_membership.in_group` when different member and non-member paths are needed.- Deleting a post is `action:post` with `operation: delete`; `action:flag_post` with `review_delete` is for moderation flows that also need a reviewable. Restoring one is `operation: recover`.-- Flag post reasons and Flag user notes name the nearest AI Agent on the item's executed input path. Conditions preserve that attribution; merged inputs from different Agents or incomplete item links retain Workflow-only attribution. This applies to new flags and does not change the Workflow filter context.+- Flag post reasons and Flag user notes name the nearest AI Agent on the item's executed input path. Conditions preserve that attribution; merged inputs from different Agents or incomplete item links retain Workflow-only attribution. Successful flags also persist the attributed Agent ID on the reviewable, making that Agent available in the Review Queue Reason filter. Agent filtering survives renames and preserves Workflow filtering and score deduplication. Ambiguous or skipped flags do not add an Agent. This applies to new flags.- `trigger:post_destroyed` and `trigger:post_recovered` expose `$json.user` as the acting moderator, not the post author; use `$json.post.username` for the author.- DM/personal-message notifications should use `action:send_personal_message` instead of chat or topic reply nodes. Only an explicit chat DM uses `action:send_chat_message` with `target: "user"` (or `"group"` for one group DM).- Forum `search`/`read` should not be used for node/schema discovery; use `workflow_node_catalog` and `workflow_validate_patch`.diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_post/v1.rbindex fa0dec3d079..d7f75081f4f 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_post/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_post/v1.rb@@ -131,6 +131,8 @@ module DiscourseWorkflows)end+ DiscourseWorkflows.record_review_agent(reviewable, exec_ctx, item_index:)+output(post, flag_type, reviewable)enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_user/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_user/v1.rbindex b8bd5d119d6..91d3a18f9f9 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_user/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/flag_user/v1.rb@@ -122,6 +122,7 @@ module DiscourseWorkflowscontext = DiscourseWorkflows.reviewable_score_context(exec_ctx.get_workflow.id)score_added = add_review_score(reviewable, actor, context)add_provenance_note(exec_ctx, reviewable, actor, config["reason"], item_index)+ DiscourseWorkflows.record_review_agent(reviewable, exec_ctx, item_index:)output(user,diff --git a/plugins/discourse-workflows/plugin.rb b/plugins/discourse-workflows/plugin.rbindex 640c0ec4635..fe579bf6cf6 100644--- a/plugins/discourse-workflows/plugin.rb+++ b/plugins/discourse-workflows/plugin.rb@@ -17,13 +17,34 @@ module ::DiscourseWorkflows"discourse_workflows:workflow:#{workflow_id}" if workflow_id.present?end- def self.review_attribution(exec_ctx, item_index:, flag_type:, escape: false)+ def self.review_agent(exec_ctx, item_index:)agents = exec_ctx.nearest_upstream_metadata("review_agent", item_index:)+ agents.first if agents&.one?+ end++ def self.record_review_agent(reviewable, exec_ctx, item_index:)+ agent = review_agent(exec_ctx, item_index:)+ return if !agent++ reviewable.with_lock do+ payload = reviewable.payload || {}+ agent_ids = Array(payload["workflow_review_agent_ids"])+ agent_id = agent.fetch("id").to_s+ next if agent_ids.include?(agent_id)++ reviewable.update!(+ payload: payload.merge("workflow_review_agent_ids" => agent_ids + [agent_id]),+ )+ end+ end++ def self.review_attribution(exec_ctx, item_index:, flag_type:, escape: false)+ agent = review_agent(exec_ctx, item_index:)values = { workflow_name: exec_ctx.get_workflow.name }key = "discourse_workflows.#{flag_type}.flagged_by_workflow"- if agents&.one?+ if agentkey = "discourse_workflows.#{flag_type}.flagged_by_agent"- values[:agent_name] = agents.first.fetch("name")+ values[:agent_name] = agent.fetch("name")endvalues.transform_values! { |value| ERB::Util.html_escape(value) } if escapeI18n.t(key, **values)@@ -116,6 +137,36 @@ after_initialize doend,)+ add_custom_reviewable_filter(+ [+ :workflow_review_agent_id,+ proc do |results, value|+ agent_id = value.to_s[/\A[1-9]\d*\z/]+ next results if !agent_id++ results.where(+ "(reviewables.payload::jsonb -> 'workflow_review_agent_ids') ? :agent_id",+ agent_id:,+ )+ end,+ ],+ reason_filters: -> do+ next [] if !defined?(AiAgent)++ AiAgent+ .where(<<~SQL)+ id IN (+ SELECT json_array_elements_text(payload -> 'workflow_review_agent_ids')::bigint+ FROM reviewables+ WHERE payload -> 'workflow_review_agent_ids' IS NOT NULL+ )+ SQL+ .order(:name)+ .pluck(:id, :name)+ .map { |id, name| { id: "discourse_workflows:agent:#{id}", name:, value: id } }+ end,+ )+add_to_class(:guardian, :can_manage_workflows?) { is_admin? }nodes_dir = File.join(File.dirname(__FILE__), "lib/discourse_workflows/nodes")diff --git a/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rb b/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rbindex 1ceedfec9bd..6bf29216ce7 100644--- a/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rb+++ b/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rb@@ -71,6 +71,9 @@ RSpec.describe "Agent attribution on workflow flags" doendexpect(execution.reload.status).to eq("success")+ expect(ReviewablePost.find_by!(target: post).payload["workflow_review_agent_ids"]).to eq(+ [spam_agent.id.to_s],+ )expect(ReviewablePost.find_by!(target: post).reviewable_scores.last.reason).to eq(I18n.t("discourse_workflows.flag_post.flagged_by_agent",@@ -118,6 +121,9 @@ RSpec.describe "Agent attribution on workflow flags" do) { DiscourseWorkflows::Executor.new(workflow, "trigger", { post_id: post.id }).run }expect(execution.status).to eq("success")+ expect(+ ReviewablePost.find_by!(target: post).payload.to_h["workflow_review_agent_ids"],+ ).to be_nilexpect(ReviewablePost.find_by!(target: post).reviewable_scores.last.reason).to eq(I18n.t("discourse_workflows.flag_post.flagged_by_workflow", workflow_name: workflow.name),)@@ -149,6 +155,9 @@ RSpec.describe "Agent attribution on workflow flags" doendexpect(execution.status).to eq("success")+ expect(ReviewableUser.find_by!(target: user).payload["workflow_review_agent_ids"]).to eq(+ [spam_agent.id.to_s],+ )expect(ReviewableUser.find_by!(target: user).reviewable_notes.last.content).to eq(I18n.t("discourse_workflows.flag_user.flagged_by_agent",@@ -156,6 +165,25 @@ RSpec.describe "Agent attribution on workflow flags" doworkflow_name: workflow.name,),)+ reviewable = ReviewableUser.find_by!(target: user)+ expect do+ DiscourseAi::Completions::Llm.with_prepared_responses([{ verdict: "yes" }.to_json]) do+ DiscourseWorkflows::Executor.new(workflow, "trigger", { username: user.username }).run+ end+ end.not_to change { reviewable.reviewable_scores.count }+ expect(reviewable.reload.payload["workflow_review_agent_ids"]).to eq([spam_agent.id.to_s])++ graph[:nodes].find { |node| node["id"] == "agent" }["parameters"]["agent_id"] = billing_agent.id+ other_workflow = Fabricate(:discourse_workflows_workflow, published: true, **graph)+ DiscourseAi::Completions::Llm.with_prepared_responses([{ verdict: "yes" }.to_json]) do+ DiscourseWorkflows::Executor.new(other_workflow, "trigger", { username: user.username }).run+ end++ expect(reviewable.reload.payload["workflow_review_agent_ids"]).to contain_exactly(+ spam_agent.id.to_s,+ billing_agent.id.to_s,+ )+ expect(reviewable.reviewable_scores.count).to eq(2)endit "attributes each flag to the nearest agent through the executed condition branches" do@@ -217,6 +245,7 @@ RSpec.describe "Agent attribution on workflow flags" doworkflow_name: workflow.name,),)+ expect(score.reviewable.payload["workflow_review_agent_ids"]).to eq([agent.id.to_s])expect(score.context).to eq("discourse_workflows:workflow:#{workflow.id}")endenddiff --git a/plugins/discourse-workflows/spec/requests/reviewables_controller_spec.rb b/plugins/discourse-workflows/spec/requests/reviewables_controller_spec.rbindex b92d97b9407..49587c3256b 100644--- a/plugins/discourse-workflows/spec/requests/reviewables_controller_spec.rb+++ b/plugins/discourse-workflows/spec/requests/reviewables_controller_spec.rb@@ -43,6 +43,72 @@ RSpec.describe ReviewablesController do)end+ it "offers successfully attributed agents and filters posts and users after an agent rename" do+ agent = Fabricate(:ai_agent)+ other_agent = Fabricate(:ai_agent)+ post_reviewable.update!(payload: { "workflow_review_agent_ids" => [agent.id.to_s] })+ user_reviewable.update!(+ payload: {+ "workflow_review_agent_ids" => [agent.id.to_s, other_agent.id.to_s],+ },+ )+ agent.update!(name: "Renamed agent")++ get "/review.json", params: { score_type: "discourse_workflows:agent:#{agent.id}" }++ expect(response.status).to eq(200)+ expect(response.parsed_body.dig("meta", "score_types")).to include(+ { "id" => "discourse_workflows:agent:#{agent.id}", "name" => agent.name },+ { "id" => "discourse_workflows:agent:#{other_agent.id}", "name" => other_agent.name },+ )+ expect(+ response.parsed_body["reviewables"].map { |reviewable| reviewable["id"] },+ ).to contain_exactly(post_reviewable.id, user_reviewable.id)++ get "/review.json",+ params: {+ type: "discourse_workflows:workflow",+ score_type: "discourse_workflows:agent:#{other_agent.id}",+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body["reviewables"].map { |reviewable| reviewable["id"] }).to eq(+ [user_reviewable.id],+ )++ SiteSetting.enable_discourse_workflows = false+ get "/review.json"++ expect(response.parsed_body.dig("meta", "score_types")).not_to include(+ { "id" => "discourse_workflows:agent:#{agent.id}", "name" => agent.name },+ )+ end++ it "omits agents without successful workflow flags from Reason" do+ agent = Fabricate(:ai_agent)++ get "/review.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body.dig("meta", "score_types")).not_to include(+ { "id" => "discourse_workflows:agent:#{agent.id}", "name" => agent.name },+ )+ end++ it "ignores malformed agent IDs" do+ [true, [1], { id: 1 }, 0, 1.9, "1 OR 1=1 --"].each do |agent_id|+ get "/review.json",+ params: {+ additional_filters: { workflow_review_agent_id: agent_id }.to_json,+ }++ expect(response.status).to eq(200)+ expect(+ response.parsed_body["reviewables"].map { |reviewable| reviewable["id"] },+ ).to contain_exactly(post_reviewable.id, user_reviewable.id, unrelated_reviewable.id)+ end+ end+it "filters posts and users by workflow Type" doget "/review.json", params: { type: "discourse_workflows:workflow" }