@@ -306,14 +306,26 @@ const cleanupFixture = (
306306 getShellSnapshot : ( input ?: { location ?: string } ) =>
307307 Effect . sync ( ( ) => {
308308 snapshotReads ++ ;
309- return { threads : input ?. location === "archive" ? [ ] : threads } ;
309+ const visible = threads . filter ( ( thread ) => thread . deletedAt === null ) ;
310+ return {
311+ threads :
312+ input ?. location === "archive"
313+ ? [ ]
314+ : visible . filter ( ( thread ) => thread . archivedAt === null ) ,
315+ archivedThreads :
316+ input ?. location === "active"
317+ ? [ ]
318+ : visible . filter ( ( thread ) => thread . archivedAt !== null ) ,
319+ } ;
310320 } ) ,
311- getThreadWorktreePaths : ( ) =>
321+ getThreadWorktreePaths : ( input ?: { includeArchived ?: boolean } ) =>
312322 Effect . sync ( ( ) =>
313323 threads . flatMap ( ( thread ) =>
314- thread . worktreePath === null
315- ? [ ]
316- : [ { threadId : thread . id , worktreePath : thread . worktreePath } ] ,
324+ thread . deletedAt === null &&
325+ ( input ?. includeArchived === true || thread . archivedAt === null ) &&
326+ thread . worktreePath !== null
327+ ? [ { threadId : thread . id , worktreePath : thread . worktreePath } ]
328+ : [ ] ,
317329 ) ,
318330 ) ,
319331 getThreadShell : ( threadId : ThreadId ) =>
@@ -1176,6 +1188,41 @@ describe("orphan worktree reports", () => {
11761188} ) ;
11771189
11781190describe ( "storage cleanup reports and local file policies" , ( ) => {
1191+ it . effect ( "automatically removes an eligible archived thread checkout" , ( ) =>
1192+ runCleanupTest (
1193+ Effect . gen ( function * ( ) {
1194+ yield * TestClock . setTime ( NOW_MS ) ;
1195+ const fixture = yield * cleanupFixture ( ) ;
1196+ fixture . setThreads ( [
1197+ shell ( {
1198+ branch : "feature" ,
1199+ worktreePath : fixture . worktree ,
1200+ archivedAt : at ( - DAY_MS ) ,
1201+ } ) ,
1202+ ] ) ;
1203+ yield * fixture . activate ( Stream . empty ) ;
1204+ const [ report ] = yield * fixture . service . reports . pipe (
1205+ Stream . filter ( ( report ) => report !== null ) ,
1206+ Stream . take ( 1 ) ,
1207+ Stream . runCollect ,
1208+ ) ;
1209+ yield * fixture . service . drain ;
1210+ expect ( report ) . toMatchObject ( {
1211+ trigger : "automatic" ,
1212+ counts : { removed : 1 , kept : 0 , failed : 0 } ,
1213+ entries : [
1214+ {
1215+ threadId : "thread-1" ,
1216+ path : fixture . worktree ,
1217+ outcome : "removed" ,
1218+ reason : "Inactive for 30 days" ,
1219+ } ,
1220+ ] ,
1221+ } ) ;
1222+ expect ( yield * fixture . fs . exists ( fixture . worktree ) ) . toBe ( false ) ;
1223+ } ) ,
1224+ ) ,
1225+ ) ;
11791226 it . live . each ( [ "before-check" , "while-measuring" ] as const ) (
11801227 "keeps a worktree protected by a missing project root (%s)" ,
11811228 ( timing ) =>
@@ -1264,6 +1311,18 @@ describe("storage cleanup reports and local file policies", () => {
12641311 shell ( { id : ThreadId . make ( "thread-2" ) , branch : "feature" , worktreePath : worktree } ) ,
12651312 ] ,
12661313 ] ,
1314+ [
1315+ "an archived thread starts using it" ,
1316+ ( worktree : string ) => [
1317+ shell ( { branch : "feature" , worktreePath : worktree } ) ,
1318+ shell ( {
1319+ id : ThreadId . make ( "thread-2" ) ,
1320+ branch : "feature" ,
1321+ worktreePath : worktree ,
1322+ archivedAt : at ( - DAY_MS ) ,
1323+ } ) ,
1324+ ] ,
1325+ ] ,
12671326 ] as const ) ( "keeps a worktree when %s during size measurement" , ( [ , threadsWhileMeasuring ] ) =>
12681327 runCleanupTest (
12691328 Effect . gen ( function * ( ) {
@@ -1542,6 +1601,17 @@ describe("storage cleanup reports and local file policies", () => {
15421601 shell ( { id : ThreadId . make ( "thread-2" ) , branch : "feature" , worktreePath : worktree } ) ,
15431602 ] ) ;
15441603 expect ( ( yield * service . runNow ) . entries [ 0 ] ?. reason ) . toBe ( "Shared by 2 threads" ) ;
1604+ setThreads ( [
1605+ shell ( { branch : "feature" , worktreePath : worktree } ) ,
1606+ shell ( {
1607+ id : ThreadId . make ( "thread-2" ) ,
1608+ branch : "feature" ,
1609+ worktreePath : worktree ,
1610+ archivedAt : at ( - DAY_MS ) ,
1611+ } ) ,
1612+ ] ) ;
1613+ expect ( ( yield * service . runNow ) . entries [ 0 ] ?. reason ) . toBe ( "Shared by 2 threads" ) ;
1614+ expect ( yield * fs . exists ( worktree ) ) . toBe ( true ) ;
15451615 fixture . disable ( ) ;
15461616 expect ( ( yield * service . runNow ) . entries ) . toEqual ( [ ] ) ;
15471617 yield * fs . remove ( worktree , { recursive : true } ) ;
0 commit comments