Skip to content

The agent that grows with you

fix(tools): sanitize inherited PYTHONHOME (#75018)

Xinyu Du850686a515b1
diff --git a/tests/tools/test_local_env_blocklist.py b/tests/tools/test_local_env_blocklist.py
index d970d3d9c85..570ad9e90d5 100644
--- a/tests/tools/test_local_env_blocklist.py
+++ b/tests/tools/test_local_env_blocklist.py
@@ -640,6 +640,63 @@ class TestPythonpathSelectiveStrip:
assert "/home/user/my-lib" in entries
+class TestPythonhomeSanitized:
+ """PYTHONHOME must not leak from the Hermes runtime into subprocesses.
+
+ The gateway inherits/sets PYTHONHOME in its process environment; a child
+ interpreter (system Python, another venv, cron no_agent scripts) that
+ inherits it redirects its stdlib search to the Hermes venv and crashes
+ with version-mismatch errors before importing anything (#75018).
+ """
+
+ def test_make_run_env_strips_pythonhome(self):
+ from tools.environments.local import _make_run_env
+ with patch.dict(os.environ, {
+ "PATH": "/usr/bin:/bin",
+ "HOME": "/home/user",
+ "PYTHONHOME": "/opt/hermes-venv",
+ }, clear=True):
+ run_env = _make_run_env({})
+ assert "PYTHONHOME" not in run_env
+
+ def test_sanitize_subprocess_env_strips_pythonhome(self):
+ from tools.environments.local import _sanitize_subprocess_env
+ result = _sanitize_subprocess_env({
+ "PATH": "/usr/bin",
+ "HOME": "/home/user",
+ "PYTHONHOME": "/opt/hermes-venv",
+ })
+ assert "PYTHONHOME" not in result
+
+ def test_hermes_subprocess_env_strips_pythonhome(self):
+ from tools.environments.local import hermes_subprocess_env
+ with patch.dict(os.environ, {
+ "PATH": "/usr/bin:/bin",
+ "HOME": "/home/user",
+ "PYTHONHOME": "/opt/hermes-venv",
+ }, clear=True):
+ result = hermes_subprocess_env()
+ assert "PYTHONHOME" not in result
+
+ def test_build_subprocess_env_strips_pythonhome(self):
+ """cron no_agent children go through build_subprocess_env; the
+ gateway's PYTHONHOME must not reach them (#75018)."""
+ from tools.environments.local import build_subprocess_env
+ with patch.dict(os.environ, {
+ "PATH": "/usr/bin:/bin",
+ "HOME": "/home/user",
+ "PYTHONHOME": "/opt/hermes-venv",
+ }, clear=True):
+ result = build_subprocess_env()
+ assert "PYTHONHOME" not in result
+
+ def test_pythonhome_removed_from_active_venv_markers(self):
+ """PYTHONHOME is part of _ACTIVE_VENV_MARKER_VARS so all builders
+ that iterate it drop the variable."""
+ from tools.environments.local import _ACTIVE_VENV_MARKER_VARS
+ assert "PYTHONHOME" in _ACTIVE_VENV_MARKER_VARS
+
+
class TestProfileScopedPassthrough:
def test_make_run_env_uses_active_profile_for_passthrough(self, monkeypatch):
"""Allowlisted values must come from the routed profile, not os.environ."""
diff --git a/tools/environments/local.py b/tools/environments/local.py
index e47834cd55b..04d91e0b8e4 100644
--- a/tools/environments/local.py
+++ b/tools/environments/local.py
@@ -347,10 +347,19 @@ _HERMES_PROVIDER_ENV_BLOCKLIST = _build_provider_env_blocklist()
# Hermes venv stays reachable via PATH (its bin dir is first), so stripping
# these markers is safe and only prevents the cross-project clobber (#23473).
#
+# PYTHONHOME is included because a gateway-inherited value redirects the
+# standard-library search of ANY child interpreter — including unrelated
+# system/venv Pythons — to the Hermes venv's stdlib, which crashes with
+# version-mismatch errors before a child script even imports a package
+# (#75018). Hermes itself treats PYTHONHOME as contamination in its own
+# child processes (managed_uv.py, sqlite_runtime.py), so stripping it from
+# subprocess envs is consistent. Users who need PYTHONHOME for a specific
+# child can set it explicitly in the command.
+#
# PYTHONPATH is NOT included here — it's handled by
-# _strip_mismatched_site_packages() which surgically removes only site-packages
-# paths that don't match the current Python ABI, preserving user-set entries.
-_ACTIVE_VENV_MARKER_VARS = ("VIRTUAL_ENV", "CONDA_PREFIX")
+# _strip_mismatched_site_packages() which removes only Hermes-owned entries,
+# preserving user-set paths.
+_ACTIVE_VENV_MARKER_VARS = ("VIRTUAL_ENV", "CONDA_PREFIX", "PYTHONHOME")
def _is_hermes_internal_secret(key: str) -> bool: