The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): run a chat /restart outside the requester's profile scope
John Paul Solivacb819d91c322
diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.pyindex 142f8ac9aef..b9f7ea8667c 100644--- a/gateway/run_shutdown.py+++ b/gateway/run_shutdown.py@@ -17,6 +17,7 @@ import sysimport threadingimport timefrom contextlib import contextmanager, nullcontext, suppress+from contextvars import Contextfrom pathlib import Pathfrom typing import Any, Callable, Dict, Optional@@ -1635,7 +1636,9 @@ class GatewayShutdownMixin:# self._restart_task: a bare asyncio.create_task() keeps only a weak reference, so the event loop# may garbage-collect a still-pending task mid-flight. The cancel loop in _stop_impl explicitly# skips _restart_task for the same reason it skips _stop_task.- self._restart_task = asyncio.create_task(_run_restart())+ # Empty Context: /restart is handled inside the requester's profile scope, and a copied context+ # would run the HOST restart as that profile (watcher HERMES_HOME, stop()'s flushes).+ self._restart_task = Context().run(lambda: asyncio.create_task(_run_restart()))return Truedef _start_systemd_watchdog(self) -> bool:diff --git a/tests/gateway/test_restart_drain.py b/tests/gateway/test_restart_drain.pyindex 193ef1a94c7..1c7b40c6115 100644--- a/tests/gateway/test_restart_drain.py+++ b/tests/gateway/test_restart_drain.py@@ -263,6 +263,43 @@ async def test_run_restart_excluded_from_stop_cancel_loop():)+async def test_restart_from_served_profile_chat_restarts_the_host_gateway(monkeypatch):+ """A /restart handled inside a served profile's runtime scope restarts the HOST gateway: the+ detached watcher relaunches `hermes gateway restart` under the launch home (under a named+ profile's home it exits 78 and nothing comes back), and stop() - which flushes pending+ messages under get_hermes_home() - runs outside the requester's profile scope."""+ from agent.secret_scope import current_secret_scope+ from hermes_constants import get_hermes_home++ launch_home = get_hermes_home()+ profile_home = launch_home / "profiles" / "research"+ profile_home.mkdir(parents=True)+ (profile_home / ".env").write_text("RESEARCH_ONLY_TOKEN=x\n", encoding="utf-8")++ runner, _adapter = make_restart_runner()+ seen = {}++ async def _recording_stop(**_kwargs):+ seen["stop_home"] = get_hermes_home()+ seen["stop_secret_scope"] = current_secret_scope()++ runner.stop = _recording_stop+ watcher_envs = []+ monkeypatch.setattr(gateway_run, "_resolve_hermes_bin", lambda: ["hermes"])+ monkeypatch.setattr(+ subprocess, "Popen", lambda _argv, **kwargs: watcher_envs.append(kwargs["env"]) or MagicMock()+ )++ async with gateway_run._async_profile_runtime_scope(profile_home):+ assert get_hermes_home() == profile_home+ assert runner.request_restart(detached=True, via_service=False) is True+ await runner._restart_task++ assert [env.get("HERMES_HOME") for env in watcher_envs] == [str(launch_home)]+ assert seen == {"stop_home": launch_home, "stop_secret_scope": None}++@pytest.mark.windows_only@pytest.mark.asyncioasync def test_windows_detached_restart_scrubs_gateway_marker(monkeypatch, tmp_path):