Skip to content

The agent that grows with you

fix(env): a tool child keeps its gateway's profile pin without the supervisor claim

Teknium98a953afa9be
diff --git a/hermes_cli/dashboard_procs.py b/hermes_cli/dashboard_procs.py
index 8eec8c31a64..27d4b8cd3f7 100644
--- a/hermes_cli/dashboard_procs.py
+++ b/hermes_cli/dashboard_procs.py
@@ -213,7 +213,8 @@ def _hermes_home_for_pid(pid: int, *, require_launch_identity: bool = False) ->
default_home = Path(env.get("HOME") or _pid_passwd_home(pid) or Path.home()) / ".hermes"
root = profile_root_for_env_home(env_home, default_home)
fixed_identity = any(env.get(k) for k in ("HERMES_SUPERVISED_CHILD", "HERMES_S6_SUPERVISED_CHILD",
- "HERMES_GATEWAY_EXTERNAL_SUPERVISOR")) or is_desktop_ssh_backend_argv(argv)
+ "HERMES_GATEWAY_EXTERNAL_SUPERVISOR",
+ "HERMES_GATEWAY_PROFILE_PIN")) or is_desktop_ssh_backend_argv(argv)
if profile is None and not fixed_identity:
if require_launch_identity:
return None # The sticky profile at launch is unknowable; never read today's value.
diff --git a/hermes_cli/gateway_runtime_start.py b/hermes_cli/gateway_runtime_start.py
index f5ebfb49908..ccb1ff0ad68 100644
--- a/hermes_cli/gateway_runtime_start.py
+++ b/hermes_cli/gateway_runtime_start.py
@@ -75,6 +75,8 @@ def spawn_unmanaged_gateway(profile_home: Path, *, deadline: float, idle_exit: b
# name. Launch-only approval/context/config flags ride the launching session's frozen
# policy, not the shared daemon or every later session.
from gateway.session_context import _VAR_MAP # the launching turn's session identity, too
+ from tools.environments.local_env_policy import carry_gateway_profile_pin
+ carry_gateway_profile_pin(env) # the supervisor markers go; the daemon's tools keep their pin
for key in (*DAEMON_STRIPPED_ENV, *_VAR_MAP):
env.pop(key, None)
remaining(deadline)
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index a14fdd81fb9..3efa91f0e68 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -549,17 +549,17 @@ def _under_gateway_supervisor(argv: list) -> bool:
service children only — consulted ONLY for gateway commands because it is
inherited by every descendant of a systemd-launched process, e.g.
self-hosted CI runners), HERMES_GATEWAY_EXTERNAL_SUPERVISOR (explicit
- opt-in). XPC_SERVICE_NAME is deliberately NOT consulted: interactive macOS
- terminals set it too.
+ opt-in) and HERMES_GATEWAY_PROFILE_PIN (its tool children's copy, which is
+ no supervisor claim). XPC_SERVICE_NAME is deliberately NOT consulted:
+ interactive macOS terminals set it too.
"""
if os.environ.get("HERMES_SUPERVISED_CHILD") or os.environ.get("HERMES_S6_SUPERVISED_CHILD"):
return True
is_gateway_cmd = next((a for a in argv if not a.startswith("-")), None) == "gateway"
if is_gateway_cmd and os.environ.get("INVOCATION_ID"):
return True
- return os.environ.get(
- "HERMES_GATEWAY_EXTERNAL_SUPERVISOR", ""
- ).strip().lower() in {"1", "true", "yes", "on"}
+ return any(os.environ.get(name, "").strip().lower() in {"1", "true", "yes", "on"}
+ for name in ("HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "HERMES_GATEWAY_PROFILE_PIN"))
def _s6_supervised_gateway_run(argv: list) -> bool:
diff --git a/tests/hermes_cli/test_daemon_launch_env_strip.py b/tests/hermes_cli/test_daemon_launch_env_strip.py
index 75b3863135a..9494129e721 100644
--- a/tests/hermes_cli/test_daemon_launch_env_strip.py
+++ b/tests/hermes_cli/test_daemon_launch_env_strip.py
@@ -17,6 +17,7 @@ from tests.waiting import wait_until
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'}
+PIN = 'HERMES_GATEWAY_PROFILE_PIN'
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"}',
@@ -40,7 +41,7 @@ def test_daemon_does_not_inherit_hook_consent_or_worker_pins(tmp_path, monkeypat
executable.write_text(
'#!' + sys.executable + '\nimport json, os\n'
+ f'open({str(witness)!r}, "w").write(json.dumps('
- + '{k: os.environ.get(k) for k in ' + repr([*LEAKS, 'HERMES_INFERENCE_MODEL']) + '}))\n',
+ + '{k: os.environ.get(k) for k in ' + repr([*LEAKS, 'HERMES_INFERENCE_MODEL', PIN]) + '}))\n',
encoding='utf-8')
executable.chmod(0o700)
monkeypatch.setattr(sys, 'executable', str(executable))
@@ -54,6 +55,8 @@ def test_daemon_does_not_inherit_hook_consent_or_worker_pins(tmp_path, monkeypat
child.kill()
child.wait(timeout=5)
seen = json.loads(Path(witness).read_text())
+ # The supervisor markers go, but the daemon's own tool children stay on its profile (#74872).
+ assert seen.pop(PIN) == '1'
assert seen == {**dict.fromkeys(LEAKS), 'HERMES_INFERENCE_MODEL': 'fallback'}
assert os.environ['HERMES_ACCEPT_HOOKS'] == '1' # the launching process keeps its own policy
diff --git a/tests/tools/test_child_env_supervisor_identity.py b/tests/tools/test_child_env_supervisor_identity.py
index ca8ae205fc6..41f8e6477d0 100644
--- a/tests/tools/test_child_env_supervisor_identity.py
+++ b/tests/tools/test_child_env_supervisor_identity.py
@@ -8,6 +8,8 @@ notify socket and watchdog likewise belong to the unit's main process only.
"""
import os
+import sys
+from unittest.mock import patch
import pytest
@@ -63,3 +65,27 @@ def test_agent_terminal_child_sees_no_supervisor_identity(tmp_path, monkeypatch)
assert {k: seen[k] for k in OWNER_ONLY} == dict.fromkeys(OWNER_ONLY)
assert _reads_as_unsupervised({k: v for k, v in seen.items() if v is not None})
+
+
[email protected]("child_env")
+def test_external_supervisor_gateways_tool_child_cli_stays_on_the_gateways_profile(tmp_path, monkeypatch):
+ """Losing the supervisor claim must not unpin the agent's own `hermes ...` commands (#74872)."""
+ from hermes_cli import main as cli_main
+
+ root = tmp_path / ".hermes"
+ (root / "profiles" / "work").mkdir(parents=True)
+ (root / "profiles" / "work" / "config.yaml").write_text("{}\n")
+ (root / "active_profile").write_text("work\n")
+ monkeypatch.setattr(sys, "argv", ["hermes", "cron", "list"])
+
+ def child_cli_home(parent_extra):
+ child = local.build_subprocess_env({**os.environ, "HERMES_HOME": str(root), **parent_extra})
+ with patch.dict(os.environ, child, clear=True):
+ cli_main._apply_profile_override()
+ return child, os.environ["HERMES_HOME"]
+
+ # Control: an unpinned root-home CLI follows the sticky profile.
+ assert child_cli_home({})[1] == str(root / "profiles" / "work")
+ child, home = child_cli_home({"HERMES_GATEWAY_EXTERNAL_SUPERVISOR": "1"})
+ assert home == str(root)
+ assert _reads_as_unsupervised(child)
diff --git a/tools/environments/local_env_policy.py b/tools/environments/local_env_policy.py
index a703dc6ea15..4c7a4a84e77 100644
--- a/tools/environments/local_env_policy.py
+++ b/tools/environments/local_env_policy.py
@@ -289,10 +289,26 @@ _ACTIVE_VENV_MARKER_VARS = ("VIRTUAL_ENV", "CONDA_PREFIX", "PYTHONHOME")
_SUPERVISED_PROCESS_IDENTITY_VARS = (
"HERMES_LAUNCHD_LABEL", "HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "NOTIFY_SOCKET", "WATCHDOG_USEC", "WATCHDOG_PID",
)
+# The supervisor markers are also profile pins: a CLI under them skips the sticky active_profile
+# (``hermes_cli.main._under_gateway_supervisor``, #74872), and HERMES_GATEWAY_EXTERNAL_SUPERVISOR is an
+# --external-supervisor gateway's ONLY one. A child that loses a marker keeps the pin under a name the
+# restart-route and launch-identity probes never read, so it stays on the gateway's profile without
+# claiming the supervisor.
+GATEWAY_PROFILE_PIN_ENV = "HERMES_GATEWAY_PROFILE_PIN"
+
+
+def carry_gateway_profile_pin(env: dict) -> dict:
+ """Mark *env* with :data:`GATEWAY_PROFILE_PIN_ENV` while it still holds a pinning supervisor marker."""
+ if env.get("HERMES_SUPERVISED_CHILD") or str(
+ env.get("HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "")).strip().lower() in {"1", "true", "yes", "on"}:
+ env[GATEWAY_PROFILE_PIN_ENV] = "1"
+ return env
def strip_supervised_process_identity(env: dict) -> dict:
- """Drop :data:`_SUPERVISED_PROCESS_IDENTITY_VARS` from a child env, in place; returns *env*."""
+ """Drop :data:`_SUPERVISED_PROCESS_IDENTITY_VARS` from a child env, in place; returns *env*.
+ The profile pin they carried survives (:func:`carry_gateway_profile_pin`)."""
+ carry_gateway_profile_pin(env)
for name in _SUPERVISED_PROCESS_IDENTITY_VARS:
env.pop(name, None)
return env