@@ -124,6 +124,55 @@ const writeTranscript = Effect.fn("AgentSessionScanner.test.writeTranscript")(fu
124124 yield * fileSystem . utimes ( input . filePath , seconds , seconds ) ;
125125} ) ;
126126
127+ /** Lays out a linked worktree the way `git worktree add` does, without running git. */
128+ const addLinkedWorktree = Effect . fn ( "AgentSessionScanner.test.addLinkedWorktree" ) (
129+ function * ( input : {
130+ readonly commonGitDir : string ;
131+ readonly directory : string ;
132+ readonly branch : string ;
133+ } ) {
134+ const fileSystem = yield * FileSystem . FileSystem ;
135+ const path = yield * Path . Path ;
136+ const gitDir = path . join ( input . commonGitDir , "worktrees" , input . branch ) ;
137+ yield * fileSystem . makeDirectory ( gitDir , { recursive : true } ) ;
138+ yield * fileSystem . makeDirectory ( input . directory , { recursive : true } ) ;
139+ yield * fileSystem . writeFileString ( path . join ( gitDir , "commondir" ) , "../..\n" ) ;
140+ yield * fileSystem . writeFileString (
141+ path . join ( gitDir , "HEAD" ) ,
142+ `ref: refs/heads/${ input . branch } \n` ,
143+ ) ;
144+ yield * fileSystem . writeFileString ( path . join ( input . directory , ".git" ) , `gitdir: ${ gitDir } \n` ) ;
145+ } ,
146+ ) ;
147+
148+ const writeClaudeSession = Effect . fn ( "AgentSessionScanner.test.writeClaudeSession" ) (
149+ function * ( input : {
150+ readonly claudeHomePath : string ;
151+ readonly sessionId : string ;
152+ readonly cwd : string ;
153+ readonly mtimeMs : number ;
154+ } ) {
155+ const path = yield * Path . Path ;
156+ yield * writeTranscript ( {
157+ filePath : path . join ( input . claudeHomePath , "projects" , input . sessionId , "session.jsonl" ) ,
158+ contents : `${ JSON . stringify ( {
159+ type : "user" ,
160+ cwd : input . cwd ,
161+ sessionId : input . sessionId ,
162+ message : { role : "user" , content : "Fix the project" } ,
163+ } ) } \n`,
164+ mtimeMs : input . mtimeMs ,
165+ } ) ;
166+ } ,
167+ ) ;
168+
169+ const importedWorktrees = ( outcomes : ReadonlyArray < AgentSessionScanner . AgentSessionRecentThread > ) =>
170+ outcomes . flatMap ( ( outcome ) =>
171+ outcome . _tag === "Importable"
172+ ? [ { sessionId : outcome . thread . providerSessionId , worktree : outcome . worktree } ]
173+ : [ ] ,
174+ ) ;
175+
127176/** Claude session line: the first record carries the real `cwd`. */
128177const claudeSessionLine = ( cwd : string ) =>
129178 `${ JSON . stringify ( { type : "user" , cwd, sessionId : "s1" } ) } \n${ JSON . stringify ( { type : "assistant" } ) } \n` ;
@@ -2418,7 +2467,6 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
24182467 it . effect ( "imports sessions from linked worktrees of the project with their branch" , ( ) =>
24192468 Effect . gen ( function * ( ) {
24202469 const path = yield * Path . Path ;
2421- const fileSystem = yield * FileSystem . FileSystem ;
24222470 const nowMs = Date . parse ( "2026-08-24T12:00:00.000Z" ) ;
24232471 yield * TestClock . setTime ( nowMs ) ;
24242472 const claudeHomePath = yield * makeTempDir ( "t3code-claude-home-" ) ;
@@ -2427,45 +2475,27 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
24272475 const repo = yield * makeTempDir ( "t3code-workspace-repo-" ) ;
24282476 const otherRepo = yield * makeTempDir ( "t3code-workspace-other-" ) ;
24292477
2430- const addWorktree = Effect . fn ( "addWorktree" ) ( function * ( input : {
2431- readonly repo : string ;
2432- readonly directory : string ;
2433- readonly name : string ;
2434- } ) {
2435- const gitDir = path . join ( input . repo , ".git" , "worktrees" , input . name ) ;
2436- yield * fileSystem . makeDirectory ( gitDir , { recursive : true } ) ;
2437- yield * fileSystem . makeDirectory ( input . directory , { recursive : true } ) ;
2438- yield * fileSystem . writeFileString ( path . join ( gitDir , "commondir" ) , "../..\n" ) ;
2439- yield * fileSystem . writeFileString (
2440- path . join ( gitDir , "HEAD" ) ,
2441- `ref: refs/heads/${ input . name } \n` ,
2442- ) ;
2443- yield * fileSystem . writeFileString (
2444- path . join ( input . directory , ".git" ) ,
2445- `gitdir: ${ gitDir } \n` ,
2446- ) ;
2447- } ) ;
24482478 const worktree = path . join ( repo , ".claude" , "worktrees" , "feature" ) ;
24492479 const managedWorktree = path . join ( configBaseDir , "worktrees" , "repo" , "managed" ) ;
24502480 const otherWorktree = path . join ( otherRepo , "elsewhere" ) ;
2451- yield * addWorktree ( { repo, directory : worktree , name : "feature" } ) ;
2452- yield * addWorktree ( { repo, directory : managedWorktree , name : "managed" } ) ;
2453- yield * addWorktree ( { repo : otherRepo , directory : otherWorktree , name : "elsewhere" } ) ;
2454-
2455- for ( const [ index , [ sessionId , cwd ] ] of [
2456- [ "root-session" , repo ] ,
2457- [ "worktree-session" , worktree ] ,
2458- [ "managed-session" , managedWorktree ] ,
2459- [ "other-session" , otherWorktree ] ,
2481+ const commonGitDir = path . join ( repo , ".git" ) ;
2482+ yield * addLinkedWorktree ( { commonGitDir, directory : worktree , branch : "feature" } ) ;
2483+ yield * addLinkedWorktree ( { commonGitDir, directory : managedWorktree , branch : "managed" } ) ;
2484+ yield * addLinkedWorktree ( {
2485+ commonGitDir : path . join ( otherRepo , ".git" ) ,
2486+ directory : otherWorktree ,
2487+ branch : "elsewhere" ,
2488+ } ) ;
2489+
2490+ for ( const [ index , session ] of [
2491+ { sessionId : "root-session" , cwd : repo } ,
2492+ { sessionId : "worktree-session" , cwd : worktree } ,
2493+ { sessionId : "managed-session" , cwd : managedWorktree } ,
2494+ { sessionId : "other-session" , cwd : otherWorktree } ,
24602495 ] . entries ( ) ) {
2461- yield * writeTranscript ( {
2462- filePath : path . join ( claudeHomePath , "projects" , `-slug-${ index } ` , `${ sessionId } .jsonl` ) ,
2463- contents : `${ JSON . stringify ( {
2464- type : "user" ,
2465- cwd,
2466- sessionId,
2467- message : { role : "user" , content : "Fix the project" } ,
2468- } ) } \n`,
2496+ yield * writeClaudeSession ( {
2497+ ...session ,
2498+ claudeHomePath,
24692499 mtimeMs : nowMs - ( index + 1 ) * 60 * 1000 ,
24702500 } ) ;
24712501 }
@@ -2477,19 +2507,46 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
24772507 workspaceRoot : repo ,
24782508 } ) ;
24792509
2480- expect (
2481- outcomes . flatMap ( ( outcome ) =>
2482- outcome . _tag === "Importable"
2483- ? [ { sessionId : outcome . thread . providerSessionId , worktree : outcome . worktree } ]
2484- : [ ] ,
2485- ) ,
2486- ) . toEqual ( [
2510+ expect ( importedWorktrees ( outcomes ) ) . toEqual ( [
24872511 { sessionId : "root-session" , worktree : null } ,
24882512 { sessionId : "worktree-session" , worktree : { path : worktree , branch : "feature" } } ,
24892513 ] ) ;
24902514 } ) ,
24912515 ) ;
24922516
2517+ it . effect ( "imports linked worktree sessions when the project's .git is a pointer" , ( ) =>
2518+ Effect . gen ( function * ( ) {
2519+ const path = yield * Path . Path ;
2520+ const fileSystem = yield * FileSystem . FileSystem ;
2521+ const nowMs = Date . parse ( "2026-08-24T12:00:00.000Z" ) ;
2522+ yield * TestClock . setTime ( nowMs ) ;
2523+ const claudeHomePath = yield * makeTempDir ( "t3code-claude-home-" ) ;
2524+ const codexHomePath = yield * makeTempDir ( "t3code-codex-home-" ) ;
2525+ const repo = yield * makeTempDir ( "t3code-workspace-repo-" ) ;
2526+ const commonGitDir = yield * makeTempDir ( "t3code-separate-git-dir-" ) ;
2527+ yield * fileSystem . writeFileString ( path . join ( repo , ".git" ) , `gitdir: ${ commonGitDir } \n` ) ;
2528+
2529+ const worktree = path . join ( repo , ".claude" , "worktrees" , "feature" ) ;
2530+ yield * addLinkedWorktree ( { commonGitDir, directory : worktree , branch : "feature" } ) ;
2531+ yield * writeClaudeSession ( {
2532+ claudeHomePath,
2533+ sessionId : "worktree-session" ,
2534+ cwd : worktree ,
2535+ mtimeMs : nowMs ,
2536+ } ) ;
2537+
2538+ const outcomes = yield * runRecentThreadOutcomes ( {
2539+ claudeHomePath,
2540+ codexHomePath,
2541+ workspaceRoot : repo ,
2542+ } ) ;
2543+
2544+ expect ( importedWorktrees ( outcomes ) ) . toEqual ( [
2545+ { sessionId : "worktree-session" , worktree : { path : worktree , branch : "feature" } } ,
2546+ ] ) ;
2547+ } ) ,
2548+ ) ;
2549+
24932550 it . effect ( "uses one deterministic provider instance for a shared session home" , ( ) =>
24942551 Effect . gen ( function * ( ) {
24952552 const path = yield * Path . Path ;
0 commit comments