@@ -1031,6 +1031,106 @@ it.effect.each([1, 3])(
10311031 } ,
10321032) ;
10331033
1034+ it . effect ( "a coalesced window dispatches the prompt saved after it opened" , ( ) => {
1035+ const workers = new Map < string , Effect . Effect < void > > ( ) ;
1036+ return withService (
1037+ ( { service, launches } ) =>
1038+ Effect . gen ( function * ( ) {
1039+ const schedule = {
1040+ type : "webhook" ,
1041+ dedupe : { coalesceKey : "{{body.id}}" , coalesceWindowSeconds : 10 } ,
1042+ } ;
1043+ const { task } = yield * service . upsert (
1044+ yield * webhookTaskInput ( { prompt : "Use the original instructions." , schedule } ) ,
1045+ ) ;
1046+ const bodyText = JSON . stringify ( { id : "review" } ) ;
1047+ yield * service . triggerWebhook (
1048+ requestFor ( task , { bodyText, body : new TextEncoder ( ) . encode ( bodyText ) } ) ,
1049+ ) ;
1050+ yield * service . upsert (
1051+ yield * webhookTaskInput ( { prompt : "Use the updated instructions." , schedule } ) ,
1052+ ) ;
1053+ yield * TestClock . adjust ( "10 seconds" ) ;
1054+ yield * workers . get ( "webhook-coalescing" ) ! ;
1055+ const launched = yield * Queue . take ( launches ) ;
1056+ assert . equal ( launched . initialMessage ?. text , "Use the updated instructions." ) ;
1057+ const owner = ( yield * service . listWebhookDeliveries ( { id : task . id } ) ) . deliveries [ 0 ] ! ;
1058+ const { delivery } = yield * service . getWebhookDelivery ( {
1059+ id : task . id ,
1060+ deliveryId : owner . id ,
1061+ } ) ;
1062+ assert . equal ( delivery . renderedPrompt , "Use the updated instructions." ) ;
1063+ } ) ,
1064+ { workers } ,
1065+ ) ;
1066+ } ) ;
1067+
1068+ it . effect ( "retrying a running window keeps the prompt it was dispatched with" , ( ) => {
1069+ const workers = new Map < string , Effect . Effect < void > > ( ) ;
1070+ return withService (
1071+ ( { service, sql, launches } ) =>
1072+ Effect . gen ( function * ( ) {
1073+ const schedule = {
1074+ type : "webhook" ,
1075+ dedupe : { coalesceKey : "{{body}}" , coalesceWindowSeconds : 10 } ,
1076+ } ;
1077+ const save = ( prompt : string ) =>
1078+ Effect . flatMap ( webhookTaskInput ( { prompt, schedule } ) , service . upsert ) ;
1079+ const { task } = yield * save ( "Prompt when the window opened" ) ;
1080+ yield * service . triggerWebhook ( requestFor ( task ) ) ;
1081+ yield * save ( "Prompt at dispatch" ) ;
1082+ yield * sql `CREATE TRIGGER fail_completion BEFORE UPDATE ON scheduled_tasks
1083+ WHEN NEW.last_run_status = 'succeeded' BEGIN SELECT RAISE(FAIL, 'completion unavailable'); END` ;
1084+ yield * TestClock . adjust ( "10 seconds" ) ;
1085+ yield * drainPendingDispatch ( workers ) ;
1086+ const launched = yield * Queue . take ( launches ) ;
1087+ assert . equal ( launched . initialMessage ?. text , "Prompt at dispatch" ) ;
1088+ yield * save ( "Prompt saved after dispatch" ) ;
1089+ yield * sql `DROP TRIGGER fail_completion` ;
1090+ yield * drainPendingDispatch ( workers ) ;
1091+ assert . equal ( ( yield * sql `SELECT 1 FROM scheduled_task_webhook_pending` ) . length , 0 ) ;
1092+ const owner = ( yield * service . listWebhookDeliveries ( { id : task . id } ) ) . deliveries [ 0 ] ! ;
1093+ const { delivery } = yield * service . getWebhookDelivery ( {
1094+ id : task . id ,
1095+ deliveryId : owner . id ,
1096+ } ) ;
1097+ assert . equal ( delivery . renderedPrompt , "Prompt at dispatch" ) ;
1098+ } ) ,
1099+ { workers } ,
1100+ ) ;
1101+ } ) ;
1102+
1103+ it . effect ( "a coalesced window fails its delivery when the task row cannot be read" , ( ) => {
1104+ const workers = new Map < string , Effect . Effect < void > > ( ) ;
1105+ return withService (
1106+ ( { service, sql, launches } ) =>
1107+ Effect . gen ( function * ( ) {
1108+ const { task } = yield * service . upsert (
1109+ yield * webhookTaskInput ( {
1110+ schedule : {
1111+ type : "webhook" ,
1112+ dedupe : { coalesceKey : "{{body}}" , coalesceWindowSeconds : 10 } ,
1113+ } ,
1114+ } ) ,
1115+ ) ;
1116+ yield * service . triggerWebhook ( requestFor ( task ) ) ;
1117+ yield * sql `UPDATE scheduled_tasks SET model_selection_json = 'not json' WHERE task_id = ${ task . id } ` ;
1118+ yield * TestClock . adjust ( "10 seconds" ) ;
1119+ yield * drainPendingDispatch ( workers ) ;
1120+ assert . equal ( yield * Queue . size ( launches ) , 0 ) ;
1121+ assert . equal ( ( yield * sql `SELECT 1 FROM scheduled_task_webhook_pending` ) . length , 0 ) ;
1122+ const [ delivery ] = yield * sql < {
1123+ outcome : string ;
1124+ error : string | null ;
1125+ } > `SELECT outcome, error FROM scheduled_task_webhook_deliveries` ;
1126+ assert . equal ( delivery ?. outcome , "dispatch_failed" ) ;
1127+ // Failing before dispatch means a later successful read cannot send the stale snapshot.
1128+ assert . equal ( delivery ?. error , "The pending webhook payload could not be prepared." ) ;
1129+ } ) ,
1130+ { workers } ,
1131+ ) ;
1132+ } ) ;
1133+
10341134it . effect ( "recovers a persisted coalescing window after restarting the service" , ( ) => {
10351135 const workers = new Map < string , Effect . Effect < void > > ( ) ;
10361136 return withService (
0 commit comments