@@ -240,20 +240,6 @@ const make = Effect.gen(function* () {
240240 ! path . isAbsolute ( relative )
241241 ) ;
242242 } ;
243- const hasTerminal = ( worktreePath : string ) =>
244- [ ...liveTerminals . values ( ) ]
245- . flatMap ( ( entries ) => [ ...entries . values ( ) ] )
246- . some ( ( terminal ) => {
247- if ( terminal . status !== "starting" && terminal . status !== "running" ) return false ;
248- const cwd = path . resolve ( terminal . cwd ) ;
249- return (
250- ( terminal . worktreePath !== null &&
251- path . resolve ( terminal . worktreePath ) === worktreePath ) ||
252- cwd === worktreePath ||
253- inside ( worktreePath , cwd )
254- ) ;
255- } ) ;
256-
257243 // pnpm imports packages by clone or reflink when its store shares a device
258244 // with the worktree. Those files have one link but share blocks with the
259245 // store, so removing them frees almost nothing. A store on another device
@@ -733,13 +719,41 @@ const make = Effect.gen(function* () {
733719 const snapshot = yield * readThreads ( ) ;
734720 const refreshedDefaultRefs = new Map < string , Set < string > > ( ) ;
735721 const defaultBranches = new Map < string , string | null > ( ) ;
736- const groups = Map . groupBy (
737- snapshot . threads . filter ( ( thread ) => thread . worktreePath !== null ) ,
738- ( thread ) => path . resolve ( thread . worktreePath ! ) ,
722+ // Threads may store one checkout through different symlinked spellings; compare real paths.
723+ const canonicalPaths = new Map < string , string > ( ) ;
724+ const canonical = Effect . fnUntraced ( function * ( target : string ) {
725+ const resolved = path . resolve ( target ) ;
726+ const cached = canonicalPaths . get ( resolved ) ;
727+ if ( cached !== undefined ) return cached ;
728+ const realPath = yield * fs . realPath ( resolved ) . pipe ( Effect . orElseSucceed ( ( ) => resolved ) ) ;
729+ canonicalPaths . set ( resolved , realPath ) ;
730+ return realPath ;
731+ } ) ;
732+ const hasTerminal = Effect . fnUntraced ( function * ( worktreeKey : string ) {
733+ for ( const terminal of [ ...liveTerminals . values ( ) ] . flatMap ( ( entries ) => [
734+ ...entries . values ( ) ,
735+ ] ) ) {
736+ if ( terminal . status !== "starting" && terminal . status !== "running" ) continue ;
737+ const cwd = yield * canonical ( terminal . cwd ) ;
738+ if (
739+ ( terminal . worktreePath !== null &&
740+ ( yield * canonical ( terminal . worktreePath ) ) === worktreeKey ) ||
741+ cwd === worktreeKey ||
742+ inside ( worktreeKey , cwd )
743+ )
744+ return true ;
745+ }
746+ return false ;
747+ } ) ;
748+ const liveThreads = snapshot . threads . filter ( ( thread ) => thread . worktreePath !== null ) ;
749+ const liveKeys = yield * Effect . forEach ( liveThreads , ( thread ) =>
750+ canonical ( thread . worktreePath ! ) ,
739751 ) ;
740- const deletedByPath = Map . groupBy ( deletedThreads , ( thread ) =>
741- path . resolve ( thread . worktreePath ! ) ,
752+ const groups = Map . groupBy ( liveThreads , ( _thread , index ) => liveKeys [ index ] ! ) ;
753+ const deletedKeys = yield * Effect . forEach ( deletedThreads , ( thread ) =>
754+ canonical ( thread . worktreePath ! ) ,
742755 ) ;
756+ const deletedByPath = Map . groupBy ( deletedThreads , ( _thread , index ) => deletedKeys [ index ] ! ) ;
743757 const candidates = [
744758 ...[ ...groups ] . map (
745759 ( [ worktreePath , group ] ) =>
@@ -755,13 +769,14 @@ const make = Effect.gen(function* () {
755769 worktreeOwner ( group ) ??
756770 group [ 0 ] ! ,
757771 ) ,
758- ...deletedThreads . filter ( ( thread ) => ! groups . has ( path . resolve ( thread . worktreePath ! ) ) ) ,
772+ ...deletedThreads . filter ( ( _thread , index ) => ! groups . has ( deletedKeys [ index ] ! ) ) ,
759773 ] ;
760774 for ( const thread of candidates ) {
761775 const settings = resolveWorktreeCleanup ( serverSettings , thread . projectId ) ;
762776 if ( ! worktreeCleanupEnabled ( settings ) ) continue ;
763777 const worktreePath = path . resolve ( thread . worktreePath ! ) ;
764- const group = groups . get ( worktreePath ) ?? [ ] ;
778+ const worktreeKey = yield * canonical ( worktreePath ) ;
779+ const group = groups . get ( worktreeKey ) ?? [ ] ;
765780 const activityAt = Math . max ( ...group . map ( storageCleanupActivityAt ) ) ;
766781 const deleted = "workspaceRoot" in thread ;
767782 const members = deleted ? [ thread , ...group ] : group ;
@@ -793,15 +808,15 @@ const make = Effect.gen(function* () {
793808 return keep ( "thread is running or has pending work" ) ;
794809 if ( group . some ( ( member ) => member . branch !== thread . branch ) )
795810 return keep ( "a subagent thread's branch differs from this worktree's branch" ) ;
796- if ( hasTerminal ( worktreePath ) ) return keep ( "open terminal" ) ;
811+ if ( yield * hasTerminal ( worktreeKey ) ) return keep ( "open terminal" ) ;
797812 // Compare canonical roots while allowing symlinked parents, never symlinked worktree directories.
798813 const realPath = yield * fs . realPath ( worktreePath ) ;
799814 const realParent = yield * fs . realPath ( path . dirname ( worktreePath ) ) ;
800815 if ( realPath !== path . join ( realParent , path . basename ( worktreePath ) ) )
801816 return keep ( "worktree is a symbolic link" ) ;
802817 if ( ! roots . some ( ( root ) => inside ( root , realPath ) ) )
803818 return keep ( "outside the managed worktree folder" ) ;
804- if ( yield * containsProjectRoot ( worktreePath , [ project , ...snapshot . projects ] ) )
819+ if ( yield * containsProjectRoot ( worktreeKey , [ project , ...snapshot . projects ] ) )
805820 return keep ( "contains a project checkout" ) ;
806821 if ( ( yield * fs . stat ( path . join ( worktreePath , ".git" ) ) ) . type !== "File" )
807822 return keep ( "not a linked worktree" ) ;
@@ -919,12 +934,14 @@ const make = Effect.gen(function* () {
919934 // Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
920935 const blockedSinceCheck = Effect . fnUntraced ( function * ( ) {
921936 const latestProjects = yield * projectStore . listShells ( ) ;
922- if ( yield * containsProjectRoot ( worktreePath , [ project , ...latestProjects ] ) )
937+ if ( yield * containsProjectRoot ( worktreeKey , [ project , ...latestProjects ] ) )
923938 return "contains a project checkout" ;
924- const latest = ( yield * projections . getThreadWorktreePaths ( {
925- includeArchived : true ,
926- } ) ) . filter ( ( entry ) => path . resolve ( entry . worktreePath ) === worktreePath ) ;
927- if ( hasTerminal ( worktreePath ) ) return "open terminal" ;
939+ const bindings = yield * projections . getThreadWorktreePaths ( { includeArchived : true } ) ;
940+ const bindingKeys = yield * Effect . forEach ( bindings , ( binding ) =>
941+ canonical ( binding . worktreePath ) ,
942+ ) ;
943+ const latest = bindings . filter ( ( _binding , index ) => bindingKeys [ index ] === worktreeKey ) ;
944+ if ( yield * hasTerminal ( worktreeKey ) ) return "open terminal" ;
928945 if ( deleted ) {
929946 if (
930947 ! resolveWorktreeCleanup ( yield * settingsService . getSettings , thread . projectId )
@@ -942,20 +959,23 @@ const make = Effect.gen(function* () {
942959 projections . getThreadShell ( entry . threadId ) ,
943960 ) ;
944961 const latestGroup = latestShells . filter ( ( shell ) => shell !== null ) ;
962+ const latestKeys = yield * Effect . forEach ( latestGroup , ( shell ) =>
963+ shell . worktreePath === null ? Effect . succeed ( null ) : canonical ( shell . worktreePath ) ,
964+ ) ;
945965 const latestOwner = worktreeOwner ( deleted ? [ thread , ...latestGroup ] : latestGroup ) ;
946966 if (
947967 latestGroup . length !== group . length ||
948968 latestOwner ?. id !== thread . id ||
949969 latestOwner ?. branch !== thread . branch ||
950- latestGroup . some ( ( shell ) => {
970+ latestGroup . some ( ( shell , index ) => {
951971 const previous = group . find ( ( member ) => member . id === shell . id ) ;
952972 return (
953973 previous === undefined ||
954974 shell . branch !== thread . branch ||
955975 ! Equal . equals ( shell . settledAt , previous . settledAt ) ||
956976 ( shell . settledAt !== null && ! Equal . equals ( shell . updatedAt , previous . updatedAt ) ) ||
957977 ! storageCleanupThreadIdle ( shell , now ) ||
958- path . resolve ( shell . worktreePath ! ) !== worktreePath ||
978+ latestKeys [ index ] !== worktreeKey ||
959979 storageCleanupActivityAt ( shell ) !== storageCleanupActivityAt ( previous )
960980 ) ;
961981 } )
@@ -971,9 +991,9 @@ const make = Effect.gen(function* () {
971991 ) ;
972992 // Ready persisted sessions may have released their runtime; only live sessions protect the path.
973993 for ( const session of sessions ) {
974- const cwd = path . resolve ( session . cwd ) ;
994+ const cwd = yield * canonical ( session . cwd ) ;
975995 if (
976- ( cwd === worktreePath || inside ( worktreePath , cwd ) ) &&
996+ ( cwd === worktreeKey || inside ( worktreeKey , cwd ) ) &&
977997 ( yield * providerSessions . isLive ( session . id ) )
978998 )
979999 return "provider session is still open" ;
0 commit comments