Skip to content

The agent that grows with you

fix(installer): preserve managed Python fallback version

fangliquanflqa2895e996876
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 28f6fa162fa..74da942c729 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -387,9 +387,9 @@ $RepoUrlSsh = "[email protected]:NousResearch/hermes-agent.git"
$RepoUrlHttps = "https://github.com/NousResearch/hermes-agent.git"
$PythonVersion = "3.11"
# Minor versions the installer accepts when the requested $PythonVersion isn't
-# available, in preference order. uv discovers both uv-managed and system
-# interpreters, so this list also matches a pre-existing system Python. Single
-# source of truth shared by Test-Python's fallback and Resolve-AvailablePythonVersion.
+# available, in preference order. Only checkout-private uv-managed interpreters
+# are eligible. Single source of truth shared by Test-Python's fallback and
+# Resolve-AvailablePythonVersion.
$PythonFallbackVersions = @("3.12", "3.13", "3.10")
$NodeVersion = "22"
# The npm range the root package.json pins in `engines.npm`. A constant rather
@@ -1225,6 +1225,7 @@ function Resolve-AvailablePythonVersion {
if ($foundPath) {
$absolute = [System.IO.Path]::GetFullPath($foundPath)
if ($absolute.StartsWith($managedPrefix, [System.StringComparison]::OrdinalIgnoreCase)) {
+ $script:PythonVersion = $ver
return $absolute
}
}
@@ -1303,7 +1304,6 @@ function Test-Python {
$ErrorActionPreference = $previousFallbackEAP
$pythonPath = Resolve-AvailablePythonVersion
if ($pythonPath) {
- $script:PythonVersion = $fallbackVer
$ver = & $pythonPath --version 2>$null
Write-Success "Python fallback installed: $ver"
return $true
diff --git a/tests/install_ps1_fake_uv.py b/tests/install_ps1_fake_uv.py
index 10df5ba345e..dfd2cf433b5 100644
--- a/tests/install_ps1_fake_uv.py
+++ b/tests/install_ps1_fake_uv.py
@@ -13,11 +13,24 @@ using System.Linq;
public static class FakeUv {
public static int Main(string[] args) {
+ if (Path.GetFileName(Environment.GetCommandLineArgs()[0]).Equals(
+ "python.exe", StringComparison.OrdinalIgnoreCase)) {
+ Console.WriteLine(Environment.GetEnvironmentVariable("FAKE_PYTHON_VERSION")
+ ?? "Python 3.11.0");
+ return 0;
+ }
+
File.AppendAllText(Environment.GetEnvironmentVariable("FAKE_UV_LOG"),
string.Join(" ", args) + Environment.NewLine);
if (args.Length >= 2 && args[0] == "python" && args[1] == "find") {
bool managed = args.Contains("--managed-python");
+ string availableVersion = Environment.GetEnvironmentVariable(
+ "FAKE_MANAGED_PYTHON_VERSION");
+ if (managed && !string.IsNullOrEmpty(availableVersion)
+ && (args.Length < 3 || args[2] != availableVersion)) {
+ return 1;
+ }
Console.WriteLine(Environment.GetEnvironmentVariable(
managed ? "FAKE_MANAGED_PYTHON" : "FAKE_THIRD_PARTY_PYTHON"));
return 0;
@@ -26,6 +39,10 @@ public static class FakeUv {
return 0;
}
if (args.Length >= 2 && args[0] == "venv" && args[1] == "venv") {
+ string forcedExit = Environment.GetEnvironmentVariable("FAKE_UV_VENV_EXIT");
+ if (!string.IsNullOrEmpty(forcedExit)) {
+ return int.Parse(forcedExit);
+ }
string managedPython = Environment.GetEnvironmentVariable("FAKE_MANAGED_PYTHON");
int pythonAt = Array.IndexOf(args, "--python");
bool correctPython = pythonAt >= 0 && pythonAt + 1 < args.Length
@@ -37,7 +54,7 @@ public static class FakeUv {
}
string scripts = Path.Combine(Environment.CurrentDirectory, "venv", "Scripts");
Directory.CreateDirectory(scripts);
- File.WriteAllText(Path.Combine(scripts, "python.exe"), "fake");
+ File.Copy(managedPython, Path.Combine(scripts, "python.exe"), true);
return 0;
}
return 2;
diff --git a/tests/test_install_ps1_managed_python_provenance.py b/tests/test_install_ps1_managed_python_provenance.py
index e0ed994b33d..6f1d62eee05 100644
--- a/tests/test_install_ps1_managed_python_provenance.py
+++ b/tests/test_install_ps1_managed_python_provenance.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import json
+import os
from pathlib import Path
import shutil
import subprocess
@@ -17,6 +18,36 @@ pytestmark = pytest.mark.windows_only
_INSTALL_PS1 = Path(__file__).resolve().parents[1] / "scripts" / "install.ps1"
+def _run_venv_stage(
+ powershell: str,
+ tmp_path: Path,
+ hermes_home: Path,
+ install_dir: Path,
+ env: dict[str, str],
+) -> subprocess.CompletedProcess[str]:
+ return subprocess.run(
+ [
+ powershell,
+ "-NoProfile",
+ "-ExecutionPolicy",
+ "Bypass",
+ "-File",
+ str(_INSTALL_PS1),
+ "-Stage",
+ "venv",
+ "-HermesHome",
+ str(hermes_home),
+ "-InstallDir",
+ str(install_dir),
+ ],
+ cwd=tmp_path,
+ env=env,
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+
+
def test_venv_stage_rejects_third_party_python_and_uses_managed_path(
tmp_path: Path,
) -> None:
@@ -34,13 +65,13 @@ def test_venv_stage_rejects_third_party_python_and_uses_managed_path(
uv.parent.mkdir(parents=True)
install_dir.mkdir()
managed_python.parent.mkdir(parents=True)
- managed_python.write_text("fake", encoding="ascii")
third_party.parent.mkdir(parents=True)
third_party.write_text("fake", encoding="ascii")
compile_fake_uv(powershell, uv)
+ shutil.copy2(uv, managed_python)
env = {
- **dict(__import__("os").environ),
+ **os.environ,
"FAKE_UV_LOG": str(log),
"FAKE_MANAGED_PYTHON": str(managed_python),
"FAKE_THIRD_PARTY_PYTHON": str(third_party),
@@ -48,36 +79,9 @@ def test_venv_stage_rejects_third_party_python_and_uses_managed_path(
"UV_NO_MANAGED_PYTHON": "1",
"UV_SYSTEM_PYTHON": "1",
}
- stdout_path = tmp_path / "installer.stdout"
- stderr_path = tmp_path / "installer.stderr"
- with stdout_path.open("w", encoding="utf-8") as stdout, stderr_path.open(
- "w", encoding="utf-8"
- ) as stderr:
- run = subprocess.run(
- [
- powershell,
- "-NoProfile",
- "-ExecutionPolicy",
- "Bypass",
- "-File",
- str(_INSTALL_PS1),
- "-Stage",
- "venv",
- "-HermesHome",
- str(hermes_home),
- "-InstallDir",
- str(install_dir),
- ],
- cwd=tmp_path,
- env=env,
- stdout=stdout,
- stderr=stderr,
- text=True,
- check=False,
- )
-
- installer_stdout = stdout_path.read_text(encoding="utf-8")
- installer_stderr = stderr_path.read_text(encoding="utf-8")
+ run = _run_venv_stage(powershell, tmp_path, hermes_home, install_dir, env)
+ installer_stdout = run.stdout
+ installer_stderr = run.stderr
frames = [
json.loads(line) for line in installer_stdout.splitlines() if line.startswith("{")
]
@@ -92,3 +96,82 @@ def test_venv_stage_rejects_third_party_python_and_uses_managed_path(
assert f"--python {managed_python}" in venv_command
assert "--managed-python" in venv_command
assert "--no-python-downloads" in venv_command
+
+
+def test_fallback_minor_is_reported_from_resolved_managed_interpreter(
+ tmp_path: Path,
+) -> None:
+ powershell = shutil.which("powershell")
+ if not powershell:
+ pytest.skip("Windows PowerShell is required")
+
+ hermes_home = tmp_path / "hermes-home"
+ install_dir = tmp_path / "install"
+ managed_python = (
+ install_dir / ".hermes-runtime" / "python" / "cpython-3.12" / "python.exe"
+ )
+ uv = hermes_home / "bin" / "uv.exe"
+ uv.parent.mkdir(parents=True)
+ managed_python.parent.mkdir(parents=True)
+ install_dir.mkdir(exist_ok=True)
+ compile_fake_uv(powershell, uv)
+ shutil.copy2(uv, managed_python)
+ env = {
+ **os.environ,
+ "FAKE_UV_LOG": str(tmp_path / "uv.log"),
+ "FAKE_MANAGED_PYTHON": str(managed_python),
+ "FAKE_MANAGED_PYTHON_VERSION": "3.12",
+ "FAKE_PYTHON_VERSION": "Python 3.12.13",
+ }
+
+ run = _run_venv_stage(powershell, tmp_path, hermes_home, install_dir, env)
+
+ assert run.returncode == 0, run.stdout + run.stderr
+ assert "Creating virtual environment with Python 3.12" in run.stdout
+ assert "Virtual environment ready (Python 3.12)" in run.stdout
+ created_python = install_dir / "venv" / "Scripts" / "python.exe"
+ version = subprocess.run(
+ [str(created_python), "--version"],
+ env=env,
+ capture_output=True,
+ text=True,
+ check=True,
+ )
+ assert version.stdout.strip() == "Python 3.12.13"
+
+
+def test_venv_failure_fails_stage_and_restores_existing_environment(
+ tmp_path: Path,
+) -> None:
+ powershell = shutil.which("powershell")
+ if not powershell:
+ pytest.skip("Windows PowerShell is required")
+
+ hermes_home = tmp_path / "hermes-home"
+ install_dir = tmp_path / "install"
+ managed_python = (
+ install_dir / ".hermes-runtime" / "python" / "cpython-3.11" / "python.exe"
+ )
+ old_python = install_dir / "venv" / "Scripts" / "python.exe"
+ uv = hermes_home / "bin" / "uv.exe"
+ for directory in (uv.parent, managed_python.parent, old_python.parent):
+ directory.mkdir(parents=True, exist_ok=True)
+ old_python.write_text("previous environment", encoding="ascii")
+ compile_fake_uv(powershell, uv)
+ shutil.copy2(uv, managed_python)
+ env = {
+ **os.environ,
+ "FAKE_UV_LOG": str(tmp_path / "uv.log"),
+ "FAKE_MANAGED_PYTHON": str(managed_python),
+ "FAKE_UV_VENV_EXIT": "37",
+ }
+
+ run = _run_venv_stage(powershell, tmp_path, hermes_home, install_dir, env)
+
+ assert run.returncode != 0
+ assert "Failed to create virtual environment (uv venv exited with 37)" in (
+ run.stdout + run.stderr
+ )
+ assert old_python.read_text(encoding="ascii") == "previous environment"
+ frames = [json.loads(line) for line in run.stdout.splitlines() if line.startswith("{")]
+ assert frames[-1]["ok"] is False
diff --git a/tests/test_install_ps1_native_stderr_eap.py b/tests/test_install_ps1_native_stderr_eap.py
index b0c44faf222..6afcd0cb878 100644
--- a/tests/test_install_ps1_native_stderr_eap.py
+++ b/tests/test_install_ps1_native_stderr_eap.py
@@ -53,20 +53,6 @@ def test_dependency_installs_relax_eap() -> None:
_assert_relaxed_call(text, r"& \$UvCmd pip install -e \$tier\.Spec")
-def test_uv_venv_failure_is_not_swallowed() -> None:
- """The redirected uv process exit code must gate venv-stage success."""
- text = _install_ps1()
- guard = re.search(
- r"\$venvExitCode\s*=\s*\$venvProcess\.ExitCode[\s\S]{0,400}?"
- r"-ne 0[\s\S]{0,200}?throw",
- text,
- )
- assert guard is not None, (
- "install.ps1 must capture the redirected uv venv process exit code and "
- "throw so a genuine creation failure is not reported as success"
- )
-
-
def test_native_eap_helper_always_restores_previous_preference() -> None:
text = _install_ps1()
m = re.search(
diff --git a/tests/test_install_ps1_venv_recreate_safety.py b/tests/test_install_ps1_venv_recreate_safety.py
index 0ff9bf66cdc..934a97fae08 100644
--- a/tests/test_install_ps1_venv_recreate_safety.py
+++ b/tests/test_install_ps1_venv_recreate_safety.py
@@ -43,16 +43,6 @@ def test_manual_recovery_hints_do_not_delete_live_venv() -> None:
assert "Do not delete venv in place" in source
-def test_previous_venv_survives_until_replacement_is_ready() -> None:
- body = _install_venv_body()
-
- create = body.index("$venvProcess.Start()")
- stale_cleanup = body.index('Get-ChildItem -Directory -Filter "venv.stale.*"')
- assert create < stale_cleanup, (
- "stale backups must not be cleaned before uv can succeed or rollback"
- )
-
-
def test_recreate_restores_parked_venv_after_failure() -> None:
body = _install_venv_body()