Skip to content

The agent that grows with you

fix(gateway): strip launch residue before the worker env constructor injects turn context

Teknium1c6bec8f0040
diff --git a/gateway/session_managed_worker.py b/gateway/session_managed_worker.py
index f5cd2949ac5..9f23bab006b 100644
--- a/gateway/session_managed_worker.py
+++ b/gateway/session_managed_worker.py
@@ -3,6 +3,7 @@ import asyncio
from contextlib import contextmanager
from dataclasses import asdict, replace
import json
+import os
import queue
import threading
from types import SimpleNamespace
@@ -267,7 +268,9 @@ def _worker_env(authority):
from tools.environments.local import _scrub_credentials, 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)
+ # Strip the RAW environ before the constructor injects this turn's own session/bridge context:
+ # a launch ``.env`` name (HERMES_SESSION_ID...) must not erase a value derived for this turn.
+ env = build_subprocess_env(base=strip_launch_profile_env(os.environ.copy(), home), scrub_secrets=True)
if routed:
# Same rule as served_profile_child_env: env_passthrough / first-party carve-outs must not
# forward launch-process provider credentials that no .env or source snapshot recorded.
diff --git a/tests/gateway/test_managed_worker_profile_env.py b/tests/gateway/test_managed_worker_profile_env.py
index d8b1301a63a..1de2269c661 100644
--- a/tests/gateway/test_managed_worker_profile_env.py
+++ b/tests/gateway/test_managed_worker_profile_env.py
@@ -1,4 +1,5 @@
"""A served secondary profile's managed worker never inherits launch-profile residue."""
+import os
from types import SimpleNamespace
from agent.secret_scope import set_multiplex_active
@@ -27,3 +28,31 @@ def test_secondary_worker_env_drops_launch_terminal_policy_and_env_settings(tmp_
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
+
+
+def test_launch_scrub_runs_before_the_turn_context_is_injected(tmp_path, monkeypatch):
+ """The launch residue strip runs on the RAW environ, before ``build_subprocess_env`` bridges this
+ turn's own session context: a launch ``.env`` that names ``HERMES_SESSION_ID`` must not erase the
+ id derived for the secondary's turn (#109338 ordering)."""
+ from gateway.run import _profile_runtime_scope
+ from gateway.session_context import scoped_current_session_id
+ 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_SESSION_ID=launch-session\nLAUNCH_ONLY=launch-value\n')
+ (beta / '.env').write_text('BETA_ONLY_TOKEN=beta-secret\n')
+ monkeypatch.setenv('HERMES_HOME', str(root))
+ monkeypatch.setenv('HERMES_SESSION_ID', 'launch-session')
+ monkeypatch.setenv('LAUNCH_ONLY', 'launch-value')
+ before = dict(os.environ)
+ set_multiplex_active(True)
+ try:
+ with _profile_runtime_scope(beta), scoped_current_session_id('owned-session'):
+ env = _worker_env(SimpleNamespace(profile_id=str(beta)))
+ finally:
+ set_multiplex_active(False)
+ assert env['HERMES_SESSION_ID'] == 'owned-session'
+ assert 'LAUNCH_ONLY' not in env
+ assert env['BETA_ONLY_TOKEN'] == 'beta-secret'
+ assert dict(os.environ) == before