@@ -836,6 +836,20 @@ const make = Effect.gen(function* () {
836836 ) ;
837837 if ( root !== null && ! isFilesystemRoot ( root , path ) ) roots . push ( root ) ;
838838 }
839+ const worktreePathReason = Effect . fn ( "StorageCleanup.worktreePathReason" ) ( function * (
840+ worktreePath : string ,
841+ ) {
842+ // Allow symlinked parents, never a symlinked worktree directory.
843+ const realPath = yield * fs . realPath ( worktreePath ) ;
844+ const realParent = yield * fs . realPath ( path . dirname ( worktreePath ) ) ;
845+ if ( realPath !== path . join ( realParent , path . basename ( worktreePath ) ) )
846+ return "worktree is a symbolic link" ;
847+ if ( ! roots . some ( ( root ) => inside ( root , realPath ) ) )
848+ return "outside the managed worktree folder" ;
849+ if ( ( yield * fs . stat ( path . join ( worktreePath , ".git" ) ) ) . type !== "File" )
850+ return "not a linked worktree" ;
851+ return null ;
852+ } ) ;
839853 const deletedRows = yield * sql < { payload_json : string ; workspaceRoot : string } > `
840854 SELECT t.payload_json, p.workspace_root AS "workspaceRoot"
841855 FROM orchestration_v2_projection_threads t
@@ -924,21 +938,6 @@ const make = Effect.gen(function* () {
924938 if ( group . some ( ( member ) => member . branch !== thread . branch ) )
925939 return keep ( "a subagent thread's branch differs from this worktree's branch" ) ;
926940 if ( hasTerminal ( worktreePath ) ) return keep ( waitForStop ( "open terminal" ) ) ;
927- // Compare canonical roots while allowing symlinked parents, never symlinked worktree directories.
928- const realPath = yield * fs . realPath ( worktreePath ) ;
929- const realParent = yield * fs . realPath ( path . dirname ( worktreePath ) ) ;
930- if ( realPath !== path . join ( realParent , path . basename ( worktreePath ) ) )
931- return keep ( "worktree is a symbolic link" ) ;
932- if ( ! roots . some ( ( root ) => inside ( root , realPath ) ) )
933- return keep ( "outside the managed worktree folder" ) ;
934- if ( yield * containsProjectRoot ( worktreePath , [ project , ...snapshot . projects ] ) )
935- return keep ( "contains a project checkout" ) ;
936- if ( ( yield * fs . stat ( path . join ( worktreePath , ".git" ) ) ) . type !== "File" )
937- return keep ( "not a linked worktree" ) ;
938- if ( yield * branchChanged ( worktreePath , thread . branch ) )
939- return keep ( "repository or branch changed" ) ;
940- const changes = yield * localChangesReason ( worktreePath , settings ) ;
941- if ( changes !== null ) return keep ( changes ) ;
942941 if (
943942 [ ...group , ...settlementChain . slice ( 1 ) ] . some (
944943 ( member ) =>
@@ -948,7 +947,6 @@ const make = Effect.gen(function* () {
948947 )
949948 )
950949 return keep ( waitForStop ( "settled moments ago" ) ) ;
951- const head = yield * git . resolveCommit ( { cwd : worktreePath , revision : "HEAD" } ) ;
952950 const old =
953951 ! deleted &&
954952 settings . worktreeAfterDays !== null &&
@@ -968,7 +966,31 @@ const make = Effect.gen(function* () {
968966 : old
969967 ? `inactive for ${ Math . floor ( ( now - activityAt ) / DAY_MS ) } days`
970968 : `settled for ${ settledDays } days` ;
971- if ( ! eligible && ( settings . worktreeUnchanged || settings . worktreeOnMerge ) ) {
969+ const notDueReason =
970+ ! deleted && settings . worktreeAfterDays !== null
971+ ? `inactive for ${ Math . floor ( ( now - activityAt ) / DAY_MS ) } of ${ settings . worktreeAfterDays } days`
972+ : settings . worktreeSettledAfterDays !== null
973+ ? settledFor === null
974+ ? "not settled"
975+ : `settled for ${ settledDays } of ${ settings . worktreeSettledAfterDays } days`
976+ : settings . worktreeOnMerge
977+ ? "not merged"
978+ : settings . worktreeUnchanged
979+ ? "has commits beyond the default branch"
980+ : "no rules apply" ;
981+ // Only Git-based rules can make a not-yet-due worktree eligible, so skip Git otherwise.
982+ if ( ! eligible && ! settings . worktreeUnchanged && ! settings . worktreeOnMerge )
983+ return keep ( notDueReason ) ;
984+ const pathReason = yield * worktreePathReason ( worktreePath ) ;
985+ if ( pathReason !== null ) return keep ( pathReason ) ;
986+ if ( yield * containsProjectRoot ( worktreePath , [ project , ...snapshot . projects ] ) )
987+ return keep ( "contains a project checkout" ) ;
988+ if ( yield * branchChanged ( worktreePath , thread . branch ) )
989+ return keep ( "repository or branch changed" ) ;
990+ const changes = yield * localChangesReason ( worktreePath , settings ) ;
991+ if ( changes !== null ) return keep ( changes ) ;
992+ const head = yield * git . resolveCommit ( { cwd : worktreePath , revision : "HEAD" } ) ;
993+ if ( ! eligible ) {
972994 const repositoryCwd = path . resolve ( project . workspaceRoot ) ;
973995 const remotes = yield * git . execute ( {
974996 operation : "StorageCleanup.listRemotes" ,
@@ -1063,18 +1085,7 @@ const make = Effect.gen(function* () {
10631085 }
10641086 if ( ! eligible && remote === null ) return keep ( "has commits beyond the project branch" ) ;
10651087 }
1066- if ( ! eligible )
1067- return keep (
1068- ! deleted && settings . worktreeAfterDays !== null
1069- ? `inactive for ${ Math . floor ( ( now - activityAt ) / DAY_MS ) } of ${ settings . worktreeAfterDays } days`
1070- : settings . worktreeSettledAfterDays !== null
1071- ? settledFor === null
1072- ? "not settled"
1073- : `settled for ${ settledDays } of ${ settings . worktreeSettledAfterDays } days`
1074- : settings . worktreeOnMerge
1075- ? "not merged"
1076- : "has commits beyond the default branch" ,
1077- ) ;
1088+ if ( ! eligible ) return keep ( notDueReason ) ;
10781089 // Re-read this path's threads and sessions so new activity cancels removal; full per-worktree snapshots take seconds.
10791090 const blockedSinceCheck = Effect . fnUntraced ( function * ( ) {
10801091 const latestProjects = yield * projectStore . listShells ( ) ;
@@ -1165,53 +1176,61 @@ const make = Effect.gen(function* () {
11651176 } ) ;
11661177 const blocked = yield * blockedSinceCheck ( ) ;
11671178 if ( blocked !== null ) return keep ( blocked ) ;
1179+ // Size is an estimate; provider startup must not wait for directory traversal.
11681180 const bytes = yield * measureWorktree ( worktreePath ) ;
1169- const blockedWhileMeasuring = yield * blockedSinceCheck ( ) ;
1170- if ( blockedWhileMeasuring !== null ) return keep ( blockedWhileMeasuring ) ;
1171- if ( yield * branchChanged ( worktreePath , thread . branch ) )
1172- return keep ( "repository or branch changed since check" ) ;
1173- if (
1174- ( yield * git . resolveCommit ( { cwd : worktreePath , revision : "HEAD" } ) ) . commitSha !==
1175- head . commitSha
1176- )
1177- return keep ( "commit changed since check" ) ;
1178- const finalChanges = yield * localChangesReason ( worktreePath , settings ) ;
1179- if ( finalChanges !== null ) return keep ( finalChanges ) ;
1180- const currentSettings = yield * settingsService . getSettings ;
1181- const current = resolveWorktreeCleanup ( currentSettings , thread . projectId ) ;
1182- if (
1183- currentSettings . autoResumeLimitedThreads !== serverSettings . autoResumeLimitedThreads ||
1184- currentSettings . snoozeLimitedThreads !== serverSettings . snoozeLimitedThreads ||
1185- Object . keys ( settings ) . some (
1186- ( key ) =>
1187- current [ key as keyof typeof settings ] !== settings [ key as keyof typeof settings ] ,
1188- )
1189- )
1190- return keep ( "settings changed since check" ) ;
1191- // Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
1192- if ( settings . worktreeKeepWhen === "tracked-changes" )
1193- yield * cleanupGit ( worktreePath , [ "clean" , "-ffdx" ] , "git clean" ) ;
1194- const submodules = yield * submoduleRemoval ( worktreePath , settings ) ;
1195- if ( submodules . reason !== null ) return keep ( submodules . reason ) ;
1196- if ( submodules . force ) {
1197- const changes = yield * localChangesReason ( worktreePath , settings ) ;
1198- if ( changes !== null ) return keep ( changes ) ;
1199- }
1200- yield * removeLinkedWorktree ( {
1201- operation : "StorageCleanup.cleanupGit" ,
1202- cwd : project . workspaceRoot ,
1181+ yield * withWorkspaceLease (
12031182 worktreePath ,
1204- // Git requires force for submodules; recursive checks above cannot make removal atomic.
1205- force : submodules . force ,
1206- execute : git . execute ,
1207- run : ( args ) => cleanupGit ( project . workspaceRoot , [ ...args ] , "git worktree remove" ) ,
1208- } ) ;
1209- entries . push ( { ...entry , outcome : "removed" , reason : sentence ( removalReason ) , bytes } ) ;
1210- yield * gitManager . invalidateStatus ( project . workspaceRoot ) ;
1211- // Preserve branch/path so ProviderTurnStartService can recreate the checkout on resume.
1212- yield * Effect . logInfo ( "storage cleanup removed worktree" , { threadId : thread . id } ) ;
1183+ Effect . gen ( function * ( ) {
1184+ const pathReason = yield * worktreePathReason ( worktreePath ) ;
1185+ if ( pathReason !== null ) return keep ( pathReason ) ;
1186+ const blockedWhileMeasuring = yield * blockedSinceCheck ( ) ;
1187+ if ( blockedWhileMeasuring !== null ) return keep ( blockedWhileMeasuring ) ;
1188+ if ( yield * branchChanged ( worktreePath , thread . branch ) )
1189+ return keep ( "repository or branch changed since check" ) ;
1190+ if (
1191+ ( yield * git . resolveCommit ( { cwd : worktreePath , revision : "HEAD" } ) ) . commitSha !==
1192+ head . commitSha
1193+ )
1194+ return keep ( "commit changed since check" ) ;
1195+ const finalChanges = yield * localChangesReason ( worktreePath , settings ) ;
1196+ if ( finalChanges !== null ) return keep ( finalChanges ) ;
1197+ const currentSettings = yield * settingsService . getSettings ;
1198+ const current = resolveWorktreeCleanup ( currentSettings , thread . projectId ) ;
1199+ if (
1200+ currentSettings . autoResumeLimitedThreads !==
1201+ serverSettings . autoResumeLimitedThreads ||
1202+ currentSettings . snoozeLimitedThreads !== serverSettings . snoozeLimitedThreads ||
1203+ Object . keys ( settings ) . some (
1204+ ( key ) =>
1205+ current [ key as keyof typeof settings ] !== settings [ key as keyof typeof settings ] ,
1206+ )
1207+ )
1208+ return keep ( "settings changed since check" ) ;
1209+ // Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
1210+ if ( settings . worktreeKeepWhen === "tracked-changes" )
1211+ yield * cleanupGit ( worktreePath , [ "clean" , "-ffdx" ] , "git clean" ) ;
1212+ const submodules = yield * submoduleRemoval ( worktreePath , settings ) ;
1213+ if ( submodules . reason !== null ) return keep ( submodules . reason ) ;
1214+ if ( submodules . force ) {
1215+ const changes = yield * localChangesReason ( worktreePath , settings ) ;
1216+ if ( changes !== null ) return keep ( changes ) ;
1217+ }
1218+ yield * removeLinkedWorktree ( {
1219+ operation : "StorageCleanup.cleanupGit" ,
1220+ cwd : project . workspaceRoot ,
1221+ worktreePath,
1222+ // Git requires force for submodules; recursive checks above cannot make removal atomic.
1223+ force : submodules . force ,
1224+ execute : git . execute ,
1225+ run : ( args ) => cleanupGit ( project . workspaceRoot , [ ...args ] , "git worktree remove" ) ,
1226+ } ) ;
1227+ entries . push ( { ...entry , outcome : "removed" , reason : sentence ( removalReason ) , bytes } ) ;
1228+ yield * gitManager . invalidateStatus ( project . workspaceRoot ) ;
1229+ // Preserve branch/path so ProviderTurnStartService can recreate the checkout on resume.
1230+ yield * Effect . logInfo ( "storage cleanup removed worktree" , { threadId : thread . id } ) ;
1231+ } ) ,
1232+ ) ;
12131233 } ) . pipe (
1214- ( effect ) => withWorkspaceLease ( worktreePath , effect ) ,
12151234 Effect . catch ( ( error ) =>
12161235 Effect . gen ( function * ( ) {
12171236 entries . push ( {
0 commit comments