Skip to content

The agent that grows with you

fix(tools): honor multiplexed per-turn HERMES_HOME in execute_code child env

Kevin Rajan7ef0b98272f5
diff --git a/tests/tools/test_code_execution_hermes_home.py b/tests/tools/test_code_execution_hermes_home.py
new file mode 100644
index 00000000000..8edbc568bf9
--- /dev/null
+++ b/tests/tools/test_code_execution_hermes_home.py
@@ -0,0 +1,75 @@
+"""execute_code child env honors the multiplexed per-turn HERMES_HOME override (#110303).
+
+Under a multiplexed Desktop/Dashboard connection one server process serves several
+profiles, binding a context-local HERMES_HOME override per turn. ``_build_child_env``
+scrubs the server process's ``os.environ`` — which carries the machine-default
+HERMES_HOME — so without the rewrite below, skill scripts run via ``execute_code``
+silently read/write the wrong profile's directory.
+"""
+
+import sys
+
+import pytest
+
+from hermes_constants import (
+ get_hermes_home_override,
+ reset_hermes_home_override,
+ set_hermes_home_override,
+)
+from tools.code_execution_env import _build_child_env
+
+
+def home_override():
+ tokens = []
+
+ def _set(path):
+ tokens.append(set_hermes_home_override(path))
+ return str(path)
+
+ yield _set
+ for token in reversed(tokens):
+ reset_hermes_home_override(token)
+
+
+def _child_env():
+ return _build_child_env(
+ rpc_endpoint="sock",
+ rpc_token="tok",
+ tmpdir="/tmp/hermes-test",
+ child_python=sys.executable,
+ )
+
+
+class TestMultiplexedHermesHome:
+ def test_override_rewrites_stale_server_default(self, monkeypatch, home_override, tmp_path):
+ """The reported bug: child must see the active profile's home, not the server default."""
+ monkeypatch.setenv("HERMES_HOME", "/machine/default/.hermes")
+ alpha_home = home_override(tmp_path / "profiles" / "alpha")
+
+ assert _child_env()["HERMES_HOME"] == alpha_home
+
+ def test_no_override_leaves_inherited_value_untouched(self, monkeypatch):
+ """Dedicated per-profile processes (no override): zero behavior change."""
+ assert get_hermes_home_override() is None
+ monkeypatch.setenv("HERMES_HOME", "/machine/default/.hermes")
+
+ assert _child_env()["HERMES_HOME"] == "/machine/default/.hermes"
+
+ def test_override_applies_when_parent_env_unset(self, monkeypatch, home_override, tmp_path):
+ monkeypatch.delenv("HERMES_HOME", raising=False)
+ beta_home = home_override(tmp_path / "profiles" / "beta")
+
+ assert _child_env()["HERMES_HOME"] == beta_home
+
+ def test_override_does_not_leak_between_profiles(self, monkeypatch, home_override, tmp_path):
+ """Sequential turns for different profiles each see their own home."""
+ monkeypatch.setenv("HERMES_HOME", "/machine/default/.hermes")
+ home_override(tmp_path / "profiles" / "alpha")
+ first = _child_env()["HERMES_HOME"]
+
+ home_override(tmp_path / "profiles" / "beta")
+ second = _child_env()["HERMES_HOME"]
+
+ assert first == str(tmp_path / "profiles" / "alpha")
+ assert second == str(tmp_path / "profiles" / "beta")
diff --git a/tools/code_execution_env.py b/tools/code_execution_env.py
index 03af9650845..a81bbfa7195 100644
--- a/tools/code_execution_env.py
+++ b/tools/code_execution_env.py
@@ -112,7 +112,7 @@ def _scrub_child_env(source_env, is_passthrough=None, is_windows=None):
def _build_child_env(*, rpc_endpoint: str, rpc_token: str, tmpdir: str,
child_python: str) -> Dict[str, str]:
"""Build the scrubbed child environment both execution paths share."""
- from hermes_constants import apply_subprocess_home_env
+ from hermes_constants import apply_subprocess_home_env, get_hermes_home_override
child_env = _scrub_child_env(os.environ)
child_env["HERMES_RPC_SOCKET"] = rpc_endpoint
child_env["HERMES_RPC_TOKEN"] = rpc_token
@@ -130,6 +130,14 @@ def _build_child_env(*, rpc_endpoint: str, rpc_token: str, tmpdir: str,
child_env["TZ"] = _tz_name
child_env.pop("HERMES_TIMEZONE", None)
apply_subprocess_home_env(child_env)
+ # Multiplexed gateway/Desktop (#110303): the server process env carries the machine-default
+ # HERMES_HOME, but this turn runs under a per-profile override (ContextVar bound per turn).
+ # The scrub above passed the stale default through; rewrite it so skill scripts see the
+ # active profile's home — the same per-turn rewrite apply_subprocess_home_env does for HOME.
+ # No override (dedicated per-profile process) → leave the inherited value untouched.
+ _home_override = get_hermes_home_override()
+ if _home_override:
+ child_env["HERMES_HOME"] = _home_override
# PYTHONPATH: the staging dir (hermes_tools.py) must always be importable even when project
# mode changes CWD. Hermes's root is added ONLY when the child runs in Hermes's Python env —
# exposing Hermes's site-packages to an external interpreter can mix incompatible compiled