Skip to content

feat(session): reclaim worktrees when a session ends - #704

Merged
mohitranka merged 1 commit into
mainfrom
feat/worktree-gc
Sep 15, 2026
Merged

mohitranka merged 1 commit into
mainfrom
feat/worktree-gc

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

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 committed HEAD. 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 you with the qualifier cleanup blocked, the switcher ranks it below rows that need an answer, and x on the row retries the removal. archive() refuses to overwrite it, and prompt queueing rejects it.
  • All-or-nothing removal — the session's own checkout and every finished child agent's worktree are preflighted together. If any one holds uncommitted work, nothing is removed and the session becomes retained. That keeps one retryable unit per session and makes retained mean exactly one thing: this session still owns a checkout on disk. A Dirty that lands between the preflight and the removal is converted to retained too, 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 records worktree_path/worktree_branch, so this reads what is already in memory; no journal walk, no new dependency, and no child rows in the navigator.
  • One verb: x — archives and cleans in both the navigator and the session switcher, always behind a confirmation. The navigator's d is 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.
  • Honest outcomes — the confirm copy no longer claims the worktree is kept, and the parked session view is restored from the roster's lifecycle rather than from the command result, because a blocked cleanup returns success by design.
  • Per-session done intents — navigator_done_pending is a set. Previously a second confirmed action silently overwrote the first.

Verification

  • cargo fmt --all -- --check — clean
  • cargo clippy -p forge-core -p forge-session -p forge-tui --all-targets — zero warnings
  • cargo test -p forge-session — 80 passed
  • forge-tui modules this change touches: multi_task 38, overlays 79, commands 69, focus 36, footer 32 — all passed

One caveat worth stating: the full forge-tui suite hangs on app::tests::editor::external_editor_resume_draws_after_terminal_reinit, which calls reinit_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:

  1. Leftovers from sessions that already ended are not swept. This stops new accumulation; it does not drain the pile already on disk.
  2. A crash between the Git removal and the database write has no retry. The next start does not resume cleanup.
  3. A directory Git has stopped registering is invisible — never seen, never preflighted, never removed. /diff-style check for an operator: ls .forge/local/worktrees | wc -l against git 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.

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
mohitranka merged commit 3d8911c into main Sep 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant