Skip to content

The agent that grows with you

fix(cron): return a strictly-later next run when the base sits in the DST fall-back hour

teknium195c7e9a0cbe1
diff --git a/cron/jobs.py b/cron/jobs.py
index 6203b5949b6..5894a56fb31 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -1138,8 +1138,23 @@ def compute_next_run(schedule: Dict[str, Any], last_run_at: Optional[str] = None
# Fall back to the base's own zone only when nothing is configured.
zone = get_timezone() or base_time.tzinfo
base_wall = base_time.astimezone(zone).replace(tzinfo=None)
- return (croniter(expr, base_wall).get_next(datetime)
- .replace(tzinfo=zone).isoformat())
+ it = croniter(expr, base_wall)
+ # Strictly-after guard for the DST fall-back hour (qwen-code#11723 class):
+ # attaching the zone to a naive wall clock resolves the repeated autumn hour
+ # to its EARLIER occurrence (fold=0), so a base inside the second occurrence
+ # got a "next run" up to an hour in the PAST — the fire path would re-fire
+ # immediately and re-anchor, looping. Try both folds of each candidate wall
+ # clock and return the earliest instant strictly after the base; a repeated
+ # hour has two instants, so two candidates always suffice.
+ base_ts = base_time.timestamp()
+ next_wall = it.get_next(datetime)
+ for _ in range(2):
+ for fold in (0, 1):
+ candidate = next_wall.replace(tzinfo=zone, fold=fold)
+ if candidate.timestamp() > base_ts:
+ return candidate.isoformat()
+ next_wall = it.get_next(datetime)
+ return next_wall.replace(tzinfo=zone).isoformat()
return None
diff --git a/tests/cron/test_compute_next_run_dst.py b/tests/cron/test_compute_next_run_dst.py
index bcc026efe17..eea211fe57e 100644
--- a/tests/cron/test_compute_next_run_dst.py
+++ b/tests/cron/test_compute_next_run_dst.py
@@ -54,3 +54,25 @@ class TestCronNextRunHoldsConfiguredWallClock:
assert (wall.hour, wall.minute) == (9, 0), \
f"drift at {last.isoformat()}: {wall.isoformat()}"
last = nxt
+
+
+class TestFallBackStrictlyAfterContract:
+ """qwen-code#11723 class: a naive wall clock re-attached to the zone resolves the
+ repeated autumn hour to its earlier occurrence, so a base inside the second
+ occurrence used to get a next_run in the PAST (immediate re-fire loop)."""
+
+ def test_base_in_second_occurrence_gets_future_instant(self, toronto):
+ base = datetime(2026, 11, 1, 1, 15, tzinfo=TORONTO, fold=1) # 01:15 EST
+ nxt = datetime.fromisoformat(
+ compute_next_run({"kind": "cron", "expr": "30 1 * * *"},
+ last_run_at=base.isoformat()))
+ assert nxt.timestamp() > base.timestamp()
+ wall = nxt.astimezone(TORONTO)
+ assert (wall.hour, wall.minute) == (1, 30)
+
+ def test_every_minute_never_returns_past(self, toronto):
+ base = datetime(2026, 11, 1, 1, 59, tzinfo=TORONTO, fold=0) # last EDT minute
+ nxt = datetime.fromisoformat(
+ compute_next_run({"kind": "cron", "expr": "* * * * *"},
+ last_run_at=base.isoformat()))
+ assert nxt.timestamp() > base.timestamp()