@@ -556,6 +556,132 @@ describe("ThreadPullRequestServiceV2 reads", () => {
556556 ) ,
557557 ) ;
558558
559+ it . effect . each ( [
560+ { state : "open" , checkedOut : false } ,
561+ { state : "merged" , checkedOut : false } ,
562+ { state : "closed" , checkedOut : false } ,
563+ { state : "merged" , checkedOut : true } ,
564+ ] as const ) (
565+ "associates a $state branch PR (checkedOut=$checkedOut) only with threads that predate its completion or saved it" ,
566+ ( { state, checkedOut } ) =>
567+ Effect . scoped (
568+ Effect . gen ( function * ( ) {
569+ const reference = {
570+ projectId : ProjectId . make ( "project-1" ) ,
571+ repository : "acme/project" ,
572+ number : 42 ,
573+ url : "https://github.com/acme/project/pull/42" ,
574+ } ;
575+ const terminalAt = "2026-09-19T00:00:00.000Z" ;
576+ const detected = {
577+ number : 42 ,
578+ url : reference . url ,
579+ title : "Previous staging work" ,
580+ baseRef : "main" ,
581+ headRef : "staging" ,
582+ state,
583+ updatedAt : terminalAt ,
584+ closedAt : state === "closed" ? terminalAt : null ,
585+ mergedAt : state === "merged" ? terminalAt : null ,
586+ repositoryKey : "github.com/acme/project" ,
587+ } ;
588+ const threads = [
589+ { ...threadShell ( "older" ) , createdAt : DateTime . makeUnsafe ( "2026-09-18T00:00:00Z" ) } ,
590+ { ...threadShell ( "same-time" ) , createdAt : DateTime . makeUnsafe ( terminalAt ) } ,
591+ threadShell ( "new" ) ,
592+ { ...threadShell ( "saved" ) , branchPullRequest : reference } ,
593+ { ...threadShell ( "explicit" ) , linkedPullRequest : reference } ,
594+ ] . map ( ( thread ) => ( { ...thread , branch : "staging" } ) ) ;
595+ const project : OrchestrationProjectShell = {
596+ id : reference . projectId ,
597+ title : "Project" ,
598+ workspaceRoot : "/workspace/project" ,
599+ defaultModelSelection : null ,
600+ scripts : [ ] ,
601+ createdAt : "2026-08-01T00:00:00.000Z" ,
602+ updatedAt : "2026-08-01T00:00:00.000Z" ,
603+ } ;
604+ const activation = yield * Deferred . make < void > ( ) ;
605+ const sweepStarted = yield * Deferred . make < void > ( ) ;
606+ const commands : Array < OrchestrationV2ServerCommand > = [ ] ;
607+ const dependencies = Layer . mergeAll (
608+ Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
609+ streamDomainEvents : Stream . never ,
610+ getPullRequestSnapshot : ( ) =>
611+ Deferred . succeed ( sweepStarted , undefined ) . pipe (
612+ Effect . as ( { snapshotSequence : 1 , threads } ) ,
613+ ) ,
614+ dispatch : ( command ) =>
615+ Effect . sync ( ( ) => {
616+ commands . push ( command ) ;
617+ return { sequence : 2 , storedEvents : [ ] } ;
618+ } ) ,
619+ } ) ,
620+ Layer . mock ( ProjectStore . ProjectStoreV2 ) ( {
621+ listShells : ( ) => Effect . succeed ( [ project ] ) ,
622+ getShell : ( ) => Effect . succeedSome ( project ) ,
623+ } ) ,
624+ Layer . mock ( GitManager . GitManager ) ( {
625+ branchPullRequest : ( ) => Effect . succeed ( detected ) ,
626+ checkedOutPullRequest : ( ) => Effect . succeed ( checkedOut ) ,
627+ } ) ,
628+ Layer . mock ( PullRequestService . PullRequestService ) ( {
629+ summary : ( ) =>
630+ Effect . succeed ( {
631+ ...detected ,
632+ ...reference ,
633+ provider : SourceControlProviderKind . make ( "github" ) ,
634+ baseBranch : detected . baseRef ,
635+ headBranch : detected . headRef ,
636+ } ) ,
637+ } ) ,
638+ Layer . mock ( RepositoryIdentityResolver . RepositoryIdentityResolver ) ( {
639+ resolve : ( ) =>
640+ Effect . succeed ( {
641+ canonicalKey : "github.com/acme/project" ,
642+ locator : {
643+ source : "git-remote" as const ,
644+ remoteName : "origin" ,
645+ remoteUrl :
"[email protected] :acme/project.git" , 646+ } ,
647+ provider : SourceControlProviderKind . make ( "github" ) ,
648+ owner : "acme" ,
649+ name : "project" ,
650+ } ) ,
651+ } ) ,
652+ Layer . mock ( BackgroundPolicy . BackgroundPolicy ) ( { } ) ,
653+ Layer . succeed ( ServerActivation . ServerActivation , Deferred . await ( activation ) ) ,
654+ Layer . succeed (
655+ SecureRandom . SecureRandom ,
656+ SecureRandom . make ( ( size ) => new Uint8Array ( size ) . fill ( 1 ) ) ,
657+ ) ,
658+ FileSystem . layerNoop ( { } ) ,
659+ Path . layer ,
660+ ) ;
661+ yield * Effect . gen ( function * ( ) {
662+ const service = yield * ThreadPullRequestService . make ;
663+ yield * service . start ( ) ;
664+ yield * Deferred . succeed ( activation , undefined ) ;
665+ yield * Deferred . await ( sweepStarted ) ;
666+ yield * service . drain ;
667+ } ) . pipe ( Effect . provide ( dependencies ) ) ;
668+
669+ expect (
670+ commands . map ( ( command ) => {
671+ if ( command . type !== "thread.pull-request.sync" ) throw new Error ( command . type ) ;
672+ expect ( command ) . not . toHaveProperty ( "linkedPullRequest" ) ;
673+ return [ command . threadId , command . branchPullRequest ] ;
674+ } ) ,
675+ ) . toEqual (
676+ ( state === "open" || checkedOut
677+ ? [ "older" , "same-time" , "new" , "explicit" ]
678+ : [ "older" , "same-time" ]
679+ ) . map ( ( id ) => [ id , reference ] ) ,
680+ ) ;
681+ } ) ,
682+ ) ,
683+ ) ;
684+
559685 it . effect . each ( [
560686 { state : "closed" , sameBranch : false } ,
561687 { state : "merged" , sameBranch : false } ,
@@ -590,6 +716,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
590716 const activation = yield * Deferred . make < void > ( ) ;
591717 const sweepStarted = yield * Deferred . make < void > ( ) ;
592718 const commands : Array < OrchestrationV2ServerCommand > = [ ] ;
719+ let snapshotThread : unknown ;
593720 const dependencies = Layer . mergeAll (
594721 Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
595722 streamDomainEvents : Stream . never ,
@@ -617,6 +744,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
617744 ] )
618745 yield * sql . unsafe ( `DROP TABLE ${ table } ` ) ;
619746 const snapshot = yield * store . getPullRequestSnapshot ( options ) ;
747+ snapshotThread = snapshot . threads [ 0 ] ;
620748 yield * Deferred . succeed ( sweepStarted , undefined ) ;
621749 return snapshot ;
622750 } ) . pipe (
@@ -681,6 +809,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
681809 yield * service . drain ;
682810 } ) . pipe ( Effect . provide ( dependencies ) ) ;
683811
812+ expect ( snapshotThread ) . toHaveProperty ( "createdAt" , NOW ) ;
684813 if ( sameBranch ) {
685814 expect ( commands ) . toEqual ( [ ] ) ;
686815 } else {
0 commit comments