The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(delegation): a stale verdict under a configured cap names the stale threshold, not the cap
teknium16b1cf3a68e53
diff --git a/tests/tools/test_delegate_stale_wait_release.py b/tests/tools/test_delegate_stale_wait_release.pyindex 1c94c216e98..cbdf99d178b 100644--- a/tests/tools/test_delegate_stale_wait_release.py+++ b/tests/tools/test_delegate_stale_wait_release.py@@ -60,3 +60,29 @@ def test_stale_heartbeat_ends_the_wait_without_a_configured_timeout(monkeypatch)assert entry["status"] == "timeout", entryassert "stopped making progress" in entry["error"]assert child.interrupted.is_set()+++def test_stale_verdict_under_a_configured_cap_reports_the_stale_threshold_not_the_cap(monkeypatch):+ """The stale verdict pre-empts the cap, so the entry must name the threshold that actually ended+ the wait — not a 3600s cap the child never reached."""+ child = _WedgedAfterFinalAnswer()+ parent = SimpleNamespace(+ session_id="parent", _current_task_id=None, _active_children=[child],+ _active_children_lock=threading.Lock(), _touch_activity=lambda _d: None, _interrupt_requested=False,+ )+ monkeypatch.setattr(delegate_tool, "_HEARTBEAT_INTERVAL", 0.01)+ monkeypatch.setattr(delegate_tool, "_HEARTBEAT_STALE_CYCLES_IDLE", 2)+ monkeypatch.setattr(delegate_tool, "_get_child_timeout", lambda: 3600)+ monkeypatch.setattr(delegate_tool, "_get_worktree_isolation", lambda: False)+ valve = threading.Timer(30.0, child.release.set)+ valve.daemon = True+ valve.start()+ try:+ entry = delegate_tool._run_single_child(0, "review", child=child, parent_agent=parent)+ finally:+ valve.cancel()+ child.release.set()++ assert entry["status"] == "timeout", entry+ assert "stopped making progress" in entry["error"] and "3600" not in entry["error"], entry["error"]+ assert entry["timeout_seconds"] == 2 * 0.01diff --git a/tools/delegate_tool_child_run.py b/tools/delegate_tool_child_run.pyindex 388bfccb3a9..fa0f56cddcb 100644--- a/tools/delegate_tool_child_run.py+++ b/tools/delegate_tool_child_run.py@@ -236,6 +236,9 @@ class _Heartbeat:# Set on the stale verdict; ``await_child`` waits on it (its worker's done-callback# sets it too) so a wedged child ends the wait instead of only ending the heartbeat.self.settled = threading.Event()+ # Threshold (seconds of frozen activity) the stale verdict fired at; None until it does.+ # ``await_child`` reads it to name the real cause when a configured cap was still pending.+ self.stale_threshold_seconds: Optional[float] = Nonedef start(self) -> None:from agent.periodic_scheduler import schedule@@ -249,7 +252,7 @@ class _Heartbeat:def tick(self):"""Returning False stops the periodic callback."""- from tools.delegate_tool import _HEARTBEAT_STALE_CYCLES_IDLE, _HEARTBEAT_STALE_CYCLES_IN_TOOL+ from tools.delegate_tool import _HEARTBEAT_INTERVAL, _HEARTBEAT_STALE_CYCLES_IDLE, _HEARTBEAT_STALE_CYCLES_IN_TOOLchild, parent_agent, task_index, last_seen = self.child, self.parent_agent, self.task_index, self.last_seentouch = getattr(parent_agent, "_touch_activity", None) if parent_agent is not None else Noneif not touch:@@ -272,12 +275,14 @@ class _Heartbeat:last_seen["ts"] = child_activity_tselse:last_seen["stale"] += 1- if last_seen["stale"] >= (_HEARTBEAT_STALE_CYCLES_IN_TOOL if child_tool else _HEARTBEAT_STALE_CYCLES_IDLE):+ stale_cycles = _HEARTBEAT_STALE_CYCLES_IN_TOOL if child_tool else _HEARTBEAT_STALE_CYCLES_IDLE+ if last_seen["stale"] >= stale_cycles:logger.warning("Subagent %d appears stale (no progress for %d heartbeat cycles, tool=%s) — abandoning its wait",task_index, last_seen["stale"], child_tool or "<none>",)# A finite/-Q turn has no gateway watchdog behind this; the wait itself must end (#109749).+ self.stale_threshold_seconds = stale_cycles * _HEARTBEAT_INTERVALself.settled.set()return Falseif child_tool:@@ -754,9 +759,12 @@ class _ChildRun:# inactivity watchdog (#109749).settled = self.heartbeat.settled if self.heartbeat is not None else threading.Event()future.add_done_callback(lambda _f: settled.set())+ # Set when the stale verdict — not the configured cap — ended the wait; the entry must name that cause.+ stale_after: Optional[float] = Nonetry:settled.wait(timeout=child_timeout)if not future.done():+ stale_after = getattr(self.heartbeat, "stale_threshold_seconds", None)raise FuturesTimeoutError()return future.result(), None, Falseexcept Exception as wait_exc:@@ -768,6 +776,8 @@ class _ChildRun:_late_pending_steer = self.close_steering()_signal_child_stop(child)is_timeout = isinstance(exc, (FuturesTimeoutError, TimeoutError))+ # What actually ended the wait: the stale threshold pre-empts a longer configured cap.+ timeout_cause = stale_after if stale_after is not None else child_timeoutduration = self.elapsed()logger.warning("Subagent %d %s after %.1fs", task_index, "timed out" if is_timeout else f"raised {type(exc).__name__}", duration)child_api_calls = 0@@ -779,19 +789,18 @@ class _ChildRun:if before_first_call:diagnostic_path = _dump_subagent_timeout_diagnostic(child=child, task_index=task_index,- # is_timeout implies a cap was configured (result(timeout=None)- # never raises FuturesTimeoutError); guard for the type checker.- timeout_seconds=float(child_timeout or 0.0), duration_seconds=float(duration),+ # A stale verdict or a configured cap; ``or 0.0`` guards the type checker.+ timeout_seconds=float(timeout_cause or 0.0), duration_seconds=float(duration),worker_thread=worker_thread_holder.get("t"), goal=self.goal,)if diagnostic_path:logger.warning("Subagent %d 0-API-call timeout — diagnostic written to %s", task_index, diagnostic_path)if not is_timeout:_err = str(exc)- elif child_timeout is None:+ elif stale_after is not None:_err = (- f"Subagent stopped making progress after {child_api_calls} API call(s) (heartbeat stale "- f"threshold) — the pending worker was abandoned."+ f"Subagent stopped making progress after {child_api_calls} API call(s) — no activity for "+ f"{stale_after:g}s (heartbeat stale threshold); the pending worker was abandoned.")elif before_first_call:_err = (@@ -809,7 +818,7 @@ class _ChildRun:_error_entry = {"task_index": task_index, "status": status, "summary": None, "error": _err, "exit_reason": status,"api_calls": child_api_calls, "duration_seconds": duration,- "timeout_seconds": child_timeout if is_timeout else None,+ "timeout_seconds": timeout_cause if is_timeout else None,"timed_out_after_seconds": duration if is_timeout else None,"timeout_phase": "before_first_llm_call" if before_first_call else "after_llm_calls" if is_timeout else None,"_child_role": getattr(child, "_delegate_role", None),