Skip to content

The agent that grows with you

fix(update): pin uv installs to the running interpreter when VIRTUAL_ENV is stale

mrmixx-max13f9d18e33cc
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index 90d6cd54c42..b0251a3b908 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -9581,10 +9581,40 @@ def _install_python_dependencies_with_optional_fallback(
in the venv Scripts dir before each install attempt so uv can write fresh
copies (Windows blocks REPLACE on a running .exe but allows RENAME). See
``_quarantine_running_hermes_exe`` for the rationale.
+
+ When ``env`` carries a ``VIRTUAL_ENV`` that does not exist (a pip /
+ site-packages install whose ``PROJECT_ROOT`` is the interpreter's
+ ``site-packages`` directory, where ``PROJECT_ROOT / "venv"`` is never
+ created), ``uv pip`` fails with ``Failed to inspect Python interpreter from
+ active virtual environment`` before doing any work. Pin the install at the
+ running interpreter instead so the update/recovery path succeeds on those
+ installs (#71510 fixed the ZIP path, #83335 fixed lazy-deps; this closes the
+ shared helper for the remaining callers).
"""
scripts_dir = _venv_scripts_dir() if _is_windows() else None
+ # A pip / site-packages install has no PROJECT_ROOT/venv; the caller still
+ # passes VIRTUAL_ENV=PROJECT_ROOT/venv, which does not exist. uv would fail
+ # before installing anything ("Failed to inspect Python interpreter from
+ # active virtual environment"). Detect the stale pointer and pin the target
+ # interpreter explicitly instead of trusting the nonexistent venv.
+ pin_python = False
+ if (
+ env
+ and env.get("VIRTUAL_ENV")
+ and not Path(env["VIRTUAL_ENV"]).is_dir()
+ and install_cmd_prefix
+ and "uv" in Path(install_cmd_prefix[0]).name.lower()
+ ):
+ # Only uv needs the explicit pin; pip resolves the target from
+ # sys.executable itself and has no --python flag.
+ pin_python = True
+ env = {**env}
+ env.pop("VIRTUAL_ENV", None)
+
def _install(args: list[str]) -> None:
+ if pin_python:
+ args = [args[0], "--python", str(sys.executable), *args[1:]]
# strict_quarantine: this is the UPDATE dependency sync. A shim that
# cannot be renamed aside proves a hard venv hold; running uv anyway
# is how installs strand half-updated (#87331). ShimQuarantineError
diff --git a/tests/hermes_cli/test_update_stale_virtualenv.py b/tests/hermes_cli/test_update_stale_virtualenv.py
new file mode 100644
index 00000000000..90c56cc1cad
--- /dev/null
+++ b/tests/hermes_cli/test_update_stale_virtualenv.py
@@ -0,0 +1,76 @@
+"""Test: _install_python_dependencies_with_optional_fallback mit stale VIRTUAL_ENV.
+
+Simuliert den heutigen Crash: pip/System-Python-Install, wo PROJECT_ROOT =
+site-packages ist und VIRTUAL_ENV=PROJECT_ROOT/venv nicht existiert.
+"""
+import os
+import sys
+import types
+import unittest
+from pathlib import Path
+from unittest import mock
+
+import hermes_cli.main as main_mod
+
+
+class StaleVirtualEnvTest(unittest.TestCase):
+ def _call(self, uv_bin, venv_path, fake_executable):
+ """Führe die Funktion mit gemocktem uv/env aus und fange den subprocess-Aufruf."""
+ captured = []
+
+ real_run_quarantined = main_mod._run_quarantined_install
+
+ def fake_quarantine(cmd, *, env=None, scripts_dir=None):
+ captured.append((list(cmd), dict(env or {})))
+ # Nicht wirklich installieren; Verhalten wie Erfolg simulieren.
+ return None
+
+ def fake_verify(prefix, *, env=None):
+ return None
+
+ with mock.patch.object(main_mod, "_run_quarantined_install", fake_quarantine), \
+ mock.patch.object(main_mod, "_verify_console_scripts_installed", fake_verify), \
+ mock.patch.object(main_mod, "_venv_scripts_dir", return_value=None), \
+ mock.patch.object(main_mod, "_is_windows", return_value=False), \
+ mock.patch.object(main_mod.sys, "executable", fake_executable), \
+ mock.patch.object(main_mod, "PROJECT_ROOT", Path("/fake/project")):
+ main_mod._install_python_dependencies_with_optional_fallback(
+ [str(uv_bin), "pip"],
+ env={"VIRTUAL_ENV": str(venv_path)},
+ group="all",
+ )
+ return captured
+
+ def test_stale_virtualenv_pins_python(self):
+ """VIRTUAL_ENV zeigt auf nicht-existierendes venv -> --python sys.executable."""
+ captured = self._call(
+ uv_bin=Path("/fake/uv"),
+ venv_path=Path("/fake/project/venv"), # existiert nicht
+ fake_executable="/fake/python311/python.exe",
+ )
+ self.assertTrue(captured, "kein subprocess-Aufruf erfasst")
+ cmd, env = captured[0]
+ # --python muss nach 'install' stehen: uv pip install --python <exe> ...
+ self.assertIn("install", cmd)
+ self.assertIn("--python", cmd)
+ self.assertEqual(cmd[cmd.index("--python") + 1], "/fake/python311/python.exe")
+ # VIRTUAL_ENV aus env entfernt
+ self.assertNotIn("VIRTUAL_ENV", env)
+
+ def test_existing_virtualenv_keeps_env(self):
+ """VIRTUAL_ENV zeigt auf existierendes venv -> unverändert, kein --python."""
+ real_venv = Path(sys.executable).resolve().parent.parent
+ if not real_venv.is_dir():
+ self.skipTest("kein echtes venv im Testlauf verfügbar")
+ captured = self._call(
+ uv_bin=Path("/fake/uv"),
+ venv_path=real_venv,
+ fake_executable=sys.executable,
+ )
+ cmd, env = captured[0]
+ self.assertNotIn("--python", cmd)
+ self.assertEqual(env.get("VIRTUAL_ENV"), str(real_venv))
+
+
+if __name__ == "__main__":
+ unittest.main(verbosity=2)