Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(scheduler): stop a dense-cron autopilot trigger going permanently silent (MUL-7973) (#9191)

Battleplus5436c0c5b684
diff --git a/server/internal/scheduler/jobs_autopilot.go b/server/internal/scheduler/jobs_autopilot.go
index 85fa5205dc..a96edb5ec8 100644
--- a/server/internal/scheduler/jobs_autopilot.go
+++ b/server/internal/scheduler/jobs_autopilot.go
@@ -218,8 +218,8 @@ func autopilotScopes(
}
// autopilotPlansForScope returns the PlansForScope hook that computes
-// every cron occurrence in (lastPlan, dbNow], keeps only the most recent
-// one, and rejects it if it is outside the dispatch-lateness window.
+// the cron occurrences in (lastPlan, dbNow] that are still inside the
+// dispatch-lateness window, and keeps only the most recent one.
// The latest-only collapse prevents a long outage from replaying every
// missed slot; the lateness guard prevents a paused / newly eligible
// trigger from firing hours after its configured time.
@@ -235,7 +235,6 @@ func autopilotScopes(
func autopilotPlansForScope(cache *autopilotScheduleCache) func(
ctx context.Context, scope Scope, now time.Time, latest LatestPlanInfo,
) ([]time.Time, error) {
- const replayWindow = 24 * time.Hour
return func(ctx context.Context, scope Scope, now time.Time, latest LatestPlanInfo) ([]time.Time, error) {
cfg, ok := cache.get(scope.ID)
if !ok {
@@ -280,8 +279,7 @@ func autopilotPlansForScope(cache *autopilotScheduleCache) func(
// created_at so only occurrences after the trigger
// existed are enumerated.
//
- // Each case feeds into a final safety cap that prevents
- // enumerating more than `replayWindow` of history at once.
+ // Each case feeds into a final floor at the lateness window.
var after time.Time
switch {
case latest.Found:
@@ -291,13 +289,11 @@ func autopilotPlansForScope(cache *autopilotScheduleCache) func(
default:
after = cfg.CreatedAt
}
- // Bound replay by CatchUpWindow so a long pause / dormant
- // trigger does not enumerate millions of historical buckets.
- // `after` is normally already recent (either latest.PlanTime
- // or last_fired_at); the cap only matters for the unusual
- // "trigger created weeks ago but never fired" path.
- if oldest := now.Add(-replayWindow); after.Before(oldest) {
- after = oldest
+ // Enumerate only dispatchable history so the 1024-entry cap cannot
+ // hide recent occurrences after a long pause. Subtract 1ns because
+ // enumeration excludes `after`, but the lateness bound is inclusive.
+ if floor := now.Add(-maxAutopilotScheduleLateness); after.Before(floor) {
+ after = floor.Add(-time.Nanosecond)
}
occs, err := service.NextOccurrencesUTC(cfg.CronExpression, cfg.Timezone, after, now)
diff --git a/server/internal/scheduler/jobs_autopilot_long_pause_test.go b/server/internal/scheduler/jobs_autopilot_long_pause_test.go
new file mode 100644
index 0000000000..b3b59b5b1e
--- /dev/null
+++ b/server/internal/scheduler/jobs_autopilot_long_pause_test.go
@@ -0,0 +1,140 @@
+package scheduler
+
+import (
+ "context"
+ "testing"
+ "time"
+)
+
+// planAutopilotTrigger runs the autopilot PlansForScope hook for a single
+// trigger whose last stored plan_time is lastPlan.
+func planAutopilotTrigger(t *testing.T, cron string, lastPlan, now time.Time) []time.Time {
+ t.Helper()
+ cache := newAutopilotScheduleCache()
+ cache.replace(map[string]autopilotTriggerConfig{
+ "trigger": {TriggerID: "trigger", CronExpression: cron, Timezone: "UTC"},
+ })
+ plans, err := autopilotPlansForScope(cache)(
+ context.Background(),
+ Scope{Kind: ScopeKindAutopilotTrigger, ID: "trigger"},
+ now,
+ LatestPlanInfo{Found: true, PlanTime: lastPlan, Status: "SUCCESS"},
+ )
+ if err != nil {
+ t.Fatalf("plans hook: %v", err)
+ }
+ return plans
+}
+
+// Dense cron schedules must resume even when the pause exceeds the
+// occurrence enumerator's 1024-entry cap (#8939).
+func TestAutopilotPlansForScopeDenseCronAfterLongPause(t *testing.T) {
+ now := time.Date(2026, 9, 4, 16, 5, 10, 0, time.UTC)
+
+ cases := []struct {
+ name string
+ cron string
+ lastPlan time.Time
+ want time.Time
+ }{
+ {
+ name: "every minute, 18h pause (1080 occurrences)",
+ cron: "* * * * *",
+ lastPlan: time.Date(2026, 9, 3, 22, 5, 0, 0, time.UTC),
+ want: time.Date(2026, 9, 4, 16, 5, 0, 0, time.UTC),
+ },
+ {
+ name: "every minute, 3-day pause",
+ cron: "* * * * *",
+ lastPlan: time.Date(2026, 9, 1, 16, 5, 0, 0, time.UTC),
+ want: time.Date(2026, 9, 4, 16, 5, 0, 0, time.UTC),
+ },
+ {
+ name: "every minute, short pause",
+ cron: "* * * * *",
+ lastPlan: time.Date(2026, 9, 4, 15, 55, 0, 0, time.UTC),
+ want: time.Date(2026, 9, 4, 16, 5, 0, 0, time.UTC),
+ },
+ {
+ name: "every two minutes, 18h pause (540 occurrences)",
+ cron: "*/2 * * * *",
+ lastPlan: time.Date(2026, 9, 3, 22, 5, 0, 0, time.UTC),
+ want: time.Date(2026, 9, 4, 16, 4, 0, 0, time.UTC),
+ },
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ plans := planAutopilotTrigger(t, tc.cron, tc.lastPlan, now)
+ if len(plans) != 1 || !plans[0].Equal(tc.want) {
+ t.Fatalf("plans = %v, want [%s]", plans, tc.want.Format(time.RFC3339))
+ }
+ })
+ }
+}
+
+// An already-stuck trigger must recover on the next tick and keep firing
+// without a cron change or trigger recreation.
+func TestAutopilotStuckTriggerSelfHealsWithoutManualRecovery(t *testing.T) {
+ // Reproduce the stale anchor reported in #8939.
+ firstTick := time.Date(2026, 9, 4, 16, 5, 10, 0, time.UTC)
+ silentSince := time.Date(2026, 9, 3, 22, 5, 0, 0, time.UTC)
+
+ plans := planAutopilotTrigger(t, "* * * * *", silentSince, firstTick)
+ wantFirst := time.Date(2026, 9, 4, 16, 5, 0, 0, time.UTC)
+ if len(plans) != 1 || !plans[0].Equal(wantFirst) {
+ t.Fatalf("first tick after the fix: plans = %v, want [%s]", plans, wantFirst.Format(time.RFC3339))
+ }
+
+ // The dispatch stores plan_time, so the next tick resumes from the
+ // healed anchor and keeps firing on the normal minute cadence.
+ nextTick := firstTick.Add(time.Minute)
+ wantNext := wantFirst.Add(time.Minute)
+ plans = planAutopilotTrigger(t, "* * * * *", plans[0], nextTick)
+ if len(plans) != 1 || !plans[0].Equal(wantNext) {
+ t.Fatalf("second tick: plans = %v, want [%s]", plans, wantNext.Format(time.RFC3339))
+ }
+}
+
+// Preserve the existing behavior for future and already-claimed anchors:
+// wait for a later occurrence instead of moving the anchor backwards.
+func TestAutopilotPlansForScopeDoesNotRewindAnchor(t *testing.T) {
+ now := time.Date(2026, 9, 4, 16, 5, 10, 0, time.UTC)
+ current := time.Date(2026, 9, 4, 16, 5, 0, 0, time.UTC)
+ future := current.Add(6 * time.Hour)
+
+ if plans := planAutopilotTrigger(t, "* * * * *", future, now); len(plans) != 0 {
+ t.Fatalf("future anchor: plans = %v, want none", plans)
+ }
+ // An anchor exactly at `now` is a healthy already-claimed slot: stay
+ // quiet this tick (no duplicate claim of the same occurrence) and fire
+ // the next one.
+ if plans := planAutopilotTrigger(t, "* * * * *", now, now); len(plans) != 0 {
+ t.Fatalf("anchor at now: plans = %v, want none this tick", plans)
+ }
+ next := current.Add(time.Minute)
+ if plans := planAutopilotTrigger(t, "* * * * *", current, now.Add(time.Minute)); len(plans) != 1 || !plans[0].Equal(next) {
+ t.Fatalf("steady state past an anchor at now: plans = %v, want [%s]", plans, next.Format(time.RFC3339))
+ }
+ if plans := planAutopilotTrigger(t, "* * * * *", future, future); len(plans) != 0 {
+ t.Fatalf("clock reaches the stored anchor: plans = %v, want none", plans)
+ }
+ firstDue := future.Add(time.Minute)
+ if plans := planAutopilotTrigger(t, "* * * * *", future, firstDue); len(plans) != 1 || !plans[0].Equal(firstDue) {
+ t.Fatalf("clock passes the stored anchor: plans = %v, want [%s]", plans, firstDue.Format(time.RFC3339))
+ }
+}
+
+// TestAutopilotPlansForScopeKeepsLatenessGuard pins the stale catch-up rule
+// the fix must not loosen: the latest due occurrence fires only while it is
+// at most maxAutopilotScheduleLateness old.
+func TestAutopilotPlansForScopeKeepsLatenessGuard(t *testing.T) {
+ lastPlan := time.Date(2026, 9, 4, 15, 0, 0, 0, time.UTC)
+ due := time.Date(2026, 9, 4, 16, 0, 0, 0, time.UTC)
+
+ if plans := planAutopilotTrigger(t, "0 * * * *", lastPlan, due.Add(maxAutopilotScheduleLateness)); len(plans) != 1 || !plans[0].Equal(due) {
+ t.Fatalf("exactly at the lateness bound: plans = %v, want [%s]", plans, due.Format(time.RFC3339))
+ }
+ if plans := planAutopilotTrigger(t, "0 * * * *", lastPlan, due.Add(maxAutopilotScheduleLateness+time.Second)); len(plans) != 0 {
+ t.Fatalf("past the lateness bound: plans = %v, want none", plans)
+ }
+}