@@ -599,6 +599,37 @@ 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+
602633function latestStableRun (
603634 projection : Pick < OrchestrationV2ThreadProjection , "runs" > ,
604635) : OrchestrationV2Run | null {
@@ -1267,7 +1298,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12671298 ) =>
12681299 Effect . gen ( function * ( ) {
12691300 if ( ! ( yield * projectionStore . canStartQueuedRun ( threadId ) ) ) return ;
1270- const projection = yield * readCommandProjection ( threadId ) ;
1301+ let projection = yield * readCommandProjection ( threadId ) ;
12711302 if (
12721303 projection . thread . archivedAt !== null ||
12731304 projection . thread . deletedAt !== null ||
@@ -1289,7 +1320,70 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12891320 if ( usageLimitBlockedRun ( projection . runs , projection . turnItems , sessionError ) !== null ) {
12901321 return ;
12911322 }
1292- const queuedRun = nextQueuedRun ( projection ) ;
1323+ let queuedRun = nextQueuedRun ( projection ) ;
1324+ while ( queuedRun !== undefined && isStalePullRequestWatchWake ( projection , queuedRun ) ) {
1325+ const cancelledAt = yield * DateTime . now ;
1326+ const staleRootNode =
1327+ queuedRun . rootNodeId === null
1328+ ? undefined
1329+ : projection . nodes . find ( ( candidate ) => candidate . id === queuedRun . rootNodeId ) ;
1330+ const staleAttempt =
1331+ queuedRun . activeAttemptId === null
1332+ ? undefined
1333+ : projection . attempts . find ( ( candidate ) => candidate . id === queuedRun . activeAttemptId ) ;
1334+ yield * writeSystemEvents ( [
1335+ {
1336+ type : "run.updated" as const ,
1337+ threadId,
1338+ runId : queuedRun . id ,
1339+ ...( queuedRun . rootNodeId === null ? { } : { nodeId : queuedRun . rootNodeId } ) ,
1340+ providerInstanceId : queuedRun . providerInstanceId ,
1341+ occurredAt : cancelledAt ,
1342+ payload : {
1343+ ...queuedRun ,
1344+ status : "cancelled" as const ,
1345+ queuePosition : null ,
1346+ completedAt : cancelledAt ,
1347+ } ,
1348+ } ,
1349+ ...( staleAttempt === undefined || staleRootNode === undefined
1350+ ? [ ]
1351+ : [
1352+ {
1353+ type : "run-attempt.updated" as const ,
1354+ threadId,
1355+ runId : queuedRun . id ,
1356+ nodeId : staleRootNode . id ,
1357+ providerInstanceId : queuedRun . providerInstanceId ,
1358+ occurredAt : cancelledAt ,
1359+ payload : {
1360+ ...staleAttempt ,
1361+ status : "cancelled" as const ,
1362+ completedAt : cancelledAt ,
1363+ } ,
1364+ } ,
1365+ ] ) ,
1366+ ...( staleRootNode === undefined
1367+ ? [ ]
1368+ : [
1369+ {
1370+ type : "node.updated" as const ,
1371+ threadId,
1372+ runId : queuedRun . id ,
1373+ nodeId : staleRootNode . id ,
1374+ providerInstanceId : queuedRun . providerInstanceId ,
1375+ occurredAt : cancelledAt ,
1376+ payload : {
1377+ ...staleRootNode ,
1378+ status : "cancelled" as const ,
1379+ completedAt : cancelledAt ,
1380+ } ,
1381+ } ,
1382+ ] ) ,
1383+ ] ) ;
1384+ projection = yield * readCommandProjection ( threadId ) ;
1385+ queuedRun = nextQueuedRun ( projection ) ;
1386+ }
12931387 if ( queuedRun === undefined ) {
12941388 return ;
12951389 }
@@ -2369,7 +2463,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23692463 threadId : command . threadId ,
23702464 messageId : command . wake . messageId ,
23712465 text : command . wake . text ,
2372- notification : command . wake . notification ,
2466+ notification : {
2467+ ...command . wake . notification ,
2468+ source :
2469+ command . wake . notification . source . kind === "monitor"
2470+ ? {
2471+ kind : "monitor" ,
2472+ pullRequest : {
2473+ host : command . host ,
2474+ repository : command . repository ,
2475+ number : command . number ,
2476+ watchStartedAt : command . startedAt ,
2477+ headSha : command . watch ?. headSha ?? null ,
2478+ ...( command . watch === null ? { watchEnded : true } : { } ) ,
2479+ } ,
2480+ }
2481+ : command . wake . notification . source ,
2482+ } ,
23732483 attachments : [ ] ,
23742484 dispatchMode : { type : "queue_after_active" } ,
23752485 createdBy : "agent" ,
0 commit comments