@@ -89,6 +89,11 @@ function backgroundWorkKindHoldsCompletion(kind: PendingBackgroundWorkTask["kind
8989
9090type PendingBackgroundWorkRun = Pick < OrchestrationV2Run , "id" | "ordinal" | "status" > ;
9191
92+ type PendingBackgroundWorkPullRequest = Pick <
93+ ThreadPullRequestLink ,
94+ "host" | "repository" | "number" | "url" | "source" | "watch"
95+ > ;
96+
9297type PendingBackgroundWorkProviderThread = Pick <
9398 OrchestrationV2ProviderThread ,
9499 "id" | "pendingBackgroundTasks"
@@ -227,11 +232,7 @@ export function derivePendingBackgroundWork(input: {
227232 * pass projection runs so policy cannot drift.
228233 */
229234 readonly runs ?: ReadonlyArray < PendingBackgroundWorkRun > ;
230- readonly pullRequests ?:
231- | ReadonlyArray <
232- Pick < ThreadPullRequestLink , "host" | "repository" | "number" | "url" | "source" | "watch" >
233- >
234- | undefined ;
235+ readonly pullRequests ?: ReadonlyArray < PendingBackgroundWorkPullRequest > | undefined ;
235236} ) : ReadonlyArray < PendingBackgroundWorkTask > {
236237 const hasActiveRun =
237238 input . hasActiveRun ??
@@ -242,6 +243,10 @@ export function derivePendingBackgroundWork(input: {
242243 if ( hasActiveRun ) {
243244 return [ ] ;
244245 }
246+ // A thread that never ran waits on nothing else, but a watch started on it still wakes it.
247+ if ( input . latestRun == null ) {
248+ return pullRequestWatchTasks ( input . pullRequests ) ;
249+ }
245250 if ( ! isLatestRunSettledForBackgroundWait ( input . latestRun ) ) {
246251 return [ ] ;
247252 }
@@ -276,15 +281,23 @@ export function derivePendingBackgroundWork(input: {
276281 byTaskId . set ( taskId , pendingTaskFromTurnItem ( taskId , item ) ) ;
277282 }
278283
279- for ( const link of input . pullRequests ?? [ ] ) {
280- if ( link . watch === undefined || link . source === "stack-dismissed" ) continue ;
281- const taskId = `pull-request-watch:${ threadPullRequestKeyOf ( link ) } ` ;
282- byTaskId . set ( taskId , {
283- taskId,
284- description : `Watching pull request #${ link . number } ` ,
285- kind : "monitor" ,
286- } ) ;
287- }
284+ for ( const task of pullRequestWatchTasks ( input . pullRequests ) ) byTaskId . set ( task . taskId , task ) ;
288285
289286 return Array . from ( byTaskId . values ( ) ) ;
290287}
288+
289+ function pullRequestWatchTasks (
290+ pullRequests : ReadonlyArray < PendingBackgroundWorkPullRequest > | undefined ,
291+ ) : Array < PendingBackgroundWorkTask > {
292+ return ( pullRequests ?? [ ] ) . flatMap ( ( link ) =>
293+ link . watch === undefined || link . source === "stack-dismissed"
294+ ? [ ]
295+ : [
296+ {
297+ taskId : `pull-request-watch:${ threadPullRequestKeyOf ( link ) } ` ,
298+ description : `Watching pull request #${ link . number } ` ,
299+ kind : "monitor" as const ,
300+ } ,
301+ ] ,
302+ ) ;
303+ }
0 commit comments