Skip to content

The agent that grows with you

fix(launcher): date a venv's site-packages from the venv, not from the caller

ethernetcc8f9370acc9
diff --git a/hermes_cli/runtime_paths.py b/hermes_cli/runtime_paths.py
index 63044d244e4..140e3cfdfe4 100644
--- a/hermes_cli/runtime_paths.py
+++ b/hermes_cli/runtime_paths.py
@@ -106,11 +106,46 @@ def selected_venv(project_root: Path) -> Path:
return environment
+def venv_python_version(venv: Path) -> tuple[int, int] | None:
+ """The interpreter version a POSIX venv actually holds, or ``None``.
+
+ ``site_packages`` must not date the tree from the CALLER's ``sys.version_info``:
+ an update can rebuild the dependency environment with a different Python than
+ the launcher that later imports it. Observed on an app-driven upgrade -- PM
+ built the environment with CPython 3.14 while the PATH shim ran 3.11, so the
+ shim composed ``lib/python3.11/site-packages`` inside a 3.14 venv, found no
+ tree, and failed *after* a successful update.
+ """
+ try:
+ for line in (venv / "pyvenv.cfg").read_text(encoding="utf-8").splitlines():
+ key, _, value = line.partition("=")
+ if key.strip() != "version":
+ continue
+ major, _, rest = value.strip().partition(".")
+ minor, _, _ = rest.partition(".")
+ if major.isdigit() and minor.isdigit():
+ return int(major), int(minor)
+ except OSError:
+ pass
+ try:
+ candidates = sorted((venv / "lib").glob("python3*"))
+ except OSError:
+ return None
+ for candidate in candidates:
+ major, _, rest = candidate.name.removeprefix("python").partition(".")
+ minor, _, _ = rest.partition(".")
+ if major.isdigit() and minor.isdigit():
+ return int(major), int(minor)
+ return None
+
+
def site_packages(venv: Path) -> Path:
import sys
- return venv / ("Lib/site-packages" if os.name == "nt" else
- f"lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages")
+ if os.name == "nt":
+ return venv / "Lib/site-packages"
+ version = venv_python_version(venv) or (sys.version_info.major, sys.version_info.minor)
+ return venv / f"lib/python{version[0]}.{version[1]}/site-packages"
def activate_dependencies(project_root: Path) -> None:
diff --git a/tests/hermes_cli/test_runtime_paths.py b/tests/hermes_cli/test_runtime_paths.py
index ef1338f1331..ef7fb038d4a 100644
--- a/tests/hermes_cli/test_runtime_paths.py
+++ b/tests/hermes_cli/test_runtime_paths.py
@@ -1,10 +1,11 @@
"""Payload stores follow the payload, not the launching shell's home."""
import json
+import os
import pytest
-from hermes_cli.runtime_paths import store_root
+from hermes_cli.runtime_paths import site_packages, store_root, venv_python_version
from hermes_constants import get_default_hermes_root
@@ -64,4 +65,35 @@ def test_payload_store_cannot_escape_payload(tmp_path, monkeypatch, escape):
with pytest.raises(RuntimeError, match="payload store escapes its root"):
store_root(repo)
monkeypatch.setenv("HERMES_RUNTIME_DIR", str(outside))
- assert store_root(repo) == outside
\ No newline at end of file
+ assert store_root(repo) == outside
+
+def test_site_packages_follows_the_venv_python_not_the_caller(tmp_path):
+ """Regression: the tree must be dated from the venv, not from this process.
+
+ An app-driven upgrade rebuilt the dependency environment with CPython 3.14
+ while the PATH shim ran 3.11, so site_packages() pointed at
+ lib/python3.11/site-packages inside a 3.14 venv. activate_dependencies()
+ then found no tree and failed the shim *after* a successful update -- it
+ only tolerates a missing tree when no generation was published.
+ """
+ venv = tmp_path / "venv"
+ (venv / "lib" / "python3.9" / "site-packages").mkdir(parents=True)
+ (venv / "pyvenv.cfg").write_text("home = /usr/bin\nversion = 3.9.20\n", encoding="utf-8")
+
+ assert venv_python_version(venv) == (3, 9)
+ if os.name != "nt":
+ # The contract: the composed path is the venv's real tree. Dated from the
+ # caller instead, it names python<this-interpreter> and misses this dir.
+ assert site_packages(venv) == venv / "lib" / "python3.9" / "site-packages"
+ assert site_packages(venv).is_dir()
+
+
+def test_venv_python_version_falls_back_to_the_lib_directory(tmp_path):
+ venv = tmp_path / "venv"
+ (venv / "lib" / "python3.12" / "site-packages").mkdir(parents=True)
+
+ assert venv_python_version(venv) == (3, 12)
+
+
+def test_venv_python_version_is_none_without_evidence(tmp_path):
+ assert venv_python_version(tmp_path / "absent") is None