Skip to content

The agent that grows with you

fix(cron): queued run-now intent survives a second trigger and the tick advance

Teknium481b6fe14498
diff --git a/cron/extra_fire.py b/cron/extra_fire.py
index 09c9d846fcc..f573d7ee94d 100644
--- a/cron/extra_fire.py
+++ b/cron/extra_fire.py
@@ -14,6 +14,20 @@ RESTORE_KEY = "manual_run_restore_at" # trigger_job's overwritten next_run_at
_CLAIM_KEY = "extra_slot"
+def queued(job: dict[str, Any]) -> bool:
+ """A ``trigger_job`` run-now not claimed yet: ``next_run_at`` still holds its "now" stamp and
+ the overwritten slot waits in RESTORE_KEY. This pair IS the durable manual intent, so the tick's
+ pre-run advance leaves such a job alone (the claim restores the slot and owns the advance)."""
+ marker = job.get("manual_run_at")
+ return marker is not None and marker == job.get("next_run_at") and RESTORE_KEY in job
+
+
+def restore_target(job: dict[str, Any]) -> Optional[str]:
+ """The slot a new ``trigger_job`` must save: a repeated trigger before the claim keeps the
+ first trigger's saved slot, never that trigger's own "now"."""
+ return job.get(RESTORE_KEY) if queued(job) else job.get("next_run_at")
+
+
def _future_slot(value: Any, now: datetime) -> Optional[str]:
from cron.jobs import _parse_aware
diff --git a/cron/jobs.py b/cron/jobs.py
index eca6938c3cb..1b0ed356d22 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -2180,7 +2180,7 @@ def trigger_job(job_id: str, extra_prompt: Optional[str] = None) -> Optional[dic
"next_run_at": manual_run_at,
# Run-now intent, so cron expression/TZ repair guards don't treat it as stale state; the
# overwritten slot comes back at claim time (an extra fire, cron/extra_fire.py).
- "manual_run_at": manual_run_at, "manual_run_restore_at": job.get("next_run_at"),
+ "manual_run_at": manual_run_at, extra_fire.RESTORE_KEY: extra_fire.restore_target(job),
"manual_run_prompt": (extra_prompt or None),
})
@@ -2607,6 +2607,7 @@ def advance_next_runs(job_ids) -> int:
job["id"] not in ids
or (is_terminal_job(job) and not _is_recoverable_error_job(job))
or job.get("schedule", {}).get("kind") not in {"cron", "interval"}
+ or extra_fire.queued(job) # the claim restores its slot; advancing erases the intent
):
continue
new_next = compute_next_run(job["schedule"], now)
diff --git a/tests/cron/test_manual_run_extra_fire.py b/tests/cron/test_manual_run_extra_fire.py
index cc903361198..baf97dc0d64 100644
--- a/tests/cron/test_manual_run_extra_fire.py
+++ b/tests/cron/test_manual_run_extra_fire.py
@@ -9,6 +9,7 @@ from datetime import datetime, timedelta
import pytest
from cron.jobs import (
+ advance_next_runs,
claim_dispatch,
claim_job_for_fire,
create_job,
@@ -78,6 +79,41 @@ def test_gateway_forwarded_trigger_restores_the_slot_at_claim_time(clock):
assert "manual_run_restore_at" not in after
+def test_second_trigger_before_the_claim_keeps_the_original_slot(clock):
+ """Two run-now requests queued before the ticker claims: the saved slot is the job's real
+ scheduled time, not the first trigger's "now"."""
+ job = create_job("remind me", "in 30m", name="reminder")
+ slot = job["next_run_at"]
+ trigger_job(job["id"])
+ clock["now"] += timedelta(seconds=20)
+ trigger_job(job["id"])
+
+ _fire(job["id"]) # the ticker's claim of the queued manual run
+
+ after = get_job(job["id"])
+ assert (after["state"], after["next_run_at"]) == ("scheduled", slot)
+ assert after["repeat"] == {"times": 1, "completed": 0}
+
+
+def test_forwarded_recurring_run_survives_the_tick_advance(clock):
+ """The gateway-forwarded run goes through the tick: get_due_jobs, then advance_next_runs
+ for the whole due set, THEN the worker's plain claim. The queued manual intent must survive
+ that advance, so the run spends no repeat budget and the schedule does not shift."""
+ job = create_job("digest", "every 60m", name="digest", repeat=1)
+ slot = job["next_run_at"]
+ clock["now"] += timedelta(minutes=20)
+ trigger_job(job["id"])
+
+ due = get_due_jobs()
+ assert [d["id"] for d in due] == [job["id"]]
+ advance_next_runs([d["id"] for d in due])
+ _fire(job["id"]) # the worker: claim_job_for_fire with neither manual nor force
+
+ after = get_job(job["id"])
+ assert (after["state"], after["next_run_at"]) == ("scheduled", slot)
+ assert after["repeat"] == {"times": 1, "completed": 0}
+
+
def test_manual_run_does_not_spend_a_recurring_repeat_budget_or_shift_the_schedule(clock):
job = create_job("digest", "every 60m", name="digest", repeat=2)
slot = job["next_run_at"]