@@ -179,6 +179,128 @@ describe("ThreadPullRequestServiceV2 reads", () => {
179179 ) ,
180180 ) ;
181181
182+ it . effect ( "leaves delegated child threads to their own refresh instead of the sweep" , ( ) =>
183+ Effect . scoped (
184+ Effect . gen ( function * ( ) {
185+ const parent = {
186+ ...threadShell ( "parent-thread" ) ,
187+ branch : "feature/parent" ,
188+ worktreePath : "/workspace/parent" ,
189+ } ;
190+ const child : OrchestrationV2ThreadShell = {
191+ ...threadShell ( "child-thread" ) ,
192+ branch : "t3/reply-ok" ,
193+ worktreePath : "/workspace/child" ,
194+ lineage : {
195+ rootThreadId : parent . id ,
196+ parentThreadId : parent . id ,
197+ relationshipToParent : "subagent" ,
198+ } ,
199+ } ;
200+ const project : OrchestrationProjectShell = {
201+ id : parent . projectId ,
202+ title : "Project" ,
203+ workspaceRoot : "/workspace/project" ,
204+ defaultModelSelection : null ,
205+ scripts : [ ] ,
206+ createdAt : "2026-08-01T00:00:00.000Z" ,
207+ updatedAt : "2026-08-01T00:00:00.000Z" ,
208+ } ;
209+ const activation = yield * Deferred . make < void > ( ) ;
210+ const domainEvents = yield * Queue . unbounded < OrchestrationV2DomainEvent > ( ) ;
211+ const sweeps = yield * Queue . unbounded < void > ( ) ;
212+ const childLookup = yield * Deferred . make < void > ( ) ;
213+ const lookups : Array < { readonly branch : string ; readonly refresh : boolean } > = [ ] ;
214+ const dependencies = Layer . mergeAll (
215+ Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
216+ streamDomainEvents : Stream . fromQueue ( domainEvents ) ,
217+ getShellSnapshot : ( ) =>
218+ Queue . offer ( sweeps , undefined ) . pipe (
219+ Effect . as ( {
220+ schemaVersion : 2 ,
221+ snapshotSequence : 1 ,
222+ threads : [ parent , child ] ,
223+ archivedThreads : [ ] ,
224+ } ) ,
225+ ) ,
226+ getThreadEventSequence : ( ) => Effect . succeed ( 1 ) ,
227+ getThreadShell : ( threadId ) =>
228+ Effect . succeed ( [ parent , child ] . find ( ( thread ) => thread . id === threadId ) ?? null ) ,
229+ } ) ,
230+ Layer . mock ( ProjectStore . ProjectStoreV2 ) ( {
231+ listShells : ( ) => Effect . succeed ( [ project ] ) ,
232+ getShell : ( ) => Effect . succeedSome ( project ) ,
233+ } ) ,
234+ Layer . mock ( GitManager . GitManager ) ( {
235+ branchPullRequest : ( input , options ) =>
236+ Effect . gen ( function * ( ) {
237+ const refresh = options ?. refresh ?? false ;
238+ lookups . push ( { branch : input . branch , refresh } ) ;
239+ if ( input . branch === child . branch && refresh ) {
240+ yield * Deferred . succeed ( childLookup , undefined ) ;
241+ }
242+ return null ;
243+ } ) ,
244+ } ) ,
245+ Layer . mock ( PullRequestService . PullRequestService ) ( { } ) ,
246+ Layer . mock ( RepositoryIdentityResolver . RepositoryIdentityResolver ) ( {
247+ resolve : ( ) =>
248+ Effect . succeed ( {
249+ canonicalKey : "github.com/acme/project" ,
250+ locator : {
251+ source : "git-remote" as const ,
252+ remoteName : "origin" ,
253+ remoteUrl :
"[email protected] :acme/project.git" , 254+ } ,
255+ provider : SourceControlProviderKind . make ( "github" ) ,
256+ owner : "acme" ,
257+ name : "project" ,
258+ } ) ,
259+ } ) ,
260+ Layer . mock ( BackgroundPolicy . BackgroundPolicy ) ( {
261+ shouldRunScopeWork : ( ) => Effect . succeed ( true ) ,
262+ shouldRunOpportunisticWork : Effect . succeed ( true ) ,
263+ } ) ,
264+ Layer . succeed ( ServerActivation . ServerActivation , Deferred . await ( activation ) ) ,
265+ Layer . succeed (
266+ Crypto . Crypto ,
267+ Crypto . make ( {
268+ randomBytes : ( size ) => new Uint8Array ( size ) . fill ( 1 ) ,
269+ digest : ( _algorithm , data ) => Effect . succeed ( data ) ,
270+ } ) ,
271+ ) ,
272+ FileSystem . layerNoop ( { } ) ,
273+ ) ;
274+ yield * Effect . gen ( function * ( ) {
275+ const service = yield * ThreadPullRequestService . make ;
276+ yield * service . start ( ) ;
277+ yield * Deferred . succeed ( activation , undefined ) ;
278+
279+ // Startup backfill sweep, then one periodic sweep.
280+ yield * Queue . take ( sweeps ) ;
281+ yield * service . drain ;
282+ yield * TestClock . adjust ( "1 minute" ) ;
283+ yield * Queue . take ( sweeps ) ;
284+ yield * service . drain ;
285+ const sweepLookups = lookups . map ( ( lookup ) => lookup . branch ) . toSorted ( ) ;
286+
287+ // The child's own turn still discovers its pull request.
288+ yield * Queue . offer ( domainEvents , {
289+ type : "checkpoint.captured" ,
290+ threadId : child . id ,
291+ } as OrchestrationV2DomainEvent ) ;
292+ yield * Deferred . await ( childLookup ) ;
293+ yield * service . drain ;
294+
295+ expect ( sweepLookups ) . toEqual ( [ parent . branch , parent . branch ] ) ;
296+ expect ( lookups . slice ( sweepLookups . length ) ) . toEqual ( [
297+ { branch : child . branch , refresh : true } ,
298+ ] ) ;
299+ } ) . pipe ( Effect . provide ( dependencies ) ) ;
300+ } ) ,
301+ ) ,
302+ ) ;
303+
182304 it . effect . each ( [
183305 { state : "closed" , sameBranch : false } ,
184306 { state : "merged" , sameBranch : false } ,
0 commit comments