@@ -24,6 +24,7 @@ import * as Layer from "effect/Layer";
2424import * as Option from "effect/Option" ;
2525import * as Schedule from "effect/Schedule" ;
2626import * as Schema from "effect/Schema" ;
27+ import * as Semaphore from "effect/Semaphore" ;
2728import type * as Scope from "effect/Scope" ;
2829
2930import {
@@ -43,6 +44,9 @@ import {
4344/** Checks take minutes; faster sweeps waste the account's shared host quota. */
4445const SWEEP_MINUTES = 2 ;
4546const READ_FAILURE_LIMIT = 8 ;
47+ /** Group reads and thread tails in flight at once, inside the host's own request limit. */
48+ const GROUP_READS = 4 ;
49+ const TAIL_READS = 4 ;
4650/** Without fingerprints, reread quiet PRs on snapshot changes or this interval to catch edits. */
4751const QUIET_REREAD_MS = 10 * 60_000 ;
4852/** Reread despite unchanged fingerprints; they miss review-thread comment edits. */
@@ -335,39 +339,48 @@ export const make = Effect.gen(function* () {
335339 paginated . every (
336340 ( thread ) => cached . tails . get ( thread . id ) ?. count === ( thread . commentCount ?? 0 ) ,
337341 ) ;
342+ // Each thread's cursor chain stays serial; separate threads page side by side.
343+ const read = yield * Effect . forEach (
344+ paginated ,
345+ ( thread ) =>
346+ Effect . gen ( function * ( ) {
347+ const count = thread . commentCount ?? 0 ;
348+ const known = reuse ? cached . tails . get ( thread . id ) : undefined ;
349+ if ( known !== undefined ) return known ;
350+ let cursor = thread . nextCommentsCursor ?? null ;
351+ const comments = new Map < string , PullRequestComment > ( ) ;
352+ const cursors = new Set < string > ( ) ;
353+ while ( cursor !== null ) {
354+ if ( cursors . has ( cursor ) ) return null ;
355+ cursors . add ( cursor ) ;
356+ const page : PullRequestThreadCommentsResult = yield * pullRequests . threadComments ( {
357+ ...reference ,
358+ threadId : thread . id ,
359+ cursor,
360+ } ) ;
361+ for ( const comment of page . comments ) {
362+ comments . set ( comment . id , {
363+ ...comment ,
364+ kind : "review-comment" ,
365+ path : thread . path ,
366+ reviewState : null ,
367+ } ) ;
368+ }
369+ cursor = page . nextCursor ;
370+ }
371+ if ( thread . comments . length + comments . size < count ) return null ;
372+ return { count, comments : [ ...comments . values ( ) ] } ;
373+ } ) ,
374+ { concurrency : TAIL_READS } ,
375+ ) ;
338376 const tails = new Map <
339377 string ,
340378 { count : number ; comments : ReadonlyArray < PullRequestComment > }
341379 > ( ) ;
342380 const remarks = [ ...activity . comments ] ;
343- for ( const thread of paginated ) {
344- let cursor = thread . nextCommentsCursor ?? null ;
345- const count = thread . commentCount ?? 0 ;
346- let tail = reuse ? cached . tails . get ( thread . id ) : undefined ;
347- if ( tail === undefined ) {
348- const comments = new Map < string , PullRequestComment > ( ) ;
349- const cursors = new Set < string > ( ) ;
350- while ( cursor !== null ) {
351- if ( cursors . has ( cursor ) ) return null ;
352- cursors . add ( cursor ) ;
353- const page : PullRequestThreadCommentsResult = yield * pullRequests . threadComments ( {
354- ...reference ,
355- threadId : thread . id ,
356- cursor,
357- } ) ;
358- for ( const comment of page . comments ) {
359- comments . set ( comment . id , {
360- ...comment ,
361- kind : "review-comment" ,
362- path : thread . path ,
363- reviewState : null ,
364- } ) ;
365- }
366- cursor = page . nextCursor ;
367- }
368- if ( thread . comments . length + comments . size < count ) return null ;
369- tail = { count, comments : [ ...comments . values ( ) ] } ;
370- }
381+ for ( const [ index , thread ] of paginated . entries ( ) ) {
382+ const tail = read [ index ] ;
383+ if ( tail == null ) return null ;
371384 tails . set ( thread . id , tail ) ;
372385 remarks . push ( ...tail . comments ) ;
373386 }
@@ -626,22 +639,26 @@ export const make = Effect.gen(function* () {
626639 ) ,
627640 { discard : true } ,
628641 ) ;
629- const fingerprints = yield * Effect . forEach ( groups , fingerprintOf , {
630- concurrency : "unbounded" ,
631- } ) ;
642+ // Every fingerprint is asked for at once so the host can batch them, and each group reads
643+ // as soon as its own answer lands rather than waiting on the slowest host.
644+ const reads = yield * Semaphore . make ( GROUP_READS ) ;
632645 yield * Effect . forEach (
633646 groups ,
634- ( group , index ) => {
635- const fingerprint = fingerprints [ index ] ! ;
636- return fingerprint === "paused"
637- ? Effect . void
638- : readGroup ( group , fingerprint ) . pipe (
639- Effect . catchCause (
640- logFailure ( "pull request watch check failed" , { pullRequest : group . key } ) ,
641- ) ,
642- ) ;
643- } ,
644- { concurrency : 4 , discard : true } ,
647+ ( group ) =>
648+ fingerprintOf ( group ) . pipe (
649+ Effect . flatMap ( ( fingerprint ) =>
650+ fingerprint === "paused"
651+ ? Effect . void
652+ : reads . withPermit (
653+ readGroup ( group , fingerprint ) . pipe (
654+ Effect . catchCause (
655+ logFailure ( "pull request watch check failed" , { pullRequest : group . key } ) ,
656+ ) ,
657+ ) ,
658+ ) ,
659+ ) ,
660+ ) ,
661+ { concurrency : "unbounded" , discard : true } ,
645662 ) ;
646663 } ) . pipe (
647664 Effect . catchCause ( logFailure ( "pull request watch sweep failed" , { } ) ) ,
0 commit comments