Skip to content

feat(doctor,setup): auto-prune stale agent worktrees#432

Merged
NagyVikt merged 1 commit intomainfrom
agent/claude/auto-prune-stale-agent-worktrees-on-setu-2026-04-24-16-38
Apr 27, 2026
Merged

feat(doctor,setup): auto-prune stale agent worktrees#432
NagyVikt merged 1 commit intomainfrom
agent/claude/auto-prune-stale-agent-worktrees-on-setu-2026-04-24-16-38

Conversation

@NagyVikt
Copy link
Copy Markdown
Collaborator

Automated by gx branch finish (PR flow).

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>
@NagyVikt NagyVikt merged commit 3ad44d6 into main Apr 27, 2026
@NagyVikt NagyVikt deleted the agent/claude/auto-prune-stale-agent-worktrees-on-setu-2026-04-24-16-38 branch April 27, 2026 08:16
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