The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(agent): declare the trim flag in _CONTROL_STATE; trim only on a completed batch
kshitijk4poore74a84f0b646
diff --git a/agent/agent_init.py b/agent/agent_init.pyindex 56e1bb192da..f4acce52529 100644--- a/agent/agent_init.py+++ b/agent/agent_init.py@@ -513,6 +513,7 @@ def _set_defaults(agent, table: Dict[str, Any]) -> None:# Control-flow state (interrupts / steer / redirect / delegation / background review)._CONTROL_STATE: Dict[str, Any] = {"_executing_tools": False, # lets _vprint print while tools run with stream consumers on+ "_trim_after_tool_batch": False, # a >=1 MB tool result was committed; trim once the batch unwinds"_tool_guardrails": ToolCallGuardrailController,"_tool_guardrail_halt_decision": None,# Interrupts. Hard cancellation is separate from redirect/message state; the Event makesdiff --git a/run_agent.py b/run_agent.pyindex 37fafd5befe..3ad32aefb9a 100644--- a/run_agent.py+++ b/run_agent.py@@ -1320,25 +1320,28 @@ class AIAgent(self._executing_tools = True # allow _vprint during tool execution even with stream consumerstry:if len(tool_calls) <= 1:- return self._execute_tool_calls_sequential(*args)-- from agent.tool_dispatch_helpers import _plan_tool_batch_segments- active_env = get_active_env(effective_task_id)- exec_cwd = Path(active_env.cwd) if active_env is not None and active_env.cwd else None- segments = _plan_tool_batch_segments(tool_calls, execution_cwd=exec_cwd)- if len(segments) == 1:- run = self._execute_tool_calls_concurrent if segments[0][0] == "parallel" else self._execute_tool_calls_sequential- return run(*args)- from agent.tool_executor import execute_tool_calls_segmented- return execute_tool_calls_segmented(self, *args, segments=segments)+ self._execute_tool_calls_sequential(*args)+ else:+ from agent.tool_dispatch_helpers import _plan_tool_batch_segments+ active_env = get_active_env(effective_task_id)+ exec_cwd = Path(active_env.cwd) if active_env is not None and active_env.cwd else None+ segments = _plan_tool_batch_segments(tool_calls, execution_cwd=exec_cwd)+ if len(segments) == 1:+ run = self._execute_tool_calls_concurrent if segments[0][0] == "parallel" else self._execute_tool_calls_sequential+ run(*args)+ else:+ from agent.tool_executor import execute_tool_calls_segmented+ execute_tool_calls_segmented(self, *args, segments=segments)finally:self._executing_tools = False- if getattr(self, "_trim_after_tool_batch", False):- # Every executor frame that held a >=1 MB raw result has unwound; only the- # spilled preview lives in ``messages`` now (agent/tool_executor.py, #70684).- self._trim_after_tool_batch = False- from hermes_cli.mem_trim import trim_memory- trim_memory(reason="large tool result")+ if self._trim_after_tool_batch:+ # Only on normal completion: every executor frame that held a >=1 MB raw result has+ # unwound and just the spilled preview lives in ``messages``. An in-flight exception+ # would pin those frames via its traceback, so that path leaves the flag for the+ # next completed batch (agent/tool_executor.py, #70684).+ self._trim_after_tool_batch = False+ from hermes_cli.mem_trim import trim_memory+ trim_memory(reason="large tool result")def _dispatch_delegate_task(self, function_args: dict) -> str:"""Single call site for delegate_task dispatch; new DELEGATE_TASK_SCHEMA fields are added only here."""diff --git a/tests/agent/test_tool_result_memory_trim.py b/tests/agent/test_tool_result_memory_trim.pyindex 22073779cd3..d61e0dd1111 100644--- a/tests/agent/test_tool_result_memory_trim.py+++ b/tests/agent/test_tool_result_memory_trim.py@@ -19,6 +19,7 @@ from tests.agent.test_start_order_gate import ( # noqa: F401 — autouse fixturdef _agent_returning(monkeypatch, payload):agent = _make_agent(monkeypatch)+ agent._trim_after_tool_batch = False # _set_defaults (agent_init._CONTROL_STATE) does this on a real AIAgentagent._tool_guardrails = MagicMock()agent._tool_guardrails.before_call = lambda name, args: MagicMock(allows_execution=True)agent._invoke_tool = MagicMock(return_value=payload)@@ -47,7 +48,7 @@ def test_large_result_flags_the_batch_and_small_does_not(monkeypatch):assert [m["role"] for m in messages] == ["tool"]assert seen == [] # the commit never trims in-frame: the raw result is still referenced hereassert big._trim_after_tool_batch is True- assert getattr(small, "_trim_after_tool_batch", False) is False+ assert small._trim_after_tool_batch is Falsedef test_execute_tool_calls_trims_once_after_every_executor_frame_unwound(monkeypatch):