From e64d646bad67c2218723971e408d57321aff89d9 Mon Sep 17 00:00:00 2001 From: teknium1 Date: Sat, 7 Mar 2026 10:32:51 -0800 Subject: [PATCH] Critical: fix bug in new subagent tool call budget to not be session-level but tool call loop level --- run_agent.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/run_agent.py b/run_agent.py index 2fce80a9a..0eee82fbd 100644 --- a/run_agent.py +++ b/run_agent.py @@ -2879,13 +2879,15 @@ class AIAgent: # Generate unique task_id if not provided to isolate VMs between concurrent tasks effective_task_id = task_id or str(uuid.uuid4()) - # Reset retry counters at the start of each conversation to prevent state leakage + # Reset retry counters and iteration budget at the start of each turn + # so subagent usage from a previous turn doesn't eat into the next one. self._invalid_tool_retries = 0 self._invalid_json_retries = 0 self._empty_content_retries = 0 self._last_content_with_tools = None self._turns_since_memory = 0 self._iters_since_skill = 0 + self.iteration_budget = IterationBudget(self.max_iterations) # Initialize conversation (copy to avoid mutating the caller's list) messages = list(conversation_history) if conversation_history else [] @@ -4044,7 +4046,12 @@ class AIAgent: final_response = f"I apologize, but I encountered repeated errors: {error_msg}" break - if api_call_count >= self.max_iterations and final_response is None: + if final_response is None and ( + api_call_count >= self.max_iterations + or self.iteration_budget.remaining <= 0 + ): + if self.iteration_budget.remaining <= 0 and not self.quiet_mode: + print(f"\n⚠️ Session iteration budget exhausted ({self.iteration_budget.used}/{self.iteration_budget.max_total} used, including subagents)") final_response = self._handle_max_iterations(messages, api_call_count) # Determine if conversation completed successfully