agents: coordinator-created worktrees and branches must be cleaned up automatically when their PR merges #75
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Context
During the session that fixed issues #68–#71, the coordinator spawned three
Agentcalls withisolation: "worktree"— one running itself (the manifest/install.sh fix) and two general-purpose subagents (the docs fix and the kyberforge dogfooding fix). Each produced real commits and opened a PR (#72, #73, #74). All three merged successfully.None of the three worktrees, and none of the associated local branches, were cleaned up as part of that workflow. They sat on disk until the user explicitly asked "delete the local worktrees now that they're merged" — and even after that request was handled,
git branchstill showed five stale local branches: the three merged feature branches (docs/68-70-subagent-orchestration-guidance,fix/69-71-factory-authoring-fixes,fix/plugin-manifest-and-worktree-hooks) plus twoworktree-agent-<id>branches that theAgenttool's worktree isolation mechanism creates automatically and thatgit worktree removedoes not delete on its own. The user had to notice this from rawgit branchoutput and ask again before a second cleanup pass happened.Two compounding mechanical gotchas made this worse:
git worktree removerefuses to run ("fatal: working trees containing submodules cannot be moved or removed") on any worktree where a test run initialized git submodules (this repo has several —tests/bats,docs/wiki, etc.) — requiredgit worktree remove --force --force(double-f) to override, which isn't discoverable without hitting the error first.git worktree removenever deletes the branch checked out in that worktree — branch cleanup is a fully separate, easy-to-forget step, and theAgenttool's own auto-generatedworktree-agent-<id>isolation branch isn't the same as the feature branch the agent actually pushed, so both need deleting independently.Recommendation
Treat "PR merged" and "worktree/branch cleanup" as one atomic step, not two separately-remembered ones. Whenever a coordinator creates a worktree (via
Agent(isolation: "worktree")orgit worktree adddirectly) to land a PR, the close-out for that PR should immediately include:pull_request_readstate, ormerged: true).git worktree remove --force --force <path>(the double--fis required whenever the worktree initialized submodules to run tests — assume it will have, in this repo).git branch -dthe feature branch and anyworktree-agent-<id>isolation branch tied to that worktree.This should be documented in this repo's
AGENTS.md"Subagent orchestration" section (added in #73) as a standing rule, so it happens as a matter of course rather than requiring the user to inspectgit worktree list/git branchand ask for cleanup twice.