Files
hermes-agent/tests/tools/test_delegate_timeout_cleanup.py
alt-glitch 43dc9a0a28 refactor(delegation): siblings read config from its owner, not back through the facade
The delegate_tool_* siblings imported names back from tools.delegate_tool
inside functions only so tests could patch `tools.delegate_tool.<name>`
(routes said so: "the facade is the patch seam"). That kept a facade <->
sibling import cycle and a function-level import at each read.

- child_run owns the heartbeat/stale-grace constants (only it reads them)
  and imports _get_child_timeout, _get_subagent_approval_callback,
  _get_worktree_isolation, _resolve_workspace_hint and terminal_tool's
  set_approval_callback at module top.
- config drops `_cfg()` (a loop back to its own `_load_config`) and the
  loop back for `_get_max_concurrent_children`, defined in the same file.
- routes, dispatch and continue import their config getters at module top.
- `_load_config` is read through `delegate_tool_config._load_config` by the
  facade, continue, results and process_registry_notifications, so one
  patch target reaches every reader, as the facade target did before.
- cronjob_tools, process_registry_notifications and subagent_lifecycle read
  these names from their owner; the facade stops importing the names its
  body never used, so a stale facade patch now fails instead of silently
  doing nothing.

Tests move their patches to the module whose code reads the name. Two
worktree tests that patched `_resolve_workspace_hint` on the facade relied
on it reaching both the facade's prompt build and child_run's worktree
setup, so they now patch both readers. Every delegate_tool* module imports
cleanly as the first import in a fresh interpreter.
2026-11-16 21:58:16 +05:30

104 lines
3.5 KiB
Python

"""Regression coverage for timed-out delegation teardown."""
from __future__ import annotations
import threading
from types import SimpleNamespace
from tools import delegate_tool
class _SlowUnwindingChild:
def __init__(self) -> None:
self.tool_progress_callback = None
self._credential_pool = None
self._delegate_saved_tool_names = []
self._delegate_role = "leaf"
self._delegate_depth = 1
self._subagent_id = None
self.model = "test-model"
self.session_prompt_tokens = 0
self.session_completion_tokens = 0
self.session_estimated_cost_usd = 0.0
self.session_cost_status = "unknown"
self.started = threading.Event()
self.interrupted = threading.Event()
self.unwinding = threading.Event()
self.allow_finish = threading.Event()
self.finished = threading.Event()
self.closed = threading.Event()
self.close_while_running = False
def run_conversation(self, **_kwargs):
self.started.set()
# Generous bounds: these gate on events the test sets promptly; a tight bound only
# turns a load-starved test thread into a spurious early exit that fires close().
assert self.interrupted.wait(timeout=30)
# Model the real child turn's finally path: it still performs session
# activity/SQLite cleanup after the parent requests interruption.
self.unwinding.set()
assert self.allow_finish.wait(timeout=30)
self.finished.set()
return {
"final_response": "",
"completed": False,
"interrupted": True,
"api_calls": 1,
"messages": [],
}
def hard_interrupt(self, _reason=None):
self.interrupted.set()
def get_activity_summary(self):
return {"api_call_count": 1}
def close(self):
if not self.finished.is_set():
self.close_while_running = True
self.closed.set()
def test_timeout_does_not_close_child_while_worker_is_unwinding(monkeypatch):
child = _SlowUnwindingChild()
parent = SimpleNamespace(
session_id="parent-timeout-test",
_current_task_id=None,
_active_children=[child],
_active_children_lock=threading.Lock(),
)
monkeypatch.setattr("tools.delegate_tool_child_run._get_child_timeout", lambda: 0.5)
monkeypatch.setattr("tools.delegate_tool_child_run._get_worktree_isolation", lambda: False)
from tools.daemon_pool import DaemonThreadPoolExecutor
submit = DaemonThreadPoolExecutor.submit
def submit_started(executor, *args, **kwargs):
future = submit(executor, *args, **kwargs)
assert child.started.wait(timeout=10)
return future
# Timeout accounting starts only after this test's child is running.
monkeypatch.setattr(DaemonThreadPoolExecutor, "submit", submit_started)
result = delegate_tool._run_single_child(
task_index=0,
goal="exercise timeout teardown",
child=child,
parent_agent=parent,
)
assert result["status"] == "timeout"
assert child.unwinding.wait(timeout=10)
try:
assert not child.closed.is_set(), (
"timed-out child.close() ran before its conversation thread unwound"
)
finally:
child.allow_finish.set()
assert child.finished.wait(timeout=10)
assert child.closed.wait(timeout=10)
assert not child.close_while_running, (
"timed-out child.close() raced its still-running conversation thread"
)