Skip to content

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

FEATURE: Cancel AI agent workflow nodes at the run timeout

Régis Hanolee575913ef9b
diff --git a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
index 3f2e154b605..7abff6c9b4f 100644
--- a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
+++ b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
@@ -224,28 +224,38 @@ if defined?(DiscourseWorkflows)
},
)
- return [[run_once_for_all_items(exec_ctx, agent)]] if mode == RUN_ONCE_FOR_ALL_ITEMS
-
- items =
- exec_ctx.input_items.map.with_index do |item, item_index|
- result =
- run_agent(
- agent,
- item_config(exec_ctx, item_index),
- exec_ctx.log,
- runner(exec_ctx, item_index),
- )
-
- wrap(result, paired_item: exec_ctx.paired_item_for(item))
+ with_cancel_manager(exec_ctx.deadline) do |cancel_manager|
+ if mode == RUN_ONCE_FOR_ALL_ITEMS
+ next [[run_once_for_all_items(exec_ctx, agent, cancel_manager)]]
end
- [items]
+ items =
+ exec_ctx.input_items.map.with_index do |item, item_index|
+ result = run_agent(agent, exec_ctx, item_index, cancel_manager)
+ wrap(result, paired_item: exec_ctx.paired_item_for(item))
+ end
+
+ [items]
+ end
end
private
- def run_once_for_all_items(exec_ctx, agent)
- result = run_agent(agent, item_config(exec_ctx, 0), exec_ctx.log, runner(exec_ctx, 0))
+ def with_cancel_manager(deadline)
+ cancel_manager = DiscourseAi::Completions::CancelManager.new
+ if deadline
+ timer =
+ Concurrent::ScheduledTask.execute([deadline - Time.current, 0].max) do
+ cancel_manager.cancel!
+ end
+ end
+ yield cancel_manager
+ ensure
+ timer&.cancel
+ end
+
+ def run_once_for_all_items(exec_ctx, agent, cancel_manager)
+ result = run_agent(agent, exec_ctx, 0, cancel_manager)
wrap(
result,
@@ -370,7 +380,10 @@ if defined?(DiscourseWorkflows)
I18n.t("discourse_ai.discourse_workflows.uploads.#{key}", **args)
end
- def run_agent(agent, config, log, runner)
+ def run_agent(agent, exec_ctx, item_index, cancel_manager)
+ config = item_config(exec_ctx, item_index)
+ runner = runner(exec_ctx, item_index)
+ log = exec_ctx.log
agent_record = agent[:record]
llm_model = agent[:llm_model]
prompt = config["prompt"].to_s
@@ -391,9 +404,11 @@ if defined?(DiscourseWorkflows)
messages: [{ type: :user, content: content }],
feature_name: "workflow",
execution_context: execution_context,
+ cancel_manager: cancel_manager,
)
result, structured = collect_reply(agent[:bot], bot_context, log)
+ raise DiscourseWorkflows::Executor::ExecutionTimedOut if cancel_manager.cancelled?
skipped =
execution_context.upload_skips.map { |skip| upload_error_text("skipped", **skip) }
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
index 2f802b1458f..eca4c8ccfa8 100644
--- a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
@@ -106,6 +106,25 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
end
end
+ it "cancels the agent reply once the execution deadline passes" do
+ cancel_manager = nil
+ allow(bot).to receive(:reply) do |bot_context|
+ cancel_manager = bot_context.cancel_manager
+ cancel_manager.wait_for_cancel(5)
+ end
+
+ expect {
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "prompt" => "Hello",
+ },
+ deadline: 0.05.seconds.from_now,
+ )
+ }.to raise_error(DiscourseWorkflows::Executor::ExecutionTimedOut)
+ expect(cancel_manager).to be_cancelled
+ end
+
it "runs the agent once per input item with item-specific parameters" do
items =
execute_node_output(