Skip to content

The agent that grows with you

fix(gateway): make managed Node suppress PATH fallback

Shawn Wangf4f08f9dae69
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 494df006e0d..827c3e4dac2 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -3204,14 +3204,23 @@ def _append_node_dir_for_service(
"""
from hermes_constants import iter_hermes_node_dirs
+ managed_node_present = False
for directory in iter_hermes_node_dirs(hermes_root):
entry = str(directory)
try:
present = directory.is_dir()
except OSError:
present = False
- if present and entry not in path_entries:
- path_entries.append(entry)
+ if present:
+ managed_node_present = True
+ if entry not in path_entries:
+ path_entries.append(entry)
+
+ # Ambient PATH lookup is a fallback, not an additional rung. Once the
+ # target Hermes home provides managed Node, consulting the invoker's PATH
+ # makes a system unit differ between sudo/root and its service user.
+ if managed_node_present:
+ return
resolved_node = shutil.which("node")
if not resolved_node:
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index 91730237daf..894e101b1cf 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -845,6 +845,54 @@ class TestDetectVenvDir:
class TestSystemUnitHermesHome:
"""HERMES_HOME in system units must reference the target user, not root."""
+ def test_managed_node_makes_system_unit_independent_of_callers_path(
+ self, monkeypatch, tmp_path
+ ):
+ """A target-managed Node must suppress caller-specific PATH fallbacks."""
+ target_home = tmp_path / "home" / "alice"
+ target_hermes = target_home / ".hermes"
+ root_home = tmp_path / "root"
+ root_hermes = root_home / ".hermes"
+ managed_bin = target_hermes / "node" / "bin"
+ managed_bin.mkdir(parents=True)
+ root_hermes.mkdir(parents=True)
+
+ monkeypatch.setattr(Path, "home", staticmethod(lambda: root_home))
+ monkeypatch.setenv("HERMES_HOME", str(root_hermes))
+ monkeypatch.setattr(
+ gateway_cli,
+ "_system_service_identity",
+ lambda run_as_user=None: ("alice", "alice", str(target_home)),
+ )
+ monkeypatch.setattr(gateway_cli, "get_hermes_home", lambda: root_hermes)
+ monkeypatch.setattr(gateway_cli, "_build_service_path_dirs", lambda: [])
+
+ monkeypatch.setattr(gateway_cli.shutil, "which", lambda name: "/root/bin/node")
+ root_unit = gateway_cli.generate_systemd_unit(system=True, run_as_user="alice")
+
+ monkeypatch.setattr(gateway_cli.shutil, "which", lambda name: "/home/alice/.local/bin/node")
+ user_unit = gateway_cli.generate_systemd_unit(system=True, run_as_user="alice")
+
+ assert root_unit == user_unit
+ assert str(managed_bin) in root_unit
+ assert "/root/bin" not in root_unit
+
+ def test_node_path_lookup_remains_fallback_without_managed_node(
+ self, monkeypatch, tmp_path
+ ):
+ """External Node installs still work when the managed tree is absent."""
+ monkeypatch.setattr(
+ "hermes_constants.iter_hermes_node_dirs", lambda root=None: []
+ )
+ monkeypatch.setattr(
+ gateway_cli.shutil, "which", lambda name: "/opt/external-node/bin/node"
+ )
+ entries: list[str] = []
+
+ gateway_cli._append_node_dir_for_service(entries, tmp_path / ".hermes")
+
+ assert entries == ["/opt/external-node/bin"]
+
def test_system_unit_uses_target_user_home_not_calling_user(self, monkeypatch):
# Simulate sudo: Path.home() returns /root, target user is alice
monkeypatch.setattr(Path, "home", staticmethod(lambda: Path("/root")))