@@ -7,6 +7,7 @@ import { getRuntimeConfig, type OpenClawConfig } from "../../config/config.js";
77import { resolveStateDir } from "../../config/paths.js" ;
88import { isMissingPathError , formatErrorMessage } from "../../infra/errors.js" ;
99import { root as fsRoot } from "../../infra/fs-safe.js" ;
10+ import { isPathInside } from "../../infra/path-guards.js" ;
1011import { createSubsystemLogger } from "../../logging/subsystem.js" ;
1112import {
1213 createStagedInputPathMatcher ,
@@ -295,9 +296,13 @@ async function canonicalPathKey(target: string): Promise<string> {
295296async function shouldPreserveOrphanCandidate (
296297 target : string ,
297298 managedPaths : ReadonlySet < string > ,
299+ customRoots : ReadonlySet < string > ,
298300) : Promise < boolean > {
299301 const targetKey = await canonicalPathKey ( target ) ;
300- if ( managedPaths . has ( targetKey ) ) {
302+ if (
303+ managedPaths . has ( targetKey ) ||
304+ [ ...customRoots ] . some ( ( root ) => isPathInside ( root , targetKey ) || isPathInside ( targetKey , root ) )
305+ ) {
301306 return true ;
302307 }
303308 // Any top-level .git entry marks uncertain user work; broken indirection only
@@ -1734,6 +1739,31 @@ export class ManagedWorktreeService {
17341739 // root can contain unrelated directories; its cleanup is registry-bound above.
17351740 const worktreesRoot = path . join ( resolveStateDir ( this . env ) , "worktrees" ) ;
17361741 const fingerprints = await fs . readdir ( worktreesRoot , { withFileTypes : true } ) . catch ( ( ) => [ ] ) ;
1742+ if ( fingerprints . length === 0 ) {
1743+ return 0 ;
1744+ }
1745+ const defaultRoot = await canonicalPathKey ( worktreesRoot ) ;
1746+ const customRoots = new Set < string > ( ) ;
1747+ // Retain roots from recorded paths after configuration changes. Canonical
1748+ // overlap protects nested roots and symlink aliases before recursive deletion.
1749+ for ( const root of [
1750+ this . getConfig ?.( ) . worktreeRoot ,
1751+ ...records . map ( ( record ) => path . dirname ( path . dirname ( record . path ) ) ) ,
1752+ ] ) {
1753+ if ( ! root ) {
1754+ continue ;
1755+ }
1756+ try {
1757+ const canonical = await canonicalPathKey ( root ) ;
1758+ if ( canonical !== defaultRoot ) {
1759+ customRoots . add ( canonical ) ;
1760+ }
1761+ } catch ( error ) {
1762+ if ( ! isMissingPathError ( error ) ) {
1763+ throw error ;
1764+ }
1765+ }
1766+ }
17371767 let deleted = 0 ;
17381768 for ( const fingerprint of fingerprints ) {
17391769 if ( ! fingerprint . isDirectory ( ) ) {
@@ -1742,7 +1772,7 @@ export class ManagedWorktreeService {
17421772 const fingerprintPath = path . join ( worktreesRoot , fingerprint . name ) ;
17431773 // A root entry can be a checkout, not a fingerprint container; descending
17441774 // before applying the same preservation rule would expose its contents to deletion.
1745- if ( await shouldPreserveOrphanCandidate ( fingerprintPath , managedPaths ) ) {
1775+ if ( await shouldPreserveOrphanCandidate ( fingerprintPath , managedPaths , customRoots ) ) {
17461776 continue ;
17471777 }
17481778 const names = await fs . readdir ( fingerprintPath , { withFileTypes : true } ) . catch ( ( ) => [ ] ) ;
@@ -1751,7 +1781,7 @@ export class ManagedWorktreeService {
17511781 continue ;
17521782 }
17531783 const candidate = path . join ( fingerprintPath , name . name ) ;
1754- if ( await shouldPreserveOrphanCandidate ( candidate , managedPaths ) ) {
1784+ if ( await shouldPreserveOrphanCandidate ( candidate , managedPaths , customRoots ) ) {
17551785 continue ;
17561786 }
17571787 await fs . rm ( candidate , { recursive : true , force : true } ) ;
0 commit comments