land: hand worktree removal to the owning session after a PR lands (no worktree GC today; 516 worktrees accumulated)
Problem
In a repo where many agent sessions each work in their own linked worktree (one worktree per task or PR), nothing in gstack ever removes a worktree after its PR lands. /land-and-deploy and gstack-ship-queue land merge the PR, but the lane's worktree stays registered and on disk.
Measured on one Windows host: 516 registered worktrees had piled up. A manual pass (snapshot each tree, archive its non-regenerable ignored files, then git worktree remove) cleared 273 of them. Every local cleanup we had was a dry-run report, a manual script or an unregistered hook, so none of them ever removed anything. gstack, gbrain and gbrain-evals docs contain no worktree lifecycle or GC step.
Proposal
Tie cleanup to the event that proves the work is finished: a landed PR.
- After
landconfirms the PR isMERGEDand the landed tree is verified,landmarks the lane's worktree as releasable. It writes a small record: path, branch, head SHA, PR, merge SHA. - The session that owns the worktree removes it with a plain
git worktree remove, never--force, once it has released the tree. That happens at session end, or on its next start when it sees the record. The coordinator never deletes another session's checkout, because work can continue in a tree after a merge. - If the tree is dirty, nothing is removed. The record says why, so a person or a periodic sweep can handle it.
A periodic sweep would still be needed for abandoned and unfinished trees. That part needs stricter rules:
- skip locked trees, trees with recent activity and trees with live processes;
- skip trees containing nested repos, symlinks or junctions;
- before any
--force, take a full snapshot (not a partial one) and archive the ignored files; - cap the number of removals per run.
Most of the sprawl, though, is trees whose PR already landed, and the land hand-off above removes those.
Pitfalls seen in practice
- In squash-merge repos, a merged branch whose remote head was deleted looks like it has unpushed commits by ancestry. The merged PR's head SHA has to be the test.
- On Windows, Git for Windows unlinks symlinked shared directories (
node_modules,.next,.venv) rather than following them, which is the behaviour you want. A Linux venv symlink created from WSL makesgit worktree removefail with "Function not implemented" and leaves a half-deleted tree. - Copied
.envfiles that differ from the main checkout's mean the tree has to be kept. Compare them byte for byte; never read them.
Happy to test a prototype on the Windows multi-worktree setup.
🤖 Generated with Claude Code
Thanks for the detailed write-up and the numbers from your Windows host. We're going to close this one.
The per-task worktrees in a setup like yours are made by the agent sessions or tools that start each task, not by gstack, and having land or a sweep delete checkouts is the kind of automatic removal that can lose work, as your own pitfalls list shows. That puts it outside what we plan to build.
For trees whose PR has landed, git worktree list plus a plain git worktree remove (never --force), run by the session that owns each tree, stays the safe route. Thanks again for offering to test.