Skip to content

The agent that grows with you

fix(cron): external worker ack deadline equals the handoff adoption grace

teknium1157aa716b7ba
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 302e7d9fffd..81bb1a937b2 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -466,8 +466,9 @@ from cron.jobs import (
clear_run_claim, get_due_jobs, heartbeat_fire_claim, heartbeat_run_claim, mark_job_run,
save_job_output, use_cron_store)
from cron.executions import (
- _TERMINAL_STATES, create_execution, finish_execution, get_execution,
- mark_execution_handoff_pending, mark_execution_running, recover_interrupted_executions)
+ _TERMINAL_STATES, HANDOFF_ADOPTION_GRACE_SECONDS, create_execution, finish_execution,
+ get_execution, mark_execution_handoff_pending, mark_execution_running,
+ recover_interrupted_executions)
# Response marker that suppresses delivery (output is still saved locally for audit).
SILENT_MARKER = "[SILENT]"
@@ -3084,9 +3085,6 @@ def _wait_for_external_cron_worker(
pass
-_EXTERNAL_WORKER_COLD_START_ACK_TIMEOUT_SECONDS = 12.0
-
-
def _launch_external_cron_worker(job: dict) -> bool:
"""Launch *job* outside the managed gateway process when required.
@@ -3196,7 +3194,11 @@ def _launch_external_cron_worker(job: dict) -> bool:
with _running_lock:
_restart_safe_waiter_job_ids.add(job_id)
- deadline = time.monotonic() + _EXTERNAL_WORKER_COLD_START_ACK_TIMEOUT_SECONDS
+ # Same window the dead-owner recovery ledger grants a pending handoff: a cold
+ # worker start (imports + secret hydration) measures ~10-12s in the field, and
+ # a dispatch deadline shorter than the adoption grace made the two guards
+ # around one handoff disagree.
+ deadline = time.monotonic() + HANDOFF_ADOPTION_GRACE_SECONDS
while time.monotonic() < deadline:
if ack_path.exists():
try:
@@ -3263,7 +3265,7 @@ def _launch_external_cron_worker(job: dict) -> bool:
"Cron external worker for job '%s' did not acknowledge within %.0fs; "
"leaving the durable execution claim untouched",
job_id,
- _EXTERNAL_WORKER_COLD_START_ACK_TIMEOUT_SECONDS,
+ HANDOFF_ADOPTION_GRACE_SECONDS,
)
return _wait_for_external_cron_worker(
process,
diff --git a/tests/cron/test_restart_safe_worker.py b/tests/cron/test_restart_safe_worker.py
index 8c6cdaf4049..b7e55bbf142 100644
--- a/tests/cron/test_restart_safe_worker.py
+++ b/tests/cron/test_restart_safe_worker.py
@@ -304,22 +304,28 @@ def test_launch_external_worker_uses_restart_safe_scope_and_acknowledges(
assert not (tmp_path / "cron/external-workers/exec-1.json").exists()
-def test_launch_external_worker_allows_delayed_cold_start_acknowledgement(
+def test_launch_external_worker_honors_ack_within_adoption_grace(
tmp_path, monkeypatch
):
+ """A cold worker that acks after 5s but inside the adoption grace is adopted, not abandoned."""
import cron.scheduler as scheduler
+ from cron.executions import HANDOFF_ADOPTION_GRACE_SECONDS
+ from tools.process_registry import GatewayChildDispatch
job = {"id": "job-cold", "execution_id": "exec-cold", "prompt": "work"}
monkeypatch.setattr(scheduler, "_get_hermes_home", lambda: tmp_path)
monkeypatch.setattr(
"tools.process_registry.restart_safe_gateway_child_argv",
- lambda command, *, unit_suffix: ["scope", "--", *command],
+ lambda command, **_kw: GatewayChildDispatch("scoped", ["scope", "--", *command]),
)
monkeypatch.setattr(
scheduler,
"mark_execution_handoff_pending",
lambda _execution_id: {"id": "exec-cold", "handoff_pending": 1},
)
+ ack_path = tmp_path / "cron/external-workers/exec-cold.ready"
+ ack_at = HANDOFF_ADOPTION_GRACE_SECONDS - 10.0
+ assert ack_at > 5.0
class FakeClock:
now = 0.0
@@ -329,23 +335,22 @@ def test_launch_external_worker_allows_delayed_cold_start_acknowledgement(
def sleep(self, seconds):
self.now += seconds
- if self.now >= 6.0 and not ack_path.exists():
+ if self.now >= ack_at and not ack_path.exists():
ack_path.write_text(
json.dumps({"pid": 4321, "execution_id": "exec-cold"}),
encoding="utf-8",
)
clock = FakeClock()
- ack_path = tmp_path / "cron/external-workers/exec-cold.ready"
class FakeProcess:
+ pid = 999
returncode = None
def poll(self):
return self.returncode
def wait(self, timeout=None):
- assert clock.now >= 6.0, "scheduler stopped waiting before cold-start ack"
raise subprocess.TimeoutExpired(cmd="worker", timeout=timeout)
monkeypatch.setattr(
@@ -358,9 +363,12 @@ def test_launch_external_worker_allows_delayed_cold_start_acknowledgement(
)
monkeypatch.setattr(scheduler.time, "monotonic", clock.monotonic)
monkeypatch.setattr(scheduler.time, "sleep", clock.sleep)
+ monkeypatch.setattr(scheduler, "_running_worker_pids", {})
assert scheduler._launch_external_cron_worker(job) is True
- assert clock.now >= 6.0
+ # The acknowledged path records the worker pid; the ownership-uncertain
+ # timeout path never does.
+ assert scheduler._running_worker_pids == {"job-cold": 4321}
def test_external_worker_exit_rechecks_exact_execution_before_failure(monkeypatch):