Skip to content

The agent that grows with you

fix(cron): pin the worker's PYTHONPATH from the sanitized env in a sibling helper; skip under a wheel install

teknium1d4dfbba485ab
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 50d567b4e02..e64e86fe49c 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -3232,17 +3232,11 @@ def _launch_external_cron_worker(job: dict) -> bool:
"HERMES_EXEC_ASK",
):
worker_env.pop(_presence_var, None)
- # The worker's entry module is `cron.scheduler`, not `hermes_cli.main`, so nothing
- # bootstraps this checkout onto its sys.path the way the gateway got it: a venv whose
- # editable install maps a moved checkout, or a host that sets PYTHONSAFEPATH (`-m` then
- # ignores cwd), dies with "No module named 'cron'" before the ack (#112729). The shared
- # sanitizer strips Hermes-owned PYTHONPATH entries because user children must not see
- # our tree; this child IS Hermes, so it gets the gateway's own tree back explicitly.
+ # `-m cron.scheduler` has no hermes_cli.main bootstrap; pin this checkout explicitly
+ # (PYTHONSAFEPATH / stale editable mapping, #112729). See cron/scheduler_worker_env.py.
+ from cron.scheduler_worker_env import pin_hermes_tree_on_pythonpath
repo_root = Path(__file__).resolve().parent.parent
- worker_env["PYTHONPATH"] = os.pathsep.join(dict.fromkeys([
- str(repo_root),
- *filter(None, os.environ.get("PYTHONPATH", "").split(os.pathsep)),
- ]))
+ worker_env = pin_hermes_tree_on_pythonpath(worker_env, repo_root)
try:
stderr_fd = os.open(stderr_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
try:
diff --git a/cron/scheduler_worker_env.py b/cron/scheduler_worker_env.py
new file mode 100644
index 00000000000..ef13355fcac
--- /dev/null
+++ b/cron/scheduler_worker_env.py
@@ -0,0 +1,42 @@
+"""Cron: import path of the restart-safe external worker.
+
+The worker is spawned as ``sys.executable -m cron.scheduler``. Its entry module is
+``cron.scheduler``, not ``hermes_cli.main``, so nothing bootstraps the gateway's checkout
+onto its ``sys.path``; historically it imported ``cron`` only through the implicit ``-m``
+cwd entry. That entry is gone under ``PYTHONSAFEPATH`` and useless when the venv's
+editable install maps a moved/deleted checkout -- the worker then dies with
+"No module named 'cron'" before its ownership ack (#112729, hypothesised cause).
+
+The shared subprocess sanitizer strips Hermes-owned PYTHONPATH entries because user
+children must not see our tree. This child IS Hermes, so the pin is applied *after* the
+env is built, on the sanitized env -- the sanitizer's other decisions (dropped runtime
+site-packages, dropped venv markers) stand.
+"""
+
+from __future__ import annotations
+
+import os
+import sysconfig
+from pathlib import Path
+
+
+def _installed_purelib() -> Path | None:
+ try:
+ return Path(sysconfig.get_paths()["purelib"]).resolve()
+ except (KeyError, OSError):
+ return None
+
+
+def pin_hermes_tree_on_pythonpath(worker_env: dict, repo_root: Path) -> dict:
+ """Prepend ``repo_root`` to the worker env's own PYTHONPATH (never ``os.environ``'s).
+
+ Skipped when ``repo_root`` is the interpreter's ``purelib``: under a wheel / pipx /
+ uv-tool install ``cron/`` lives in site-packages itself, which is already importable,
+ and pinning it would move site-packages ahead of the stdlib on ``sys.path``.
+ """
+ root = str(repo_root)
+ if _installed_purelib() == Path(root).resolve():
+ return worker_env
+ existing = [e for e in worker_env.get("PYTHONPATH", "").split(os.pathsep) if e]
+ worker_env["PYTHONPATH"] = os.pathsep.join(dict.fromkeys([root, *existing]))
+ return worker_env
diff --git a/tests/cron/test_restart_safe_worker.py b/tests/cron/test_restart_safe_worker.py
index 20f0c172913..26998a3901c 100644
--- a/tests/cron/test_restart_safe_worker.py
+++ b/tests/cron/test_restart_safe_worker.py
@@ -560,6 +560,44 @@ def test_launch_external_worker_pins_the_gateways_tree_on_pythonpath(
assert spawned[0][1]["cwd"] == str(repo_root)
+def test_launch_external_worker_pin_extends_the_sanitized_env_not_os_environ(
+ tmp_path, monkeypatch,
+):
+ """The pin prepends the checkout to the PYTHONPATH the shared sanitizer *kept*; it
+ must not rebuild from raw ``os.environ`` (which would resurrect entries
+ ``build_subprocess_env`` stripped). Under a wheel/pipx install the checkout IS
+ purelib, already importable -- pinning it would hoist site-packages above the stdlib,
+ so the pin is skipped there."""
+ import cron.scheduler as scheduler
+ import cron.scheduler_worker_env as worker_env_mod
+ from tools.process_registry import GatewayChildDispatch
+
+ job = {"id": "job-1", "execution_id": "exec-1", "prompt": "work"}
+ monkeypatch.setattr(scheduler, "_get_hermes_home", lambda: tmp_path)
+ monkeypatch.setattr(
+ "tools.process_registry.restart_safe_gateway_child_argv",
+ lambda command, **_: GatewayChildDispatch("degraded", command),
+ )
+ monkeypatch.setenv("PYTHONPATH", str(tmp_path / "raw-environ-only"))
+ monkeypatch.setattr(
+ "tools.environments.local.build_subprocess_env",
+ lambda **_: {"PATH": os.environ.get("PATH", ""),
+ "PYTHONPATH": str(tmp_path / "kept-by-sanitizer")},
+ )
+ spawned, _payloads, _handoff, _get = _stub_external_worker_launch(scheduler, monkeypatch)
+ repo_root = Path(scheduler.__file__).resolve().parent.parent
+
+ assert scheduler._launch_external_cron_worker(job) is True
+ entries = spawned[0][1]["env"]["PYTHONPATH"].split(os.pathsep)
+ assert entries == [str(repo_root), str(tmp_path / "kept-by-sanitizer")]
+
+ # Wheel / pipx layout: repo_root == purelib -> untouched.
+ monkeypatch.setattr(worker_env_mod, "_installed_purelib", lambda: repo_root)
+ untouched = {"PYTHONPATH": str(tmp_path / "kept-by-sanitizer")}
+ assert worker_env_mod.pin_hermes_tree_on_pythonpath(dict(untouched), repo_root) == untouched
+ assert "PYTHONPATH" not in worker_env_mod.pin_hermes_tree_on_pythonpath({}, repo_root)
+
+
def test_shared_run_path_hands_gateway_fire_to_external_worker(monkeypatch):
import cron.scheduler as scheduler