Files
hermes-agent/tests/hermes_cli/test_cli_background_delegation.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

52 lines
2.1 KiB
Python

"""Classic-CLI /bg: a background delegate_task joins inside the bg_* run (#140569).
The /bg answer is printed once and nothing drains completions for its bg_* session, so a detached
child's result would sit in the ledger forever.
"""
import json
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import cli as cli_module
from tests.hermes_cli.test_cli_approval_ui import _make_background_cli_stub
def test_background_task_delegation_runs_inline(monkeypatch):
import tools.delegate_tool as dt
monkeypatch.setattr(dt, "_build_child_agent", lambda **kw: SimpleNamespace(_subagent_id="child"))
monkeypatch.setattr("tools.delegate_tool_routes._resolve_delegation_credentials", lambda *a, **kw: {
"model": "test", "provider": None, "base_url": None, "api_key": None,
"api_mode": None, "command": None, "args": None,
})
monkeypatch.setattr(dt, "_run_single_child", lambda **kw: {
"task_index": 0, "status": "completed", "summary": "child work product",
"api_calls": 1, "duration_seconds": 0, "model": "test", "exit_reason": "completed",
})
detached = MagicMock(return_value={"delegation_id": "detached"})
monkeypatch.setattr("tools.async_delegation.dispatch_async_delegation_batch", detached)
seen = {}
class FakeAgent:
def __init__(self, **kwargs):
self._print_fn = None
self.thinking_callback = None
def run_conversation(self, **kwargs):
parent = SimpleNamespace(_delegate_depth=0, _subagent_id=None)
seen["tool"] = dt.delegate_task(goal="inspect workspace", background=True, parent_agent=parent)
return {"final_response": "done", "messages": []}
def close(self):
pass
cli = _make_background_cli_stub()
with patch("run_agent.AIAgent", FakeAgent), patch.object(cli_module, "_cprint"), \
patch.object(cli_module, "ChatConsole"):
cli._handle_background_command("/bg inspect workspace")
for thread in list(cli._background_tasks.values()):
thread.join(timeout=10)
detached.assert_not_called()
assert "child work product" in json.dumps(seen["tool"])