33 MessageId ,
44 type OrchestrationV2Notification ,
55 type PullRequestActivity ,
6+ type PullRequestComment ,
67 type PullRequestRef ,
8+ type PullRequestThreadCommentsResult ,
79 type ThreadPullRequestLink ,
810 type ThreadPullRequestWatch ,
911} from "@t3tools/contracts" ;
@@ -83,6 +85,12 @@ export const make = Effect.gen(function* () {
8385
8486 // Passes in a row that failed, per watch. Kept in memory: a restart only delays the stop.
8587 const readFailures = new Map < string , number > ( ) ;
88+ // Replies past each long thread's first page, per watch, so a pass pages a thread again only
89+ // when the host's count of it moves. Kept in memory: a restart pages each thread once more.
90+ const threadTails = new Map <
91+ string ,
92+ Map < string , { readonly count : number ; readonly comments : ReadonlyArray < PullRequestComment > } >
93+ > ( ) ;
8694
8795 // Host-level identity, with the repository as linked, the way pull request sync reads it.
8896 const identityOf = ( link : ThreadPullRequestLink ) => ( {
@@ -128,40 +136,51 @@ export const make = Effect.gen(function* () {
128136 } ) . pipe ( Effect . catch ( ( ) => record ( target , null ) ) ) ;
129137
130138 const readRemarks = Effect . fn ( "PullRequestWatchReactor.readRemarks" ) (
131- function * ( reference : PullRequestRef , activity : PullRequestActivity ) {
139+ function * ( target : WatchTarget , reference : PullRequestRef , activity : PullRequestActivity ) {
132140 // Comment cursors cannot account for missing threads. Only finish a truncated read
133141 // when the host confirms that every thread was listed.
134142 if ( activity . commentsTruncated && activity . reviewThreadsTruncated !== false ) return null ;
135143
136- const comments = new Map ( activity . comments . map ( ( comment ) => [ comment . id , comment ] ) ) ;
144+ const key = failureKey ( target ) ;
145+ let tails = threadTails . get ( key ) ;
146+ if ( tails === undefined ) {
147+ tails = new Map ( ) ;
148+ threadTails . set ( key , tails ) ;
149+ }
150+ const remarks = [ ...activity . comments ] ;
137151 for ( const thread of activity . reviewThreads ) {
138- const ids = new Set ( thread . comments . map ( ( comment ) => comment . id ) ) ;
139- const cursors = new Set < string > ( ) ;
140152 let cursor = thread . nextCommentsCursor ?? null ;
141- while ( cursor !== null ) {
142- if ( cursors . has ( cursor ) ) return null ;
143- cursors . add ( cursor ) ;
144- const page = yield * pullRequests . threadComments ( {
145- ...reference ,
146- threadId : thread . id ,
147- cursor,
148- } ) ;
149- for ( const comment of page . comments ) {
150- ids . add ( comment . id ) ;
151- comments . set ( comment . id , {
152- ...comment ,
153- kind : "review-comment" ,
154- path : thread . path ,
155- reviewState : null ,
153+ if ( cursor === null ) continue ;
154+ const count = thread . commentCount ?? 0 ;
155+ let tail = tails . get ( thread . id ) ;
156+ if ( tail ?. count !== count ) {
157+ const comments = new Map < string , PullRequestComment > ( ) ;
158+ const cursors = new Set < string > ( ) ;
159+ while ( cursor !== null ) {
160+ if ( cursors . has ( cursor ) ) return null ;
161+ cursors . add ( cursor ) ;
162+ const page : PullRequestThreadCommentsResult = yield * pullRequests . threadComments ( {
163+ ...reference ,
164+ threadId : thread . id ,
165+ cursor,
156166 } ) ;
167+ for ( const comment of page . comments ) {
168+ comments . set ( comment . id , {
169+ ...comment ,
170+ kind : "review-comment" ,
171+ path : thread . path ,
172+ reviewState : null ,
173+ } ) ;
174+ }
175+ cursor = page . nextCursor ;
157176 }
158- cursor = page . nextCursor ;
177+ if ( thread . comments . length + comments . size < count ) return null ;
178+ tail = { count, comments : [ ...comments . values ( ) ] } ;
179+ tails . set ( thread . id , tail ) ;
159180 }
160- if ( ids . size < ( thread . commentCount ?? 0 ) ) return null ;
181+ remarks . push ( ... tail . comments ) ;
161182 }
162- return [ ...comments . values ( ) ] . sort ( ( left , right ) =>
163- left . createdAt . localeCompare ( right . createdAt ) ,
164- ) ;
183+ return remarks . sort ( ( left , right ) => left . createdAt . localeCompare ( right . createdAt ) ) ;
165184 } ,
166185 Effect . catch ( ( error ) =>
167186 Effect . logWarning ( "pull request watch comment pagination failed" , { error } ) . pipe (
@@ -206,7 +225,7 @@ export const make = Effect.gen(function* () {
206225 if ( detail . state !== "open" ) return yield * record ( target , null ) ;
207226
208227 // Never advance the remark watermark past comments an incomplete read could have missed.
209- const remarks = yield * readRemarks ( reference , activity ) ;
228+ const remarks = yield * readRemarks ( target , reference , activity ) ;
210229 const report = evaluatePullRequestWatch ( watch , detail , remarks ) ;
211230 if ( report . changes . length > 0 ) {
212231 return yield * record (
@@ -233,6 +252,7 @@ export const make = Effect.gen(function* () {
233252 ) ;
234253 const keys = new Set ( targets . map ( failureKey ) ) ;
235254 for ( const key of readFailures . keys ( ) ) if ( ! keys . has ( key ) ) readFailures . delete ( key ) ;
255+ for ( const key of threadTails . keys ( ) ) if ( ! keys . has ( key ) ) threadTails . delete ( key ) ;
236256 yield * Effect . forEach (
237257 targets ,
238258 ( target ) =>
0 commit comments