Skip to content

The agent that grows with you

fix(cron): grace_minutes=0 skips real misses instead of every run

Teknium3035c0c0ec5d
diff --git a/cron/jobs.py b/cron/jobs.py
index 789fded8a7b..0357623a42b 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -948,11 +948,12 @@ def _compute_grace_seconds(schedule: dict, grace_minutes: Any = None) -> int:
"""How late a job can be and still catch up rather than fast-forward: half the period, clamped
to [120s, 2h], so daily jobs catch up but frequent jobs fast-forward quickly.
- A per-job ``grace_minutes`` overrides that window outright (0 = never catch up: a missed
- occurrence is skipped, not run late)."""
+ A per-job ``grace_minutes`` overrides that window (0 = never catch up), floored at the
+ 120s tick tolerance: the ticker always fires a few seconds after the slot, so a 0s window
+ would classify every on-time run as missed."""
override = jobs_time.grace_seconds_override(grace_minutes)
if override is not None:
- return override
+ return max(override, _MIN_GRACE_SECONDS)
period_seconds = _schedule_cadence_seconds(schedule)
if not period_seconds:
return _MIN_GRACE_SECONDS
@@ -3098,11 +3099,14 @@ def _fast_forward_missed_recurring(d: _DueJob, grace: int) -> bool:
if not new_next:
return False
d.scan.persist(d.job["id"], next_run_at=new_next)
+ # An explicit per-job grace_minutes is the user's catch-up limit for THIS job: past it the
+ # miss is skipped even while the global cron.catch_up_missed default still runs it once.
if (_instant_after(_ensure_aware(datetime.fromisoformat(new_next)), d.scan.now)
- and not _cron_config_number("catch_up_missed", True, lambda value: value is not False)):
+ and (jobs_time.grace_seconds_override(d.job.get("grace_minutes")) is not None
+ or not _cron_config_number("catch_up_missed", True, lambda value: value is not False))):
logger.info(
"Job '%s' missed its scheduled time (%s, grace=%ds). "
- "Skipping missed occurrence because cron.catch_up_missed is false; next run: %s",
+ "Skipping missed occurrence (grace_minutes or cron.catch_up_missed=false); next run: %s",
d.label, d.next_run, grace, new_next)
d.scan.on_saved.append(lambda job=d.job: record_cron_missed(job))
return True
diff --git a/tests/cron/test_per_job_timezone.py b/tests/cron/test_per_job_timezone.py
index ccb0f9639b3..3c017d37cce 100644
--- a/tests/cron/test_per_job_timezone.py
+++ b/tests/cron/test_per_job_timezone.py
@@ -259,7 +259,7 @@ class TestGraceMinutesOverride:
prompt="p", schedule="every 5m", model="fixture", deliver="local",
grace_minutes=30)
- @pytest.mark.parametrize("minutes, expected", [(0, 0), (45, 45 * 60)])
+ @pytest.mark.parametrize("minutes, expected", [(0, 120), (45, 45 * 60)]) # 0 floors at tick tolerance
def test_stored_grace_round_trips(self, la_profile, cron_store, minutes, expected):
job = jobs.create_job(
prompt="p", schedule=WEEKDAY_9AM, model="fixture", deliver="local",
@@ -267,3 +267,50 @@ class TestGraceMinutesOverride:
stored = next(r for r in jobs.load_jobs() if r["id"] == job["id"])
assert stored["grace_minutes"] == minutes, "0 must not be dropped as falsy"
assert jobs._compute_grace_seconds(stored["schedule"], stored["grace_minutes"]) == expected
+
+
+class TestSlotMovesFollowThePin:
+ def test_manual_run_settle_recomputes_in_the_job_zone(self, la_profile, cron_store):
+ """A finished extra fire whose slot passed takes the next natural occurrence: in the
+ JOB's zone, or the stale-lattice guard later re-anchors past it (one lost run)."""
+ from cron import extra_fire
+
+ cron_store.set(datetime(2026, 10, 22, 12, 0, tzinfo=NY))
+ job = {"schedule": jobs.parse_schedule(DAILY_9AM), "timezone": "Asia/Hong_Kong",
+ "next_run_at": datetime(2026, 10, 22, 9, 0, tzinfo=HK).isoformat()}
+ extra_fire.settle(job, cron_store.now().isoformat())
+ assert datetime.fromisoformat(job["next_run_at"]) == datetime(2026, 10, 23, 9, 0, tzinfo=HK)
+
+ def test_distribution_zone_change_recomputes_the_instant(self, la_profile, cron_store):
+ from cron.job_definition import merge_job_definition
+
+ cron_store.set(datetime(2026, 10, 22, 12, 0, tzinfo=NY))
+ local = jobs.create_job(prompt="p", schedule=DAILY_9AM, model="fixture",
+ deliver="local", timezone="Asia/Hong_Kong")
+ authored = {"prompt": "p", "schedule": local["schedule"], "timezone": "America/New_York"}
+ merged = merge_job_definition(dict(local), authored)
+ assert datetime.fromisoformat(merged["next_run_at"]) == datetime(2026, 10, 23, 9, 0, tzinfo=NY)
+
+
+class TestZeroGraceAtTickResolution:
+ """The ticker fires seconds after the slot; ``0`` must mean "skip real misses", not "every
+ run is a miss" (which, with catch-up disabled, silently stopped the job firing at all)."""
+
+ @pytest.mark.parametrize("catch_up_disabled", [False, True])
+ def test_on_time_tick_fires_as_scheduled(self, la_profile, cron_store, tmp_path, catch_up_disabled):
+ if catch_up_disabled:
+ (tmp_path / "config.yaml").write_text("cron:\n catch_up_missed: false\n", encoding="utf-8")
+ cron_store.set(datetime(2026, 10, 22, 9, 0, 20, tzinfo=NY))
+ job_id = _add_job(DAILY_9AM, timezone="America/New_York", grace_minutes=0,
+ next_run_at=datetime(2026, 10, 22, 9, 0, tzinfo=NY))
+ assert [j["id"] for j in jobs.get_due_jobs()] == [job_id]
+ stored = next(r for r in jobs.load_jobs() if r["id"] == job_id)
+ assert stored["last_dispatch"]["kind"] != "catch_up"
+
+ def test_explicit_grace_skips_a_real_miss_even_with_default_catch_up(self, la_profile, cron_store):
+ cron_store.set(datetime(2026, 10, 22, 10, 0, tzinfo=NY))
+ job_id = _add_job(DAILY_9AM, timezone="America/New_York", grace_minutes=0,
+ next_run_at=datetime(2026, 10, 22, 9, 0, tzinfo=NY))
+ assert jobs.get_due_jobs() == []
+ stored = next(r for r in jobs.load_jobs() if r["id"] == job_id)
+ assert datetime.fromisoformat(stored["next_run_at"]) == datetime(2026, 10, 23, 9, 0, tzinfo=NY)