Skip to content

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

FIX: remove subagent call limit (#45449)

Same602396633ec
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 69021e720d1..670ae7c473d 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -596,7 +596,6 @@ en:
agent_not_allowed: "The requested subagent is not allowed."
unavailable_agent: "The requested subagent is unavailable or inaccessible."
missing_model: "The requested subagent has no available language model."
- call_limit: "The delegation call limit has been reached."
completion_limit: "The delegation completion limit has been reached."
token_limit: "The shared turn work allowance or child work allocation has been reached."
cancelled: "The delegated task was cancelled."
diff --git a/plugins/discourse-ai/lib/agents/agent.rb b/plugins/discourse-ai/lib/agents/agent.rb
index 11d61c2bd66..b3a4384a002 100644
--- a/plugins/discourse-ai/lib/agents/agent.rb
+++ b/plugins/discourse-ai/lib/agents/agent.rb
@@ -413,9 +413,7 @@ module DiscourseAi
return if context.subagent_depth.to_i >= SubagentRunner::MAX_SUBAGENT_DEPTH
state = context.subagent_execution_state
- if !state&.spawn_available? || !state.completion_available? || state.remaining_tokens <= 0
- return
- end
+ return if !state&.completion_available? || state.remaining_tokens <= 0
records_by_id = AiAgent.where(id: self.class.subagent_ids, enabled: true).index_by(&:id)
models_by_agent_id = SubagentRunner.resolve_models(records_by_id.values)
diff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rb
index b58e83794a0..a36e1d7f07c 100644
--- a/plugins/discourse-ai/lib/agents/bot.rb
+++ b/plugins/discourse-ai/lib/agents/bot.rb
@@ -418,7 +418,6 @@ module DiscourseAi
tool_invocation_budget_exhausted ||=
tool_batch_count >= MAX_TOOL_CALLS_PER_COMPLETION ||
- spawn_agent_budget_exhausted?(tool, context) ||
tool_invocation_budget_exhausted?(context)
chain_next_response =
@@ -516,21 +515,13 @@ module DiscourseAi
private
- def spawn_agent_budget_exhausted?(tool, context)
- tool.is_a?(Tools::SpawnAgent) && !context.subagent_execution_state.spawn_available?
- end
-
def tool_invocation_budget_exhausted?(context)
runtime_tools = context.runtime_tools.presence || agent.available_tools
return false if runtime_tools.empty?
runtime_tools.all? do |tool_class|
- if tool_class <= Tools::SpawnAgent
- !context.subagent_execution_state.spawn_available?
- else
- limit = agent.options[tool_class].to_h.with_indifferent_access[:max_invocations].to_i
- context.tool_invocation_limit_reached?(tool_class.name, limit: limit)
- end
+ limit = agent.options[tool_class].to_h.with_indifferent_access[:max_invocations].to_i
+ context.tool_invocation_limit_reached?(tool_class.name, limit: limit)
end
end
diff --git a/plugins/discourse-ai/lib/agents/subagent_execution_state.rb b/plugins/discourse-ai/lib/agents/subagent_execution_state.rb
index 56e155793a0..0e65f131770 100644
--- a/plugins/discourse-ai/lib/agents/subagent_execution_state.rb
+++ b/plugins/discourse-ai/lib/agents/subagent_execution_state.rb
@@ -3,15 +3,14 @@
module DiscourseAi
module Agents
class SubagentExecutionState
- MAX_SPAWN_CALLS = 5
- MAX_COMPLETIONS = 100
+ # Bound failed or no-progress loops, reserving the last completion for the root answer.
+ MAX_COMPLETIONS = 300
attr_reader :execution_context, :root_token_budget
def initialize(execution_context:, root_token_budget:, completion_count: 0)
@execution_context = execution_context
@root_token_budget = root_token_budget
- @spawn_count = 0
@completion_count = completion_count
execution_context.work_budget ||=
DiscourseAi::Completions::TurnWorkBudget.new(limit: root_token_budget)
@@ -19,19 +18,6 @@ module DiscourseAi
@mutex = Mutex.new
end
- def spawn_available?
- @mutex.synchronize { @spawn_count < MAX_SPAWN_CALLS }
- end
-
- def reserve_spawn
- @mutex.synchronize do
- return false if @spawn_count >= MAX_SPAWN_CALLS
-
- @spawn_count += 1
- true
- end
- end
-
def completion_count
@mutex.synchronize { @completion_count }
end
diff --git a/plugins/discourse-ai/lib/agents/subagent_runner.rb b/plugins/discourse-ai/lib/agents/subagent_runner.rb
index 15642afa366..a418812fab1 100644
--- a/plugins/discourse-ai/lib/agents/subagent_runner.rb
+++ b/plugins/discourse-ai/lib/agents/subagent_runner.rb
@@ -52,7 +52,6 @@ module DiscourseAi
state = @context.subagent_execution_state
return error("cancelled", child_record) if @context.cancel_manager&.cancelled?
return error("token_limit", child_record) if state.remaining_tokens <= 0
- return error("call_limit", child_record) if !state.reserve_spawn
yield(@prompt) if block_given?
@@ -97,8 +96,9 @@ module DiscourseAi
parent_record = AiAgent.find_by(id: @parent_agent.id)
return error("unavailable_parent") if !parent_record
return error("agent_not_allowed") if !parent_record.subagent_ids.include?(@child_id)
- return error("call_limit") if [email protected]_execution_state&.spawn_available?
- return error("completion_limit") if [email protected]_execution_state.completion_available?
+ if [email protected]_execution_state&.completion_available?
+ return error("completion_limit")
+ end
error("token_limit") if @context.subagent_execution_state.remaining_tokens <= 0
end
diff --git a/plugins/discourse-ai/spec/lib/agents/subagent_runner_spec.rb b/plugins/discourse-ai/spec/lib/agents/subagent_runner_spec.rb
index cf82e37d51a..c61a0b7a533 100644
--- a/plugins/discourse-ai/spec/lib/agents/subagent_runner_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/subagent_runner_spec.rb
@@ -333,10 +333,9 @@ RSpec.describe DiscourseAi::Agents::SubagentRunner do
expect(agent.runtime_tools(context: context).map(&:name)).not_to include("spawn_agent")
end
- it "requests a final answer when the shared spawn budget is exhausted" do
- parent.update!(tools: [["Search", nil, false]])
+ it "keeps delegation available after more than five child runs while shared work remains" do
spawn_calls =
- Array.new(DiscourseAi::Agents::SubagentExecutionState::MAX_SPAWN_CALLS) do |index|
+ Array.new(6) do |index|
DiscourseAi::Completions::ToolCall.new(
id: "spawn-#{index}",
name: "spawn_agent",
@@ -362,10 +361,113 @@ RSpec.describe DiscourseAi::Agents::SubagentRunner do
expect(raw_context.last.first).to eq("Final answer")
expect(prompts.length).to eq(responses.length)
+ expect(prompts.map(&:tool_choice)).to all(eq(nil))
+ results = raw_context.select { |entry| entry[2] == "tool" && entry[3] == "spawn_agent" }
+ expect(results.map { |entry| JSON.parse(entry.first)["response"] }).to eq(
+ Array.new(6) { |index| "Child result #{index}" },
+ )
+ expect(bot.agent.runtime_tools(context: context).map(&:name)).to include("spawn_agent")
+ expect(context.subagent_execution_state.completion_count).to eq(responses.length)
+ tokenizer = model.to_llm.tokenizer
+ generation_tokens =
+ responses.sum do |response|
+ output = DiscourseAi::Completions::GeneratedOutput.new
+ output << response
+ output.size(tokenizer)
+ end
+ evidence_tokens =
+ results.sum { |entry| tokenizer.size(JSON.parse(entry.first).except("response").to_json) }
+ expect(context.execution_context.work_budget.used).to eq(generation_tokens + evidence_tokens)
+ end
+ end
+
+ it "rejects further delegation when repeated child output exhausts shared work" do
+ child_response = "Child evidence"
+ token_budget = model.to_llm.tokenizer.size(child_response) * 6
+ execution_context = DiscourseAi::Completions::ExecutionContext.new
+ state =
+ DiscourseAi::Agents::SubagentExecutionState.new(
+ execution_context: execution_context,
+ root_token_budget: token_budget,
+ )
+ context =
+ DiscourseAi::Agents::BotContext.new(
+ user: user,
+ execution_context: execution_context,
+ subagent_execution_state: state,
+ )
+ runner =
+ described_class.new(
+ parent_agent: parent.class_instance.new,
+ child_id: child.id,
+ prompt: "Check the evidence",
+ context: context,
+ parent_llm: model.to_llm,
+ )
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(Array.new(6, child_response)) do |canned|
+ 6.times { expect(runner.run).to include(status: "ok", response: child_response) }
+
+ expect(execution_context.work_budget.used).to eq(token_budget)
+ expect(state.remaining_tokens).to eq(0)
+ expect(runner.run[:error]).to eq(I18n.t("discourse_ai.ai_bot.subagent_errors.token_limit"))
+ expect(canned.completions).to eq(6)
+ expect(state.completion_count).to eq(6)
+ end
+ end
+
+ it "stops repeated failed delegations at the shared completion safety limit" do
+ execution_context = DiscourseAi::Completions::ExecutionContext.new
+ failed_calls =
+ Array.new(7) do |index|
+ DiscourseAi::Completions::ToolCall.new(
+ id: "invalid-spawn-#{index}",
+ name: "spawn_agent",
+ parameters: {
+ agent_id: child.id,
+ prompt: "",
+ },
+ )
+ end
+ state =
+ DiscourseAi::Agents::SubagentExecutionState.new(
+ execution_context: execution_context,
+ root_token_budget: 10_000,
+ completion_count:
+ DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - failed_calls.length - 1,
+ )
+ context =
+ DiscourseAi::Agents::BotContext.new(
+ user: user,
+ messages: [{ type: :user, content: "Check the evidence" }],
+ execution_context: execution_context,
+ subagent_execution_state: state,
+ )
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [*failed_calls, "Unable to complete the delegated work"],
+ ) do |canned, _, prompts|
+ bot = DiscourseAi::Agents::Bot.as(user, agent: parent.class_instance.new, model: model)
+ raw_context = bot.reply(context)
+ results = raw_context.select { |entry| entry[2] == "tool" && entry[3] == "spawn_agent" }
+
+ expect(results.length).to eq(failed_calls.length)
+ expect(results.map { |entry| JSON.parse(entry.first) }).to all(
+ include(
+ "status" => "error",
+ "error" => I18n.t("discourse_ai.ai_bot.subagent_errors.invalid_prompt"),
+ ),
+ )
+ expect(raw_context.last.first).to eq("Unable to complete the delegated work")
+ expect(canned.completions).to eq(failed_calls.length + 1)
+ expect(state.completion_count).to eq(
+ DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS,
+ )
+ expect(state.remaining_tokens).to be > 0
expect(prompts.last.tool_choice).to eq(:none)
expect(prompts.last.messages.last).to eq(
type: :user,
- content: DiscourseAi::Agents::Bot::TOOL_INVOCATION_BUDGET_FINAL_ANSWER_HINT,
+ content: DiscourseAi::Agents::Bot::COMPLETION_LIMIT_FINAL_ANSWER_HINT,
)
end
end
@@ -777,7 +879,7 @@ RSpec.describe DiscourseAi::Agents::SubagentRunner do
expect(ReviewableAiToolAction.count).to eq(0)
end
- it "rejects invalid prompts and exhausted calls" do
+ it "rejects oversized prompts and excessive delegation depth" do
execution_context =
DiscourseAi::Completions::ExecutionContext.new(
token_usage_tracker: DiscourseAi::Completions::TokenUsageTracker.new,
@@ -815,17 +917,5 @@ RSpec.describe DiscourseAi::Agents::SubagentRunner do
parent_llm: DiscourseAi::Completions::Llm.proxy(model),
).run
expect(depth_result[:error]).to eq(I18n.t("discourse_ai.ai_bot.subagent_errors.depth_limit"))
- context.subagent_depth = 0
-
- 5.times { expect(state.reserve_spawn).to eq(true) }
- runner =
- DiscourseAi::Agents::SubagentRunner.new(
- parent_agent: parent.class_instance.new,
- child_id: child.id,
- prompt: "Check this",
- context: context,
- parent_llm: DiscourseAi::Completions::Llm.proxy(model),
- )
- expect(runner.run[:error]).to eq(I18n.t("discourse_ai.ai_bot.subagent_errors.call_limit"))
end
end
diff --git a/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rb b/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rb
index dffe5176a74..2a88087188d 100644
--- a/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/context_preparation_spec.rb
@@ -529,17 +529,18 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation do
end
it "falls back intact when completion reservation fails softly and drops history on hard overflow" do
+ model.assign_attributes(max_prompt_tokens: 4000, max_output_tokens: 200)
tracker = DiscourseAi::Completions::TokenUsageTracker.new
execution = DiscourseAi::Completions::ExecutionContext.new(token_usage_tracker: tracker)
state =
DiscourseAi::Agents::SubagentExecutionState.new(
execution_context: execution,
root_token_budget: 4000,
+ completion_count: DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 1,
)
- 99.times { state.reserve_completion }
DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do |canned|
llm = DiscourseAi::Completions::Llm.proxy(model)
- soft_prompt = history_prompt(repetitions: 600)
+ soft_prompt = history_prompt(repetitions: 60)
original = soft_prompt.messages.deep_dup
expect(
described_class.new(llm, threshold: 20).prepare!(
@@ -549,10 +550,16 @@ RSpec.describe DiscourseAi::Completions::ContextPreparation do
).to eq(:skipped)
expect(soft_prompt.messages).to eq(original)
expect(soft_prompt.skip_trim).to eq(true)
- hard_prompt = history_prompt(repetitions: 4000)
- expect(
- described_class.new(llm).prepare!(hard_prompt, subagent_execution_state: state),
- ).to eq(:compressed)
+ hard_prompt = history_prompt(repetitions: 400)
+ logger =
+ track_log_messages do
+ expect(
+ described_class.new(llm).prepare!(hard_prompt, subagent_execution_state: state),
+ ).to eq(:compressed)
+ end
+ expect(logger.warnings).to include(
+ "DiscourseAi: context_preparation reason=history_dropped original_reason=completion_limit dropped_history_messages=4",
+ )
expect_history_dropped(hard_prompt)
expect(canned.completions).to eq(0)
expect(state.reserve_root_final_completion).to eq(true)
diff --git a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb b/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
index 3f69994fec6..0cf1c909949 100644
--- a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
@@ -191,7 +191,9 @@ RSpec.describe DiscourseAi::Completions::Llm do
/turn_budget_exhausted/,
)
execution.work_budget = DiscourseAi::Completions::TurnWorkBudget.new(limit: 4000)
- 99.times { state.reserve_completion }
+ (DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 1).times do
+ expect(state.reserve_completion).to eq(true)
+ end
expect { llm.generate("Helper", user: user, execution_context: execution) }.to raise_error(
DiscourseAi::Completions::ContextPreparation::Error,
/completion_limit/,
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb
index ca5a2de3ccc..75c61b3f060 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/stream_reply_custom_tools_session_spec.rb
@@ -613,7 +613,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
end
key = described_class.redis_key(token)
state = JSON.parse(Discourse.redis.get(key))
- state["completion_count"] = 96
+ state["completion_count"] = DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 4
Discourse.redis.set(key, state.to_json)
DiscourseAi::Completions::Llm.with_prepared_responses(
["First summary", "Complete summary", call],
@@ -625,7 +625,9 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
),
)
end
- expect(JSON.parse(Discourse.redis.get(key))["completion_count"]).to eq(99)
+ expect(JSON.parse(Discourse.redis.get(key))["completion_count"]).to eq(
+ DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 1,
+ )
DiscourseAi::Completions::Llm.with_prepared_responses(
["Final answer at the execution limit", "Test title"],
@@ -664,7 +666,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
key = described_class.redis_key(token)
state = JSON.parse(Discourse.redis.get(key))
expect(state["prompt"]["messages"].map { |message| message["type"] }).to eq(%w[system user])
- state["completion_count"] = 98
+ state["completion_count"] = DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 2
Discourse.redis.set(key, state.to_json)
summary = "The completed caller action returned useful evidence."
final = "Final answer using the completed action"
@@ -717,7 +719,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
end
key = described_class.redis_key(token)
state = JSON.parse(Discourse.redis.get(key))
- state["completion_count"] = 98
+ state["completion_count"] = DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 2
Discourse.redis.set(key, state.to_json)
DiscourseAi::Completions::Llm.with_prepared_responses(
["Completed caller evidence summarized."],
@@ -760,7 +762,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
end
key = described_class.redis_key(token)
state = JSON.parse(Discourse.redis.get(key))
- state["completion_count"] = 98
+ state["completion_count"] = DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 2
Discourse.redis.set(key, state.to_json)
topic = Topic.find(state["topic_id"])
result = "Completed evidence " * 2000
@@ -786,7 +788,8 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
}.to raise_error(LlmCreditAllocation::CreditLimitExceeded)
end
failed_state = JSON.parse(Discourse.redis.get(key))
- completion_count = failure_stage == "maintenance" ? 99 : 100
+ completion_count = DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS
+ completion_count -= 1 if failure_stage == "maintenance"
expect(failed_state["completion_count"]).to eq(completion_count)
expect(failed_state["failed_tool_results"]).to eq(
[{ "tool_call_id" => call.id, "content" => result }],
@@ -816,7 +819,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
end
key = described_class.redis_key(token)
state = JSON.parse(Discourse.redis.get(key))
- state["completion_count"] = 99
+ state["completion_count"] = DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS - 1
Discourse.redis.set(key, state.to_json)
result = "Completed evidence " * 6000
DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do |canned|
@@ -847,7 +850,7 @@ RSpec.describe DiscourseAi::AiBot::StreamReplyCustomToolsSession do
end
key = described_class.redis_key(token)
state = JSON.parse(Discourse.redis.get(key))
- state["completion_count"] = 100
+ state["completion_count"] = DiscourseAi::Agents::SubagentExecutionState::MAX_COMPLETIONS
Discourse.redis.set(key, state.to_json)
result = "Already completed"
DiscourseAi::Completions::Llm.with_prepared_responses(["unused"]) do |canned|