@@ -599,6 +599,102 @@ function nextQueuedRun(
599599 return queuedRunsInDeliveryOrder ( projection ) [ 0 ] ;
600600}
601601
602+ function isStalePullRequestWatchWake (
603+ projection : Pick < OrchestrationV2ThreadProjection , "thread" | "messages" > ,
604+ run : OrchestrationV2Run ,
605+ ) : boolean {
606+ const source = projection . messages . find ( ( message ) => message . id === run . userMessageId )
607+ ?. notification ?. source ;
608+ if ( source ?. kind !== "monitor" || source . pullRequest === undefined ) {
609+ return false ;
610+ }
611+ const stamp = source . pullRequest ;
612+ const link = threadPullRequestsOf ( projection . thread ) . find (
613+ ( candidate ) =>
614+ candidate . source !== "stack-dismissed" && threadPullRequestKeysEqual ( candidate , stamp ) ,
615+ ) ;
616+ if ( link === undefined ) {
617+ return true ;
618+ }
619+ if ( stamp . watchEnded === true ) {
620+ // The closed notice is the watch's final delivery, so it still goes out once
621+ // the watch is gone; only a newer watch generation supersedes it.
622+ return link . watch !== undefined && link . watch . startedAt !== stamp . watchStartedAt ;
623+ }
624+ return (
625+ link . watch === undefined ||
626+ link . watch . startedAt !== stamp . watchStartedAt ||
627+ stamp . headSha !== link . watch . headSha ||
628+ link . snapshot ?. state === "merged" ||
629+ link . snapshot ?. state === "closed"
630+ ) ;
631+ }
632+
633+ function queuedRunCancellationEvents (
634+ projection : Pick < OrchestrationV2ThreadProjection , "nodes" | "attempts" > ,
635+ run : OrchestrationV2Run ,
636+ now : DateTime . Utc ,
637+ ) : ReadonlyArray < Omit < OrchestrationV2DomainEvent , "id" > > {
638+ const rootNode =
639+ run . rootNodeId === null
640+ ? undefined
641+ : projection . nodes . find ( ( candidate ) => candidate . id === run . rootNodeId ) ;
642+ const attempt =
643+ run . activeAttemptId === null
644+ ? undefined
645+ : projection . attempts . find ( ( candidate ) => candidate . id === run . activeAttemptId ) ;
646+ return [
647+ {
648+ type : "run.updated" as const ,
649+ threadId : run . threadId ,
650+ runId : run . id ,
651+ ...( run . rootNodeId === null ? { } : { nodeId : run . rootNodeId } ) ,
652+ providerInstanceId : run . providerInstanceId ,
653+ occurredAt : now ,
654+ payload : {
655+ ...run ,
656+ status : "cancelled" as const ,
657+ queuePosition : null ,
658+ completedAt : now ,
659+ } ,
660+ } ,
661+ ...( attempt === undefined || rootNode === undefined
662+ ? [ ]
663+ : [
664+ {
665+ type : "run-attempt.updated" as const ,
666+ threadId : run . threadId ,
667+ runId : run . id ,
668+ nodeId : rootNode . id ,
669+ providerInstanceId : run . providerInstanceId ,
670+ occurredAt : now ,
671+ payload : {
672+ ...attempt ,
673+ status : "cancelled" as const ,
674+ completedAt : now ,
675+ } ,
676+ } ,
677+ ] ) ,
678+ ...( rootNode === undefined
679+ ? [ ]
680+ : [
681+ {
682+ type : "node.updated" as const ,
683+ threadId : run . threadId ,
684+ runId : run . id ,
685+ nodeId : rootNode . id ,
686+ providerInstanceId : run . providerInstanceId ,
687+ occurredAt : now ,
688+ payload : {
689+ ...rootNode ,
690+ status : "cancelled" as const ,
691+ completedAt : now ,
692+ } ,
693+ } ,
694+ ] ) ,
695+ ] ;
696+ }
697+
602698function latestStableRun (
603699 projection : Pick < OrchestrationV2ThreadProjection , "runs" > ,
604700) : OrchestrationV2Run | null {
@@ -1099,60 +1195,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
10991195 readonly now : DateTime . Utc ;
11001196 } ) =>
11011197 Effect . gen ( function * ( ) {
1102- const rootNode =
1103- input . run . rootNodeId === null
1104- ? undefined
1105- : input . projection . nodes . find ( ( candidate ) => candidate . id === input . run . rootNodeId ) ;
1106- const attempt =
1107- input . run . activeAttemptId === null
1108- ? undefined
1109- : input . projection . attempts . find (
1110- ( candidate ) => candidate . id === input . run . activeAttemptId ,
1111- ) ;
11121198 const emitEvent = emit ( input . events , input . command ) ;
1113- yield * emitEvent ( {
1114- type : "run.updated" ,
1115- threadId : input . run . threadId ,
1116- runId : input . run . id ,
1117- ...( input . run . rootNodeId === null ? { } : { nodeId : input . run . rootNodeId } ) ,
1118- providerInstanceId : input . run . providerInstanceId ,
1119- occurredAt : input . now ,
1120- payload : {
1121- ...input . run ,
1122- status : "cancelled" ,
1123- queuePosition : null ,
1124- completedAt : input . now ,
1125- } ,
1126- } ) ;
1127- if ( attempt !== undefined && rootNode !== undefined ) {
1128- yield * emitEvent ( {
1129- type : "run-attempt.updated" ,
1130- threadId : input . run . threadId ,
1131- runId : input . run . id ,
1132- nodeId : rootNode . id ,
1133- providerInstanceId : input . run . providerInstanceId ,
1134- occurredAt : input . now ,
1135- payload : {
1136- ...attempt ,
1137- status : "cancelled" ,
1138- completedAt : input . now ,
1139- } ,
1140- } ) ;
1141- }
1142- if ( rootNode !== undefined ) {
1143- yield * emitEvent ( {
1144- type : "node.updated" ,
1145- threadId : input . run . threadId ,
1146- runId : input . run . id ,
1147- nodeId : rootNode . id ,
1148- providerInstanceId : input . run . providerInstanceId ,
1149- occurredAt : input . now ,
1150- payload : {
1151- ...rootNode ,
1152- status : "cancelled" ,
1153- completedAt : input . now ,
1154- } ,
1155- } ) ;
1199+ for ( const event of queuedRunCancellationEvents ( input . projection , input . run , input . now ) ) {
1200+ yield * emitEvent ( event ) ;
11561201 }
11571202 } ) ;
11581203
@@ -1267,7 +1312,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12671312 ) =>
12681313 Effect . gen ( function * ( ) {
12691314 if ( ! ( yield * projectionStore . canStartQueuedRun ( threadId ) ) ) return ;
1270- const projection = yield * readCommandProjection ( threadId ) ;
1315+ let projection = yield * readCommandProjection ( threadId ) ;
12711316 if (
12721317 projection . thread . archivedAt !== null ||
12731318 projection . thread . deletedAt !== null ||
@@ -1289,7 +1334,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12891334 if ( usageLimitBlockedRun ( projection . runs , projection . turnItems , sessionError ) !== null ) {
12901335 return ;
12911336 }
1292- const queuedRun = nextQueuedRun ( projection ) ;
1337+ let queuedRun = nextQueuedRun ( projection ) ;
1338+ while ( queuedRun !== undefined && isStalePullRequestWatchWake ( projection , queuedRun ) ) {
1339+ const cancelledAt = yield * DateTime . now ;
1340+ yield * writeSystemEvents ( queuedRunCancellationEvents ( projection , queuedRun , cancelledAt ) ) ;
1341+ projection = yield * readCommandProjection ( threadId ) ;
1342+ queuedRun = nextQueuedRun ( projection ) ;
1343+ }
12931344 if ( queuedRun === undefined ) {
12941345 return ;
12951346 }
@@ -2369,7 +2420,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23692420 threadId : command . threadId ,
23702421 messageId : command . wake . messageId ,
23712422 text : command . wake . text ,
2372- notification : command . wake . notification ,
2423+ notification : {
2424+ ...command . wake . notification ,
2425+ source :
2426+ command . wake . notification . source . kind === "monitor"
2427+ ? {
2428+ kind : "monitor" ,
2429+ pullRequest : {
2430+ host : command . host ,
2431+ repository : command . repository ,
2432+ number : command . number ,
2433+ watchStartedAt : command . startedAt ,
2434+ headSha : command . watch ?. headSha ?? null ,
2435+ ...( command . watch === null ? { watchEnded : true } : { } ) ,
2436+ } ,
2437+ }
2438+ : command . wake . notification . source ,
2439+ } ,
23732440 attachments : [ ] ,
23742441 dispatchMode : { type : "queue_after_active" } ,
23752442 createdBy : "agent" ,
@@ -7794,60 +7861,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
77947861 } ) ;
77957862 return ;
77967863 }
7797- const queuedRootNode =
7798- queuedRun . rootNodeId === null
7799- ? undefined
7800- : projection . nodes . find ( ( candidate ) => candidate . id === queuedRun . rootNodeId ) ;
7801- const queuedAttempt =
7802- queuedRun . activeAttemptId === null
7803- ? undefined
7804- : projection . attempts . find ( ( candidate ) => candidate . id === queuedRun . activeAttemptId ) ;
7805-
78067864 const now = yield * DateTime . now ;
78077865 const emitEvent = emit ( events , command ) ;
7808- yield * emitEvent ( {
7809- type : "run.updated" ,
7810- threadId : command . threadId ,
7811- runId : queuedRun . id ,
7812- ...( queuedRun . rootNodeId === null ? { } : { nodeId : queuedRun . rootNodeId } ) ,
7813- providerInstanceId : queuedRun . providerInstanceId ,
7814- occurredAt : now ,
7815- payload : {
7816- ...queuedRun ,
7817- status : "cancelled" ,
7818- queuePosition : null ,
7819- completedAt : now ,
7820- } ,
7821- } ) ;
7822- if ( queuedAttempt !== undefined && queuedRootNode !== undefined ) {
7823- yield * emitEvent ( {
7824- type : "run-attempt.updated" ,
7825- threadId : command . threadId ,
7826- runId : queuedRun . id ,
7827- nodeId : queuedRootNode . id ,
7828- providerInstanceId : queuedRun . providerInstanceId ,
7829- occurredAt : now ,
7830- payload : {
7831- ...queuedAttempt ,
7832- status : "cancelled" ,
7833- completedAt : now ,
7834- } ,
7835- } ) ;
7836- }
7837- if ( queuedRootNode !== undefined ) {
7838- yield * emitEvent ( {
7839- type : "node.updated" ,
7840- threadId : command . threadId ,
7841- runId : queuedRun . id ,
7842- nodeId : queuedRootNode . id ,
7843- providerInstanceId : queuedRun . providerInstanceId ,
7844- occurredAt : now ,
7845- payload : {
7846- ...queuedRootNode ,
7847- status : "cancelled" ,
7848- completedAt : now ,
7849- } ,
7850- } ) ;
7866+ for ( const event of queuedRunCancellationEvents ( projection , queuedRun , now ) ) {
7867+ yield * emitEvent ( event ) ;
78517868 }
78527869 } ) ;
78537870
0 commit comments