@@ -735,6 +735,7 @@ export const layer = Layer.effect(
735735 task : ScheduledTask ,
736736 trigger : "scheduled" | "manual" | "webhook" ,
737737 webhook ?: { readonly deliveryId : string ; readonly prompt : string } ,
738+ projectId ?: ScheduledTask [ "projectId" ] ,
738739 ) {
739740 const reserved = yield * Ref . modify ( activeRuns , ( active ) => {
740741 if ( active . has ( task . id ) ) return [ false , active ] as const ;
@@ -749,6 +750,7 @@ export const layer = Layer.effect(
749750 return task ;
750751 }
751752
753+ let reservedAt : string | undefined ;
752754 return yield * Effect . gen ( function * ( ) {
753755 const startedAt = yield * localNow ;
754756 const startedAtIso = iso ( startedAt ) ;
@@ -757,7 +759,7 @@ export const layer = Layer.effect(
757759 // state. The task may have been deleted, paused, or postponed since
758760 // the poll loaded it — none of those may fire.
759761 const active = yield * findTask ( task . id ) ;
760- if ( active === null ) {
762+ if ( active === null || ( projectId !== undefined && active . projectId !== projectId ) ) {
761763 if ( webhook !== undefined ) {
762764 return yield * new WebhookDeliverySkipped ( {
763765 reason : "The task was deleted before this delivery ran." ,
@@ -794,6 +796,7 @@ export const layer = Layer.effect(
794796 }
795797
796798 yield * markRunning ( active . id , startedAtIso ) ;
799+ reservedAt = startedAtIso ;
797800 yield * notifyChanged ;
798801
799802 const fireKey =
@@ -877,7 +880,9 @@ export const layer = Layer.effect(
877880 }
878881 return completed ;
879882 } ) . pipe (
880- Effect . onError ( ( cause ) => releaseStuckRun ( task , errorMessage ( cause ) ) ) ,
883+ Effect . onError ( ( cause ) =>
884+ reservedAt === undefined ? Effect . void : releaseStuckRun ( task , errorMessage ( cause ) ) ,
885+ ) ,
881886 Effect . ensuring (
882887 Ref . update ( activeRuns , ( active ) => {
883888 const next = new Set ( active ) ;
@@ -1265,7 +1270,7 @@ export const layer = Layer.effect(
12651270 taskId : input . id ,
12661271 } ) ;
12671272 }
1268- const next = yield * runTask ( task , "manual" ) . pipe (
1273+ const next = yield * runTask ( task , "manual" , undefined , input . projectId ) . pipe (
12691274 Effect . mapError ( ( cause ) =>
12701275 taskError ( "Could not run schedule task." , { taskId : input . id , cause } ) ,
12711276 ) ,
0 commit comments