Skip to content

The agent that grows with you

fix(terminal): re-add managed PATH entries after login profiles reset them

liuhao1024214c3e68b051
diff --git a/tests/tools/test_login_shell_path_preservation.py b/tests/tools/test_login_shell_path_preservation.py
new file mode 100644
index 00000000000..7eac41e2afe
--- /dev/null
+++ b/tests/tools/test_login_shell_path_preservation.py
@@ -0,0 +1,103 @@
+"""Behavioural regression: the login-shell env snapshot must keep Hermes-managed
+PATH entries even when a login profile resets PATH.
+
+Debian's ``/etc/profile`` unconditionally resets PATH for non-root shells,
+dropping the venv dir the process environment put there — so the snapshot
+(``export -p``) serialises the reset PATH and bare ``python3``/``pip`` resolve
+to the system interpreter (#56634). These tests read the snapshot back from a
+real ``LocalEnvironment`` instead of asserting on injected markers.
+
+See: https://github.com/NousResearch/hermes-agent/issues/56634
+"""
+
+import pathlib
+import re
+
+import pytest
+
+from tools.environments.local import LocalEnvironment
+
+_PATH_LINE = re.compile(r'^(?:declare -x|export) PATH="(.*)"$', re.M)
+
+
+def _managed_setup(tmp_path, monkeypatch, *, profile_body: str):
+ """A resetting (or no-op) profile plus one fake Hermes-managed bin dir."""
+ profile = tmp_path / "profile.sh"
+ profile.write_text(profile_body)
+ managed = tmp_path / "managed-bin"
+ managed.mkdir()
+ monkeypatch.setenv("HOME", str(tmp_path))
+ monkeypatch.setattr(
+ "tools.environments.local._resolve_shell_init_files", lambda: [str(profile)]
+ )
+ monkeypatch.setattr(
+ "tools.environments.local._resolve_hermes_bin_dir", lambda: str(managed)
+ )
+ monkeypatch.setattr(
+ "tools.environments.local._managed_runtime_path_entries", lambda: []
+ )
+ return managed
+
+
+def _snapshot_path_value(env: LocalEnvironment) -> list[str]:
+ content = pathlib.Path(env._snapshot_path).read_text()
+ match = _PATH_LINE.search(content)
+ assert match, f"no PATH line in snapshot:\n{content}"
+ return match.group(1).split(":")
+
+
+class TestLoginShellPathPreservation:
+ @pytest.mark.platforms("posix")
+ def test_snapshot_keeps_managed_dir_when_profile_resets_path(
+ self, tmp_path, monkeypatch
+ ):
+ """Debian-style profile reset (PATH=... before the snapshot's export -p)
+ must not drop the managed dir from the captured snapshot."""
+ managed = _managed_setup(
+ tmp_path, monkeypatch, profile_body='PATH="/usr/local/bin:/usr/bin:/bin"\n'
+ )
+
+ env = LocalEnvironment(
+ cwd=str(tmp_path), env={"PATH": f"{managed}:/usr/local/bin:/usr/bin:/bin"}
+ )
+ try:
+ assert str(managed) in _snapshot_path_value(env)
+ finally:
+ env.cleanup()
+
+ @pytest.mark.platforms("posix")
+ def test_snapshot_does_not_duplicate_managed_dir(self, tmp_path, monkeypatch):
+ """A profile that leaves PATH alone: the restore's case guard must not
+ prepend a second copy of an entry the shell already has."""
+ managed = _managed_setup(
+ tmp_path, monkeypatch, profile_body=": # profile that keeps PATH\n"
+ )
+
+ env = LocalEnvironment(
+ cwd=str(tmp_path), env={"PATH": f"{managed}:/usr/local/bin:/usr/bin:/bin"}
+ )
+ try:
+ entries = _snapshot_path_value(env)
+ assert entries.count(str(managed)) == 1
+ finally:
+ env.cleanup()
+
+ @pytest.mark.platforms("posix")
+ def test_non_login_path_keeps_managed_dir_without_profile(
+ self, tmp_path, monkeypatch
+ ):
+ """Non-login commands never source the profile, so the resetting profile
+ can't drop the managed dir — the baseline the login path is compared to."""
+ managed = _managed_setup(
+ tmp_path, monkeypatch, profile_body='PATH="/usr/local/bin:/usr/bin:/bin"\n'
+ )
+
+ env = LocalEnvironment(
+ cwd=str(tmp_path), env={"PATH": f"{managed}:/usr/local/bin:/usr/bin:/bin"}
+ )
+ try:
+ proc = env._run_bash('printf "%s" "$PATH"', login=False, timeout=30)
+ out, _ = proc.communicate(timeout=30)
+ assert str(managed) in out.split(":")
+ finally:
+ env.cleanup()
diff --git a/tools/environments/local.py b/tools/environments/local.py
index 9853ebfec64..5a93248b190 100644
--- a/tools/environments/local.py
+++ b/tools/environments/local.py
@@ -861,6 +861,25 @@ def _prepend_shell_init(cmd_string: str, files: list[str]) -> str:
return "\n".join(prelude) + "\n" + cmd_string
+def _prepend_managed_path_restore(cmd_string: str) -> str:
+ """Prepend idempotent PATH-restore lines for Hermes-managed dirs.
+
+ Login profiles can reset PATH outright — Debian's ``/etc/profile`` does so
+ for non-root shells, dropping the venv dir the process env put there
+ (#56634). Sits between the init-file prelude and the command (the env
+ snapshot's ``export -p``), so restored entries survive into the snapshot.
+ Each ``case`` guard re-adds its dir only when absent, keeping reruns and
+ profiles that kept the entries duplicate-free."""
+ if _IS_WINDOWS:
+ return cmd_string # native PATH stays ``;``-joined; bash sees a converted copy
+ entries = [d for d in (_resolve_hermes_bin_dir(), *_managed_runtime_path_entries())
+ if d and '"' not in d]
+ if not entries:
+ return cmd_string
+ lines = [f'case ":$PATH:" in *":{d}:"*) ;; *) PATH="{d}:$PATH" ;; esac' for d in entries]
+ return "\n".join(lines) + "\n" + cmd_string
+
+
# --- Process-group teardown (POSIX) ---
def _wait_for_group_exit(proc, pgid: int, timeout: float) -> bool:
"""Wait until the process group is gone, reaping the wrapper as we go (a dead
@@ -1107,8 +1126,12 @@ class LocalEnvironment(BaseEnvironment):
stdin_data: str | None = None) -> subprocess.Popen:
bash = _find_bash()
# Login invocations (init_session's env snapshot) source the user's rc /
- # custom init files so nvm/asdf/pyenv land on PATH in the snapshot.
+ # custom init files so nvm/asdf/pyenv land on PATH in the snapshot; the
+ # managed-dir restore runs after that prelude so a profile that resets
+ # PATH (Debian's /etc/profile) can't drop Hermes' own dirs from the
+ # snapshot (#56634).
if login:
+ cmd_string = _prepend_managed_path_restore(cmd_string)
cmd_string = _prepend_shell_init(cmd_string, _resolve_shell_init_files())
args = [bash, *(["-l"] if login else []), "-c", cmd_string]
self._recover_cwd()