local_pythonpath.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """Hermes-owned PYTHONPATH stripping for child processes. Launchers prepend the repo
  2. root and the Hermes venv's site-packages so the backend can ``import tools``; leaked
  3. into a child Python of a DIFFERENT version they load the backend's C extensions and
  4. crash. Only entries proven Hermes-owned by *path provenance* are removed — never by a
  5. cross-version heuristic. Module state (``_hermes_repo_root_aliases``, ``_in_venv``,
  6. ``_hermes_site_packages``) lives in ``tools.environments.local`` (via :func:`_state`)
  7. so tests monkeypatching it there keep working."""
  8. import logging
  9. import os
  10. import platform
  11. import sys
  12. from pathlib import Path
  13. from tools.environments.local_env_policy import _ACTIVE_VENV_MARKER_VARS
  14. _IS_WINDOWS = platform.system() == "Windows"
  15. logger = logging.getLogger("tools.environments.local")
  16. def _state():
  17. """Return the ``tools.environments.local`` module (owner of the caches)."""
  18. from tools.environments import local
  19. return local
  20. def _same_path(left: Path, right: Path) -> bool:
  21. """Compare path spellings with host filesystem case semantics."""
  22. return [os.path.normcase(p) for p in left.parts] == [os.path.normcase(p) for p in right.parts]
  23. def _build_hermes_repo_root_aliases(
  24. resolved_root: Path, lexical_root: Path, configured_home: Path,
  25. ) -> tuple[Path, ...]:
  26. """Exact repo-root spellings emitted by Hermes launchers. Mirrors
  27. ``gateway_windows._preserve_hermes_home_path`` (physical path under the resolved
  28. HERMES_HOME -> configured spelling) so a junction-backed install matches without
  29. treating arbitrary HERMES_HOME descendants as Hermes-owned. A repo-level junction
  30. (possibly cross-drive) is accepted only when a strict resolve proves
  31. <root>/<repo dirname> is the physical root (fail-closed)."""
  32. candidates = [resolved_root, lexical_root]
  33. # Profile re-home: with --profile the configured home is <root>/profiles/<name>
  34. # and the repo lives beside the profiles dir (as get_default_hermes_root() does).
  35. home_candidates = [configured_home]
  36. if configured_home.parent.name == "profiles":
  37. home_candidates.append(configured_home.parent.parent)
  38. for home in home_candidates:
  39. try:
  40. resolved_home = home.resolve()
  41. home_key = os.path.normcase(str(resolved_home))
  42. if os.path.commonpath([home_key, os.path.normcase(str(resolved_root))]) == home_key:
  43. candidates.append(home / os.path.relpath(str(resolved_root), str(resolved_home)))
  44. except (OSError, ValueError):
  45. pass
  46. # Repo-level junction recovery (commonpath raises across drives, so the
  47. # home-relative mapping above cannot express a cross-drive link).
  48. for home in home_candidates:
  49. repo_candidate = home / resolved_root.name
  50. try:
  51. if repo_candidate.resolve(strict=True) == resolved_root.resolve(strict=True):
  52. candidates.append(repo_candidate)
  53. except OSError:
  54. pass
  55. aliases: list[Path] = []
  56. for candidate in candidates:
  57. if not any(_same_path(candidate, existing) for existing in aliases):
  58. aliases.append(candidate)
  59. return tuple(aliases)
  60. def _validated_runtime_venv(env: dict) -> Path | None:
  61. """Producer-owned runtime venv, or None: PM's committed generation, else a sealed
  62. payload's shipped venv (its interpreter is not a venv, so ``_in_venv`` misses it),
  63. else VIRTUAL_ENV. The variable alone is not provenance (users carry unrelated
  64. venvs): require the legacy Windows base-Python producer's exact ``<repo>/venv``
  65. layout AND a real ``pyvenv.cfg``."""
  66. from pm.environments import payload_venv, runtime_facts_path, selected_venv
  67. root = Path(__file__).resolve().parents[2]
  68. if runtime_facts_path(root).is_file():
  69. return selected_venv(root)
  70. try:
  71. shipped = payload_venv(root)
  72. except (RuntimeError, OSError): # a manifest that escapes its root reads as "not a payload"
  73. shipped = None
  74. # Require the real venv, as the VIRTUAL_ENV branch does: a stray manifest.json beside a
  75. # checkout must not hand back a phantom path that shadows the user's own venv.
  76. if shipped is not None and (shipped / "pyvenv.cfg").is_file():
  77. return shipped
  78. candidate = Path(env.get("VIRTUAL_ENV") or "")
  79. if not env.get("VIRTUAL_ENV") or not any(
  80. _same_path(candidate, root / "venv") for root in _state()._hermes_repo_root_aliases):
  81. return None
  82. try:
  83. return candidate if (candidate / "pyvenv.cfg").is_file() else None
  84. except OSError:
  85. return None
  86. def _get_hermes_site_packages(env: dict) -> list[Path]:
  87. """Exact site-packages dirs owned by the Hermes runtime (cached):
  88. ``site.getsitepackages()`` with a ``sys.prefix`` fallback, plus a validated
  89. Windows base-interpreter launch's ``VIRTUAL_ENV/Lib/site-packages``."""
  90. local = _state()
  91. if local._hermes_site_packages is None:
  92. result: list[Path] = []
  93. if local._in_venv:
  94. try:
  95. import site
  96. result.extend(Path(sp) for sp in site.getsitepackages())
  97. except Exception:
  98. pass
  99. if not result:
  100. pyver = f"python{sys.version_info[0]}.{sys.version_info[1]}"
  101. result.append(Path(sys.prefix) / "Lib" / "site-packages" if _IS_WINDOWS
  102. else Path(sys.prefix) / "lib" / pyver / "site-packages")
  103. local._hermes_site_packages = list(result)
  104. result = list(local._hermes_site_packages)
  105. runtime_venv = _validated_runtime_venv(env)
  106. if runtime_venv is not None:
  107. from pm.environments import site_packages
  108. runtime_site_packages = site_packages(runtime_venv)
  109. if not any(_same_path(runtime_site_packages, existing) for existing in result):
  110. result.append(runtime_site_packages)
  111. return result
  112. def _strip_hermes_owned_pythonpath_and_runtime_markers(env: dict) -> None:
  113. """Strip Hermes-owned PYTHONPATH entries, then the runtime marker vars. Order is
  114. load-bearing: PYTHONPATH filtering runs BEFORE the markers go so a validated Windows
  115. base-interpreter launch (VIRTUAL_ENV -> <repo>/venv) can still prove ownership."""
  116. _strip_hermes_owned_pythonpath(env)
  117. for _marker in _ACTIVE_VENV_MARKER_VARS:
  118. env.pop(_marker, None)
  119. def _strip_hermes_owned_pythonpath(env: dict) -> None:
  120. """Remove Hermes-owned PYTHONPATH entries: only exact matches of the repo root
  121. (any launcher spelling) and runtime site-packages — never descendants, which are
  122. user paths. Empty components (= cwd) and everything else are preserved.
  123. Everything else -- user libs, Nix plugin paths, a pythonX.Y/site-packages entry meant for a DIFFERENT
  124. child version -- is preserved byte-for-byte: ownership is decided by path provenance, never by a
  125. cross-version heuristic (#74817 follow-up).
  126. """
  127. pp = env.get("PYTHONPATH")
  128. if not pp:
  129. return
  130. owned_paths = [*_get_hermes_site_packages(env), *_state()._hermes_repo_root_aliases]
  131. entries = pp.split(os.pathsep)
  132. stripped = [e for e in entries if e and any(_same_path(Path(e), p) for p in owned_paths)]
  133. kept = [e for e in entries if e not in stripped]
  134. if kept:
  135. env["PYTHONPATH"] = os.pathsep.join(kept)
  136. else:
  137. env.pop("PYTHONPATH", None)
  138. if stripped:
  139. logger.debug("Stripped Hermes-owned entries from PYTHONPATH: %s", stripped)