Skip to content

The agent that grows with you

fix(gateway): scrub launch-profile env residue before spawning a secondary managed worker (R3, reported by @rahlquist)

teknium124df00b6d5d5
diff --git a/gateway/session_managed_worker.py b/gateway/session_managed_worker.py
index 076c6357cc1..3bb4e6b829c 100644
--- a/gateway/session_managed_worker.py
+++ b/gateway/session_managed_worker.py
@@ -183,8 +183,10 @@ def _worker_env(authority):
if not is_multiplex_active() or not home.is_absolute():
return None
from agent.secret_scope import build_profile_secret_scope
- from tools.environments.local import build_subprocess_env
- env = build_subprocess_env(scrub_secrets=True)
+ from tools.environments.local import build_subprocess_env, strip_launch_profile_env
+ # The scrub removes credentials, not settings: the launch profile's TERMINAL_* policy and
+ # its ``.env`` settings would otherwise reach the secondary's worker (cron/kanban rule).
+ env = strip_launch_profile_env(build_subprocess_env(scrub_secrets=True), home)
env.update({k: v for k, v in build_profile_secret_scope(home).items() if v is not None})
env['HERMES_HOME'] = str(home)
from hermes_constants import apply_subprocess_home_env
diff --git a/tests/gateway/test_managed_worker_profile_env.py b/tests/gateway/test_managed_worker_profile_env.py
new file mode 100644
index 00000000000..d8b1301a63a
--- /dev/null
+++ b/tests/gateway/test_managed_worker_profile_env.py
@@ -0,0 +1,29 @@
+"""A served secondary profile's managed worker never inherits launch-profile residue."""
+from types import SimpleNamespace
+
+from agent.secret_scope import set_multiplex_active
+
+
+def test_secondary_worker_env_drops_launch_terminal_policy_and_env_settings(tmp_path, monkeypatch):
+ """Same contract as the cron and kanban dispatchers: a worker for served profile X gets the
+ env a standalone ``hermes -p X`` gateway would build — X's HERMES_HOME and X's ``.env`` over a
+ base without the launch profile's ``TERMINAL_*`` policy or launch-only ``.env`` settings."""
+ from gateway.session_managed_worker import _worker_env
+ root = tmp_path / '.hermes'
+ beta = root / 'profiles' / 'beta'
+ beta.mkdir(parents=True)
+ (root / '.env').write_text('HERMES_MODEL=launch-model\nTERMINAL_ENV=docker\n')
+ (beta / '.env').write_text('BETA_ONLY_TOKEN=beta-secret\n')
+ monkeypatch.setenv('HERMES_HOME', str(root))
+ monkeypatch.setenv('HERMES_MODEL', 'launch-model')
+ monkeypatch.setenv('TERMINAL_ENV', 'docker')
+ monkeypatch.setenv('TERMINAL_CWD', str(root))
+ set_multiplex_active(True)
+ try:
+ env = _worker_env(SimpleNamespace(profile_id=str(beta)))
+ finally:
+ set_multiplex_active(False)
+ assert env['HERMES_HOME'] == str(beta)
+ assert env['BETA_ONLY_TOKEN'] == 'beta-secret'
+ assert not {k for k in env if k.startswith('TERMINAL_')}, env
+ assert 'HERMES_MODEL' not in env