@@ -232,9 +232,13 @@ export class ScheduledTaskService extends Context.Service<
232232 readonly delete : (
233233 input : ScheduledTaskDeleteInput ,
234234 ) => Effect . Effect < ScheduledTaskDeleteResult , ScheduledTaskError > ;
235+ /** `threadId` is the bound thread, or the thread this run launched (null if the launch failed). */
235236 readonly runNow : (
236237 input : ScheduledTaskRunNowInput ,
237- ) => Effect . Effect < ScheduledTaskRunNowResult , ScheduledTaskError > ;
238+ ) => Effect . Effect <
239+ ScheduledTaskRunNowResult & { readonly threadId : ThreadId | null } ,
240+ ScheduledTaskError
241+ > ;
238242 /** Issues a new URL token for a webhook task; the old URL stops working at once. */
239243 readonly rotateWebhookToken : (
240244 input : ScheduledTaskRotateWebhookTokenInput ,
@@ -722,7 +726,7 @@ export const layer = Layer.effect(
722726 if ( trigger !== "scheduled" ) {
723727 return yield * taskError ( "Schedule task is already running." , { taskId : task . id } ) ;
724728 }
725- return task ;
729+ return { task, threadId : null } ;
726730 }
727731
728732 return yield * Effect . gen ( function * ( ) {
@@ -744,7 +748,7 @@ export const layer = Layer.effect(
744748 if ( trigger !== "scheduled" ) {
745749 return yield * taskError ( "Schedule task not found." , { taskId : task . id } ) ;
746750 }
747- return task ;
751+ return { task, threadId : null } ;
748752 }
749753 // A next_run_at corrupted between the poll read and this re-read must
750754 // not defect the poll; an unparseable value is treated as not due.
@@ -756,7 +760,7 @@ export const layer = Layer.effect(
756760 Option . isNone ( parsedNextRunAt ) ||
757761 DateTime . toEpochMillis ( parsedNextRunAt . value ) > DateTime . toEpochMillis ( startedAt ) )
758762 ) {
759- return active ;
763+ return { task : active , threadId : null } ;
760764 }
761765 // A queued delivery must not run a task that was paused, deleted and
762766 // recreated under the same id, or switched to another trigger while
@@ -834,6 +838,12 @@ export const layer = Layer.effect(
834838 const runSucceeded = result . _tag === "Success" ;
835839 const lastRunStatus = runSucceeded ? ( "succeeded" as const ) : ( "failed" as const ) ;
836840 const lastRunError = runSucceeded ? null : errorMessage ( result . cause ) ;
841+ const threadId =
842+ active . threadId !== null
843+ ? ThreadId . make ( active . threadId )
844+ : result . _tag === "Success" && "threadId" in result . value
845+ ? result . value . threadId
846+ : null ;
837847 // Re-read the task so the next run is computed from the schedule as it
838848 // is *now* (the user may have edited or deleted it while we ran).
839849 const current = yield * findTask ( task . id ) ;
@@ -861,7 +871,7 @@ export const layer = Layer.effect(
861871 } ) ;
862872 yield * notifyChanged ;
863873 }
864- return completed ;
874+ return { task : completed , threadId } ;
865875 } ) . pipe (
866876 Effect . onError ( ( cause ) => releaseStuckRun ( task , errorMessage ( cause ) ) ) ,
867877 Effect . ensuring (
@@ -1165,12 +1175,11 @@ export const layer = Layer.effect(
11651175 taskId : input . id ,
11661176 } ) ;
11671177 }
1168- const next = yield * runTask ( task , "manual" ) . pipe (
1178+ return yield * runTask ( task , "manual" ) . pipe (
11691179 Effect . mapError ( ( cause ) =>
11701180 taskError ( "Could not run schedule task." , { taskId : input . id , cause } ) ,
11711181 ) ,
11721182 ) ;
1173- return { task : next } ;
11741183 } ) ;
11751184
11761185 const rotateWebhookToken : ScheduledTaskService [ "Service" ] [ "rotateWebhookToken" ] = ( input ) =>
@@ -1621,7 +1630,7 @@ export const layer = Layer.effect(
16211630 Metrics . increment ( Metrics . webhookRunsTotal , { outcome } ) ,
16221631 ] ) ;
16231632 yield * runTask ( task , "webhook" , { deliveryId, prompt : rendered . prompt } ) . pipe (
1624- Effect . flatMap ( ( completed ) =>
1633+ Effect . flatMap ( ( { task : completed } ) =>
16251634 completed . lastRunStatus === "failed"
16261635 ? runOutcome ( "failed" ) . pipe (
16271636 Effect . andThen ( markDeliveryFailed ( deliveryId , "The run failed to start." ) ) ,
0 commit comments