The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(env): a supervisor's identity stays on the process it started
Teknium37351b6a6f0d
diff --git a/tests/tools/test_child_env_supervisor_identity.py b/tests/tools/test_child_env_supervisor_identity.pynew file mode 100644index 00000000000..ca8ae205fc6--- /dev/null+++ b/tests/tools/test_child_env_supervisor_identity.py@@ -0,0 +1,65 @@+"""A supervisor's identity describes only the process it started, never that process's children.++The launchd wrapper forwards ``HERMES_LAUNCHD_LABEL`` to the gateway it starts (#120087), and+``gateway run --external-supervisor`` stamps ``HERMES_GATEWAY_EXTERNAL_SUPERVISOR`` into its own+environ. A gateway an agent starts from its terminal tool is owned by no supervisor, so it must not+answer ``launchd``/supervised and take the exit-75 restart route nobody would answer. systemd's+notify socket and watchdog likewise belong to the unit's main process only.+"""++import os++import pytest++from gateway.restart import is_gateway_supervisor_process, launchd_job_label+from tests.tools._child_env_fixtures import child_env, observe_terminal+from tools.environments import local++SUPERVISED_GATEWAY_ENV = {+ "HERMES_LAUNCHD_LABEL": "ai.hermes.gateway",+ "XPC_SERVICE_NAME": "0",+ "HERMES_GATEWAY_EXTERNAL_SUPERVISOR": "1",+ "NOTIFY_SOCKET": "/run/user/1000/systemd/notify",+ "WATCHDOG_USEC": "30000000",+ "WATCHDOG_PID": "4242",+}+OWNER_ONLY = ("HERMES_LAUNCHD_LABEL", "HERMES_GATEWAY_EXTERNAL_SUPERVISOR",+ "NOTIFY_SOCKET", "WATCHDOG_USEC", "WATCHDOG_PID")+++def _reads_as_unsupervised(env):+ return not is_gateway_supervisor_process(env) and launchd_job_label(env) is None++[email protected]("build", [+ lambda base: local.build_subprocess_env(base),+ lambda base: local.build_subprocess_env(base, scrub_secrets=False),+ lambda base: local.hermes_subprocess_env(base_env=base),+], ids=["scrubbed", "unscrubbed", "hermes_subprocess_env"])[email protected]("child_env")+def test_child_env_factories_drop_the_supervised_gateways_identity(build):+ parent = {**os.environ, **SUPERVISED_GATEWAY_ENV}+ # Control: the gateway itself still resolves its launchd job and the restart route.+ assert launchd_job_label(parent) == "ai.hermes.gateway" and is_gateway_supervisor_process(parent)++ child = build(parent)++ assert _reads_as_unsupervised(child)+ assert not set(OWNER_ONLY) & set(child)+ # XPC_SERVICE_NAME is launchd's own per-process stamp ("0" below the wrapper); `hermes update`+ # compares it to decide whether it runs inside a job, so it passes through untouched.+ assert child["XPC_SERVICE_NAME"] == "0"++[email protected]("child_env")+def test_agent_terminal_child_sees_no_supervisor_identity(tmp_path, monkeypatch):+ for key, value in SUPERVISED_GATEWAY_ENV.items():+ monkeypatch.setenv(key, value)+ env = local.LocalEnvironment(cwd=str(tmp_path), timeout=30)+ try:+ seen = observe_terminal(env, [*OWNER_ONLY, "XPC_SERVICE_NAME"])+ finally:+ env.cleanup()++ 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})diff --git a/tools/environments/local.py b/tools/environments/local.pyindex 8e2e93df5ed..9853ebfec64 100644--- a/tools/environments/local.py+++ b/tools/environments/local.py@@ -25,7 +25,7 @@ from tools.environments.local_env_policy import (_is_hermes_internal_secret, _is_provider_env_blocklisted, _is_terminal_first_party_env,_home_adapter_secret_env, _matches_terminal_first_party_prefix, _plugin_terminal_env_strip_keys,_registered_adapter_secret_env, _registry_adapter_secret_env,- strip_profile_gate_env)+ strip_profile_gate_env, strip_supervised_process_identity)from tools.environments.local_pythonpath import (_build_hermes_repo_root_aliases, _strip_hermes_owned_pythonpath_and_runtime_markers)@@ -300,6 +300,7 @@ def _finalize_child_env(env: dict) -> dict:_apply_profile_home(env)_inject_session_context_env(env)_strip_hermes_owned_pythonpath_and_runtime_markers(env)+ strip_supervised_process_identity(env)_apply_windows_msys_bash_env_defaults(env)from agent.delegation_context import delegated_child_subprocess_envreturn delegated_child_subprocess_env(env)@@ -381,9 +382,10 @@ def build_subprocess_env(strip_launch_profile: bool = False) -> dict[str, str]:"""Single factory for child-process envs. ``base=None`` snapshots ``os.environ``.``scrub_secrets=True`` -> :func:`_sanitize_subprocess_env` (profile home inherent,- ``inherit_profile_home`` ignored). ``scrub_secrets=False`` keeps the base- byte-for-byte (git credential flows, ``bws``/``op``); ``inherit_profile_home``- bridges HERMES_HOME + HOME and ``extra`` is applied last so caller overrides win.+ ``inherit_profile_home`` ignored). ``scrub_secrets=False`` keeps the base's secrets+ (git credential flows, ``bws``/``op``); ``inherit_profile_home``+ bridges HERMES_HOME + HOME and ``extra`` is applied last so caller overrides win. Both modes+ drop the supervised process's own identity (:func:`strip_supervised_process_identity`).``strip_launch_profile`` drops the LAUNCH profile's ``.env`` residue from the base first(:func:`strip_launch_profile_env`; a no-op unless a routed home is active) so a child thatacts for a routed profile sees only that profile's declared names, never the launch profile's.@@ -407,6 +409,7 @@ def build_subprocess_env(_apply_profile_home(env)if extra:env.update(extra)+ strip_supervised_process_identity(env)from agent.delegation_context import delegated_child_subprocess_envreturn delegated_child_subprocess_env(env)diff --git a/tools/environments/local_env_policy.py b/tools/environments/local_env_policy.pyindex a6827380676..a703dc6ea15 100644--- a/tools/environments/local_env_policy.py+++ b/tools/environments/local_env_policy.py@@ -276,6 +276,27 @@ def _is_terminal_first_party_env(name: str) -> bool:# _strip_hermes_owned_pythonpath() which removes only Hermes-owned entries, preserving user-set paths._ACTIVE_VENV_MARKER_VARS = ("VIRTUAL_ENV", "CONDA_PREFIX", "PYTHONHOME")+# Identity a supervisor hands to the ONE process it started, which no child of that process owns.+# HERMES_LAUNCHD_LABEL is the stderr-timestamp wrapper's re-export of its launchd job label for the+# gateway it starts (#120087); HERMES_GATEWAY_EXTERNAL_SUPERVISOR is stamped into the gateway's own+# environ by ``gateway run --external-supervisor``. Inherited, a gateway an agent starts from its+# terminal answers ``launchd``/supervised and takes the exit-75 restart route no supervisor answers.+# NOTIFY_SOCKET/WATCHDOG_* name the systemd unit's main process (sd_notify's unset_environment).+# Kept on purpose: XPC_SERVICE_NAME (launchd's own per-process stamp, "0" below the wrapper, which+# ``hermes update`` compares to tell it runs inside a job), INVOCATION_ID/JOURNAL_STREAM (systemd+# sets them for every process of the unit, and the gateway-child dispatch reads them there), and the+# launch markers HERMES_SUPERVISED_CHILD / HERMES_S6_SUPERVISED_CHILD (profile pinning, s6 redirect).+_SUPERVISED_PROCESS_IDENTITY_VARS = (+ "HERMES_LAUNCHD_LABEL", "HERMES_GATEWAY_EXTERNAL_SUPERVISOR", "NOTIFY_SOCKET", "WATCHDOG_USEC", "WATCHDOG_PID",+)+++def strip_supervised_process_identity(env: dict) -> dict:+ """Drop :data:`_SUPERVISED_PROCESS_IDENTITY_VARS` from a child env, in place; returns *env*."""+ for name in _SUPERVISED_PROCESS_IDENTITY_VARS:+ env.pop(name, None)+ return env+def _is_hermes_internal_secret(key: str) -> bool:"""True for Hermes-internal secrets injected under *dynamic* names the static