@@ -1061,6 +1061,12 @@ export const layer = Layer.effect(
10611061 next . delete ( input . id ) ;
10621062 return next ;
10631063 } ) ,
1064+ // Old deliveries still finishing must not fill a recreated task's queue.
1065+ Ref . update ( webhookQueued , ( queued ) => {
1066+ const next = new Map ( queued ) ;
1067+ next . delete ( input . id ) ;
1068+ return next ;
1069+ } ) ,
10641070 ] ) ,
10651071 ) ,
10661072 Effect . andThen ( notifyChanged ) ,
@@ -1092,15 +1098,21 @@ export const layer = Layer.effect(
10921098 }
10931099 const token = yield * newWebhookToken ;
10941100 const now = yield * localNow ;
1095- yield * sql `
1101+ // Matching created_at keeps a rotation from landing on a task deleted
1102+ // and recreated under the same id since it was loaded.
1103+ const updated = yield * sql < { task_id : string } > `
10961104 UPDATE scheduled_tasks
10971105 SET webhook_token = ${ token } , updated_at = ${ iso ( now ) }
1098- WHERE task_id = ${ input . id }
1106+ WHERE task_id = ${ input . id } AND created_at = ${ task . createdAt }
1107+ RETURNING task_id
10991108 ` . pipe (
11001109 Effect . mapError ( ( cause ) =>
11011110 taskError ( "Could not rotate webhook token." , { taskId : input . id , cause } ) ,
11021111 ) ,
11031112 ) ;
1113+ if ( updated . length === 0 ) {
1114+ return yield * taskError ( "Schedule task was deleted or replaced." , { taskId : input . id } ) ;
1115+ }
11041116 yield * notifyChanged ;
11051117 return { task : yield * loadTask ( input . id ) } ;
11061118 } ) ;
@@ -1341,30 +1353,28 @@ export const layer = Layer.effect(
13411353 }
13421354
13431355 const rendered = renderWebhookPrompt ( task . prompt , request ) ;
1344- yield * log ( "accepted" , {
1345- signatureVerified : signature !== null ,
1346- missing : rendered . missing ,
1347- renderedPrompt : rendered . prompt ,
1348- } ) ;
1349- // Bound the deliveries waiting behind a slow run, so steady traffic to
1350- // a stuck task cannot pile up parked fibers.
1356+ // Bound the deliveries one task holds, so steady traffic to a stuck
1357+ // task cannot pile up parked fibers. A refused request is not logged,
1358+ // so it cannot push real deliveries out of the log.
13511359 const queued = yield * Ref . modify ( webhookQueued , ( counts ) => {
13521360 const count = counts . get ( task . id ) ?? 0 ;
13531361 return count >= WEBHOOK_MAX_QUEUED_PER_TASK
13541362 ? ( [ false , counts ] as const )
13551363 : ( [ true , new Map ( counts ) . set ( task . id , count + 1 ) ] as const ) ;
13561364 } ) ;
1357- if ( ! queued ) {
1358- yield * markDeliveryFailed ( deliveryId , "Too many deliveries are waiting for this task." ) ;
1359- return { _tag : "rate_limited" as const } ;
1360- }
1365+ if ( ! queued ) return { _tag : "rate_limited" as const } ;
13611366 const release = Ref . update ( webhookQueued , ( counts ) => {
13621367 const next = new Map ( counts ) ;
13631368 const count = ( next . get ( task . id ) ?? 1 ) - 1 ;
13641369 if ( count <= 0 ) next . delete ( task . id ) ;
13651370 else next . set ( task . id , count ) ;
13661371 return next ;
13671372 } ) ;
1373+ yield * log ( "accepted" , {
1374+ signatureVerified : signature !== null ,
1375+ missing : rendered . missing ,
1376+ renderedPrompt : rendered . prompt ,
1377+ } ) . pipe ( Effect . onError ( ( ) => release ) ) ;
13681378 const permit = yield * webhookPermit ( task . id ) ;
13691379 yield * runTask ( task , "webhook" , { deliveryId, prompt : rendered . prompt } ) . pipe (
13701380 Effect . flatMap ( ( completed ) =>
0 commit comments