Conversation
When `gx branch finish --cleanup` fails mid-cleanup (crash, network loss, killed process), the agent branch is deleted but the worktree under `.omc/agent-worktrees/` or `.omx/agent-worktrees/` is left stranded in a detached-HEAD state. Over time the repo accumulates orphaned worktrees (observed on agents-hivemind: 9 stranded worktrees, 7 detached-HEAD). `scripts/agent-worktree-prune.sh` already handles these, but was only invoked manually via `gx cleanup` or by the background daemon started via `gx agents start`. Neither `gx setup` nor `gx doctor` called it -- their existing `autoFinishReadyAgentBranches` sweep only covered agent branches that still existed locally with unmerged commits. Changes: - Add `pruneStaleAgentWorktrees(repoRoot, options)` to `src/doctor/index.js`, invoking the existing `worktreePrune` script with `--delete-branches --delete-remote-branches --include-pr-merged --force-dirty --idle-minutes <60>`. - Wire the helper into `gx setup` and `gx doctor` (protected-base sandbox paths and direct paths) after the existing `autoFinishReadyAgentBranches` call. - Honor `--dry-run` and add opt-out env var `GUARDEX_SKIP_AUTO_WORKTREE_PRUNE=1` mirroring the existing `GUARDEX_SKIP_AUTO_FINISH_READY_BRANCHES=1` pattern. - Set `GUARDEX_DOCTOR_SANDBOX=1` on the nested sandbox child so the nested pass skips prune/auto-finish (activates the previously dead `:873` guard as originally documented). - Thread `worktreePrune` payload into doctor's `--json` output alongside the existing `autoFinish` field. `--force-dirty` is safe here because the prune script's dirty-skip only runs after a `remove_reason` is already set (temporary, detached, merged-agent-branch, merged-agent-pr, clean-agent). Active work on an unmerged branch never reaches that gate. The `--idle-minutes 60` window additionally filters recently-touched orphans. Tests (test/doctor.test.js): - `gx doctor auto-prunes detached-HEAD agent worktrees under .omc/agent-worktrees` - `gx doctor preserves stranded worktrees when GUARDEX_SKIP_AUTO_WORKTREE_PRUNE=1` Note: the pre-existing baseline failure `doctor auto-finishes clean pending agent branches against the current local base branch` in commit 27026a1 (`agent/planner` vs `agent/claude` mismatch in `runBranchStart`) is unrelated to this change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Automated by gx branch finish (PR flow).