scheduler_worker_env.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Cron: import path of the restart-safe external worker.
  2. The worker is spawned as ``sys.executable -m cron.scheduler``. Its entry module is
  3. ``cron.scheduler``, not ``hermes_cli.main``, so nothing bootstraps the gateway's checkout
  4. onto its ``sys.path``; historically it imported ``cron`` only through the implicit ``-m``
  5. cwd entry. That entry is gone under ``PYTHONSAFEPATH`` and useless when the venv's
  6. editable install maps a moved/deleted checkout -- the worker then dies with
  7. "No module named 'cron'" before its ownership ack (#112729, hypothesised cause).
  8. The shared subprocess sanitizer strips Hermes-owned PYTHONPATH entries because user
  9. children must not see our tree. This child IS Hermes, so the pin is applied *after* the
  10. env is built, on the sanitized env. On a self-managed (shell-installer / PM) install the
  11. sanitizer's drop of the runtime site-packages cannot stand this time: the worker inherits
  12. this process's interpreter, which is PM's store Python and owns no third-party
  13. dependencies, so the committed generation's site-packages is restored here too or the
  14. child dies at its first dependency import (``No module named 'ruamel'``, #122222) before
  15. it can publish its ownership acknowledgement.
  16. """
  17. from __future__ import annotations
  18. import logging
  19. import os
  20. import sysconfig
  21. from pathlib import Path
  22. logger = logging.getLogger(__name__)
  23. def _installed_purelib() -> Path | None:
  24. try:
  25. return Path(sysconfig.get_paths()["purelib"]).resolve()
  26. except (KeyError, OSError):
  27. return None
  28. def _committed_dependency_site_packages(project_root: Path) -> Path | None:
  29. """The dependency ``site-packages`` PM committed for this install, or ``None``.
  30. Asked of PM's own committed selection -- the record ``activate_dependencies`` resolves
  31. at process boot -- rather than re-derived from this process's ``sys.path``: the record
  32. belongs to this install (``install_state_dir``, shared by every profile it serves). A
  33. runner that owns its dependencies (wheel / pipx / developer venv / Nix: no committed
  34. generation) has nothing to restore, so ``None`` means "pin the tree only" and nothing
  35. is invented.
  36. """
  37. try:
  38. from pm.environments import committed_venv, site_packages
  39. environment = committed_venv(project_root)
  40. except Exception as exc:
  41. # An unreadable record: pin the tree only. The cron worker's own boot re-reads it and
  42. # fails the dispatch with PM's error.
  43. logger.warning(
  44. "cron worker: could not read the committed dependency environment: %s", exc
  45. )
  46. return None
  47. if environment is None:
  48. return None
  49. selected = site_packages(environment)
  50. return selected if selected.is_dir() else None
  51. def pin_hermes_tree_on_pythonpath(worker_env: dict, repo_root: Path) -> dict:
  52. """Prepend ``repo_root`` -- and, when this runner has one, the committed dependency
  53. generation's ``site-packages`` -- to the worker env's own PYTHONPATH (never
  54. ``os.environ``'s).
  55. Skipped when ``repo_root`` is the interpreter's ``purelib``: under a wheel / pipx /
  56. uv-tool install ``cron/`` lives in site-packages itself, which is already importable,
  57. and pinning it would move site-packages ahead of the stdlib on ``sys.path``.
  58. Order (checkout, generation, sanitizer-kept) mirrors ``activate_dependencies``' own
  59. ``sys.path``. For the cron worker, its boot (``cron/worker_bootstrap.py``) then re-selects
  60. and leases the committed generation before any third-party import, and exits the worker
  61. if it cannot.
  62. """
  63. root = str(repo_root)
  64. if _installed_purelib() == Path(root).resolve():
  65. return worker_env
  66. existing = [e for e in worker_env.get("PYTHONPATH", "").split(os.pathsep) if e]
  67. dependency = _committed_dependency_site_packages(Path(root))
  68. pinned = [root, *([str(dependency)] if dependency is not None else [])]
  69. worker_env["PYTHONPATH"] = os.pathsep.join(dict.fromkeys([*pinned, *existing]))
  70. return worker_env