@@ -185,6 +185,10 @@ describe("ThreadPullRequestServiceV2 reads", () => {
185185 Effect . scoped (
186186 Effect . gen ( function * ( ) {
187187 const thread = { ...threadShell ( "turn-end-thread" ) , branch : "t3/turn-end" } ;
188+ const otherThread = {
189+ ...threadShell ( "other-turn-end-thread" ) ,
190+ branch : "t3/other-turn-end" ,
191+ } ;
188192 const project : OrchestrationProjectShell = {
189193 id : thread . projectId ,
190194 title : "Project" ,
@@ -201,11 +205,20 @@ describe("ThreadPullRequestServiceV2 reads", () => {
201205 { type : "checkpoint.captured" , threadId : thread . id } ,
202206 { type : "checkpoint.captured" , threadId : thread . id } ,
203207 { type : "run.updated" , threadId : thread . id , payload : { status : "completed" } } ,
208+ { type : "checkpoint.captured" , threadId : otherThread . id } ,
209+ { type : "run.updated" , threadId : otherThread . id , payload : { status : "completed" } } ,
210+ ] as Array < OrchestrationV2DomainEvent > ;
211+ // Arrives during the thread's own lookup, which may already have read the old host state.
212+ const duringLookup = [
213+ { type : "checkpoint.captured" , threadId : thread . id } ,
214+ { type : "thread.metadata-updated" , threadId : thread . id } ,
204215 ] as Array < OrchestrationV2DomainEvent > ;
205216 const activation = yield * Deferred . make < void > ( ) ;
206217 const sweepStarted = yield * Deferred . make < void > ( ) ;
207218 const turnEndQueued = yield * Deferred . make < void > ( ) ;
208- const lookups : Array < { readonly refresh : boolean } > = [ ] ;
219+ const lookupStarted = yield * Deferred . make < void > ( ) ;
220+ const followUpQueued = yield * Deferred . make < void > ( ) ;
221+ const lookups : Array < { readonly branch : string ; readonly refresh : boolean } > = [ ] ;
209222 const dependencies = Layer . mergeAll (
210223 Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
211224 // The batch arrives while the worker is busy with the startup sweep.
@@ -215,13 +228,20 @@ describe("ThreadPullRequestServiceV2 reads", () => {
215228 Stream . concat (
216229 Stream . fromEffect ( Deferred . succeed ( turnEndQueued , undefined ) ) . pipe ( Stream . drain ) ,
217230 ) ,
231+ Stream . concat ( Stream . fromEffect ( Deferred . await ( lookupStarted ) ) . pipe ( Stream . drain ) ) ,
232+ Stream . concat ( Stream . fromIterable ( duringLookup ) ) ,
233+ Stream . concat (
234+ Stream . fromEffect ( Deferred . succeed ( followUpQueued , undefined ) ) . pipe ( Stream . drain ) ,
235+ ) ,
218236 Stream . concat ( Stream . never ) ,
219237 ) ,
220- getPullRequestSnapshot : ( ) =>
238+ getPullRequestSnapshot : ( { threadId } ) =>
221239 Effect . succeed ( {
222240 schemaVersion : 2 ,
223241 snapshotSequence : 1 ,
224- threads : [ thread ] ,
242+ threads : [ thread , otherThread ] . filter (
243+ ( candidate ) => threadId === undefined || candidate . id === threadId ,
244+ ) ,
225245 archivedThreads : [ ] ,
226246 } ) ,
227247 } ) ,
@@ -230,13 +250,18 @@ describe("ThreadPullRequestServiceV2 reads", () => {
230250 getShell : ( ) => Effect . succeedSome ( project ) ,
231251 } ) ,
232252 Layer . mock ( GitManager . GitManager ) ( {
233- branchPullRequest : ( _input , options ) =>
253+ branchPullRequest : ( input , options ) =>
234254 Effect . gen ( function * ( ) {
235- const refresh = options ?. refresh ?? false ;
236- lookups . push ( { refresh } ) ;
237- if ( ! refresh ) {
255+ lookups . push ( { branch : input . branch , refresh : options ?. refresh ?? false } ) ;
256+ if ( ! ( yield * Deferred . isDone ( turnEndQueued ) ) ) {
238257 yield * Deferred . succeed ( sweepStarted , undefined ) ;
239258 yield * Deferred . await ( turnEndQueued ) ;
259+ } else if (
260+ input . branch === thread . branch &&
261+ ! ( yield * Deferred . isDone ( followUpQueued ) )
262+ ) {
263+ yield * Deferred . succeed ( lookupStarted , undefined ) ;
264+ yield * Deferred . await ( followUpQueued ) ;
240265 }
241266 return null ;
242267 } ) ,
@@ -272,12 +297,19 @@ describe("ThreadPullRequestServiceV2 reads", () => {
272297 const service = yield * ThreadPullRequestService . make ;
273298 yield * service . start ( ) ;
274299 yield * Deferred . succeed ( activation , undefined ) ;
275- yield * Deferred . await ( turnEndQueued ) ;
300+ yield * Deferred . await ( followUpQueued ) ;
276301 yield * service . drain ;
277302 } ) . pipe ( Effect . provide ( dependencies ) ) ;
278303
279- // The startup sweep, then one refresh lookup for the whole turn-end batch.
280- expect ( lookups ) . toEqual ( [ { refresh : false } , { refresh : true } ] ) ;
304+ // The startup sweep, one refresh lookup per thread for its turn-end batch, then one
305+ // follow-up refresh for the events that arrived during the thread's lookup.
306+ expect ( lookups ) . toEqual ( [
307+ { branch : thread . branch , refresh : false } ,
308+ { branch : otherThread . branch , refresh : false } ,
309+ { branch : thread . branch , refresh : true } ,
310+ { branch : otherThread . branch , refresh : true } ,
311+ { branch : thread . branch , refresh : true } ,
312+ ] ) ;
281313 } ) ,
282314 ) ,
283315 ) ;
0 commit comments