Repository navigation
feat(session): reclaim worktrees when a session ends - #704
Merged
Merged
Conversation
Couple cleanup to the end of a session instead of leaving it to a manual second keystroke. A clean managed worktree is removed when its session retires; one holding uncommitted work is never deleted. - Add `SessionLifecycle::Retained` for a session whose checkout could not be removed. It is a failure surface rather than a resting state, so the roster reports it as needing the operator and `x` retries it. - Preflight the session's own checkout and every finished child agent's worktree together. Removal is all-or-nothing so `retained` keeps one meaning: this session still owns a checkout on disk. - Add `AgentSession::descendant_worktrees` so the supervisor can reclaim child checkouts. The task registry already records where each one lives, so no journal walk is needed. - One verb: `x` archives and cleans in both the navigator and the session switcher, always behind a confirmation. The navigator's `d` is unbound. - Drop a promise the confirm copy can no longer keep: it no longer claims the worktree is kept. - A blocked removal must not read as an archive failure, so the parked session view is restored from the roster's state rather than from the command result. Deliberately out of scope: worktrees orphaned by sessions that already ended, a crash between the Git removal and the database write, and directories Git has stopped registering. Every mechanism here derives from `git worktree list`, so all three are invisible to it. Verified with cargo fmt, cargo clippy (no warnings), cargo test for forge-session (81 passed) and the forge-tui modules this change touches.
mohitranka
force-pushed
the
feat/worktree-gc
branch
from
September 15, 2026 20:47
c577d57 to
2650b1d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Managed worktrees are now reclaimed when a session ends, instead of waiting on a manual second keystroke.
Why
Every managed session owns a Git worktree under
.forge/local/worktrees, created from the initiating worktree's committedHEAD. The removal machinery already existed, but only as a separate manual action on an already-archived row, so stale checkouts accumulated on disk. This couples cleanup to the point where the session's resources are released anyway.How
SessionLifecycle::Retained— a fifth lifecycle state for an archived session whose checkout could not be removed. It is a failure surface rather than a resting state: the navigator renders it● needs youwith the qualifiercleanup blocked, the switcher ranks it below rows that need an answer, andxon the row retries the removal.archive()refuses to overwrite it, and prompt queueing rejects it.retained. That keeps one retryable unit per session and makesretainedmean exactly one thing: this session still owns a checkout on disk. ADirtythat lands between the preflight and the removal is converted toretainedtoo, rather than surfacing as an error.AgentSession::descendant_worktrees— the supervisor needs to reclaim child checkouts (children's worktrees are siblings of the session's, not nested inside it). The task registry already recordsworktree_path/worktree_branch, so this reads what is already in memory; no journal walk, no new dependency, and no child rows in the navigator.x— archives and cleans in both the navigator and the session switcher, always behind a confirmation. The navigator'sdis unbound, so the same key means the same thing in both surfaces. The primary refusal and the unsaved-editor guard run before the confirmation, so no confirm is ever shown for something that is then refused.navigator_done_pendingis a set. Previously a second confirmed action silently overwrote the first.Verification
cargo fmt --all -- --check— cleancargo clippy -p forge-core -p forge-session -p forge-tui --all-targets— zero warningscargo test -p forge-session— 80 passedforge-tuimodules this change touches:multi_task38,overlays79,commands69,focus36,footer32 — all passedOne caveat worth stating: the full
forge-tuisuite hangs onapp::tests::editor::external_editor_resume_draws_after_terminal_reinit, which callsreinit_terminal()/clear_terminal()and needs a real TTY. It is in a file this change does not touch and exercises nothing in the diff, but I could not run it against a pristine tree to prove that, so CI is the first place the whole suite runs end to end.Out of scope
Three deliberate exclusions, listed plainly because every mechanism here derives from
git worktree list, so all three are invisible to it:/diff-style check for an operator:ls .forge/local/worktrees | wc -lagainstgit worktree list | wc -l.Follow-ups
descendant_worktrees()has no tests of its own.FORGE-DESIGN.md§12 describes archive-and-cleanup as one step; the implementation is two supervisor commands with the removal waiting on the archive.