Skip to content

The agent that grows with you

fix(gateway): keep a launchd gateway's markers out of the unmanaged daemon

Sidney VanNessda844cc5475c
diff --git a/hermes_cli/gateway_runtime_start.py b/hermes_cli/gateway_runtime_start.py
index 091f619d9f2..3df3113815f 100644
--- a/hermes_cli/gateway_runtime_start.py
+++ b/hermes_cli/gateway_runtime_start.py
@@ -30,6 +30,11 @@ DAEMON_STRIPPED_ENV = (
# A terminal's shell under a systemd service (gnome-terminal-server, a user unit) carries these; the
# unmanaged daemon would then identify as "systemd" and maintenance would refuse it as supervised.
"INVOCATION_ID", "JOURNAL_STREAM",
+ # Same for any process below a launchd/--external-supervisor gateway (its agents' tools, scripts they
+ # run): the daemon would identify as "launchd", never arm --idle-exit, and outlive every client.
+ # HERMES_S6_SUPERVISED_CHILD stays: without it `gateway run` in an s6 container redirects to s6.
+ "XPC_SERVICE_NAME", "LAUNCHD_SOCKET", "HERMES_LAUNCHD_LABEL",
+ "HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "HERMES_SUPERVISED_CHILD",
)
diff --git a/tests/hermes_cli/test_daemon_launch_env_strip.py b/tests/hermes_cli/test_daemon_launch_env_strip.py
index 59bdfd7510e..034850b8035 100644
--- a/tests/hermes_cli/test_daemon_launch_env_strip.py
+++ b/tests/hermes_cli/test_daemon_launch_env_strip.py
@@ -9,8 +9,12 @@ import pytest
# Every launch-scoped value a client process may hold when it auto-starts the shared daemon
# (SWEEP_daemon-launch-env.md): consent, Kanban worker pins, the launching turn's session identity,
-# human-presence and one-shot markers, a bot-to-bot turn author, a terminal's systemd markers.
-LEAKS = {'HERMES_ACCEPT_HOOKS': '1', 'HERMES_KANBAN_TASK': 't_1', 'HERMES_KANBAN_BOARD': 'other',
+# human-presence and one-shot markers, a bot-to-bot turn author, a terminal's systemd markers, and the
+# launchd/external-supervisor markers every process below a service-managed gateway inherits.
+SUPERVISOR_LEAKS = {'XPC_SERVICE_NAME': 'ai.hermes.gateway', 'LAUNCHD_SOCKET': '/tmp/launchd-x/Listeners',
+ 'HERMES_LAUNCHD_LABEL': 'ai.hermes.gateway', 'HERMES_GATEWAY_EXTERNAL_SUPERVISOR': '1',
+ 'HERMES_SUPERVISED_CHILD': '1'}
+LEAKS = {**SUPERVISOR_LEAKS, 'HERMES_ACCEPT_HOOKS': '1', 'HERMES_KANBAN_TASK': 't_1', 'HERMES_KANBAN_BOARD': 'other',
'HERMES_KANBAN_DB': '/x/kanban.db', 'HERMES_SESSION_SOURCE': 'kanban', 'TERMINAL_CWD': '/x',
'HERMES_TENANT': 'acme', 'HERMES_SESSION_SOURCE_EXPLICIT': '1', 'HERMES_TURN_AUTHOR': '{"handle":"x"}',
'HERMES_INTERACTIVE': '1', 'HERMES_GATEWAY_SESSION': '1', 'HERMES_SINGLE_QUERY_SESSION': '1',
@@ -47,3 +51,38 @@ def test_daemon_does_not_inherit_hook_consent_or_worker_pins(tmp_path, monkeypat
seen = json.loads(Path(witness).read_text())
assert seen == {**dict.fromkeys(LEAKS), 'HERMES_INFERENCE_MODEL': 'fallback'}
assert os.environ['HERMES_ACCEPT_HOOKS'] == '1' # the launching process keeps its own policy
+
+
[email protected]_gateway_lookalike # stub interpreter records its supervisor then exits; reaped below
+def test_daemon_started_below_a_launchd_gateway_is_unsupervised(tmp_path, monkeypatch):
+ # An agent of a launchd gateway auto-starts a daemon for another home: that daemon has no supervisor,
+ # so it must declare "manual" (which arms --idle-exit) instead of claiming its launcher's launchd job.
+ from hermes_cli import gateway_runtime_start as start
+ home = tmp_path / 'home'
+ home.mkdir(mode=0o700)
+ witness = home / 'supervisor.json'
+ repo = Path(__file__).resolve().parents[2]
+ executable = tmp_path / 'owned-interpreter'
+ executable.write_text(
+ '#!' + sys.executable + '\nimport json, sys\n'
+ + f'sys.path.insert(0, {str(repo)!r})\n'
+ + 'from gateway.control_socket import _detect_supervisor\n'
+ + 'from gateway.restart import is_supervised_gateway_launch\n'
+ + f'open({str(witness)!r}, "w").write(json.dumps('
+ + '[_detect_supervisor(), is_supervised_gateway_launch()]))\n',
+ encoding='utf-8')
+ executable.chmod(0o700)
+ monkeypatch.setattr(sys, 'executable', str(executable))
+ for key, value in SUPERVISOR_LEAKS.items():
+ monkeypatch.setenv(key, value)
+ for key in ('INVOCATION_ID', 'HERMES_DESKTOP_MANAGED', 'HERMES_S6_SUPERVISED_CHILD'):
+ monkeypatch.delenv(key, raising=False)
+ child = start.spawn_unmanaged_gateway(home, deadline=time.monotonic() + 5, idle_exit=True)
+ try:
+ assert child.wait(timeout=20) == 0
+ finally:
+ if child.poll() is None:
+ child.kill()
+ child.wait(timeout=5)
+ assert json.loads(Path(witness).read_text()) == ['manual', False]