Skip to content

The agent that grows with you

fix(cron): run the interrupted-pull recovery before the external worker's ack

mzyasd038b199275a
diff --git a/cron/AGENTS.md b/cron/AGENTS.md
index 761d1203075..ef7af5ae79d 100644
--- a/cron/AGENTS.md
+++ b/cron/AGENTS.md
@@ -80,9 +80,10 @@ Hardening invariants — each guards a real failure; don't weaken without answer
exits before its ownership ack (a reported dispatch failure) rather than run on an unleased
generation the collector may delete. The gateway never re-runs the boot — `hermes_bootstrap`
already did at its own launch (#122222). The worker's `__main__` imports `hermes_bootstrap`
- (`finish_worker_boot`) before it reads the payload, and only then drops the marker: a
- source-update relaunch (`-I`, no `PYTHONPATH`) must replay the whole worker, never land after
- the ack with the payload gone and no dependency boot.
+ (`finish_worker_boot`) and runs `run_agent`'s interrupted-pull recovery before it reads the
+ payload, and only then drops the marker: a source-update relaunch (`-I`, no `PYTHONPATH`) or a
+ post-restore re-exec must replay the whole worker, never land after the ack with the payload
+ gone and no dependency boot.
- Cron sessions pass `skip_memory=True`; memory providers intentionally do not run during cron.
- Cron execution has its own session. Eligible continuable deliveries may mirror or seed the
reply-facing conversation: origin, origin-less home fallback, user-written bare-platform home,
diff --git a/cron/worker_bootstrap.py b/cron/worker_bootstrap.py
index 255f8c30ad4..1a43e118dec 100644
--- a/cron/worker_bootstrap.py
+++ b/cron/worker_bootstrap.py
@@ -61,7 +61,14 @@ def finish_worker_boot() -> None:
Not from ``worker_bootstrap()``: while ``-m cron.scheduler`` executes ``cron/__init__.py``,
``sys.argv[0]`` is still ``-m`` and ``__main__`` has no spec, so the relaunch command could
not name the module to re-run. In ``__main__`` both are set.
+
+ ``run_agent``'s other import-time relaunch -- restoring a tree a killed ``hermes update``
+ half-wrote -- runs here too: the worker never loads ``hermes_cli.main``, so left to
+ ``run_one_job`` it re-executed the worker after the ack, onto a deleted payload.
"""
import hermes_bootstrap # noqa: F401
+ from hermes_cli import _early_recovery
+ if _early_recovery.restore_interrupted_pull():
+ _early_recovery.relaunch_after_restore()
os.environ.pop(WORKER_MARKER, None)
diff --git a/tests/cron/test_restart_safe_worker.py b/tests/cron/test_restart_safe_worker.py
index 7d73b7e6ed3..c641074a8be 100644
--- a/tests/cron/test_restart_safe_worker.py
+++ b/tests/cron/test_restart_safe_worker.py
@@ -782,10 +782,15 @@ from pathlib import Path
import pm.environments
pm.environments.activate_dependencies = lambda root: None
+import hermes_cli._early_recovery as early_recovery
import hermes_cli.venv_sync as venv_sync
payload, ack, record, marker = Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3]), sys.argv[4]
-venv_sync.prepare_launch = lambda root, argv: Path(sys.executable)
+if sys.argv[5] == "source_update":
+ venv_sync.prepare_launch = lambda root, argv: Path(sys.executable)
+else:
+ venv_sync.prepare_launch = lambda root, argv: None
+ early_recovery.restore_interrupted_pull = lambda *args, **kwargs: True
def relaunch(*args):
@@ -803,13 +808,14 @@ runpy.run_module("cron.scheduler", run_name="__main__", alter_sys=True)
"""
-def test_source_update_relaunch_replays_the_worker_before_its_ack(tmp_path):
- """A worker whose ``hermes_bootstrap`` finishes a source update relaunches into an ``-I``
- interpreter that ignores the pinned PYTHONPATH. Reached through ``run_agent`` inside
- ``run_one_job``, that relaunch came after the ack: the payload was deleted, the marker
- consumed, and the new process died on ``ruamel`` with the adopted run left ``unknown``.
- The relaunch must instead replay the whole worker: payload unread, no ack, marker set,
- re-running ``cron.scheduler`` as ``__main__``."""
[email protected]("cause", ["source_update", "interrupted_pull"])
+def test_relaunch_replays_the_worker_before_its_ack(tmp_path, cause):
+ """``run_agent``'s import may relaunch the process: ``hermes_bootstrap`` finishing a source
+ update (into an ``-I`` interpreter that ignores the pinned PYTHONPATH), or the restore of a
+ tree a killed ``hermes update`` half-wrote. Reached inside ``run_one_job``, either came after
+ the ack: the payload was deleted, the marker consumed, and the new process died on ``ruamel``
+ or found no payload, leaving the adopted run ``unknown``. The relaunch must instead replay
+ the whole worker: payload unread, no ack, marker set, the same worker re-run."""
import cron.worker_bootstrap as worker_bootstrap
repo_root = Path(worker_bootstrap.__file__).resolve().parent.parent
@@ -823,13 +829,16 @@ def test_source_update_relaunch_replays_the_worker_before_its_ack(tmp_path):
env[worker_bootstrap.WORKER_MARKER] = "1"
child = subprocess.run(
[sys.executable, "-c", _RELAUNCH_PROBE, str(payload), str(ack), str(record),
- worker_bootstrap.WORKER_MARKER],
+ worker_bootstrap.WORKER_MARKER, cause],
cwd=repo_root, env=env, capture_output=True, text=True, timeout=120,
)
assert record.exists(), f"worker never relaunched (exit {child.returncode}): {child.stderr}"
seen = json.loads(record.read_text())
assert (seen["payload"], seen["ack"], seen["marker"]) == (True, False, "1")
- assert "runpy.run_module('cron.scheduler', run_name='__main__'" in seen["command"][-1]
+ if cause == "source_update":
+ assert "runpy.run_module('cron.scheduler', run_name='__main__'" in seen["command"][-1]
+ else:
+ assert str(payload) in seen["command"]
_REAL_BOOT_PROBE = """