11import { siblingPullRequestUrl } from "@t3tools/shared/changeRequestUrl" ;
22import {
33 CommandId ,
4+ type ProjectId ,
45 type PullRequestSummary ,
56 type ThreadId ,
67 type ThreadPullRequestKey ,
@@ -158,7 +159,7 @@ export const make = Effect.gen(function* () {
158159 // linking dozens of pull requests) is read together and shares the summary batches.
159160 let requestedSweepQueued = false ;
160161 const retryStacks = new Set < string > ( ) ;
161- const missing = new Set < string > ( ) ;
162+ const missing = new Map < string , ProjectId > ( ) ;
162163 // Rate limit pauses by project and host, since each project reads with its own credential.
163164 // A paused host refuses every read without asking it, so the sweep leaves its pull requests
164165 // due until the pause ends rather than failing each of them every minute.
@@ -208,7 +209,12 @@ export const make = Effect.gen(function* () {
208209
209210 for ( const key of lastSyncedAt . keys ( ) ) if ( ! groups . has ( key ) ) lastSyncedAt . delete ( key ) ;
210211 for ( const key of retryStacks ) if ( ! groups . has ( key ) ) retryStacks . delete ( key ) ;
211- for ( const key of missing ) if ( ! groups . has ( key ) ) missing . delete ( key ) ;
212+ for ( const [ key , projectId ] of missing ) {
213+ if ( groups . get ( key ) ?. [ 0 ] ?. thread . projectId !== projectId ) {
214+ missing . delete ( key ) ;
215+ lastSyncedAt . delete ( key ) ;
216+ }
217+ }
212218 for ( const key of requested . keys ( ) ) if ( ! groups . has ( key ) ) requested . delete ( key ) ;
213219
214220 const linkedThisSweep = new Set < string > ( ) ;
@@ -296,7 +302,7 @@ export const make = Effect.gen(function* () {
296302 Effect . tapError ( ( error ) =>
297303 error . reason === "not-found"
298304 ? Effect . sync ( ( ) => {
299- missing . add ( key ) ;
305+ missing . set ( key , ref . projectId ) ;
300306 // Not-found is a host answer: consume only this refresh and start its backoff.
301307 lastSyncedAt . set ( key , nowMs ) ;
302308 if ( requested . get ( key ) === generation ) requested . delete ( key ) ;
@@ -432,6 +438,28 @@ export const make = Effect.gen(function* () {
432438 Effect . catchCause ( logSkipped ( "pull request refresh after run skipped" , { threadId } ) ) ,
433439 ) ;
434440
441+ const requestUnsyncedLinks = Effect . fnUntraced (
442+ function * ( links : ReadonlyArray < ThreadPullRequestLink > ) {
443+ const unsynced = links . filter ( ( link ) => link . snapshot === null ) ;
444+ const threads = unsynced . some ( ( link ) => missing . has ( threadPullRequestKeyOf ( link ) ) )
445+ ? yield * projections . getThreadsWithPullRequests ( )
446+ : [ ] ;
447+ for ( const link of unsynced ) {
448+ const projectId = missing . get ( threadPullRequestKeyOf ( link ) ) ;
449+ if ( projectId !== undefined ) {
450+ const first = threads . find ( ( thread ) =>
451+ visibleThreadPullRequests ( thread . pullRequests ?? [ ] ) . some ( ( candidate ) =>
452+ threadPullRequestKeysEqual ( candidate , link ) ,
453+ ) ,
454+ ) ;
455+ if ( first === undefined || first . projectId === projectId ) continue ;
456+ }
457+ yield * requestSync ( link ) ;
458+ }
459+ } ,
460+ Effect . catchCause ( logSkipped ( "pull request refresh after link update skipped" , { } ) ) ,
461+ ) ;
462+
435463 const start : PullRequestSyncReactor [ "Service" ] [ "start" ] = Effect . fn (
436464 "PullRequestSyncReactor.start" ,
437465 ) ( function * ( ) {
@@ -449,17 +477,10 @@ export const make = Effect.gen(function* () {
449477 Stream . runForEach ( events , ( event ) => {
450478 switch ( event . type ) {
451479 case "thread.pull-request-link-updated" :
452- return event . payload . snapshot === null &&
453- ! missing . has ( threadPullRequestKeyOf ( event . payload ) )
454- ? requestSync ( event . payload )
455- : Effect . void ;
480+ return requestUnsyncedLinks ( [ event . payload ] ) ;
456481 case "thread.pull-request-synced" :
457- return Effect . forEach (
458- visibleThreadPullRequests ( event . payload . pullRequests ?? [ ] ) . filter (
459- ( link ) => link . snapshot === null && ! missing . has ( threadPullRequestKeyOf ( link ) ) ,
460- ) ,
461- requestSync ,
462- { discard : true } ,
482+ return requestUnsyncedLinks (
483+ visibleThreadPullRequests ( event . payload . pullRequests ?? [ ] ) ,
463484 ) ;
464485 // An agent can merge or close its pull request from a shell (`gh pr merge`), which
465486 // sends no merge notification. When a run that ran such a command ends, read the
0 commit comments