The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): preserve venv across self-relaunch
frozen5c2ddeb55a83
diff --git a/hermes_cli/relaunch.py b/hermes_cli/relaunch.pyindex 0ae7ec57e9b..316bf12f628 100644--- a/hermes_cli/relaunch.py+++ b/hermes_cli/relaunch.py@@ -60,24 +60,46 @@ def _extract_inherited_flags(argv: Sequence[str]) -> list[str]:def resolve_hermes_bin() -> Optional[str]:"""Hermes entry point: ``sys.argv[0]`` if a real executable, else ``which hermes``, else ``None``- (caller falls back to ``python -m hermes_cli.main``)."""+ (caller falls back to ``python -m hermes_cli.main``).++ Python launchers are never returned: on Windows a ``.py`` can't be exec'd directly, and on+ POSIX the git installer runs the extensionless source launcher under the managed venv+ interpreter while its ``#!/usr/bin/env python3`` shebang would pick the system Python and+ lose the venv. Falling through to ``sys.executable -m hermes_cli.main`` keeps the venv.+ """argv0 = sys.argv[0]_is_windows = sys.platform == "win32"def _is_python_script(p: str) -> bool:return p.lower().endswith((".py", ".pyc"))+ def _has_python_shebang(p: str) -> bool:+ try:+ with open(p, "rb") as fh:+ shebang = fh.readline(256).lower()+ except OSError:+ return False+ return shebang.startswith(b"#!") and b"python" in shebang++ def _is_unsafe_python_launcher(p: str) -> bool:+ if _is_windows:+ return _is_python_script(p)+ return _has_python_shebang(p)+# Absolute executable (nix store, venv wrappers, …), then relative-to-CWD, then PATH.if (os.path.isabs(argv0) and os.path.isfile(argv0) and os.access(argv0, os.X_OK)- and not (_is_windows and _is_python_script(argv0))+ and not _is_unsafe_python_launcher(argv0)):return argv0if not argv0.startswith("-") and os.path.isfile(argv0):abs_path = os.path.abspath(argv0)- if os.access(abs_path, os.X_OK) and not (_is_windows and _is_python_script(abs_path)):+ if os.access(abs_path, os.X_OK) and not _is_unsafe_python_launcher(abs_path):return abs_path- return shutil.which("hermes") or None+ path_bin = shutil.which("hermes")+ if path_bin and not _is_unsafe_python_launcher(path_bin):+ return path_bin+ return Nonedef build_relaunch_argv(diff --git a/tests/hermes_cli/test_relaunch.py b/tests/hermes_cli/test_relaunch.pyindex ffb611950e4..5a79bbfcf9a 100644--- a/tests/hermes_cli/test_relaunch.py+++ b/tests/hermes_cli/test_relaunch.py@@ -215,16 +215,47 @@ class TestResolveHermesBinWindowsPyGuard:# Must NOT be the .py — must be the hermes.exe PATH entry.assert bin_path == r"C:\venv\Scripts\hermes.exe"- @pytest.mark.linux_only- def test_posix_still_accepts_py_argv0(self, monkeypatch, tmp_path):- """POSIX behaviour unchanged: argv[0] pointing at an executable- script (including .py with a shebang + chmod +x) is fine to return- because POSIX exec can route through the shebang line."""+ def test_posix_python_launcher_falls_through_to_path(self, monkeypatch, tmp_path):+ """A Python source launcher must not be re-executed through its shebang.++ The git installer invokes ``hermes`` with the venv interpreter while the+ source launcher's shebang uses ``/usr/bin/env python3``. Re-executing+ that source file directly can therefore escape the venv and lose core+ dependencies such as PyYAML.+ """+ if sys.platform == "win32":+ pytest.skip("POSIX semantics")script = tmp_path / "hermes"script.write_text("#!/usr/bin/env python3\n")script.chmod(0o755)+ wrapper = tmp_path / "bin" / "hermes"+ wrapper.parent.mkdir()+ wrapper.write_text("#!/usr/bin/env bash\n")+ wrapper.chmod(0o755)monkeypatch.setattr(relaunch_mod.sys, "argv", [str(script), "chat"])- assert relaunch_mod.resolve_hermes_bin() == str(script)+ monkeypatch.setattr(+ relaunch_mod.shutil, "which",+ lambda name: str(wrapper) if name == "hermes" else None,+ )+ assert relaunch_mod.resolve_hermes_bin() == str(wrapper)++ def test_posix_python_launcher_on_path_falls_back_to_current_python(+ self, monkeypatch, tmp_path+ ):+ """PATH must not re-select the Python launcher rejected from argv[0]."""+ if sys.platform == "win32":+ pytest.skip("POSIX semantics")+ script = tmp_path / "hermes"+ script.write_text("#!/usr/bin/env python3\n")+ script.chmod(0o755)+ monkeypatch.setattr(relaunch_mod.sys, "argv", [str(script), "chat"])+ monkeypatch.setattr(+ relaunch_mod.shutil,+ "which",+ lambda name: str(script) if name == "hermes" else None,+ )++ assert relaunch_mod.resolve_hermes_bin() is None@pytest.mark.windows_onlydef test_windows_py_argv0_with_no_hermes_on_path_returns_none(self, monkeypatch, tmp_path):