44 SourceControlProviderKind ,
55 ThreadId ,
66 type OrchestrationProjectShell ,
7+ type OrchestrationV2DomainEvent ,
78 type OrchestrationV2ThreadShell ,
89 type OrchestrationV2ServerCommand ,
910} from "@t3tools/contracts" ;
@@ -15,8 +16,11 @@ import * as Effect from "effect/Effect";
1516import * as FileSystem from "effect/FileSystem" ;
1617import * as Layer from "effect/Layer" ;
1718import * as Option from "effect/Option" ;
19+ import * as Queue from "effect/Queue" ;
1820import * as Stream from "effect/Stream" ;
21+ import * as TestClock from "effect/testing/TestClock" ;
1922
23+ import * as BackgroundPolicy from "../background/BackgroundPolicy.ts" ;
2024import * as GitManager from "../git/GitManager.ts" ;
2125import * as ProjectStore from "./ProjectStore.ts" ;
2226import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts" ;
@@ -78,6 +82,105 @@ describe("ThreadPullRequestServiceV2 reads", () => {
7882 } ;
7983 } ;
8084
85+ it . effect ( "does not sweep branch pull requests while no client is connected" , ( ) =>
86+ Effect . scoped (
87+ Effect . gen ( function * ( ) {
88+ // Reproduces #23702: an idle server with one active branch thread kept
89+ // calling GitHub for that branch every minute with no client connected.
90+ const thread = { ...threadShell ( "idle-branch-thread" ) , branch : "feature/idle" } ;
91+ const project : OrchestrationProjectShell = {
92+ id : thread . projectId ,
93+ title : "Project" ,
94+ workspaceRoot : "/workspace/project" ,
95+ defaultModelSelection : null ,
96+ scripts : [ ] ,
97+ createdAt : "2026-08-01T00:00:00.000Z" ,
98+ updatedAt : "2026-08-01T00:00:00.000Z" ,
99+ } ;
100+ const activation = yield * Deferred . make < void > ( ) ;
101+ const domainEvents = yield * Queue . unbounded < OrchestrationV2DomainEvent > ( ) ;
102+ const turnLookup = yield * Deferred . make < void > ( ) ;
103+ const lookups : Array < { readonly refresh : boolean } > = [ ] ;
104+ const dependencies = Layer . mergeAll (
105+ Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
106+ streamDomainEvents : Stream . fromQueue ( domainEvents ) ,
107+ getShellSnapshot : ( ) =>
108+ Effect . succeed ( {
109+ schemaVersion : 2 ,
110+ snapshotSequence : 1 ,
111+ threads : [ thread ] ,
112+ archivedThreads : [ ] ,
113+ } ) ,
114+ getThreadEventSequence : ( ) => Effect . succeed ( 1 ) ,
115+ getThreadShell : ( ) => Effect . succeed ( thread ) ,
116+ } ) ,
117+ Layer . mock ( ProjectStore . ProjectStoreV2 ) ( {
118+ listShells : ( ) => Effect . succeed ( [ project ] ) ,
119+ getShell : ( ) => Effect . succeed ( Option . some ( project ) ) ,
120+ } ) ,
121+ Layer . mock ( GitManager . GitManager ) ( {
122+ branchPullRequest : ( _input , options ) =>
123+ Effect . gen ( function * ( ) {
124+ const refresh = options ?. refresh ?? false ;
125+ lookups . push ( { refresh } ) ;
126+ if ( refresh ) yield * Deferred . succeed ( turnLookup , undefined ) ;
127+ return null ;
128+ } ) ,
129+ } ) ,
130+ Layer . mock ( PullRequestService . PullRequestService ) ( { } ) ,
131+ Layer . mock ( RepositoryIdentityResolver . RepositoryIdentityResolver ) ( {
132+ resolve : ( ) =>
133+ Effect . succeed ( {
134+ canonicalKey : "github.com/acme/project" ,
135+ locator : {
136+ source : "git-remote" as const ,
137+ remoteName : "origin" ,
138+ remoteUrl :
"[email protected] :acme/project.git" , 139+ } ,
140+ provider : "github" as const ,
141+ owner : "acme" ,
142+ name : "project" ,
143+ } ) ,
144+ } ) ,
145+ // No client holds an activity lease.
146+ Layer . mock ( BackgroundPolicy . BackgroundPolicy ) ( {
147+ hasDemand : ( ) => Effect . succeed ( false ) ,
148+ shouldRunScopeWork : ( ) => Effect . succeed ( false ) ,
149+ shouldRunOpportunisticWork : Effect . succeed ( false ) ,
150+ } ) ,
151+ Layer . succeed ( ServerActivation . ServerActivation , Deferred . await ( activation ) ) ,
152+ Layer . succeed (
153+ Crypto . Crypto ,
154+ Crypto . make ( {
155+ randomBytes : ( size ) => new Uint8Array ( size ) . fill ( 1 ) ,
156+ digest : ( _algorithm , data ) => Effect . succeed ( data ) ,
157+ } ) ,
158+ ) ,
159+ FileSystem . layerNoop ( { } ) ,
160+ ) ;
161+ yield * Effect . gen ( function * ( ) {
162+ const service = yield * ThreadPullRequestService . make ;
163+ yield * service . start ( ) ;
164+ yield * Deferred . succeed ( activation , undefined ) ;
165+
166+ // A finished turn still discovers the pull request for its branch.
167+ yield * Queue . offer ( domainEvents , {
168+ type : "checkpoint.captured" ,
169+ threadId : thread . id ,
170+ } as OrchestrationV2DomainEvent ) ;
171+ yield * Deferred . await ( turnLookup ) ;
172+ yield * service . drain ;
173+ const lookupsBeforeIdle = lookups . length ;
174+
175+ yield * TestClock . adjust ( "10 minutes" ) ;
176+ yield * service . drain ;
177+
178+ expect ( lookups . slice ( lookupsBeforeIdle ) ) . toEqual ( [ ] ) ;
179+ } ) . pipe ( Effect . provide ( dependencies ) ) ;
180+ } ) ,
181+ ) ,
182+ ) ;
183+
81184 it . effect . each ( [
82185 { state : "closed" , sameBranch : false } ,
83186 { state : "merged" , sameBranch : false } ,
@@ -163,6 +266,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
163266 name : "project" ,
164267 } ) ,
165268 } ) ,
269+ Layer . mock ( BackgroundPolicy . BackgroundPolicy ) ( { } ) ,
166270 Layer . succeed ( ServerActivation . ServerActivation , Deferred . await ( activation ) ) ,
167271 Layer . succeed (
168272 Crypto . Crypto ,
0 commit comments