@@ -11,6 +11,8 @@ import { Location } from "./location.js"
1111import { ScheduleRunTable , ScheduleTaskTable } from "./schedule/sql.js"
1212import { nextRunAt } from "./schedule/time.js"
1313import { Session } from "./session.js"
14+ import { SessionEvent } from "./session/event.js"
15+ import { SessionTable } from "./session/sql.js"
1416
1517export const ID = Schedule . ID
1618export 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