The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a process watcher runs in its owning profile's scope throughout
Teknium571a5911ae6a
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.pyindex 5c0f724dfd4..04d07fcad48 100644--- a/gateway/run_notifications.py+++ b/gateway/run_notifications.py@@ -1776,6 +1776,13 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotificareturn future is not Noneasync def _run_process_watcher(self, watcher: dict) -> None:+ """Run :meth:`_watch_process` inside the owning profile's scope, start to finish. A watcher starts+ in an empty context (live-armed from a tool thread, or recovered at startup), and the output it+ prepares runs that profile's own ``transform_terminal_output`` hooks before any delivery."""+ async with self._completion_event_scope(watcher):+ await self._watch_process(watcher)++ async def _watch_process(self, watcher: dict) -> None:"""Poll a background process and push updates until it exits. Mode(``display.background_process_notifications``): concise (default one-liner; failures appendthe output tail) / all (running updates + final raw) / result (final raw) / error (final raw@@ -1788,10 +1795,7 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotificachat_id = watcher.get("chat_id", "")thread_id = watcher.get("thread_id", "")agent_notify = watcher.get("notify_on_complete", False)- # The mode belongs to the profile that started the process; recovered watchers run in the- # root context, so resolve it under the owning profile's scope (no-op for the default).- async with self._completion_event_scope(watcher):- notify_mode = self._load_background_notifications_mode()+ notify_mode = self._load_background_notifications_mode() # the owning profile's (scope bound above)logger.debug("Process watcher started: %s (every %ss, notify=%s, agent_notify=%s)",session_id, interval, notify_mode, agent_notify)silent = notify_mode == "off" and not agent_notify@@ -1803,11 +1807,10 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotificasession = process_registry.get(session_id)if session is None:break- async with self._completion_event_scope(watcher):- surface = should_surface_notification({- "owner_task_id": getattr(session, "owner_task_id", ""),- "task_id": getattr(session, "task_id", ""),- })+ surface = should_surface_notification({+ "owner_task_id": getattr(session, "owner_task_id", ""),+ "task_id": getattr(session, "task_id", ""),+ })if not surface:# Recheck live ownership next tick: handoff retains notification intent.if session.exited:@@ -1883,11 +1886,10 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotifica):message_text = self._format_process_final_message(session_id, session, notify_mode)from gateway.warning_notifications import present_notification- async with self._completion_event_scope(watcher):- # Non-zero exit is the automatic diagnostic; a clean completion is the requested result.- await present_notification(- lambda: self._send_watcher_message(platform_name, chat_id, thread_id, message_text, watcher),- platform=platform_name, diagnostic=session.exit_code not in {0, None})+ # Non-zero exit is the automatic diagnostic; a clean completion is the requested result.+ await present_notification(+ lambda: self._send_watcher_message(platform_name, chat_id, thread_id, message_text, watcher),+ platform=platform_name, diagnostic=session.exit_code not in {0, None})breakelif has_new_output and notify_mode == "all" and not agent_notify:# New output — deliver a status update (only in "all" mode; agent_notify watchersdiff --git a/tests/gateway/test_handoff_completion_parent_context.py b/tests/gateway/test_handoff_completion_parent_context.pyindex 93050d5f6c6..a776db27e54 100644--- a/tests/gateway/test_handoff_completion_parent_context.py+++ b/tests/gateway/test_handoff_completion_parent_context.py@@ -7,9 +7,14 @@ turn ran as a delegated child, and its own ``terminal(background=true, notify_onback ``notify_on_complete: false`` with the "You are a subagent" note (heartbeats unarmed, sudorefused, the Kanban board fenced).-Real daemon (``python -m gateway.run``), local session over the websocket, scripted loopback model:-the parent delegates; the child starts a notify watcher and hands it off; its completion starts a-parent turn that starts a second notify process, whose completion must re-enter the session.+A watcher therefore starts in an empty context and binds the OWNING profile's scope for its whole+run: the output it prepares goes through that profile's ``transform_terminal_output`` hooks, not the+launch profile's.++Real daemon (``python -m gateway.run``) serving two profiles, each with its own output-hook plugin; a+local session on the SECONDARY profile over the websocket, scripted loopback model: the parent+delegates; the child starts a notify watcher and hands it off; its completion starts a parent turn+that starts a second notify process, whose completion must re-enter the session."""import asyncioimport contextlib@@ -24,7 +29,9 @@ from pathlib import Pathimport pytest-from tests.gateway.fixtures.local_recovery_probe import child_env, daemon, rpc, websocket+from websockets.asyncio.client import connect++from tests.gateway.fixtures.local_recovery_probe import child_env, control, daemon, rpcfrom tests.waiting import async_wait_untilCHILD_GOAL = 'CHILD_GOAL start a watcher and hand it to the parent'@@ -119,36 +126,53 @@ def _user_texts(home, sid):"SELECT content FROM messages WHERE session_id=? AND role='user' ORDER BY id", (sid,))]+def _profile_home(target, marker, config):+ """A profile home whose output-hook plugin stamps ``HOOK_<MARKER>`` on every terminal output."""+ plugin = target / 'plugins' / 'output_mark'+ plugin.mkdir(parents=True)+ (plugin / 'plugin.yaml').write_text('name: output_mark\n')+ (plugin / '__init__.py').write_text(+ 'def register(ctx):\n'+ f' ctx.register_hook("transform_terminal_output", lambda **kw: "HOOK_{marker}\\n" + kw["output"])\n')+ (target / 'config.yaml').write_text(json.dumps(config))+ (target / '.env').write_text('OPENROUTER_API_KEY=loopback-only\n')++@pytest.mark.platforms("linux") # the daemon is stopped with SIGINT, which raises on Windowsdef test_handed_off_completion_turn_runs_as_the_parent(tmp_path):root = Path(__file__).resolve().parents[2]- home, user, work = tmp_path / 'state', tmp_path / 'user', tmp_path / 'work'- home.mkdir(mode=0o700)+ # A custom root (``<root>/profiles/<name>``, as in Docker): ``$HOME/.hermes`` is the live system.+ user, work, home = tmp_path / 'user', tmp_path / 'work', tmp_path / 'state'+ secondary = home / 'profiles' / 'second'+ secondary.mkdir(parents=True, mode=0o700)user.mkdir()work.mkdir()peer = ThreadingHTTPServer(('127.0.0.1', 0), ScriptedModel)peer.requests, peer.lock = [], threading.Lock()peer.first_proc, peer.delegated, peer.second_started = '', False, False- peer.release, peer.handoff_error = tmp_path / 'release-first', ''+ peer.release, peer.handoff_error = tmp_path / 'release first', ''thread = threading.Thread(target=peer.serve_forever, daemon=True)thread.start()url = f'http://127.0.0.1:{peer.server_port}/v1'config = {- 'gateway': {'multiplex_profiles': False},+ 'gateway': {'multiplex_profiles': True},'model': {'provider': 'openrouter', 'default': 'openai/gpt-5', 'base_url': url,'api_key': 'loopback-only', 'api_mode': 'chat_completions'},'auxiliary': {'title_generation': {'enabled': False}},'display': {'background_process_notifications': 'off'},+ 'plugins': {'enabled': ['output_mark']},}- (home / 'config.yaml').write_text(json.dumps(config))+ _profile_home(home, 'LAUNCH', config)+ _profile_home(secondary, 'SECOND', config)env = child_env()env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root),+ XDG_CONFIG_HOME=str(user / '.config'), HERMES_GATEWAY_LOCK_DIR=str(tmp_path / 'locks'),OPENROUTER_API_KEY='loopback-only', OPENAI_API_KEY='loopback-only', PYTHONUNBUFFERED='1')out = {}def second_result():assert not peer.handoff_error, ('the handoff was refused', peer.handoff_error)- for content in _tool_results(home, out['sid']):+ for content in _tool_results(secondary, out['sid']):with contextlib.suppress(ValueError, TypeError):data = json.loads(content)if isinstance(data, dict) and data.get('session_id') and data['session_id'] != peer.first_proc:@@ -156,7 +180,10 @@ def test_handed_off_completion_turn_runs_as_the_parent(tmp_path):return Noneasync def probe(desc):- async with websocket(home, desc) as ws:+ grant = control(home, 'session-ticket', {'profile_id': str(secondary), 'instance_id': desc['instance_id'],+ 'purpose': 'interactive'})+ async with connect(desc['api_origin'].replace('http:', 'ws:') + '/api/ws',+ subprotocols=['hermes-gateway-v1', 'hermes-gateway-ticket.' + grant['ticket']]) as ws:created = await rpc(ws, 'session.create', request_id='handoff-parent', source='cli',toolsets=['terminal', 'delegation'], cwd=str(work))out['sid'] = sid = created['result']['session_id']@@ -164,23 +191,28 @@ def test_handed_off_completion_turn_runs_as_the_parent(tmp_path):await async_wait_until(second_result, timeout=90, what='the parent starting a process in the handoff turn')out['second'] = second = second_result()# The second process is the parent's own: its completion re-enters the session.- await async_wait_until(lambda: any(second['session_id'] in t for t in _user_texts(home, sid)),+ await async_wait_until(lambda: any(second['session_id'] in t for t in _user_texts(secondary, sid)),timeout=60, what="the parent's own completion re-entering the session")try:with daemon(root, home, env, barrier=False) as (proc, desc):- asyncio.run(probe(desc))- proc.send_signal(signal.SIGINT)- proc.wait(timeout=30)+ try:+ asyncio.run(probe(desc))+ finally:+ peer.release.touch()+ proc.send_signal(signal.SIGINT)+ proc.wait(timeout=30)finally:peer.shutdown()peer.server_close()thread.join(timeout=5)- users = _user_texts(home, out['sid'])+ users = _user_texts(secondary, out['sid'])assert any(f'Background process {peer.first_proc} completed' in t for t in users), userssecond = out['second']assert second.get('notify_on_complete') is True and 'subagent_note' not in second, second- # Commands the parent runs in that turn are its own, not stamped with the child's Kanban fence.second_notice = next(t for t in users if f"Background process {second['session_id']}" in t)+ # Commands the parent runs in that turn are its own, not stamped with the child's Kanban fence.assert 'SECOND_DONE fence=none' in second_notice, second_notice+ # Its output went through the secondary profile's hooks, not the launch profile's.+ assert 'HOOK_SECOND' in second_notice and 'HOOK_LAUNCH' not in second_notice, second_notice