Skip to content

The agent that grows with you

fix(cron): restart-safe worker imports the gateway's own checkout instead of relying on cwd

teknium19269b19e4f52
diff --git a/cron/scheduler.py b/cron/scheduler.py
index d8394923d26..50d567b4e02 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -3232,12 +3232,23 @@ 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.
+ 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)),
+ ]))
try:
stderr_fd = os.open(stderr_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
try:
process = subprocess.Popen(
dispatch.argv,
- cwd=str(Path(__file__).resolve().parent.parent),
+ cwd=str(repo_root),
env=worker_env,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
diff --git a/tests/cron/test_restart_safe_worker.py b/tests/cron/test_restart_safe_worker.py
index 68737a0482d..20f0c172913 100644
--- a/tests/cron/test_restart_safe_worker.py
+++ b/tests/cron/test_restart_safe_worker.py
@@ -533,6 +533,33 @@ def test_launch_external_worker_degrades_by_default_with_real_helper(
assert not (tmp_path / "cron/external-workers/exec-1.json").exists()
+def test_launch_external_worker_pins_the_gateways_tree_on_pythonpath(
+ tmp_path, monkeypatch,
+):
+ """#112729: the worker starts in ``cron.scheduler`` (no ``hermes_cli.main`` bootstrap),
+ so its import path must be explicit — a rotted editable mapping or PYTHONSAFEPATH
+ otherwise kills it with "No module named 'cron'" before the ack. The spawn env carries
+ the gateway's own checkout first and keeps the gateway's other PYTHONPATH entries."""
+ import cron.scheduler as scheduler
+ 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 / "user-libs"))
+ spawned, _payloads, _handoff, _get = _stub_external_worker_launch(scheduler, monkeypatch)
+
+ assert scheduler._launch_external_cron_worker(job) is True
+ repo_root = Path(scheduler.__file__).resolve().parent.parent
+ entries = spawned[0][1]["env"]["PYTHONPATH"].split(os.pathsep)
+ assert entries[0] == str(repo_root)
+ assert str(tmp_path / "user-libs") in entries
+ assert spawned[0][1]["cwd"] == str(repo_root)
+
+
def test_shared_run_path_hands_gateway_fire_to_external_worker(monkeypatch):
import cron.scheduler as scheduler
diff --git a/website/docs/user-guide/features/cron.md b/website/docs/user-guide/features/cron.md
index f3839321bcc..b5fc4af89bf 100644
--- a/website/docs/user-guide/features/cron.md
+++ b/website/docs/user-guide/features/cron.md
@@ -374,6 +374,8 @@ cron:
The lasting fix is a user session for the gateway user: `sudo loginctl enable-linger <gateway-user>` (and `XDG_RUNTIME_DIR` / `DBUS_SESSION_BUS_ADDRESS` in the unit for system-level installs), then restart the gateway. Kanban workers always require a scope and fail closed regardless of this key.
+The worker is the gateway's own interpreter running `python -m cron.scheduler`, with the gateway's checkout pinned on its `PYTHONPATH` (plus any entries the gateway itself was started with), so it imports the same Hermes tree the gateway runs — regardless of the venv's editable-install mapping, the unit's `WorkingDirectory`, or `PYTHONSAFEPATH` on the host. A worker that dies before acknowledging the handoff records its own stderr tail in the job's last error and in the execution ledger, so the failing import (or whatever killed it) is named instead of a bare exit code.
+
### Execution history
Hermes records each claimed cron attempt in the profile-local