Skip to content

The agent that grows with you

fix(gateway-runtime): build the auto-started daemon's env from the target profile

Tekniumbbd68d15f4b1
diff --git a/hermes_cli/gateway_runtime_start.py b/hermes_cli/gateway_runtime_start.py
index cf9ca2ee4d3..620260fa101 100644
--- a/hermes_cli/gateway_runtime_start.py
+++ b/hermes_cli/gateway_runtime_start.py
@@ -41,7 +41,10 @@ def spawn_unmanaged_gateway(profile_home: Path, *, deadline: float, idle_exit: b
selector = [] if home.parent.name == "profiles" else ["--profile", "default"]
command = [sys.executable, "-m", "hermes_cli.main", *selector, "gateway", "run", "--quiet",
*(["--idle-exit"] if idle_exit else [])]
- env = dict(os.environ)
+ # The target profile's own secrets, never the launching client's: a `hermes -p work` client's
+ # dotenv would otherwise become the default daemon's frozen launch env (host_gateway_child_env).
+ from tools.environments.local import served_profile_child_env
+ env = served_profile_child_env(base=dict(os.environ), target_home=home, inherit_credentials=True)
if sys.platform == "win32":
from hermes_cli.gateway_windows import windowless_gateway_restart_spec
command, _, overlay = windowless_gateway_restart_spec(command)
diff --git a/tests/hermes_cli/test_daemon_env_profile_donation.py b/tests/hermes_cli/test_daemon_env_profile_donation.py
new file mode 100644
index 00000000000..1b653d5ac99
--- /dev/null
+++ b/tests/hermes_cli/test_daemon_env_profile_donation.py
@@ -0,0 +1,47 @@
+"""A named-profile client that auto-starts the host daemon never donates its own dotenv to it (N43)."""
+import json
+import os
+from pathlib import Path
+import sys
+import time
+
+import pytest
+
+
[email protected]_gateway_lookalike # stub interpreter records env then exits; reaped below
+def test_host_daemon_does_not_inherit_named_profile_dotenv(tmp_path, monkeypatch):
+ from hermes_cli import gateway_runtime_start as start
+ from hermes_cli.env_loader import load_hermes_dotenv
+ root = tmp_path / 'root'
+ work = root / 'profiles' / 'work'
+ work.mkdir(parents=True, mode=0o700)
+ (root / '.env').write_text('OPENROUTER_API_KEY=root-key\n', encoding='utf-8')
+ (work / '.env').write_text('TELEGRAM_BOT_TOKEN=work-bot-token\nOPENROUTER_API_KEY=work-key\n'
+ 'HERMES_LANGUAGE=fr\n', encoding='utf-8')
+ for key in ('TELEGRAM_BOT_TOKEN', 'OPENROUTER_API_KEY', 'HERMES_LANGUAGE'):
+ monkeypatch.delenv(key, raising=False)
+ # The `hermes -p work` client: its process home is the profile and its .env is in os.environ.
+ monkeypatch.setenv('HERMES_HOME', str(work))
+ load_hermes_dotenv(hermes_home=work)
+ assert os.environ['TELEGRAM_BOT_TOKEN'] == 'work-bot-token'
+ monkeypatch.setenv('SHELL_INJECTED_SETTING', 'kept')
+ witness = tmp_path / 'env.json'
+ executable = tmp_path / 'owned-interpreter'
+ names = ['TELEGRAM_BOT_TOKEN', 'OPENROUTER_API_KEY', 'HERMES_LANGUAGE', 'HERMES_HOME', 'SHELL_INJECTED_SETTING']
+ executable.write_text(
+ '#!' + sys.executable + '\nimport json, os\n'
+ + f'open({str(witness)!r}, "w").write(json.dumps('
+ + '{k: os.environ.get(k) for k in ' + repr(names) + '}))\n', encoding='utf-8')
+ executable.chmod(0o700)
+ monkeypatch.setattr(sys, 'executable', str(executable))
+ child = start.spawn_unmanaged_gateway(root, deadline=time.monotonic() + 5)
+ try:
+ assert child.wait(timeout=10) == 0
+ finally:
+ if child.poll() is None:
+ child.kill()
+ child.wait(timeout=5)
+ seen = json.loads(Path(witness).read_text())
+ assert seen == {'TELEGRAM_BOT_TOKEN': None, 'OPENROUTER_API_KEY': 'root-key', 'HERMES_LANGUAGE': None,
+ 'HERMES_HOME': str(root.resolve()), 'SHELL_INJECTED_SETTING': 'kept'}
diff --git a/tests/hermes_cli/test_gateway_runtime_ensure.py b/tests/hermes_cli/test_gateway_runtime_ensure.py
index 9c1160d1acc..06b5895f59a 100644
--- a/tests/hermes_cli/test_gateway_runtime_ensure.py
+++ b/tests/hermes_cli/test_gateway_runtime_ensure.py
@@ -199,6 +199,7 @@ def test_unmanaged_child_uses_explicit_home_and_survives_launcher_exit(tmp_path,
f"home=Path({str(home)!r})\n"
"real=start.subprocess.Popen\n"
"def boundary(argv, **kw):\n"
+ " if 'hermes_cli.main' not in argv: return real(argv, **kw) # e.g. git from the env builder\n"
" assert argv[-3:] == ['gateway','run','--quiet']\n"
" assert '--replace' not in argv and '--force' not in argv\n"
" assert kw['env']['HERMES_HOME'] == str(home)\n"