Skip to content

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

Go

fix(autopilots): store no next run for a schedule that never fires (#10239)

pseudoyu2daf77e0fe3b
diff --git a/server/internal/handler/autopilot.go b/server/internal/handler/autopilot.go
index ad2a25b439..7d9cb053c3 100644
--- a/server/internal/handler/autopilot.go
+++ b/server/internal/handler/autopilot.go
@@ -30,6 +30,14 @@ func computeNextRun(cronExpr, timezone string) (time.Time, error) {
return service.ComputeNextRun(cronExpr, timezone)
}
+// nextRunTimestamp maps a computed next run onto the nullable next_run_at
+// column. A cron that parses but never fires ("0 0 30 2 *") is saveable, and
+// the cron library reports "no next occurrence" as the zero time: that is
+// stored as NULL, not as a next run in year 0001.
+func nextRunTimestamp(t time.Time) pgtype.Timestamptz {
+ return pgtype.Timestamptz{Time: t, Valid: !t.IsZero()}
+}
+
// ── Response types ──────────────────────────────────────────────────────────
type AutopilotResponse struct {
@@ -1651,7 +1659,7 @@ func (h *Handler) CreateAutopilotTrigger(w http.ResponseWriter, r *http.Request)
writeError(w, http.StatusBadRequest, err.Error())
return
}
- nextRunAt = pgtype.Timestamptz{Time: t, Valid: true}
+ nextRunAt = nextRunTimestamp(t)
case "webhook":
// Mint the token BEFORE the INSERT so the row never exists in a
// half-written kind=webhook + webhook_token=NULL state. If the
@@ -2014,7 +2022,7 @@ func (h *Handler) UpdateAutopilotTrigger(w http.ResponseWriter, r *http.Request)
writeError(w, http.StatusBadRequest, err.Error())
return
}
- params.NextRunAt = pgtype.Timestamptz{Time: t, Valid: true}
+ params.NextRunAt = nextRunTimestamp(t)
}
userID := uuidToString(actor.UserID)
diff --git a/server/internal/handler/autopilot_never_firing_trigger_test.go b/server/internal/handler/autopilot_never_firing_trigger_test.go
new file mode 100644
index 0000000000..d723e9ea8a
--- /dev/null
+++ b/server/internal/handler/autopilot_never_firing_trigger_test.go
@@ -0,0 +1,132 @@
+package handler
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+)
+
+// A cron that parses but never fires ("0 0 30 2 *") can be saved on purpose:
+// the preview endpoint answers it with an empty list instead of a 400. robfig
+// reports "no next occurrence" as the zero time, and storing that as
+// next_run_at made the trigger read "next run: year 0001" and, through the
+// list's min(next_run_at), hid the real next run of every sibling trigger.
+
+func createScheduleTriggerViaHandler(t *testing.T, autopilotID, cron string) AutopilotTriggerResponse {
+ t.Helper()
+ w := httptest.NewRecorder()
+ req := newRequest("POST", "/api/autopilots/"+autopilotID+"/triggers", map[string]any{
+ "kind": "schedule", "cron_expression": cron, "timezone": "UTC",
+ })
+ req = withURLParam(req, "id", autopilotID)
+ testHandler.CreateAutopilotTrigger(w, req)
+ if w.Code != http.StatusCreated {
+ t.Fatalf("CreateAutopilotTrigger: expected 201, got %d body=%s", w.Code, w.Body.String())
+ }
+ var resp AutopilotTriggerResponse
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
+ t.Fatalf("decode: %v", err)
+ }
+ return resp
+}
+
+func storedTriggerNextRunIsNull(t *testing.T, triggerID string) bool {
+ t.Helper()
+ var isNull bool
+ if err := testPool.QueryRow(context.Background(),
+ `SELECT next_run_at IS NULL FROM autopilot_trigger WHERE id = $1`, triggerID).Scan(&isNull); err != nil {
+ t.Fatalf("read next_run_at: %v", err)
+ }
+ return isNull
+}
+
+func TestCreateAutopilotTrigger_NeverFiringCronHasNoNextRun(t *testing.T) {
+ if testHandler == nil {
+ t.Skip("database not available")
+ }
+ agentID := createWebhookTestAgent(t, "NeverFiring Create Agent")
+ apID := createWebhookTestAutopilot(t, agentID, "active", "run_only")
+
+ never := createScheduleTriggerViaHandler(t, apID, "0 0 30 2 *")
+ if never.NextRunAt != nil {
+ t.Errorf("next_run_at = %q, want null for a cron that never fires", *never.NextRunAt)
+ }
+ if !storedTriggerNextRunIsNull(t, never.ID) {
+ t.Error("stored next_run_at is not NULL for a cron that never fires")
+ }
+
+ // The list reports the autopilot's real next run, not the sibling's
+ // missing one.
+ hourly := createScheduleTriggerViaHandler(t, apID, "0 * * * *")
+ if hourly.NextRunAt == nil {
+ t.Fatal("an hourly trigger must have a next run")
+ }
+ w := httptest.NewRecorder()
+ testHandler.ListAutopilots(w, newRequest("GET", "/api/autopilots?workspace_id="+testWorkspaceID, nil))
+ if w.Code != http.StatusOK {
+ t.Fatalf("ListAutopilots: expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ var list struct {
+ Autopilots []struct {
+ ID string `json:"id"`
+ NextRunAt *string `json:"next_run_at"`
+ } `json:"autopilots"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &list); err != nil {
+ t.Fatalf("decode list: %v", err)
+ }
+ for _, a := range list.Autopilots {
+ if a.ID != apID {
+ continue
+ }
+ if a.NextRunAt == nil {
+ t.Fatal("list next_run_at is null, want the hourly trigger's next run")
+ }
+ got, err := time.Parse(time.RFC3339, *a.NextRunAt)
+ if err != nil {
+ t.Fatalf("parse list next_run_at %q: %v", *a.NextRunAt, err)
+ }
+ want, err := time.Parse(time.RFC3339, *hourly.NextRunAt)
+ if err != nil {
+ t.Fatalf("parse trigger next_run_at %q: %v", *hourly.NextRunAt, err)
+ }
+ if !got.Equal(want) {
+ t.Errorf("list next_run_at = %s, want the hourly trigger's %s", got, want)
+ }
+ return
+ }
+ t.Fatalf("autopilot %s missing from the list", apID)
+}
+
+func TestUpdateAutopilotTrigger_NeverFiringCronClearsNextRun(t *testing.T) {
+ if testHandler == nil {
+ t.Skip("database not available")
+ }
+ agentID := createWebhookTestAgent(t, "NeverFiring Update Agent")
+ apID := createWebhookTestAutopilot(t, agentID, "active", "run_only")
+ trig := createScheduleTriggerViaHandler(t, apID, "0 * * * *")
+ if storedTriggerNextRunIsNull(t, trig.ID) {
+ t.Fatal("an hourly trigger must start with a next run")
+ }
+
+ w := httptest.NewRecorder()
+ req := newRequest("PATCH", "/api/autopilots/"+apID+"/triggers/"+trig.ID, map[string]any{"cron_expression": "0 0 30 2 *"})
+ req = withURLParams(req, "id", apID, "triggerId", trig.ID)
+ testHandler.UpdateAutopilotTrigger(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("UpdateAutopilotTrigger: expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ var resp AutopilotTriggerResponse
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
+ t.Fatalf("decode: %v", err)
+ }
+ if resp.NextRunAt != nil {
+ t.Errorf("next_run_at = %q, want null after switching to a cron that never fires", *resp.NextRunAt)
+ }
+ if !storedTriggerNextRunIsNull(t, trig.ID) {
+ t.Error("stored next_run_at is not NULL after switching to a cron that never fires")
+ }
+}
diff --git a/server/internal/scheduler/jobs_autopilot.go b/server/internal/scheduler/jobs_autopilot.go
index a96edb5ec8..cef98d0602 100644
--- a/server/internal/scheduler/jobs_autopilot.go
+++ b/server/internal/scheduler/jobs_autopilot.go
@@ -389,9 +389,11 @@ func autopilotHandler(
tz = trigger.Timezone.String
}
if next, ok := advancedNextRun(trigger.CronExpression.String, tz, in.PlanTime, time.Now()); ok {
+ // The zero time means the cron has no further occurrence the
+ // library can find; store NULL rather than year 0001.
_ = queries.AdvanceTriggerNextRun(ctx, db.AdvanceTriggerNextRunParams{
ID: trigger.ID,
- NextRunAt: pgtype.Timestamptz{Time: next, Valid: true},
+ NextRunAt: pgtype.Timestamptz{Time: next, Valid: !next.IsZero()},
})
} else {
_ = queries.TouchAutopilotTriggerFiredAt(ctx, trigger.ID)