@@ -346,20 +346,6 @@ const make = Effect.gen(function* () {
346346 ! path . isAbsolute ( relative )
347347 ) ;
348348 } ;
349- const hasTerminal = ( worktreePath : string ) =>
350- [ ...liveTerminals . values ( ) ]
351- . flatMap ( ( entries ) => [ ...entries . values ( ) ] )
352- . some ( ( terminal ) => {
353- if ( terminal . status !== "starting" && terminal . status !== "running" ) return false ;
354- const cwd = path . resolve ( terminal . cwd ) ;
355- return (
356- ( terminal . worktreePath !== null &&
357- pathKey ( terminal . worktreePath ) === pathKey ( worktreePath ) ) ||
358- pathKey ( cwd ) === pathKey ( worktreePath ) ||
359- inside ( worktreePath , cwd )
360- ) ;
361- } ) ;
362-
363349 // pnpm imports packages by clone or reflink when its store shares a device
364350 // with the worktree. Those files have one link but share blocks with the
365351 // store, so removing them frees almost nothing. A store on another device
@@ -500,18 +486,21 @@ const make = Effect.gen(function* () {
500486 } ) ;
501487
502488 const containsProjectRoot = Effect . fn ( "StorageCleanup.containsProjectRoot" ) ( function * (
503- worktreePath : string ,
489+ worktreePaths : ReadonlyArray < string > ,
504490 projects : ReadonlyArray < { readonly workspaceRoot : string } > ,
505491 ) {
492+ const contains = ( projectPath : string ) =>
493+ worktreePaths . some (
494+ ( worktreePath ) =>
495+ pathKey ( projectPath ) === pathKey ( worktreePath ) || inside ( worktreePath , projectPath ) ,
496+ ) ;
506497 for ( const project of projects ) {
507498 const projectPath = path . resolve ( project . workspaceRoot ) ;
508- if ( pathKey ( projectPath ) === pathKey ( worktreePath ) || inside ( worktreePath , projectPath ) )
509- return true ;
499+ if ( contains ( projectPath ) ) return true ;
510500 const realPath = yield * fs
511501 . realPath ( projectPath )
512502 . pipe ( Effect . orElseSucceed ( ( ) => projectPath ) ) ;
513- if ( pathKey ( realPath ) === pathKey ( worktreePath ) || inside ( worktreePath , realPath ) )
514- return true ;
503+ if ( contains ( realPath ) ) return true ;
515504 }
516505 return false ;
517506 } ) ;
@@ -1040,20 +1029,58 @@ const make = Effect.gen(function* () {
10401029 const threadsById = new Map ( snapshot . threads . map ( ( thread ) => [ thread . id , thread ] ) ) ;
10411030 const refreshedDefaultRefs = new Map < string , Set < string > > ( ) ;
10421031 const defaultBranches = new Map < string , string | null > ( ) ;
1043- const groups = Map . groupBy (
1044- snapshot . threads . filter ( ( thread ) => thread . worktreePath !== null ) ,
1045- ( thread ) => path . resolve ( thread . worktreePath ! ) ,
1032+ // Threads may store one checkout through different symlinked spellings; compare real paths.
1033+ // Keys are only compared, never opened, so they take pathKey's Windows case folding.
1034+ const canonicalPaths = new Map < string , string > ( ) ;
1035+ const canonical = Effect . fnUntraced ( function * ( target : string ) {
1036+ const resolved = path . resolve ( target ) ;
1037+ const cached = canonicalPaths . get ( resolved ) ;
1038+ if ( cached !== undefined ) return cached ;
1039+ const realPath = yield * fs . realPath ( resolved ) . pipe ( Effect . orElseSucceed ( ( ) => resolved ) ) ;
1040+ const key = pathKey ( realPath ) ;
1041+ canonicalPaths . set ( resolved , key ) ;
1042+ return key ;
1043+ } ) ;
1044+ // A cwd in a deleted folder cannot be resolved, so stored spellings are compared too.
1045+ const occupies = Effect . fnUntraced ( function * (
1046+ worktreePaths : ReadonlyArray < string > ,
1047+ target : string ,
1048+ ) {
1049+ const targets = [ path . resolve ( target ) , yield * canonical ( target ) ] ;
1050+ return worktreePaths . some ( ( worktree ) =>
1051+ targets . some ( ( cwd ) => pathKey ( cwd ) === pathKey ( worktree ) || inside ( worktree , cwd ) ) ,
1052+ ) ;
1053+ } ) ;
1054+ const hasTerminal = Effect . fnUntraced ( function * ( worktreePaths : ReadonlyArray < string > ) {
1055+ for ( const terminal of [ ...liveTerminals . values ( ) ] . flatMap ( ( entries ) => [
1056+ ...entries . values ( ) ,
1057+ ] ) ) {
1058+ if ( terminal . status !== "starting" && terminal . status !== "running" ) continue ;
1059+ if (
1060+ ( terminal . worktreePath !== null &&
1061+ ( yield * occupies ( worktreePaths , terminal . worktreePath ) ) ) ||
1062+ ( yield * occupies ( worktreePaths , terminal . cwd ) )
1063+ )
1064+ return true ;
1065+ }
1066+ return false ;
1067+ } ) ;
1068+ const liveThreads = snapshot . threads . filter ( ( thread ) => thread . worktreePath !== null ) ;
1069+ const liveKeys = yield * Effect . forEach ( liveThreads , ( thread ) =>
1070+ canonical ( thread . worktreePath ! ) ,
10461071 ) ;
1047- const deletedByPath = Map . groupBy ( deletedThreads , ( thread ) =>
1048- path . resolve ( thread . worktreePath ! ) ,
1072+ const groups = Map . groupBy ( liveThreads , ( _thread , index ) => liveKeys [ index ] ! ) ;
1073+ const deletedKeys = yield * Effect . forEach ( deletedThreads , ( thread ) =>
1074+ canonical ( thread . worktreePath ! ) ,
10491075 ) ;
1076+ const deletedByPath = Map . groupBy ( deletedThreads , ( _thread , index ) => deletedKeys [ index ] ! ) ;
10501077 const candidates = [
10511078 ...[ ...groups ] . map (
1052- ( [ worktreePath , group ] ) =>
1079+ ( [ worktreeKey , group ] ) =>
10531080 // A deleted thread still owns the worktree its remaining subagents share,
10541081 // but only deletion cleanup acts on it; otherwise the live rules apply.
10551082 deletedByPath
1056- . get ( worktreePath )
1083+ . get ( worktreeKey )
10571084 ?. find (
10581085 ( deleted ) =>
10591086 resolveWorktreeCleanup ( serverSettings , deleted . projectId ) . worktreeOnDelete &&
@@ -1062,13 +1089,15 @@ const make = Effect.gen(function* () {
10621089 worktreeOwner ( group ) ??
10631090 group [ 0 ] ! ,
10641091 ) ,
1065- ...deletedThreads . filter ( ( thread ) => ! groups . has ( path . resolve ( thread . worktreePath ! ) ) ) ,
1092+ ...deletedThreads . filter ( ( _thread , index ) => ! groups . has ( deletedKeys [ index ] ! ) ) ,
10661093 ] ;
10671094 for ( const thread of candidates ) {
10681095 const settings = resolveWorktreeCleanup ( serverSettings , thread . projectId ) ;
10691096 if ( ! worktreeCleanupEnabled ( settings ) ) continue ;
10701097 const worktreePath = path . resolve ( thread . worktreePath ! ) ;
1071- const group = groups . get ( worktreePath ) ?? [ ] ;
1098+ const worktreeKey = yield * canonical ( worktreePath ) ;
1099+ const worktreePaths = [ worktreePath , worktreeKey ] ;
1100+ const group = groups . get ( worktreeKey ) ?? [ ] ;
10721101 const activityAt = Math . max ( ...group . map ( storageCleanupActivityAt ) ) ;
10731102 const deleted = "workspaceRoot" in thread ;
10741103 const settlementChain = deleted
@@ -1122,7 +1151,7 @@ const make = Effect.gen(function* () {
11221151 return keep ( waitForStop ( "thread is running or has pending work" ) ) ;
11231152 if ( group . some ( ( member ) => member . branch !== thread . branch ) )
11241153 return keep ( "a subagent thread's branch differs from this worktree's branch" ) ;
1125- if ( hasTerminal ( worktreePath ) ) return keep ( waitForStop ( "open terminal" ) ) ;
1154+ if ( yield * hasTerminal ( worktreePaths ) ) return keep ( waitForStop ( "open terminal" ) ) ;
11261155 if (
11271156 [ ...group , ...settlementChain . slice ( 1 ) ] . some (
11281157 ( member ) =>
@@ -1168,7 +1197,7 @@ const make = Effect.gen(function* () {
11681197 return keep ( notDueReason ) ;
11691198 const pathReason = yield * worktreePathReason ( worktreePath ) ;
11701199 if ( pathReason !== null ) return keep ( pathReason ) ;
1171- if ( yield * containsProjectRoot ( worktreePath , [ project , ...snapshot . projects ] ) )
1200+ if ( yield * containsProjectRoot ( worktreePaths , [ project , ...snapshot . projects ] ) )
11721201 return keep ( "contains a project checkout" ) ;
11731202 if ( yield * branchChanged ( worktreePath , thread . branch ) )
11741203 return keep ( "repository or branch changed" ) ;
@@ -1274,12 +1303,14 @@ const make = Effect.gen(function* () {
12741303 // Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
12751304 const blockedSinceCheck = Effect . fnUntraced ( function * ( ) {
12761305 const latestProjects = yield * projectStore . listShells ( ) ;
1277- if ( yield * containsProjectRoot ( worktreePath , [ project , ...latestProjects ] ) )
1306+ if ( yield * containsProjectRoot ( worktreePaths , [ project , ...latestProjects ] ) )
12781307 return "contains a project checkout" ;
1279- const latest = ( yield * projections . getThreadWorktreePaths ( {
1280- includeArchived : true ,
1281- } ) ) . filter ( ( entry ) => path . resolve ( entry . worktreePath ) === worktreePath ) ;
1282- if ( hasTerminal ( worktreePath ) ) return waitForStop ( "open terminal" ) ;
1308+ const bindings = yield * projections . getThreadWorktreePaths ( { includeArchived : true } ) ;
1309+ const bindingKeys = yield * Effect . forEach ( bindings , ( binding ) =>
1310+ canonical ( binding . worktreePath ) ,
1311+ ) ;
1312+ const latest = bindings . filter ( ( _binding , index ) => bindingKeys [ index ] === worktreeKey ) ;
1313+ if ( yield * hasTerminal ( worktreePaths ) ) return waitForStop ( "open terminal" ) ;
12831314 if ( deleted ) {
12841315 if (
12851316 ! resolveWorktreeCleanup ( yield * settingsService . getSettings , thread . projectId )
@@ -1300,20 +1331,23 @@ const make = Effect.gen(function* () {
13001331 projections . getThreadShell ( entry . threadId ) ,
13011332 ) ;
13021333 const latestGroup = latestShells . filter ( ( shell ) => shell !== null ) ;
1334+ const latestKeys = yield * Effect . forEach ( latestGroup , ( shell ) =>
1335+ shell . worktreePath === null ? Effect . succeed ( null ) : canonical ( shell . worktreePath ) ,
1336+ ) ;
13031337 const latestOwner = worktreeOwner ( deleted ? [ thread , ...latestGroup ] : latestGroup ) ;
13041338 if (
13051339 latestGroup . length !== group . length ||
13061340 latestOwner ?. id !== thread . id ||
13071341 latestOwner ?. branch !== thread . branch ||
1308- latestGroup . some ( ( shell ) => {
1342+ latestGroup . some ( ( shell , index ) => {
13091343 const previous = group . find ( ( member ) => member . id === shell . id ) ;
13101344 return (
13111345 previous === undefined ||
13121346 shell . branch !== thread . branch ||
13131347 ! Equal . equals ( shell . settledAt , previous . settledAt ) ||
13141348 ( shell . settledAt !== null && ! Equal . equals ( shell . updatedAt , previous . updatedAt ) ) ||
13151349 ! storageCleanupThreadIdle ( shell , now , serverSettings ) ||
1316- path . resolve ( shell . worktreePath ! ) !== worktreePath ||
1350+ latestKeys [ index ] !== worktreeKey ||
13171351 storageCleanupActivityAt ( shell ) !== storageCleanupActivityAt ( previous )
13181352 ) ;
13191353 } )
@@ -1350,9 +1384,8 @@ const make = Effect.gen(function* () {
13501384 ) ;
13511385 // Ready persisted sessions may have released their runtime; only live sessions protect the path.
13521386 for ( const session of sessions ) {
1353- const cwd = path . resolve ( session . cwd ) ;
13541387 if (
1355- ( pathKey ( cwd ) === pathKey ( worktreePath ) || inside ( worktreePath , cwd ) ) &&
1388+ ( yield * occupies ( worktreePaths , session . cwd ) ) &&
13561389 ( yield * providerSessions . isLive ( session . id ) )
13571390 )
13581391 return waitForStop ( "provider session is still open" ) ;
0 commit comments