Skip to content

Commit 99123eb

Browse files
committed
fix(schedule): recover runs and correct local time reporting
1 parent fc6231e commit 99123eb

6 files changed

Lines changed: 477 additions & 95 deletions

File tree

‎packages/app/src/runtime/server/schedule.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ describe("nextRunAfter", () => {
2424
expect(weekdayLabels([4], "en-US", "America/Los_Angeles")).toEqual(["Thu"])
2525
})
2626

27+
test("resolves DST gaps forward and ambiguous local times to their first occurrence", () => {
28+
expect(parseDateTimeLocal("2026-03-08T02:30", "America/New_York")).toBe(Date.UTC(2026, 2, 8, 7, 30))
29+
expect(parseDateTimeLocal("2025-11-02T01:30", "America/New_York")).toBe(Date.UTC(2025, 10, 2, 5, 30))
30+
})
31+
2732
test("returns a future once spec unchanged", () => {
2833
const atMs = NOW + 60_000
2934
expect(nextRunAfter({ kind: "once", atMs }, NOW)).toBe(atMs)

‎packages/app/src/runtime/server/schedule.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,23 @@ function dateParts(value: number, timezone: string) {
118118

119119
function localTimeToTimestamp(year: number, month: number, day: number, hour: number, minute: number, timezone: string) {
120120
const local = Date.UTC(year, month - 1, day, hour, minute)
121-
let instant = local
122-
for (let attempt = 0; attempt < 4; attempt++) {
121+
const dayMs = 24 * 60 * 60 * 1000
122+
const offsets = new Set(
123+
[local - 2 * dayMs, local - dayMs, local, local + dayMs, local + 2 * dayMs].map((instant) => {
124+
const parts = dateParts(instant, timezone)
125+
return Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute) - instant
126+
}),
127+
)
128+
const candidates = Array.from(offsets, (offset) => {
129+
const instant = local - offset
123130
const parts = dateParts(instant, timezone)
124-
const represented = Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute)
125-
const adjustment = local - represented
126-
if (adjustment === 0) return instant
127-
instant += adjustment
128-
}
129-
return instant
131+
return { instant, local: Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute) }
132+
})
133+
const exact = candidates.filter((candidate) => candidate.local === local).sort((a, b) => a.instant - b.instant)[0]
134+
if (exact) return exact.instant
135+
return (
136+
candidates
137+
.filter((candidate) => candidate.local > local)
138+
.sort((a, b) => a.local - b.local || a.instant - b.instant)[0]?.instant ?? Number.NaN
139+
)
130140
}

‎packages/cli/src/commands/handlers/schedule.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ export const runNow = Runtime.handler(Commands.commands.schedule.commands["run-n
107107
const task = yield* request(server.endpoint.url, (signal) =>
108108
sdk.schedule.runNow({ scheduleID: input.id }, { signal }),
109109
)
110-
if (task.lastError) {
111-
process.stdout.write(`Ran ${task.id}, but the run recorded an error:${EOL}${task.lastError}${EOL}`)
110+
const run = task.recentRuns?.[0]
111+
if (run?.status === "failed") {
112+
process.stdout.write(`Run for ${task.id} failed:${EOL}${run.errorText ?? "Unknown error"}${EOL}`)
112113
return
113114
}
114-
write(`Ran ${task.id}`)
115+
write(run?.status === "completed" ? `Completed run for ${task.id}` : `Started run for ${task.id}`)
115116
}).pipe(Effect.catch(report)),
116117
)
117118

‎packages/cli/test/schedule.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,41 @@ describe("schedule command", () => {
148148
await server.stop(true)
149149
}
150150
})
151+
152+
test("run-now reports the current run state instead of a previous task error", async () => {
153+
const previousError = "previous attempt failed"
154+
const taskID = "task_cli_run"
155+
const server = Bun.serve({
156+
port: 0,
157+
fetch(request) {
158+
const url = new URL(request.url)
159+
if (url.pathname === "/api/info")
160+
return Response.json({ version: OPENCODE_VERSION, pid: process.pid, urls: [], paths: { tmp: "/tmp" } })
161+
if (url.pathname === `/api/schedule/${taskID}/run-now`)
162+
return Response.json({
163+
id: taskID,
164+
prompt: "Summarize the open PRs",
165+
spec: { kind: "daily", hour: 9, minute: 30 },
166+
timezone: "UTC",
167+
directory: process.cwd(),
168+
enabled: true,
169+
runCount: 2,
170+
lastError: previousError,
171+
recentRuns: [{ taskID, status: "running", startedAtMs: Date.now() }],
172+
} satisfies ScheduleListOutput[number])
173+
return new Response("Not found", { status: 404 })
174+
},
175+
})
176+
177+
try {
178+
const result = await cli(["schedule", "run-now", taskID, "--server", server.url.toString()])
179+
expect({ exitCode: result.exitCode, stderr: result.stderr }).toEqual({ exitCode: 0, stderr: "" })
180+
expect(result.stdout).toContain(`Started run for ${taskID}`)
181+
expect(result.stdout).not.toContain(previousError)
182+
} finally {
183+
await server.stop(true)
184+
}
185+
})
151186
})
152187

153188
async function cli(args: string[], env: Record<string, string> = {}) {

‎packages/core/src/schedule.ts‎

Lines changed: 166 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { Location } from "./location.js"
1111
import { ScheduleRunTable, ScheduleTaskTable } from "./schedule/sql.js"
1212
import { nextRunAt } from "./schedule/time.js"
1313
import { Session } from "./session.js"
14+
import { SessionEvent } from "./session/event.js"
15+
import { SessionTable } from "./session/sql.js"
1416

1517
export const ID = Schedule.ID
1618
export type ID = Schedule.ID
@@ -99,27 +101,65 @@ const layer = Layer.effect(
99101
}
100102
})
101103

102-
// Inserts the new run row and prunes that task's history to the MAX_RUNS most recent rows.
103-
const recordRun = Effect.fn("ScheduleTask.recordRun")(function* (taskID: ID, runID: string, startedAtMs: number) {
104-
yield* db
105-
.insert(ScheduleRunTable)
106-
.values({ id: runID, task_id: taskID, status: "running", started_at: startedAtMs })
107-
.run()
104+
const announce = (info: Schedule.Info) =>
105+
bus.publish(Schedule.Event.Changed, { info }, { location: { directory: info.directory } }).pipe(Effect.asVoid)
106+
107+
const announceTask = Effect.fn("ScheduleTask.announceTask")(function* (taskID: ID) {
108+
const row = yield* db.select().from(ScheduleTaskTable).where(eq(ScheduleTaskTable.id, taskID)).get().pipe(Effect.orDie)
109+
if (!row) return
110+
yield* announce(yield* infoFrom(row))
111+
})
112+
113+
const finishRun = Effect.fn("ScheduleTask.finishRun")(function* (
114+
runID: string,
115+
sessionID: Session.ID | undefined,
116+
status: Exclude<Schedule.RunStatus, "running">,
117+
errorText: string | undefined,
118+
finishedAtMs: number,
119+
) {
120+
return yield* db
121+
.transaction(() =>
122+
Effect.gen(function* () {
123+
const run = yield* db
124+
.update(ScheduleRunTable)
125+
.set({
126+
session_id: sessionID ?? null,
127+
status,
128+
finished_at: finishedAtMs,
129+
error_text: errorText ?? null,
130+
})
131+
.where(and(eq(ScheduleRunTable.id, runID), eq(ScheduleRunTable.status, "running")))
132+
.returning({ taskID: ScheduleRunTable.task_id })
133+
.get()
134+
.pipe(Effect.orDie)
135+
if (!run) return undefined
136+
yield* db
137+
.update(ScheduleTaskTable)
138+
.set({ last_session_id: sessionID ?? null, last_error: errorText ?? null })
139+
.where(eq(ScheduleTaskTable.id, run.taskID))
140+
.run()
141+
.pipe(Effect.orDie)
142+
return run.taskID
143+
}),
144+
)
108145
.pipe(Effect.orDie)
109-
const keep = yield* db
146+
})
147+
148+
const finishSessionRun = Effect.fn("ScheduleTask.finishSessionRun")(function* (
149+
sessionID: Session.ID,
150+
status: Exclude<Schedule.RunStatus, "running">,
151+
errorText: string | undefined,
152+
finishedAtMs: number,
153+
) {
154+
const run = yield* db
110155
.select({ id: ScheduleRunTable.id })
111156
.from(ScheduleRunTable)
112-
.where(eq(ScheduleRunTable.task_id, taskID))
113-
.orderBy(desc(ScheduleRunTable.started_at), desc(ScheduleRunTable.id))
114-
.limit(MAX_RUNS)
115-
.all()
116-
.pipe(Effect.orDie)
117-
if (keep.length < MAX_RUNS) return
118-
yield* db
119-
.delete(ScheduleRunTable)
120-
.where(and(eq(ScheduleRunTable.task_id, taskID), notInArray(ScheduleRunTable.id, keep.map((row) => row.id))))
121-
.run()
157+
.where(and(eq(ScheduleRunTable.session_id, sessionID), eq(ScheduleRunTable.status, "running")))
158+
.get()
122159
.pipe(Effect.orDie)
160+
if (!run) return
161+
const taskID = yield* finishRun(run.id, sessionID, status, errorText, finishedAtMs)
162+
if (taskID) yield* announceTask(taskID)
123163
})
124164

125165
const specFrom = (row: Row): Schedule.Spec => {
@@ -173,27 +213,59 @@ const layer = Layer.effect(
173213
const now = Date.now()
174214
const spec = specFrom(row)
175215
const nextRunMs = spec.kind === "once" ? null : yield* computeNext(spec, now, row.timezone).pipe(Effect.orDie)
216+
const runID = `run_${Identifier.ascending()}`
176217
const claimedRow = yield* db
177-
.update(ScheduleTaskTable)
178-
.set({
179-
last_run_at_ms: now,
180-
run_count: sql`${ScheduleTaskTable.run_count} + 1`,
181-
next_run_at_ms: nextRunMs,
182-
...(spec.kind === "once" ? { enabled: false } : {}),
183-
})
184-
.where(
185-
claimNextMs === undefined
186-
? eq(ScheduleTaskTable.id, row.id)
187-
: and(eq(ScheduleTaskTable.id, row.id), eq(ScheduleTaskTable.next_run_at_ms, claimNextMs)),
218+
.transaction(() =>
219+
Effect.gen(function* () {
220+
const claimed = yield* db
221+
.update(ScheduleTaskTable)
222+
.set({
223+
last_run_at_ms: now,
224+
run_count: sql`${ScheduleTaskTable.run_count} + 1`,
225+
next_run_at_ms: nextRunMs,
226+
...(spec.kind === "once" ? { enabled: false } : {}),
227+
})
228+
.where(
229+
claimNextMs === undefined
230+
? eq(ScheduleTaskTable.id, row.id)
231+
: and(eq(ScheduleTaskTable.id, row.id), eq(ScheduleTaskTable.next_run_at_ms, claimNextMs)),
232+
)
233+
.returning()
234+
.get()
235+
.pipe(Effect.orDie)
236+
if (!claimed) return undefined
237+
238+
yield* db
239+
.insert(ScheduleRunTable)
240+
.values({ id: runID, task_id: claimed.id, status: "running", started_at: now })
241+
.run()
242+
.pipe(Effect.orDie)
243+
const keep = yield* db
244+
.select({ id: ScheduleRunTable.id })
245+
.from(ScheduleRunTable)
246+
.where(eq(ScheduleRunTable.task_id, claimed.id))
247+
.orderBy(desc(ScheduleRunTable.started_at), desc(ScheduleRunTable.id))
248+
.limit(MAX_RUNS)
249+
.all()
250+
.pipe(Effect.orDie)
251+
if (keep.length >= MAX_RUNS) {
252+
yield* db
253+
.delete(ScheduleRunTable)
254+
.where(
255+
and(
256+
eq(ScheduleRunTable.task_id, claimed.id),
257+
notInArray(ScheduleRunTable.id, keep.map((item) => item.id)),
258+
),
259+
)
260+
.run()
261+
.pipe(Effect.orDie)
262+
}
263+
return claimed
264+
}),
188265
)
189-
.returning()
190-
.get()
191266
.pipe(Effect.orDie)
192267
if (!claimedRow) return undefined
193268

194-
const runID = `run_${Identifier.ascending()}`
195-
yield* recordRun(claimedRow.id, runID, now).pipe(Effect.orDie)
196-
197269
const title = "[scheduled] " + (claimedRow.name ?? claimedRow.prompt.slice(0, 50))
198270
// Execution is forked into the service scope so tick never blocks and run-now responses
199271
// return before the prompt finishes.
@@ -231,38 +303,23 @@ const layer = Layer.effect(
231303
yield* sessions.prompt({ sessionID: created.value.id, text: claimedRow.prompt, resume: false })
232304
yield* sessions.resume(created.value.id)
233305
}).pipe(Effect.exit)
306+
const interrupted = Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)
307+
if (interrupted) return
234308
const failure = Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined
235309
const lastError = failure === undefined ? undefined : failure instanceof Error ? failure.message : String(failure)
236-
yield* db
237-
.update(ScheduleRunTable)
238-
.set({
239-
session_id: sessionID ?? null,
240-
status: lastError === undefined ? "completed" : "failed",
241-
finished_at: Date.now(),
242-
error_text: lastError ?? null,
243-
})
244-
.where(eq(ScheduleRunTable.id, runID))
245-
.run()
246-
.pipe(Effect.orDie)
247-
yield* db
248-
.update(ScheduleTaskTable)
249-
.set({ last_session_id: sessionID ?? null, last_error: lastError ?? null })
250-
.where(eq(ScheduleTaskTable.id, claimedRow.id))
251-
.run()
252-
.pipe(Effect.orDie)
253-
const fresh = yield* db.select().from(ScheduleTaskTable).where(eq(ScheduleTaskTable.id, claimedRow.id)).get().pipe(Effect.orDie)
254-
if (!fresh) return
255-
const info = yield* infoFrom(fresh)
256-
yield* announce(info)
310+
const taskID = yield* finishRun(
311+
runID,
312+
sessionID,
313+
lastError === undefined ? "completed" : "failed",
314+
lastError,
315+
Date.now(),
316+
)
317+
if (taskID) yield* announceTask(taskID)
257318
}).pipe(Effect.forkIn(scope))
258319

259320
return yield* infoFrom(claimedRow)
260321
})
261322

262-
// Publishes schedule.changed scoped to the task's directory so location-filtered event streams see it.
263-
const announce = (info: Schedule.Info) =>
264-
bus.publish(Schedule.Event.Changed, { info }, { location: { directory: info.directory } }).pipe(Effect.asVoid)
265-
266323
const tick = Effect.fn("ScheduleTask.tick")(function* () {
267324
const now = Date.now()
268325
const due = yield* db
@@ -282,13 +339,59 @@ const layer = Layer.effect(
282339
}
283340
})
284341

285-
// A server restart interrupts in-flight runs; their rows would otherwise stay "running" forever.
286-
yield* db
287-
.update(ScheduleRunTable)
288-
.set({ status: "failed", finished_at: Date.now(), error_text: "Interrupted by server restart" })
342+
const unsubscribe = yield* bus.listen((event) => {
343+
if (Schema.is(SessionEvent.Execution.Succeeded)(event))
344+
return finishSessionRun(event.data.sessionID, "completed", undefined, event.created)
345+
if (Schema.is(SessionEvent.Execution.Failed)(event))
346+
return finishSessionRun(event.data.sessionID, "failed", event.data.error.message, event.created)
347+
if (Schema.is(SessionEvent.Execution.Interrupted)(event) && event.data.reason !== "shutdown")
348+
return finishSessionRun(
349+
event.data.sessionID,
350+
"failed",
351+
`Session execution interrupted (${event.data.reason})`,
352+
event.created,
353+
)
354+
return Effect.void
355+
})
356+
yield* Scope.addFinalizer(scope, unsubscribe)
357+
358+
// Preserve claimed V2 Sessions for SessionRestart to resume. Finished Sessions
359+
// use their projected terminal outcome; records without one cannot be recovered.
360+
const running = yield* db
361+
.select()
362+
.from(ScheduleRunTable)
289363
.where(eq(ScheduleRunTable.status, "running"))
290-
.run()
364+
.all()
291365
.pipe(Effect.orDie)
366+
yield* Effect.forEach(
367+
running,
368+
Effect.fnUntraced(function* (run) {
369+
if (!run.session_id) {
370+
const taskID = yield* finishRun(run.id, undefined, "failed", "Interrupted by server restart", Date.now())
371+
if (taskID) yield* announceTask(taskID)
372+
return
373+
}
374+
const session = yield* db
375+
.select({ time_suspended: SessionTable.time_suspended, idle_outcome: SessionTable.idle_outcome })
376+
.from(SessionTable)
377+
.where(eq(SessionTable.id, run.session_id))
378+
.get()
379+
.pipe(Effect.orDie)
380+
if (session && session.time_suspended !== null) return
381+
const status = session?.idle_outcome === "succeeded" ? "completed" : "failed"
382+
const errorText =
383+
session?.idle_outcome === "failed"
384+
? "Scheduled Session failed before its run record was finalized"
385+
: session?.idle_outcome === "interrupted"
386+
? "Scheduled Session execution was interrupted"
387+
: status === "failed"
388+
? "No recoverable Session execution was found after server restart"
389+
: undefined
390+
const taskID = yield* finishRun(run.id, run.session_id, status, errorText, Date.now())
391+
if (taskID) yield* announceTask(taskID)
392+
}),
393+
{ discard: true },
394+
)
292395

293396
yield* runTicks().pipe(Effect.forkScoped)
294397

0 commit comments

Comments
 (0)