Skip to content

The agent that grows with you

fix(update): scrub interpreter-steering env keys from the critical-module import probe (#115032)

Konstantin Khlopkov8abfd2811844
diff --git a/hermes_cli/update_cmd_deps.py b/hermes_cli/update_cmd_deps.py
index 87782607762..aa366f0782c 100644
--- a/hermes_cli/update_cmd_deps.py
+++ b/hermes_cli/update_cmd_deps.py
@@ -42,6 +42,17 @@ def _editable_install_is_current(git_cmd, cwd, pre_pull_sha: str | None) -> bool
# *imported*, catching cross-module breakage (a name pulled from a sibling no longer exists).
_UPDATE_CRITICAL_MODULES = "hermes_cli.main", "run_agent", "model_tools", "toolsets"
+# Env keys stripped from the import-health probe child: they steer the interpreter at a
+# different tree, so an inherited PYTHONPATH pointing at an older checkout satisfies the
+# probe's imports from the stale copy and blesses a candidate missing the module entirely
+# (#115032). Same tuple as the staged-binary probe in macos_tcc_anchor.
+_PROBE_ENV_DENYLIST = (
+ "PYTHONPATH",
+ "PYTHONHOME",
+ "PYTHONSTARTUP",
+ "__PYVENV_LAUNCHER__",
+)
+
def _critical_module_import_failures(
root, *, report_runtime_errors: bool = False) -> dict[str, tuple[str, str]]:
@@ -89,8 +100,14 @@ def _critical_module_import_failures(
venv_python = venv_python_path(venv_dir, windows=_m()._is_windows())
if venv_python.exists():
interpreter = str(venv_python)
+ # The candidate stays importable through the probe's cwd and its editable install;
+ # the scrub only removes paths the guard never meant to vouch for.
+ probe_env = dict(os.environ)
+ for denied_key in _PROBE_ENV_DENYLIST:
+ probe_env.pop(denied_key, None)
result = bounded_probe_run(
[interpreter, "-c", probe], timeout=120, cwd=str(root), raise_on_spawn_failure=True,
+ env=probe_env,
)
except (OSError, subprocess.SubprocessError):
# Keep this guard advisory: a probe we could not even spawn (unreadable venv
diff --git a/tests/hermes_cli/test_update_import_guard.py b/tests/hermes_cli/test_update_import_guard.py
index 235a1f1e366..699804ef11a 100644
--- a/tests/hermes_cli/test_update_import_guard.py
+++ b/tests/hermes_cli/test_update_import_guard.py
@@ -339,6 +339,54 @@ def test_import_guard_ignores_missing_third_party_dependency(monkeypatch, tmp_pa
assert update_cmd._validate_critical_modules_import(tmp_path) == (True, None, None)
+def test_import_guard_rejects_module_satisfied_only_by_inherited_pythonpath(
+ monkeypatch, tmp_path
+):
+ """A stale checkout on PYTHONPATH must not stand in for a candidate module.
+
+ The probe child used to inherit the updater's environment wholesale, so
+ with PYTHONPATH pointing at an older tree the critical module imported
+ fine from there and a candidate lacking it entirely read as healthy
+ (#115032).
+ """
+ stale = tmp_path / "stale"
+ stale.mkdir()
+ (stale / "hermes_stale_supply.py").write_text("VALUE = 'from the stale tree'\n")
+
+ monkeypatch.setattr(
+ update_cmd, "_UPDATE_CRITICAL_MODULES", ("hermes_stale_supply",)
+ )
+ monkeypatch.setattr(
+ update_cmd_deps, "_UPDATE_CRITICAL_MODULES", ("hermes_stale_supply",)
+ )
+ monkeypatch.setenv("PYTHONPATH", str(stale))
+
+ ok, module, error = update_cmd._validate_critical_modules_import(tmp_path)
+
+ assert ok is False
+ assert module == "hermes_stale_supply"
+ assert error is not None and "hermes_stale_supply" in error
+
+
+def test_import_guard_accepts_candidate_with_foreign_pythonpath(monkeypatch, tmp_path):
+ """The env scrub must not overreach: a candidate that carries the module
+ still passes while a foreign PYTHONPATH is set (#115032)."""
+ stale = tmp_path / "stale"
+ stale.mkdir()
+ (stale / "hermes_stale_supply.py").write_text("VALUE = 'stale'\n")
+ (tmp_path / "hermes_stale_supply.py").write_text("VALUE = 'candidate'\n")
+
+ monkeypatch.setattr(
+ update_cmd, "_UPDATE_CRITICAL_MODULES", ("hermes_stale_supply",)
+ )
+ monkeypatch.setattr(
+ update_cmd_deps, "_UPDATE_CRITICAL_MODULES", ("hermes_stale_supply",)
+ )
+ monkeypatch.setenv("PYTHONPATH", str(stale))
+
+ assert update_cmd._validate_critical_modules_import(tmp_path) == (True, None, None)
+
+
def test_import_guard_flags_missing_first_party_module(monkeypatch, tmp_path):
"""A missing *first-party* module IS skew — the update dropped a file."""
(tmp_path / "tools").mkdir()