fix(teams): close remaining cleanup gaps for worktrees and session dirs#40
fix(teams): close remaining cleanup gaps for worktrees and session dirs#40
Conversation
Addresses SYM-41 / GitHub #9 — remaining gaps after PRs #14 and #30: 1. Standalone git worktree prune on startup: adds pruneStaleWorktreeRefs() called in session_start to clean up dangling .git/worktrees/ entries from sessions whose team directories were already deleted (crash, manual rm, or partial cleanup). Lightweight and safe to run every time. 2. Pass repoCwd to startup GC: gcStaleTeamDirs now receives ctx.cwd so cleanupWorktrees can find the repo root even when worktree dirs are gone. 3. Smarter session_shutdown cleanup: replaces teamDirHasAnyTasks with teamDirHasActiveTasks — team dirs with only completed tasks are now cleaned up at session end instead of waiting 24h for startup GC. Completed tasks have already been reported back and serve no purpose. 4. Removes hadTeammates guard: session_shutdown now attempts dir cleanup regardless of whether RPC teammates were active. The safety checks (no live attach claim, no online workers, no active tasks) are sufficient guards. 5. Passes repoCwd to cleanupTeamDir in session_shutdown for proper worktree metadata cleanup. Tests: 8 new integration tests covering pruneStaleWorktreeRefs (orphan cleanup, non-git dir) and gcStaleTeamDirs (completed-only tasks are GC'd). All 325 smoke + 56 integration-cleanup tests pass.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 14e3528fe4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } catch { | ||
| // Unreadable task file — skip. |
There was a problem hiding this comment.
Treat unreadable task JSON as active during shutdown cleanup
teamDirHasActiveTasks now skips task files that fail readFile/JSON.parse, so a team with malformed or partially written task JSON can be misclassified as having no active tasks; in session_shutdown that allows cleanupTeamDir(...) to delete the entire team directory and lose unresolved task data. The previous teamDirHasAnyTasks logic was fail-closed for this case, so this change introduces a new data-loss path when task files are corrupted or in an unexpected format.
Useful? React with 👍 / 👎.
Summary
Closes remaining cleanup gaps for worktrees and session directories (SYM-41 / #9).
PRs #14 and #30 added the core cleanup infrastructure (
cleanupWorktrees,gcStaleTeamDirs,session_shutdownhandler). This PR closes the remaining gaps:Changes
1. Standalone
git worktree pruneon startup (worktree.ts)pruneStaleWorktreeRefs()function — runsgit worktree pruneon the working reposession_startto clean up dangling.git/worktrees/entries from sessions whose team directories were already deleted (crash, manual rm, partial cleanup)2. Pass
repoCwdto startup GC (leader.ts)gcStaleTeamDirsnow receivesctx.cwdsocleanupWorktreescan find the repo root even when worktree directories are gone from disk3. Smarter
session_shutdowncleanup (leader.ts)teamDirHasAnyTaskswithteamDirHasActiveTasks— checks for pending/in-progress tasks only4. Remove
hadTeammatesguard (leader.ts)session_shutdownnow attempts dir cleanup regardless of whether RPC teammates were active5. Pass
repoCwdtocleanupTeamDirin shutdown (leader.ts)Testing
integration-cleanup-test.mts:pruneStaleWorktreeRefscleans orphaned.git/worktrees/metadatapruneStaleWorktreeRefshandles non-git directories gracefullygcStaleTeamDirsremoves dirs with only completed tasksResolves #9