@@ -2,10 +2,12 @@ import {
22 EventId ,
33 ProjectId ,
44 ProviderInstanceId ,
5+ SourceControlProviderKind ,
56 ThreadId ,
67 type OrchestrationProjectShell ,
78 type OrchestrationV2DomainEvent ,
89 type OrchestrationV2ThreadShell ,
10+ type OrchestrationV2ServerCommand ,
911} from "@t3tools/contracts" ;
1012import { describe , expect , it } from "@effect/vitest" ;
1113import * as Crypto from "effect/Crypto" ;
@@ -169,6 +171,124 @@ describe("ThreadPullRequestServiceV2 reads", () => {
169171 } ;
170172 } ;
171173
174+ it . effect . each ( [
175+ { state : "closed" , sameBranch : false } ,
176+ { state : "merged" , sameBranch : false } ,
177+ { state : "closed" , sameBranch : true } ,
178+ { state : "merged" , sameBranch : true } ,
179+ ] as const ) (
180+ "restores a $state automatic PR only for its own branch (sameBranch=$sameBranch)" ,
181+ ( { state, sameBranch } ) =>
182+ Effect . scoped (
183+ Effect . gen ( function * ( ) {
184+ const savedReference = {
185+ projectId : ProjectId . make ( "project-1" ) ,
186+ repository : "acme/project" ,
187+ number : 42 ,
188+ url : "https://github.com/acme/project/pull/42" ,
189+ } ;
190+ const thread = {
191+ ...threadShell ( "branch-pr-thread" ) ,
192+ branch : "main" ,
193+ branchPullRequest : savedReference ,
194+ linkedPullRequest : savedReference ,
195+ } ;
196+ const project : OrchestrationProjectShell = {
197+ id : thread . projectId ,
198+ title : "Project" ,
199+ workspaceRoot : "/workspace/project" ,
200+ defaultModelSelection : null ,
201+ scripts : [ ] ,
202+ createdAt : "2026-08-01T00:00:00.000Z" ,
203+ updatedAt : "2026-08-01T00:00:00.000Z" ,
204+ } ;
205+ const activation = yield * Deferred . make < void > ( ) ;
206+ const sweepStarted = yield * Deferred . make < void > ( ) ;
207+ const commands : Array < OrchestrationV2ServerCommand > = [ ] ;
208+ const dependencies = Layer . mergeAll (
209+ Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
210+ streamDomainEvents : Stream . never ,
211+ getShellSnapshot : ( ) =>
212+ Deferred . succeed ( sweepStarted , undefined ) . pipe (
213+ Effect . as ( {
214+ schemaVersion : 2 ,
215+ snapshotSequence : 1 ,
216+ threads : [ thread ] ,
217+ archivedThreads : [ ] ,
218+ } ) ,
219+ ) ,
220+ dispatch : ( command ) =>
221+ Effect . sync ( ( ) => {
222+ commands . push ( command ) ;
223+ return { sequence : 2 , storedEvents : [ ] } ;
224+ } ) ,
225+ } ) ,
226+ Layer . mock ( ProjectStore . ProjectStoreV2 ) ( {
227+ listShells : ( ) => Effect . succeed ( [ project ] ) ,
228+ getShell : ( ) => Effect . succeed ( Option . some ( project ) ) ,
229+ } ) ,
230+ Layer . mock ( GitManager . GitManager ) ( {
231+ branchPullRequest : ( ) => Effect . succeed ( null ) ,
232+ } ) ,
233+ Layer . mock ( PullRequestService . PullRequestService ) ( {
234+ summary : ( ) =>
235+ Effect . succeed ( {
236+ ...savedReference ,
237+ provider : SourceControlProviderKind . make ( "github" ) ,
238+ title : "Previous branch work" ,
239+ state,
240+ headBranch : sameBranch ? thread . branch : "feature/previous-work" ,
241+ baseBranch : "release" ,
242+ updatedAt : "2026-09-19T00:00:00.000Z" ,
243+ } ) ,
244+ } ) ,
245+ Layer . mock ( RepositoryIdentityResolver . RepositoryIdentityResolver ) ( {
246+ resolve : ( ) =>
247+ Effect . succeed ( {
248+ canonicalKey : "github.com/acme/project" ,
249+ locator : {
250+ source : "git-remote" as const ,
251+ remoteName : "origin" ,
252+ remoteUrl :
"[email protected] :acme/project.git" , 253+ } ,
254+ provider : "github" as const ,
255+ owner : "acme" ,
256+ name : "project" ,
257+ } ) ,
258+ } ) ,
259+ Layer . succeed ( ServerActivation . ServerActivation , Deferred . await ( activation ) ) ,
260+ Layer . succeed (
261+ Crypto . Crypto ,
262+ Crypto . make ( {
263+ randomBytes : ( size ) => new Uint8Array ( size ) . fill ( 1 ) ,
264+ digest : ( _algorithm , data ) => Effect . succeed ( data ) ,
265+ } ) ,
266+ ) ,
267+ FileSystem . layerNoop ( { } ) ,
268+ ) ;
269+ yield * Effect . gen ( function * ( ) {
270+ const service = yield * ThreadPullRequestService . make ;
271+ yield * service . start ( ) ;
272+ yield * Deferred . succeed ( activation , undefined ) ;
273+ yield * Deferred . await ( sweepStarted ) ;
274+ yield * service . drain ;
275+ } ) . pipe ( Effect . provide ( dependencies ) ) ;
276+
277+ if ( sameBranch ) {
278+ expect ( commands ) . toEqual ( [ ] ) ;
279+ } else {
280+ expect ( commands ) . toHaveLength ( 1 ) ;
281+ expect ( commands [ 0 ] ) . toMatchObject ( {
282+ type : "thread.pull-request.sync" ,
283+ threadId : thread . id ,
284+ branchPullRequest : null ,
285+ } ) ;
286+ expect ( commands [ 0 ] ) . not . toHaveProperty ( "linkedPullRequest" ) ;
287+ }
288+ } ) ,
289+ ) ,
290+ ) ;
291+
172292 it . effect ( "an event for one thread reads that thread's shell, not every thread's" , ( ) =>
173293 Effect . scoped (
174294 Effect . gen ( function * ( ) {
0 commit comments