The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): launch-profile managed workers keep env-only tool credentials under multiplex
Teknium51d50e18e9a9
diff --git a/gateway/session_managed_worker.py b/gateway/session_managed_worker.pyindex 9f23bab006b..d17237d36c6 100644--- a/gateway/session_managed_worker.py+++ b/gateway/session_managed_worker.py@@ -251,8 +251,9 @@ def _prompt_frame(authority, ref, row, worker, frame):def _worker_env(authority):"""Child env for the OWNING profile under multiplex: its HERMES_HOME plus its ``.env``secrets over a scrubbed base, never the launch profile's process environment (the same- rule MCP stdio children and shell hooks follow). Single-profile gateways inherit the- process env byte-for-byte, exactly as before."""+ rule MCP stdio children and shell hooks follow) — except for the launch profile's own+ worker, whose scope legitimately includes its frozen launch env. Single-profile gateways+ inherit the process env byte-for-byte, exactly as before."""from pathlib import Pathfrom agent.secret_scope import is_multiplex_activefrom tools.environments.local import _is_routed_home@@ -271,17 +272,37 @@ def _worker_env(authority):# 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)+ secrets = build_profile_secret_scope(home)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._scrub_credentials(env, inherit_credentials=False)- env.update({k: v for k, v in build_profile_secret_scope(home).items() if v is not None})+ else:+ secrets = {**_launch_env_only_credentials(home, env), **secrets}+ env.update({k: v for k, v in secrets.items() if v is not None})env['HERMES_HOME'] = str(home)from hermes_constants import apply_subprocess_home_envapply_subprocess_home_env(env)return env+def _launch_env_only_credentials(home, env):+ """Credentials the LAUNCH profile's owner resolves from its frozen launch env+ (``launch_secret_scope``: systemd ``Environment=`` / ``op run`` keys with no ``.env`` line) that+ the scrub removed from *env* — the same mapping the owner's own ``get_secret`` reads for this+ profile. Credential names only: non-credential names the constructor removed on purpose+ (unbound session context, venv markers) stay removed, and Hermes-internal secrets+ (``AUXILIARY_*_API_KEY``, relay auth) never reach a child. Only the launch home's worker calls+ this; a routed home's worker sees its own files only."""+ from tools.environments.local import _scrub_credentials+ from tools.environments.local_env_policy import _is_hermes_internal_secret+ from tui_gateway.launch_profile_policy import launch_secret_scope+ missing = {k: v for k, v in launch_secret_scope(home).items()+ if k not in env and not _is_hermes_internal_secret(k)}+ plain = _scrub_credentials(dict(missing), inherit_credentials=False)+ return {k: v for k, v in missing.items() if k not in plain}++def _interrupted_before_bootstrap(authority, row):authority.pending_results[row['admission_id']] = {'result': {'final_response': '', 'interrupted': True}, 'usage': {}}diff --git a/tests/gateway/test_managed_worker_env_boundary.py b/tests/gateway/test_managed_worker_env_boundary.pyindex 7c92a50d66f..b65ea4a7ebd 100644--- a/tests/gateway/test_managed_worker_env_boundary.py+++ b/tests/gateway/test_managed_worker_env_boundary.py@@ -17,7 +17,7 @@ def test_secondary_profile_worker_gets_no_launch_credentials(tmp_path, monkeypatfor key, value in {"OPENROUTER_API_KEY": "LAUNCH-or", "MY_SERVICE_TOKEN": "LAUNCH-mysvc","NOUS_API_KEY": "LAUNCH-nous", "VAULT_ONLY_TOKEN": "LAUNCH-vault"}.items():monkeypatch.setenv(key, value)- import hermes_cli.env_loader as env_loader+ from hermes_cli import env_loadermonkeypatch.setitem(env_loader._SECRET_SOURCES, "VAULT_ONLY_TOKEN", "bitwarden")from agent import secret_scopefrom gateway.session_managed_worker import _worker_env@@ -34,6 +34,36 @@ def test_secondary_profile_worker_gets_no_launch_credentials(tmp_path, monkeypatassert own is None # single-profile: the launch profile's own worker inherits byte-for-byte+def test_launch_profile_worker_keeps_env_only_tool_credentials_under_multiplex(tmp_path, monkeypatch):+ """Under multiplex the launch profile's own worker resolves what its owner resolves for that+ profile (``launch_secret_scope``): a tool key injected only into the launch environment+ (systemd ``Environment=``, ``op run``) reaches it, while a secondary's worker never sees it."""+ launch = tmp_path / ".hermes"+ sec = launch / "profiles" / "l106742sec"+ sec.mkdir(parents=True)+ (launch / ".env").write_text("OPENROUTER_API_KEY=LAUNCH-or\n")+ (sec / ".env").write_text("OPENROUTER_API_KEY=SEC-or\n")+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setenv("HERMES_HOME", str(launch))+ monkeypatch.setenv("FIRECRAWL_API_KEY", "ENVONLY-fc")+ monkeypatch.setenv("AUXILIARY_VISION_API_KEY", "ENVONLY-aux")+ from agent.secret_scope import get_secret, set_multiplex_active+ from gateway.session_managed_worker import _worker_env+ from tui_gateway.launch_profile_policy import activate_multi_profile_hosting, launch_profile_runtime_scope+ activate_multi_profile_hosting()+ try:+ with launch_profile_runtime_scope(launch):+ assert get_secret("FIRECRAWL_API_KEY") == "ENVONLY-fc" # what the owner resolves+ own = _worker_env(SimpleNamespace(profile_id=str(launch)))+ other = _worker_env(SimpleNamespace(profile_id=str(sec)))+ finally:+ set_multiplex_active(False)+ assert own["FIRECRAWL_API_KEY"] == "ENVONLY-fc" and own["OPENROUTER_API_KEY"] == "LAUNCH-or"+ assert "AUXILIARY_VISION_API_KEY" not in own # Hermes-internal secrets never reach a child+ assert "FIRECRAWL_API_KEY" not in other and other["OPENROUTER_API_KEY"] == "SEC-or"+ assert sorted(k for k, v in other.items() if "ENVONLY" in str(v) or "LAUNCH" in str(v)) == []++@pytest.mark.parametrize("bad", ["../../../../escape", "x/../../y", "..\\..\\win", "C:\\evil", ""])def test_outbox_dir_only_names_paths_inside_worker_outboxes(tmp_path, bad):from agent.managed_worker import outbox_dir