@@ -146,7 +146,9 @@ export function storageCleanupActivityAt(thread: OrchestrationV2ThreadShell): nu
146146}
147147
148148/** Only a complete subagent chain within this worktree shares ownership. */
149- function worktreeOwner ( threads : ReadonlyArray < OrchestrationV2ThreadShell > ) {
149+ function worktreeOwner < T extends Pick < OrchestrationV2ThreadShell , "id" | "lineage" > > (
150+ threads : ReadonlyArray < T > ,
151+ ) {
150152 if ( threads . length === 1 ) return threads [ 0 ] ;
151153 const byId = new Map ( threads . map ( ( thread ) => [ thread . id , thread ] ) ) ;
152154 const tops = threads . filter ( ( thread ) => ! byId . has ( thread . lineage . parentThreadId ! ) ) ;
@@ -750,8 +752,24 @@ const make = Effect.gen(function* () {
750752 snapshot . threads . filter ( ( thread ) => thread . worktreePath !== null ) ,
751753 ( thread ) => path . resolve ( thread . worktreePath ! ) ,
752754 ) ;
755+ const deletedByPath = Map . groupBy ( deletedThreads , ( thread ) =>
756+ path . resolve ( thread . worktreePath ! ) ,
757+ ) ;
753758 const candidates = [
754- ...[ ...groups . values ( ) ] . map ( ( group ) => worktreeOwner ( group ) ?? group [ 0 ] ! ) ,
759+ ...[ ...groups ] . map (
760+ ( [ worktreePath , group ] ) =>
761+ // A deleted thread still owns the worktree its remaining subagents share,
762+ // but only deletion cleanup acts on it; otherwise the live rules apply.
763+ deletedByPath
764+ . get ( worktreePath )
765+ ?. find (
766+ ( deleted ) =>
767+ resolveWorktreeCleanup ( serverSettings , deleted . projectId ) . worktreeOnDelete &&
768+ worktreeOwner ( [ deleted , ...group ] ) === deleted ,
769+ ) ??
770+ worktreeOwner ( group ) ??
771+ group [ 0 ] ! ,
772+ ) ,
755773 ...deletedThreads . filter ( ( thread ) => ! groups . has ( path . resolve ( thread . worktreePath ! ) ) ) ,
756774 ] ;
757775 for ( const thread of candidates ) {
@@ -761,6 +779,7 @@ const make = Effect.gen(function* () {
761779 const group = groups . get ( worktreePath ) ?? [ ] ;
762780 const activityAt = Math . max ( ...group . map ( storageCleanupActivityAt ) ) ;
763781 const deleted = "workspaceRoot" in thread ;
782+ const members = deleted ? [ thread , ...group ] : group ;
764783 const project = deleted
765784 ? { workspaceRoot : thread . workspaceRoot }
766785 : snapshot . projects . find ( ( entry ) => entry . id === thread . projectId ) ;
@@ -779,12 +798,12 @@ const make = Effect.gen(function* () {
779798 yield * Effect . gen ( function * ( ) {
780799 if ( ! ( yield * fs . exists ( worktreePath ) ) ) return ;
781800 if ( deleted && ! settings . worktreeOnDelete ) return keep ( "no rules apply" ) ;
782- if ( group . length > 1 && worktreeOwner ( group ) === undefined )
783- return keep ( `shared by ${ group . length } threads` ) ;
801+ if ( members . length > 1 && worktreeOwner ( members ) === undefined )
802+ return keep ( `shared by ${ members . length } threads` ) ;
784803 if ( project === undefined ) return keep ( "project is unavailable" ) ;
785804 if (
786805 thread . branch === null ||
787- ( ! deleted && ! group . every ( ( member ) => storageCleanupThreadIdle ( member , now ) ) )
806+ ! group . every ( ( member ) => storageCleanupThreadIdle ( member , now ) )
788807 )
789808 return keep ( "thread is running or has pending work" ) ;
790809 if ( group . some ( ( member ) => member . branch !== thread . branch ) )
@@ -807,7 +826,6 @@ const make = Effect.gen(function* () {
807826 const changes = yield * localChangesReason ( worktreePath , settings ) ;
808827 if ( changes !== null ) return keep ( changes ) ;
809828 if (
810- ! deleted &&
811829 group . some (
812830 ( member ) =>
813831 member . settledAt !== null &&
@@ -928,7 +946,6 @@ const make = Effect.gen(function* () {
928946 if ( hasTerminal ( worktreePath ) ) return "open terminal" ;
929947 if ( deleted ) {
930948 if (
931- latest . length > 0 ||
932949 ! resolveWorktreeCleanup ( yield * settingsService . getSettings , thread . projectId )
933950 . worktreeOnDelete
934951 )
@@ -940,32 +957,30 @@ const make = Effect.gen(function* () {
940957 WHERE thread_id = ${ thread . id } AND status NOT IN ('succeeded', 'cancelled') LIMIT 1
941958 ` . pipe ( NodeSqliteClient . readOnly ) ;
942959 if ( pendingCleanup . length > 0 ) return "thread deletion is still pending" ;
943- } else {
944- const latestShells = yield * Effect . forEach ( latest , ( entry ) =>
945- projections . getThreadShell ( entry . threadId ) ,
946- ) ;
947- const latestGroup = latestShells . filter ( ( shell ) => shell !== null ) ;
948- const latestOwner = worktreeOwner ( latestGroup ) ;
949- if (
950- latestGroup . length !== group . length ||
951- latestOwner ?. id !== thread . id ||
952- latestOwner ?. branch !== thread . branch ||
953- latestGroup . some ( ( shell ) => {
954- const previous = group . find ( ( member ) => member . id === shell . id ) ;
955- return (
956- previous === undefined ||
957- shell . branch !== thread . branch ||
958- ! Equal . equals ( shell . settledAt , previous . settledAt ) ||
959- ( shell . settledAt !== null &&
960- ! Equal . equals ( shell . updatedAt , previous . updatedAt ) ) ||
961- ! storageCleanupThreadIdle ( shell , now ) ||
962- path . resolve ( shell . worktreePath ! ) !== worktreePath ||
963- storageCleanupActivityAt ( shell ) !== storageCleanupActivityAt ( previous )
964- ) ;
965- } )
966- )
967- return "thread activity or shared worktree changed since check" ;
968960 }
961+ const latestShells = yield * Effect . forEach ( latest , ( entry ) =>
962+ projections . getThreadShell ( entry . threadId ) ,
963+ ) ;
964+ const latestGroup = latestShells . filter ( ( shell ) => shell !== null ) ;
965+ const latestOwner = worktreeOwner ( deleted ? [ thread , ...latestGroup ] : latestGroup ) ;
966+ if (
967+ latestGroup . length !== group . length ||
968+ latestOwner ?. id !== thread . id ||
969+ latestOwner ?. branch !== thread . branch ||
970+ latestGroup . some ( ( shell ) => {
971+ const previous = group . find ( ( member ) => member . id === shell . id ) ;
972+ return (
973+ previous === undefined ||
974+ shell . branch !== thread . branch ||
975+ ! Equal . equals ( shell . settledAt , previous . settledAt ) ||
976+ ( shell . settledAt !== null && ! Equal . equals ( shell . updatedAt , previous . updatedAt ) ) ||
977+ ! storageCleanupThreadIdle ( shell , now ) ||
978+ path . resolve ( shell . worktreePath ! ) !== worktreePath ||
979+ storageCleanupActivityAt ( shell ) !== storageCleanupActivityAt ( previous )
980+ ) ;
981+ } )
982+ )
983+ return "thread activity or shared worktree changed since check" ;
969984 // Sessions can outlive their run and can be shared across app threads.
970985 const sessionRows = yield * sql < { payload_json : string } > `
971986 SELECT payload_json FROM orchestration_v2_projection_provider_sessions
0 commit comments