@@ -778,6 +778,139 @@ it.live("preserves late tracked edits when submodules were never initialized", (
778778) ;
779779
780780describe ( "subagent worktree ownership" , ( ) => {
781+ it . effect . each ( [
782+ "child" ,
783+ "grandchild" ,
784+ "immediate" ,
785+ "active-parent" ,
786+ "recent-settle" ,
787+ "settle-grace" ,
788+ "recent-child-activity" ,
789+ "running-child" ,
790+ "pending-child" ,
791+ "unsettled-child" ,
792+ "pinned-child" ,
793+ "fork" ,
794+ "missing-parent" ,
795+ "cycle" ,
796+ ] as const ) ( "checks isolated subagent settlement (%s)" , ( scenario ) =>
797+ Effect . gen ( function * ( ) {
798+ yield * TestClock . setTime ( NOW_MS ) ;
799+ const fixture = yield * cleanupFixture ( {
800+ ...DEFAULT_SERVER_SETTINGS . storageCleanup ,
801+ worktreeSettledAfterDays : scenario === "immediate" ? 0 : 8 ,
802+ } ) ;
803+ const root = shell ( {
804+ settledAt :
805+ scenario === "active-parent"
806+ ? null
807+ : scenario === "recent-settle" || scenario === "settle-grace"
808+ ? at ( - 30_000 )
809+ : scenario === "immediate"
810+ ? at ( - 120_000 )
811+ : at ( - 20 * DAY_MS ) ,
812+ settledOverride : scenario === "active-parent" ? null : "settled" ,
813+ updatedAt : scenario === "settle-grace" ? at ( - 30_000 ) : at ( - 10 * DAY_MS ) ,
814+ } ) ;
815+ const child = shell ( {
816+ id : ThreadId . make ( "child" ) ,
817+ branch : "feature" ,
818+ worktreePath : fixture . worktree ,
819+ lineage : {
820+ parentThreadId :
821+ scenario === "missing-parent"
822+ ? ThreadId . make ( "missing" )
823+ : scenario === "cycle"
824+ ? ThreadId . make ( "child" )
825+ : scenario === "grandchild"
826+ ? ThreadId . make ( "middle" )
827+ : root . id ,
828+ rootThreadId : root . id ,
829+ relationshipToParent : scenario === "fork" ? "fork" : "subagent" ,
830+ } ,
831+ status : scenario === "running-child" ? "running" : "completed" ,
832+ pendingBackgroundTasks :
833+ scenario === "pending-child" ? [ { taskId : "task" , kind : "command" } ] : [ ] ,
834+ latestRunCompletedAt : scenario === "recent-child-activity" ? at ( - 2 * DAY_MS ) : null ,
835+ settledOverride : scenario === "unsettled-child" ? "active" : null ,
836+ pinnedAt : scenario === "pinned-child" ? at ( - DAY_MS ) : null ,
837+ } ) ;
838+ const middle = shell ( {
839+ id : ThreadId . make ( "middle" ) ,
840+ lineage : {
841+ parentThreadId : root . id ,
842+ rootThreadId : root . id ,
843+ relationshipToParent : "subagent" ,
844+ } ,
845+ } ) ;
846+ fixture . setThreads ( [ child , middle , root ] ) ;
847+ const report = yield * fixture . service . runNow ;
848+ const removed = [ "child" , "grandchild" , "immediate" ] . includes ( scenario ) ;
849+ expect ( yield * fixture . fs . exists ( fixture . worktree ) ) . toBe ( ! removed ) ;
850+ expect ( report . entries [ 0 ] ) . toMatchObject ( {
851+ threadId : child . id ,
852+ outcome : removed ? "removed" : "kept" ,
853+ reason : removed
854+ ? scenario === "immediate"
855+ ? "Settled for 0 days"
856+ : "Settled for 20 days"
857+ : scenario === "settle-grace"
858+ ? "Settled moments ago"
859+ : scenario === "recent-settle"
860+ ? "Settled for 0 of 8 days"
861+ : scenario === "recent-child-activity"
862+ ? "Settled for 2 of 8 days"
863+ : scenario === "running-child" || scenario === "pending-child"
864+ ? "Thread is running or has pending work"
865+ : "Not settled" ,
866+ } ) ;
867+ } ) . pipe ( Effect . provide ( cleanupTestLayer ) , Effect . scoped ) ,
868+ ) ;
869+
870+ it . live . each ( [ "parent-unsettled" , "parent-resettled" , "child-unsettled" ] as const ) (
871+ "cancels inherited settlement during measurement (%s)" ,
872+ ( change ) =>
873+ runCleanupTest (
874+ Effect . gen ( function * ( ) {
875+ const fixture = yield * pausedMeasurementFixture ( {
876+ ...DEFAULT_SERVER_SETTINGS . storageCleanup ,
877+ worktreeSettledAfterDays : 0 ,
878+ } ) ;
879+ const root = shell ( { settledAt : at ( - 20 * DAY_MS ) , settledOverride : "settled" } ) ;
880+ const child = shell ( {
881+ id : ThreadId . make ( "child" ) ,
882+ branch : "feature" ,
883+ worktreePath : fixture . worktree ,
884+ lineage : {
885+ parentThreadId : root . id ,
886+ rootThreadId : root . id ,
887+ relationshipToParent : "subagent" ,
888+ } ,
889+ } ) ;
890+ fixture . setThreads ( [ child , root ] ) ;
891+ const run = yield * fixture . service . runNow . pipe ( Effect . forkChild ) ;
892+ yield * Deferred . await ( fixture . measuring ) ;
893+ fixture . setThreads ( [
894+ { ...child , settledOverride : change === "child-unsettled" ? "active" : null } ,
895+ change === "child-unsettled"
896+ ? root
897+ : {
898+ ...root ,
899+ settledAt : change === "parent-resettled" ? at ( 0 ) : null ,
900+ settledOverride : change === "parent-resettled" ? "settled" : "active" ,
901+ updatedAt : at ( 0 ) ,
902+ } ,
903+ ] ) ;
904+ yield * Deferred . succeed ( fixture . resume , undefined ) ;
905+ expect ( ( yield * Fiber . join ( run ) ) . entries [ 0 ] ) . toMatchObject ( {
906+ outcome : "kept" ,
907+ reason : "Parent settlement changed since check" ,
908+ } ) ;
909+ expect ( yield * fixture . fs . exists ( fixture . worktree ) ) . toBe ( true ) ;
910+ } ) ,
911+ ) ,
912+ ) ;
913+
781914 it . effect . each ( [
782915 "idle" ,
783916 "grandchild" ,
0 commit comments