@@ -677,6 +677,144 @@ describe("subagent worktree ownership", () => {
677677} ) ;
678678
679679describe ( "orphan worktree reports" , ( ) => {
680+ it . live ( "keeps orphan results visible when thread results fill the report" , ( ) =>
681+ runCleanupTest (
682+ Effect . gen ( function * ( ) {
683+ const fixture = yield * cleanupFixture ( ) ;
684+ fixture . setThreads (
685+ Array . from ( { length : 205 } , ( _ , index ) =>
686+ shell ( {
687+ id : ThreadId . make ( `busy-${ index } ` ) ,
688+ branch : "busy" ,
689+ worktreePath : `${ fixture . config . worktreesDir } /busy-${ index } ` ,
690+ status : "running" ,
691+ } ) ,
692+ ) ,
693+ ) ;
694+ for ( let index = 0 ; index < 205 ; index ++ ) {
695+ yield * fixture . fs . makeDirectory ( `${ fixture . config . worktreesDir } /busy-${ index } ` ) ;
696+ }
697+ const orphans = [ "one" , "two" ] . map (
698+ ( name ) => `${ fixture . config . worktreesDir } /repository/${ name } ` ,
699+ ) ;
700+ for ( const orphan of orphans ) {
701+ yield * fixture . fs . makeDirectory ( orphan , { recursive : true } ) ;
702+ yield * fixture . fs . utimes ( orphan , 1 , 1 ) ;
703+ }
704+ const report = yield * fixture . service . runNow ;
705+ expect ( report . entries ) . toHaveLength ( 200 ) ;
706+ expect (
707+ report . entries
708+ . slice ( 0 , 2 )
709+ . map ( ( entry ) => entry . path )
710+ . sort ( ) ,
711+ ) . toEqual ( orphans ) ;
712+ expect ( report . counts ) . toEqual ( { removed : 0 , kept : 207 , failed : 0 } ) ;
713+ expect ( report . omittedCount ) . toBe ( 7 ) ;
714+ } ) ,
715+ ) ,
716+ ) ;
717+
718+ it . live . each ( [ "canonical" , "win32" , "linux" ] as const ) (
719+ "skips an ancestor of a referenced thread worktree (%s)" ,
720+ ( platform ) =>
721+ runCleanupTest (
722+ Effect . gen ( function * ( ) {
723+ const fixture = yield * cleanupFixture ( ) . pipe (
724+ Effect . provideService ( HostProcess . Platform , platform === "win32" ? "win32" : "linux" ) ,
725+ ) ;
726+ fixture . setThreads ( [ ] ) ;
727+ const ancestor = `${ fixture . config . worktreesDir } /repository/stack` ;
728+ const worktree = `${ ancestor } /feature` ;
729+ yield * fixture . command ( fixture . repo , [ "worktree" , "add" , "-b" , "nested" , worktree ] ) ;
730+ const alias = `${ fixture . repo } /alias` ;
731+ if ( platform === "canonical" ) yield * fixture . fs . symlink ( worktree , alias ) ;
732+ fixture . setStoredWorktreePaths ( [
733+ platform === "canonical"
734+ ? alias
735+ : platform === "win32"
736+ ? worktree . toUpperCase ( )
737+ : worktree ,
738+ ] ) ;
739+ yield * fixture . fs . utimes ( ancestor , 1 , 1 ) ;
740+ expect ( ( yield * fixture . service . runNow ) . entries ) . toEqual ( [ ] ) ;
741+ expect ( yield * fixture . fs . exists ( worktree ) ) . toBe ( true ) ;
742+ } ) ,
743+ ) ,
744+ ) ;
745+
746+ it . live ( "waits an hour before reporting a folder without a thread row" , ( ) =>
747+ runCleanupTest (
748+ Effect . gen ( function * ( ) {
749+ const fixture = yield * cleanupFixture ( ) ;
750+ fixture . setThreads ( [ ] ) ;
751+ const folder = `${ fixture . config . worktreesDir } /review-guide-123/checkout` ;
752+ yield * fixture . command ( fixture . repo , [ "worktree" , "add" , "-b" , "review" , folder ] ) ;
753+ expect ( ( yield * fixture . service . runNow ) . entries ) . toEqual ( [ ] ) ;
754+ yield * fixture . fs . utimes ( folder , 1 , 1 ) ;
755+ expect ( ( yield * fixture . service . runNow ) . entries ) . toEqual ( [
756+ expect . objectContaining ( {
757+ path : folder ,
758+ outcome : "kept" ,
759+ reason : "No thread uses this worktree" ,
760+ } ) ,
761+ ] ) ;
762+ expect ( yield * fixture . fs . exists ( `${ folder } /tracked.txt` ) ) . toBe ( true ) ;
763+ } ) ,
764+ ) ,
765+ ) ;
766+
767+ it . live ( "continues reporting when a project root cannot be probed" , ( ) =>
768+ runCleanupTest (
769+ Effect . gen ( function * ( ) {
770+ const fixture = yield * cleanupFixture ( ) ;
771+ fixture . setThreads ( [ ] ) ;
772+ const loop = `${ fixture . repo } /loop` ;
773+ yield * fixture . fs . symlink ( loop , loop ) ;
774+ fixture . setProjectRoots ( [ fixture . repo , loop ] ) ;
775+ const orphan = `${ fixture . config . worktreesDir } /repository/orphan` ;
776+ yield * fixture . fs . makeDirectory ( orphan , { recursive : true } ) ;
777+ yield * fixture . fs . utimes ( orphan , 1 , 1 ) ;
778+ expect ( ( yield * fixture . service . runNow ) . entries ) . toEqual ( [
779+ expect . objectContaining ( { path : orphan , outcome : "kept" , bytes : 0 } ) ,
780+ ] ) ;
781+ } ) ,
782+ ) ,
783+ ) ;
784+
785+ it . live ( "reuses measured orphan sizes until the folder is no longer an orphan" , ( ) =>
786+ runCleanupTest (
787+ Effect . gen ( function * ( ) {
788+ const fs = yield * FileSystem . FileSystem ;
789+ let orphan = "" ;
790+ let walks = 0 ;
791+ const fixture = yield * cleanupFixture ( ) . pipe (
792+ Effect . provideService ( FileSystem . FileSystem , {
793+ ...fs ,
794+ readDirectory : ( directory , options ) => {
795+ if ( directory === orphan ) walks ++ ;
796+ return fs . readDirectory ( directory , options ) ;
797+ } ,
798+ } ) ,
799+ ) ;
800+ fixture . setThreads ( [ ] ) ;
801+ orphan = `${ fixture . config . worktreesDir } /repository/orphan` ;
802+ yield * fs . makeDirectory ( orphan , { recursive : true } ) ;
803+ yield * fs . writeFileString ( `${ orphan } /file` , "x" ) ;
804+ yield * fs . utimes ( orphan , 1 , 1 ) ;
805+ expect ( ( yield * fixture . service . runNow ) . entries [ 0 ] ?. bytes ) . toBe ( 1 ) ;
806+ yield * fs . writeFileString ( `${ orphan } /file` , "changed" ) ;
807+ expect ( ( yield * fixture . service . runNow ) . entries [ 0 ] ?. bytes ) . toBe ( 1 ) ;
808+ expect ( walks ) . toBe ( 1 ) ;
809+ fixture . setStoredWorktreePaths ( [ orphan ] ) ;
810+ expect ( ( yield * fixture . service . runNow ) . entries ) . toEqual ( [ ] ) ;
811+ fixture . setStoredWorktreePaths ( [ ] ) ;
812+ expect ( ( yield * fixture . service . runNow ) . entries [ 0 ] ?. bytes ) . toBe ( 7 ) ;
813+ expect ( walks ) . toBe ( 2 ) ;
814+ } ) ,
815+ ) ,
816+ ) ;
817+
680818 it . live . each ( [ "nested" , "direct" ] as const ) (
681819 "reports a linked orphan's size without removing its files (%s)" ,
682820 ( location ) =>
@@ -693,6 +831,7 @@ describe("orphan worktree reports", () => {
693831 yield * fixture . fs . writeFileString ( `${ outside } /external` , "outside data" ) ;
694832 yield * fixture . fs . symlink ( outside , `${ orphan } /linked-directory` ) ;
695833 yield * fixture . fs . symlink ( `${ outside } /missing` , `${ orphan } /dangling-link` ) ;
834+ yield * fixture . fs . utimes ( orphan , 1 , 1 ) ;
696835 const report = yield * fixture . service . runNow ;
697836 expect ( report . entries ) . toEqual ( [
698837 {
@@ -737,6 +876,7 @@ describe("orphan worktree reports", () => {
737876 ) ;
738877 if ( location !== "active" )
739878 fixture . setStoredWorktreePaths ( [ fixture . worktree , worktreePath ] ) ;
879+ yield * fixture . fs . utimes ( worktreePath , 1 , 1 ) ;
740880 const report = yield * fixture . service . runNow ;
741881 expect ( report . entries . filter ( ( entry ) => entry . threadId === null ) ) . toEqual ( [ ] ) ;
742882 expect ( yield * fixture . fs . exists ( worktreePath ) ) . toBe ( true ) ;
@@ -763,6 +903,7 @@ describe("orphan worktree reports", () => {
763903 ? `${ worktreePath } /nested`
764904 : alias ,
765905 ] ) ;
906+ yield * fixture . fs . utimes ( worktreePath , 1 , 1 ) ;
766907 expect ( ( yield * fixture . service . runNow ) . entries ) . toEqual ( [ ] ) ;
767908 expect ( yield * fixture . fs . exists ( worktreePath ) ) . toBe ( true ) ;
768909 } ) ,
@@ -821,6 +962,7 @@ describe("orphan worktree reports", () => {
821962 fixture . setProjectRoots ( [ alias ] ) ;
822963 }
823964 fixture . setSettings ( { worktreesDirectory : root } ) ;
965+ yield * fs . utimes ( orphan , 1 , 1 ) ;
824966 const report = yield * fixture . service . runNow ;
825967 expect ( report . entries ) . toEqual ( [
826968 expect . objectContaining ( {
@@ -846,6 +988,7 @@ describe("orphan worktree reports", () => {
846988 yield * fixture . fs . makeDirectory ( removed ) ;
847989 yield * fixture . fs . remove ( removed , { recursive : true } ) ;
848990 fixture . setProjectRoots ( [ fixture . repo , removed ] ) ;
991+ yield * fixture . fs . utimes ( orphan , 1 , 1 ) ;
849992 const report = yield * fixture . service . runNow ;
850993 expect ( report . entries ) . toEqual ( [
851994 expect . objectContaining ( {
@@ -870,6 +1013,7 @@ describe("orphan worktree reports", () => {
8701013 yield * fixture . fs . makeDirectory ( orphan , { recursive : true } ) ;
8711014 yield * fixture . fs . writeFileString ( `${ orphan } /file` , "x" ) ;
8721015 fixture . setProjectRoots ( [ fixture . repo , project ] ) ;
1016+ yield * fixture . fs . utimes ( orphan , 1 , 1 ) ;
8731017 const report = yield * fixture . service . runNow ;
8741018 expect ( report . entries ) . toEqual ( [
8751019 expect . objectContaining ( { path : orphan , outcome : "kept" , bytes : 1 } ) ,
@@ -910,6 +1054,7 @@ describe("orphan worktree reports", () => {
9101054 const folder = `${ fixture . config . worktreesDir } /repository/leftover` ;
9111055 yield * fixture . fs . makeDirectory ( folder , { recursive : true } ) ;
9121056 yield * fixture . fs . writeFileString ( `${ folder } /payload` , "leftover data" ) ;
1057+ yield * fixture . fs . utimes ( folder , 1 , 1 ) ;
9131058 const report = yield * fixture . service . runNow ;
9141059 expect ( report . entries ) . toEqual ( [
9151060 expect . objectContaining ( {
@@ -938,6 +1083,7 @@ describe("orphan worktree reports", () => {
9381083 yield * fixture . fs . makeDirectory ( orphan , { recursive : true } ) ;
9391084 yield * fixture . fs . writeFileString ( `${ orphan } /file` , "x" ) ;
9401085 yield * fixture . fs . chmod ( blocked , 0 ) ;
1086+ yield * fixture . fs . utimes ( orphan , 1 , 1 ) ;
9411087 const report = yield * fixture . service . runNow . pipe (
9421088 Effect . ensuring ( fixture . fs . chmod ( blocked , 0o700 ) . pipe ( Effect . orDie ) ) ,
9431089 ) ;
@@ -964,6 +1110,7 @@ describe("orphan worktree reports", () => {
9641110 const nested = `${ parent } /Nested` ;
9651111 for ( const folder of [ referenced , project , nested ] ) {
9661112 yield * fixture . fs . makeDirectory ( `${ folder } /child` , { recursive : true } ) ;
1113+ yield * fixture . fs . utimes ( folder , 1 , 1 ) ;
9671114 }
9681115 fixture . setStoredWorktreePaths ( [ fixture . worktree , referenced . toUpperCase ( ) ] ) ;
9691116 fixture . setProjectRoots ( [ fixture . repo , project , `${ nested } /child` ] ) ;
@@ -984,6 +1131,7 @@ describe("orphan worktree reports", () => {
9841131 const orphan = `${ fixture . config . worktreesDir } /repository/orphan-${ i } ` ;
9851132 yield * fixture . fs . makeDirectory ( orphan , { recursive : true } ) ;
9861133 yield * fixture . fs . writeFileString ( `${ orphan } /file` , "x" ) ;
1134+ yield * fixture . fs . utimes ( orphan , 1 , 1 ) ;
9871135 }
9881136 const report = yield * fixture . service . runNow ;
9891137 expect ( report . entries ) . toHaveLength ( 200 ) ;
0 commit comments