@@ -5,6 +5,7 @@ import * as Context from "effect/Context";
55import * as Crypto from "effect/Crypto" ;
66import * as DateTime from "effect/DateTime" ;
77import * as Effect from "effect/Effect" ;
8+ import * as FileSystem from "effect/FileSystem" ;
89import * as Layer from "effect/Layer" ;
910import * as Schedule from "effect/Schedule" ;
1011import type * as Scope from "effect/Scope" ;
@@ -37,6 +38,7 @@ export const make = Effect.gen(function* () {
3738 const git = yield * GitManager . GitManager ;
3839 const pullRequests = yield * PullRequestService . PullRequestService ;
3940 const crypto = yield * Crypto . Crypto ;
41+ const fileSystem = yield * FileSystem . FileSystem ;
4042
4143 const sweep = Effect . fn ( "ThreadSettlementReactor.sweep" ) ( function * (
4244 mergedPullRequest : PullRequestService . PullRequestMergeEvent | null ,
@@ -54,6 +56,26 @@ export const make = Effect.gen(function* () {
5456 mergedPullRequest . repository . toLowerCase ( ) &&
5557 thread . linkedPullRequest . number === mergedPullRequest . number ) ) ,
5658 ) ;
59+ // Use the same cwd as the sidebar so both paths share GitManager's PR cache.
60+ const lookupCwdByThreadId = new Map < string , string > ( ) ;
61+ yield * Effect . forEach (
62+ candidates ,
63+ ( thread ) =>
64+ Effect . gen ( function * ( ) {
65+ const project = projects . get ( thread . projectId ) ;
66+ if ( project === undefined || thread . linkedPullRequest != null ) return ;
67+ const worktreeExists =
68+ thread . worktreePath !== null &&
69+ ( yield * fileSystem . exists ( thread . worktreePath ) . pipe ( Effect . orElseSucceed ( ( ) => false ) ) ) ;
70+ lookupCwdByThreadId . set (
71+ thread . id ,
72+ worktreeExists && thread . worktreePath !== null
73+ ? thread . worktreePath
74+ : project . workspaceRoot ,
75+ ) ;
76+ } ) ,
77+ { concurrency : 8 , discard : true } ,
78+ ) ;
5779 const lookupKey = ( thread : ( typeof candidates ) [ number ] ) => {
5880 if ( thread . linkedPullRequest != null ) {
5981 return JSON . stringify ( [
@@ -64,11 +86,9 @@ export const make = Effect.gen(function* () {
6486 ] ) ;
6587 }
6688 if ( thread . branch === null ) return JSON . stringify ( [ "none" , thread . id ] ) ;
67- const project = projects . get ( thread . projectId ) ;
89+ const cwd = lookupCwdByThreadId . get ( thread . id ) ;
6890 return JSON . stringify (
69- project === undefined
70- ? [ "missing-project" , thread . id ]
71- : [ "branch" , project . workspaceRoot , thread . branch ] ,
91+ cwd === undefined ? [ "missing-project" , thread . id ] : [ "branch" , cwd , thread . branch ] ,
7292 ) ;
7393 } ;
7494 const groups = Map . groupBy ( candidates , lookupKey ) ;
@@ -100,11 +120,11 @@ export const make = Effect.gen(function* () {
100120 } satisfies SettlementPullRequest ;
101121 }
102122 if ( thread . branch === null ) return null ;
103- const project = projects . get ( thread . projectId ) ;
104- if ( project === undefined ) {
123+ const cwd = lookupCwdByThreadId . get ( thread . id ) ;
124+ if ( cwd === undefined ) {
105125 return yield * Effect . die ( new Error ( "thread project not found" ) ) ;
106126 }
107- return yield * git . branchPullRequest ( { cwd : project . workspaceRoot , branch : thread . branch } ) ;
127+ return yield * git . branchPullRequest ( { cwd, branch : thread . branch } ) ;
108128 } ) ;
109129
110130 yield * Effect . forEach (
0 commit comments