The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): stop the tool-activity heartbeat when the executor abandons its worker
teknium1ba135b366b06
diff --git a/agent/tool_executor.py b/agent/tool_executor.pyindex dde39df095b..4ae7b023879 100644--- a/agent/tool_executor.py+++ b/agent/tool_executor.py@@ -578,12 +578,22 @@ def _run_tool_activity_heartbeat(stop_event: threading.Event,label: str,interval: float = _TOOL_ACTIVITY_HEARTBEAT_INTERVAL_S,+ worker_tid: int | None = None,) -> None:"""Daemon thread stamping ``agent._touch_activity`` every ``interval`` seconds until``stop_event`` is set, so the gateway inactivity watchdog never abandons a turn whose- tool runs silently. Wedged tools stay bounded by the tool layer's own timeouts."""+ tool runs silently. Wedged tools stay bounded by the tool layer's own timeouts and by the+ executor deadline — but a worker the executor gave up on never reaches its ``stop_event``,+ so the heartbeat also exits once ``worker_tid`` carries the interrupt bit the abandoning+ executor raises (``_interrupt_worker_tids``). Otherwise a tool wedged in a kernel probe+ keeps reporting "activity" for the rest of the run and the inactivity watchdog, the second+ line of defense, can never fire (#111922)."""+ from tools.interrupt import is_thread_interrupted+try:while not stop_event.wait(interval):+ if is_thread_interrupted(worker_tid):+ returnagent._touch_activity(label)except Exception:pass # a heartbeat must never break the agent loop@@ -599,7 +609,7 @@ def _run_with_activity_heartbeat(agent, function_name: str, fn):# here, so a single heartbeat covers every tool.target=_run_tool_activity_heartbeat,args=(agent, stop, f"tool running: {function_name}"),- kwargs={"interval": _TOOL_ACTIVITY_HEARTBEAT_INTERVAL_S},+ kwargs={"interval": _TOOL_ACTIVITY_HEARTBEAT_INTERVAL_S, "worker_tid": threading.current_thread().ident},daemon=True,name=f"tool-activity-hb-{function_name[:24]}",)diff --git a/tests/agent/test_tool_activity_heartbeat.py b/tests/agent/test_tool_activity_heartbeat.pyindex 6a93f4a0516..bdd85e401e5 100644--- a/tests/agent/test_tool_activity_heartbeat.py+++ b/tests/agent/test_tool_activity_heartbeat.py@@ -268,3 +268,92 @@ def test_concurrent_tool_call_heartbeat(monkeypatch):agent._execute_tool_calls_concurrent(msg, messages, "task")assert len(touches) >= 3, f"expected mid-call heartbeats, got {len(touches)}"+++def test_heartbeat_exits_once_worker_tid_is_interrupted():+ """The heartbeat must not outlive the worker it speaks for (#111922).++ The executor abandons a wedged worker by raising its interrupt bit+ (``_interrupt_worker_tids``); that worker never reaches ``stop_event.set()``,+ so the heartbeat has to stop on the bit itself, or the abandoned tool keeps+ the inactivity watchdog pinned for the rest of the run.+ """+ import agent.tool_executor as te+ from tools.interrupt import set_interrupt++ touches: list = []+ stop = threading.Event()+ fake_worker_tid = 10**9 + 111922 # not a live thread; only the bit matters++ class _Agent:+ def _touch_activity(self, desc):+ touches.append(desc)++ thread = threading.Thread(+ target=te._run_tool_activity_heartbeat,+ args=(_Agent(), stop, "tool running: terminal"),+ kwargs={"interval": 0.05, "worker_tid": fake_worker_tid},+ daemon=True,+ )+ thread.start()+ try:+ time.sleep(0.12)+ assert touches, "heartbeat never stamped while the worker was live"+ set_interrupt(True, fake_worker_tid)+ thread.join(timeout=1.0)+ assert not thread.is_alive(), "heartbeat kept running after its worker was abandoned"+ n = len(touches)+ time.sleep(0.12)+ assert len(touches) == n+ finally:+ set_interrupt(False, fake_worker_tid)+ stop.set()+++def test_sequential_timeout_stops_abandoned_workers_heartbeat(monkeypatch):+ """After the sequential deadline abandons a non-cooperative tool, no more+ ``tool running:`` stamps arrive — the timed-out tool no longer fakes liveness (#111922)."""+ import agent.tool_executor as te++ monkeypatch.setattr(te, "_TOOL_ACTIVITY_HEARTBEAT_INTERVAL_S", 0.05)+ monkeypatch.setattr(te, "_SEQUENTIAL_INTERRUPT_POLL_SECONDS", 0.05)+ monkeypatch.setattr(te, "_resolve_sequential_tool_timeout", lambda: 0.3)+ monkeypatch.setattr(te, "_emit_terminal_post_tool_call", lambda agent, **kw: None)+ # The middleware's execution seam: ``_run_with_activity_heartbeat`` around ``execute`` on the worker.+ monkeypatch.setattr(+ te, "_run_agent_tool_execution_middleware",+ lambda agent, execute, function_name="terminal", **kw: te._ManagedToolResult(+ result=te._run_with_activity_heartbeat(agent, function_name, lambda: execute({})),+ args={}, middleware_trace=[], blocked=False, dispatched=True,+ ),+ )++ class _Agent:+ _interrupt_requested = False+ _tool_interrupt_reason = None++ def __init__(self):+ self._tool_worker_threads = set()+ self._tool_worker_threads_lock = threading.Lock()+ self.stamps: list = []++ def _touch_activity(self, desc):+ self.stamps.append((time.monotonic(), desc))++ def interrupt(self, *a, **k):+ pass++ agent = _Agent()+ release = threading.Event()+ try:+ managed = te._run_sequential_tool_execution_middleware(+ agent, function_name="terminal", function_args={}, effective_task_id="t",+ tool_call_id="tc1", execute=lambda args: release.wait() or "late",+ )+ abandoned_at = time.monotonic()+ assert isinstance(managed.result, te._ToolTimeoutResult)+ time.sleep(0.3) # several heartbeat intervals past abandonment+ late = [d for ts, d in agent.stamps if ts > abandoned_at + 0.06 and d.startswith("tool running:")]+ assert not late, f"abandoned tool kept stamping activity: {late[:3]}"+ finally:+ release.set()