@@ -135,6 +135,31 @@ export function storageCleanupActivityAt(thread: OrchestrationV2ThreadShell): nu
135135 ) ;
136136}
137137
138+ /** Only a complete subagent chain shares ownership; forks remain separate. */
139+ function worktreeOwner ( threads : ReadonlyArray < OrchestrationV2ThreadShell > ) {
140+ if ( threads . length === 1 ) return threads [ 0 ] ;
141+ const owner = threads . find ( ( thread ) => thread . id === thread . lineage . rootThreadId ) ;
142+ if ( owner === undefined ) return undefined ;
143+ const byId = new Map ( threads . map ( ( thread ) => [ thread . id , thread ] ) ) ;
144+ for ( const thread of threads ) {
145+ let current = thread ;
146+ const seen = new Set < string > ( ) ;
147+ while ( current . id !== owner . id ) {
148+ if (
149+ seen . has ( current . id ) ||
150+ current . lineage . relationshipToParent !== "subagent" ||
151+ current . lineage . rootThreadId !== owner . id
152+ )
153+ return undefined ;
154+ seen . add ( current . id ) ;
155+ const parent = byId . get ( current . lineage . parentThreadId ! ) ;
156+ if ( parent === undefined ) return undefined ;
157+ current = parent ;
158+ }
159+ }
160+ return owner ;
161+ }
162+
138163/**
139164 * Whether the host's pull request proves this worktree's head was merged. A
140165 * squash or rebase merge leaves the head outside the default branch, so the
@@ -441,13 +466,15 @@ const make = Effect.gen(function* () {
441466 ( thread ) => path . resolve ( thread . worktreePath ! ) ,
442467 ) ;
443468 const candidates = [
444- ...[ ...groups . values ( ) ] . map ( ( group ) => group [ 0 ] ! ) ,
469+ ...[ ...groups . values ( ) ] . map ( ( group ) => worktreeOwner ( group ) ?? group [ 0 ] ! ) ,
445470 ...deletedThreads . filter ( ( thread ) => ! groups . has ( path . resolve ( thread . worktreePath ! ) ) ) ,
446471 ] ;
447472 for ( const thread of candidates ) {
448473 const settings = resolveWorktreeCleanup ( serverSettings , thread . projectId ) ;
449474 if ( ! worktreeCleanupEnabled ( settings ) ) continue ;
450475 const worktreePath = path . resolve ( thread . worktreePath ! ) ;
476+ const group = groups . get ( worktreePath ) ?? [ ] ;
477+ const activityAt = Math . max ( ...group . map ( storageCleanupActivityAt ) ) ;
451478 const deleted = "workspaceRoot" in thread ;
452479 const project = deleted
453480 ? { workspaceRoot : thread . workspaceRoot }
@@ -466,10 +493,10 @@ const make = Effect.gen(function* () {
466493 yield * Effect . gen ( function * ( ) {
467494 if ( ! ( yield * fs . exists ( worktreePath ) ) ) return ;
468495 if ( deleted && ! settings . worktreeOnDelete ) return keep ( "no rules apply" ) ;
469- const shared = groups . get ( worktreePath ) ?. length ?? 0 ;
470- if ( shared > 1 ) return keep ( `shared by ${ shared } threads` ) ;
496+ if ( group . length > 1 && worktreeOwner ( group ) === undefined )
497+ return keep ( `shared by ${ group . length } threads` ) ;
471498 if ( project === undefined ) return keep ( "project is unavailable" ) ;
472- if ( ! deleted && ! storageCleanupThreadIdle ( thread , now ) )
499+ if ( ! deleted && ! group . every ( ( member ) => storageCleanupThreadIdle ( member , now ) ) )
473500 return keep ( "thread is running or has pending work" ) ;
474501 if ( hasTerminal ( worktreePath ) ) return keep ( "open terminal" ) ;
475502 // Roots are canonical, so compare canonical paths. A symlinked parent
@@ -495,18 +522,19 @@ const make = Effect.gen(function* () {
495522 if (
496523 ! deleted &&
497524 thread . settledAt !== null &&
498- now - DateTime . toEpochMillis ( thread . updatedAt ) < SETTLE_SCRIPT_GRACE_MS
525+ now - Math . max ( DateTime . toEpochMillis ( thread . updatedAt ) , activityAt ) <
526+ SETTLE_SCRIPT_GRACE_MS
499527 )
500528 return keep ( "settled moments ago" ) ;
501529 const head = yield * git . resolveCommit ( { cwd : worktreePath , revision : "HEAD" } ) ;
502530 const old =
503531 ! deleted &&
504532 settings . worktreeAfterDays !== null &&
505- storageCleanupActivityAt ( thread ) < now - settings . worktreeAfterDays * DAY_MS ;
533+ activityAt < now - settings . worktreeAfterDays * DAY_MS ;
506534 const settledFor =
507535 deleted || thread . settledAt === null
508536 ? null
509- : now - DateTime . toEpochMillis ( thread . settledAt ) ;
537+ : now - Math . max ( DateTime . toEpochMillis ( thread . settledAt ) , activityAt ) ;
510538 const settledDays = settledFor === null ? null : Math . floor ( settledFor / DAY_MS ) ;
511539 const settled =
512540 settledFor !== null &&
@@ -516,7 +544,7 @@ const make = Effect.gen(function* () {
516544 let removalReason = deleted
517545 ? "thread was deleted"
518546 : old
519- ? `inactive for ${ Math . floor ( ( now - storageCleanupActivityAt ( thread ) ) / DAY_MS ) } days`
547+ ? `inactive for ${ Math . floor ( ( now - activityAt ) / DAY_MS ) } days`
520548 : `settled for ${ settledDays } days` ;
521549 if ( ! eligible && ( settings . worktreeUnchanged || settings . worktreeOnMerge ) ) {
522550 const repositoryCwd = path . resolve ( project . workspaceRoot ) ;
@@ -564,7 +592,7 @@ const make = Effect.gen(function* () {
564592 if ( ! eligible )
565593 return keep (
566594 ! deleted && settings . worktreeAfterDays !== null
567- ? `inactive for ${ Math . floor ( ( now - storageCleanupActivityAt ( thread ) ) / DAY_MS ) } of ${ settings . worktreeAfterDays } days`
595+ ? `inactive for ${ Math . floor ( ( now - activityAt ) / DAY_MS ) } of ${ settings . worktreeAfterDays } days`
568596 : settings . worktreeSettledAfterDays !== null
569597 ? settledFor === null
570598 ? "not settled"
@@ -575,7 +603,7 @@ const make = Effect.gen(function* () {
575603 ) ;
576604 // Threads, queued deletion work and provider sessions as they are now, so a
577605 // queued turn, resumed session or new thread sharing this path cancels
578- // the removal. Read only this path and this thread : a full snapshot per
606+ // the removal. Read only this path's threads : a full snapshot per
579607 // worktree takes seconds on large databases.
580608 const blockedSinceCheck = Effect . fnUntraced ( function * ( ) {
581609 const latestProjects = yield * projectStore . listShells ( ) ;
@@ -600,15 +628,24 @@ const make = Effect.gen(function* () {
600628 ` ;
601629 if ( pendingCleanup . length > 0 ) return "thread deletion is still pending" ;
602630 } else {
603- const latestShell =
604- latest . length === 1 && latest [ 0 ] ! . threadId === thread . id
605- ? yield * projections . getThreadShell ( thread . id )
606- : null ;
631+ const latestShells = yield * Effect . forEach ( latest , ( entry ) =>
632+ projections . getThreadShell ( entry . threadId ) ,
633+ ) ;
634+ const latestGroup = latestShells . filter ( ( shell ) => shell !== null ) ;
635+ const latestOwner = worktreeOwner ( latestGroup ) ;
607636 if (
608- latestShell === null ||
609- ! storageCleanupThreadIdle ( latestShell , now ) ||
610- storageCleanupActivityAt ( latestShell ) !== storageCleanupActivityAt ( thread ) ||
611- ! Equal . equals ( latestShell . settledAt , thread . settledAt )
637+ latestGroup . length !== group . length ||
638+ latestOwner ?. id !== thread . id ||
639+ ! Equal . equals ( latestOwner . settledAt , thread . settledAt ) ||
640+ latestGroup . some ( ( shell ) => {
641+ const previous = group . find ( ( member ) => member . id === shell . id ) ;
642+ return (
643+ previous === undefined ||
644+ ! storageCleanupThreadIdle ( shell , now ) ||
645+ path . resolve ( shell . worktreePath ! ) !== worktreePath ||
646+ storageCleanupActivityAt ( shell ) !== storageCleanupActivityAt ( previous )
647+ ) ;
648+ } )
612649 )
613650 return "thread activity or shared worktree changed since check" ;
614651 }
0 commit comments