@@ -5,8 +5,12 @@ import {
55 ThreadId ,
66 type OrchestrationV2ThreadShell ,
77} from "@t3tools/contracts" ;
8+ import * as Deferred from "effect/Deferred" ;
89import * as Effect from "effect/Effect" ;
10+ import * as Fiber from "effect/Fiber" ;
911import * as Layer from "effect/Layer" ;
12+ import * as Option from "effect/Option" ;
13+ import * as TestClock from "effect/testing/TestClock" ;
1014
1115import * as PullRequestService from "../pullRequest/PullRequestService.ts" ;
1216import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts" ;
@@ -93,6 +97,7 @@ it.effect.each(
9397 ) . map ( ( scenario ) => [ scenario . label , scenario ] as const ) ,
9498) ( "%s" , ( [ , scenario ] ) => {
9599 const refreshed : string [ ] = [ ] ;
100+ const lookedUp = Deferred . makeUnsafe < void > ( ) ;
96101 const threadId = ThreadId . make ( "thread-pr-refresh" ) ;
97102 const runId = RunId . make ( "completed-run" ) ;
98103 const layer = RunFinalization . layerObserver . pipe (
@@ -114,11 +119,11 @@ it.effect.each(
114119 } ) ,
115120 refreshStatus : ( ) =>
116121 Effect . die ( "turn completion must preserve known PRs and lookup backoff" ) ,
117- refreshPullRequestStatus : ( cwd ) =>
118- Effect . sync ( ( ) => {
119- refreshed . push ( cwd ) ;
120- return null ;
121- } ) ,
122+ // Requested synchronously by refresh; the lookup itself runs in the background.
123+ refreshPullRequestStatus : ( cwd ) => {
124+ refreshed . push ( cwd ) ;
125+ return Deferred . succeed ( lookedUp , undefined ) . pipe ( Effect . as ( null ) ) ;
126+ } ,
122127 } ) ,
123128 Layer . mock ( ProjectionStore . ProjectionStoreV2 ) ( {
124129 getThreadShell : ( ) =>
@@ -134,6 +139,105 @@ it.effect.each(
134139 return Effect . gen ( function * ( ) {
135140 const observer = yield * RunFinalization . RunFinalizationObserver ;
136141 yield * observer . refresh ( { cwd : "/repo" , threadId, runId } ) ;
142+ if ( scenario . expected . length > 0 ) yield * Deferred . await ( lookedUp ) ;
137143 assert . deepEqual ( refreshed , [ ...scenario . expected ] ) ;
138144 } ) . pipe ( Effect . provide ( layer ) ) ;
139145} ) ;
146+
147+ it . effect ( "returns from the post-turn refresh without waiting for the PR lookup" , ( ) => {
148+ const threadId = ThreadId . make ( "thread-slow-forge" ) ;
149+ const runId = RunId . make ( "completed-run" ) ;
150+ const layer = RunFinalization . layerObserver . pipe (
151+ Layer . provide (
152+ Layer . mergeAll (
153+ Layer . mock ( WorkspaceEntries . WorkspaceEntries ) ( { refresh : ( ) => Effect . void } ) ,
154+ Layer . mock ( PullRequestService . PullRequestService ) ( {
155+ refreshAfterTurn : ( ) => Effect . void ,
156+ } ) ,
157+ Layer . mock ( VcsStatusBroadcaster . VcsStatusBroadcaster ) ( {
158+ refreshLocalStatus : ( ) =>
159+ Effect . succeed ( {
160+ isRepo : true ,
161+ hasPrimaryRemote : true ,
162+ isDefaultRef : false ,
163+ refName : "feature" ,
164+ hasWorkingTreeChanges : false ,
165+ workingTree : { files : [ ] , insertions : 0 , deletions : 0 } ,
166+ } ) ,
167+ refreshPullRequestStatus : ( ) => Effect . never ,
168+ } ) ,
169+ Layer . mock ( ProjectionStore . ProjectionStoreV2 ) ( {
170+ getThreadShell : ( ) =>
171+ Effect . succeed ( {
172+ id : threadId ,
173+ branch : "feature" ,
174+ activeRunId : null ,
175+ } as OrchestrationV2ThreadShell ) ,
176+ } ) ,
177+ ) ,
178+ ) ,
179+ ) ;
180+ return Effect . gen ( function * ( ) {
181+ const observer = yield * RunFinalization . RunFinalizationObserver ;
182+ const refresh = yield * observer
183+ . refresh ( { cwd : "/repo" , threadId, runId } )
184+ . pipe ( Effect . timeoutOption ( "1 minute" ) , Effect . forkChild ( { startImmediately : true } ) ) ;
185+ yield * TestClock . adjust ( "1 minute" ) ;
186+ const result = yield * Fiber . join ( refresh ) ;
187+ assert . isTrue (
188+ Option . isSome ( result ) ,
189+ "turn finalization waited on the forge PR lookup, blocking the thread's next turn" ,
190+ ) ;
191+ } ) . pipe ( Effect . provide ( layer ) ) ;
192+ } ) ;
193+
194+ it . effect ( "replaces a still-running PR lookup when a newer turn finishes" , ( ) => {
195+ const threadId = ThreadId . make ( "thread-back-to-back" ) ;
196+ const runId = RunId . make ( "completed-run" ) ;
197+ const firstInterrupted = Deferred . makeUnsafe < void > ( ) ;
198+ const secondLookedUp = Deferred . makeUnsafe < void > ( ) ;
199+ let lookups = 0 ;
200+ const layer = RunFinalization . layerObserver . pipe (
201+ Layer . provide (
202+ Layer . mergeAll (
203+ Layer . mock ( WorkspaceEntries . WorkspaceEntries ) ( { refresh : ( ) => Effect . void } ) ,
204+ Layer . mock ( PullRequestService . PullRequestService ) ( {
205+ refreshAfterTurn : ( ) => Effect . void ,
206+ } ) ,
207+ Layer . mock ( VcsStatusBroadcaster . VcsStatusBroadcaster ) ( {
208+ refreshLocalStatus : ( ) =>
209+ Effect . succeed ( {
210+ isRepo : true ,
211+ hasPrimaryRemote : true ,
212+ isDefaultRef : false ,
213+ refName : "feature" ,
214+ hasWorkingTreeChanges : false ,
215+ workingTree : { files : [ ] , insertions : 0 , deletions : 0 } ,
216+ } ) ,
217+ refreshPullRequestStatus : ( ) =>
218+ ++ lookups === 1
219+ ? Effect . never . pipe (
220+ Effect . onInterrupt ( ( ) => Deferred . succeed ( firstInterrupted , undefined ) ) ,
221+ )
222+ : Deferred . succeed ( secondLookedUp , undefined ) . pipe ( Effect . as ( null ) ) ,
223+ } ) ,
224+ Layer . mock ( ProjectionStore . ProjectionStoreV2 ) ( {
225+ getThreadShell : ( ) =>
226+ Effect . succeed ( {
227+ id : threadId ,
228+ branch : "feature" ,
229+ activeRunId : null ,
230+ } as OrchestrationV2ThreadShell ) ,
231+ } ) ,
232+ ) ,
233+ ) ,
234+ ) ;
235+ return Effect . gen ( function * ( ) {
236+ const observer = yield * RunFinalization . RunFinalizationObserver ;
237+ yield * observer . refresh ( { cwd : "/repo" , threadId, runId } ) ;
238+ yield * observer . refresh ( { cwd : "/repo" , threadId, runId } ) ;
239+ yield * Deferred . await ( firstInterrupted ) ;
240+ yield * Deferred . await ( secondLookedUp ) ;
241+ assert . strictEqual ( lookups , 2 ) ;
242+ } ) . pipe ( Effect . provide ( layer ) ) ;
243+ } ) ;
0 commit comments