Skip to content

The agent that grows with you

fix(env): inline the launch-home check; prove A->B->A on two temp homes (#125530, salvage #125572)

teknium1e317f5205ee7
diff --git a/hermes_cli/env_loader.py b/hermes_cli/env_loader.py
index c35aea5d24c..3875582b6a4 100644
--- a/hermes_cli/env_loader.py
+++ b/hermes_cli/env_loader.py
@@ -388,21 +388,6 @@ def _sanitize_env_file_if_needed(path: Path) -> None:
pass # best-effort — don't block gateway startup
-def _is_launch_home_load(home_path: Path) -> bool:
- """Is THIS load for the process's own (launch) profile rather than a routed one?
-
- Same launch-home identity as ``_reapply_terminal_config_bridge``: the override being set only
- proves SOME profile is routed — under multiplex the launch profile's own scoped bodies also
- bind an override naming the launch home (``tui_gateway.model_switch
- ._profile_runtime_scope_tokens(None)``), and its .env is process configuration, not a foreign
- secret (#125530).
- """
- try:
- return Path(home_path).resolve() == _process_hermes_home().resolve()
- except OSError:
- return False
-
-
def load_hermes_dotenv(
*,
hermes_home: str | os.PathLike | None = None,
@@ -416,16 +401,17 @@ def load_hermes_dotenv(
from hermes_constants import get_process_hermes_home
home_path = Path(hermes_home) if hermes_home else get_process_hermes_home()
- # Multiplex gateway: while a routed profile-home override is active, copying THAT profile's .env
- # into os.environ would expose its credentials to sibling turns and every spawned child. The
- # PROCESS home's .env is different: it is the launch profile's own file, already shared process
- # configuration, so it still loads (#125530) — skipping it silently hid launch-profile-only
- # credentials (e.g. an OPENROUTER_API_KEY backing fallback_providers) from the process env. Only
- # a load whose target is a FOREIGN (routed) profile home is skipped.
+ # Multiplex gateway: while a routed profile-home override is active, copying that profile's .env
+ # into os.environ would expose its credentials to sibling turns and every spawned child. The launch
+ # home's own .env is process configuration and still loads: the launch profile's scoped bodies bind
+ # an override naming the launch home too, and skipping it hid launch-only credentials such as a
+ # fallback_providers key from the process env (#125530). External sources still refresh against
+ # the profile mapping.
from agent.secret_scope import is_multiplex_active
from hermes_constants import get_hermes_home_override
- if is_multiplex_active() and get_hermes_home_override() is not None and not _is_launch_home_load(home_path):
+ if (is_multiplex_active() and get_hermes_home_override() is not None
+ and home_path.resolve() != _process_hermes_home().resolve()):
home_key = str(home_path.resolve())
if home_key not in _SCOPED_SKIP_LOGGED:
_SCOPED_SKIP_LOGGED.add(home_key)
diff --git a/tests/gateway/test_multiplex_credential_isolation.py b/tests/gateway/test_multiplex_credential_isolation.py
index c38ecb8c71e..a835b4e12e2 100644
--- a/tests/gateway/test_multiplex_credential_isolation.py
+++ b/tests/gateway/test_multiplex_credential_isolation.py
@@ -136,56 +136,41 @@ def test_turn_scoped_dotenv_reload_does_not_pollute_process_env(tmp_path, monkey
def test_launch_home_dotenv_still_loads_under_multiplex(tmp_path, monkeypatch):
- """#125530: the guard must skip only FOREIGN (routed) home loads.
-
- The launch profile's own scoped bodies bind an override naming the launch
- home (``_profile_runtime_scope_tokens(None)``), so a launch-home load can
- legitimately arrive with an override set. Skipping it hid the launch
- home's `.env` from the process env, silently breaking fallback_providers
- whose key lives only there. A routed profile's `.env` must still never be
- copied into ``os.environ``.
+ """#125530: the multiplex guard skips only FOREIGN (routed) home loads.
+
+ The launch profile's own scoped bodies bind an override naming the launch home, so a
+ launch-home load legitimately arrives with an override set; skipping it hid the launch
+ home's ``.env`` (a fallback_providers key) from the process env. A routed profile's
+ ``.env`` is still never copied into ``os.environ``.
"""
import os
from hermes_cli.env_loader import load_hermes_dotenv
- from hermes_constants import (
- get_process_hermes_home,
- reset_hermes_home_override,
- set_hermes_home_override,
- )
-
- launch_home = get_process_hermes_home().resolve()
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+
+ launch = tmp_path / "launch"
+ routed = tmp_path / "profiles" / "routed"
+ launch.mkdir()
+ routed.mkdir(parents=True)
+ (launch / ".env").write_text("LAUNCH_ONLY_FALLBACK_KEY=launch-key\n", encoding="utf-8")
+ (routed / ".env").write_text("LAUNCH_ONLY_FALLBACK_KEY=routed-secret\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(launch))
monkeypatch.delenv("LAUNCH_ONLY_FALLBACK_KEY", raising=False)
-
ss.set_multiplex_active(True)
- home_token = set_hermes_home_override(launch_home)
- try:
- # The launch home's OWN .env is process configuration: it must load.
- (launch_home / ".env").write_text(
- f"LAUNCH_ONLY_FALLBACK_KEY=launch-key-{launch_home.name}\n", encoding="utf-8"
- )
+
+ def _load(home):
+ token = set_hermes_home_override(str(home))
try:
- loaded = load_hermes_dotenv(hermes_home=launch_home)
- assert loaded == [launch_home / ".env"]
- assert os.environ.get("LAUNCH_ONLY_FALLBACK_KEY") == f"launch-key-{launch_home.name}"
+ return load_hermes_dotenv(hermes_home=home)
finally:
- monkeypatch.delenv("LAUNCH_ONLY_FALLBACK_KEY", raising=False)
- finally:
- reset_hermes_home_override(home_token)
- ss.set_multiplex_active(False)
-
- # A FOREIGN routed home's .env is still never copied into os.environ.
- foreign = tmp_path / "profiles" / "routed"
- foreign.mkdir(parents=True)
- (foreign / ".env").write_text("LAUNCH_ONLY_FALLBACK_KEY=foreign-secret\n", encoding="utf-8")
- ss.set_multiplex_active(True)
- home_token = set_hermes_home_override(foreign)
- try:
- assert load_hermes_dotenv(hermes_home=foreign) == []
- assert "LAUNCH_ONLY_FALLBACK_KEY" not in os.environ
- finally:
- reset_hermes_home_override(home_token)
- ss.set_multiplex_active(False)
+ reset_hermes_home_override(token)
+
+ assert _load(launch) == [launch / ".env"]
+ assert os.environ["LAUNCH_ONLY_FALLBACK_KEY"] == "launch-key"
+ assert _load(routed) == []
+ assert os.environ["LAUNCH_ONLY_FALLBACK_KEY"] == "launch-key"
+ assert _load(launch) == [launch / ".env"]
+ assert os.environ["LAUNCH_ONLY_FALLBACK_KEY"] == "launch-key"
def test_cold_profile_hydrates_external_source_without_global_env(