Skip to content

Checkup fails when a stale checkup/issue-* branch already exists #1338

@sohni-tagirisa

Description

@sohni-tagirisa

Problem

pdd checkup (PR/fix mode) aborts during worktree setup when a stale checkup/issue-N branch is already present. _setup_worktree in pdd/agentic_checkup_orchestrator.py deletes the existing branch but ignores the result of _delete_branch and unconditionally sets has_branch = False. When the delete fails — which happens when the branch is still registered to a worktree directory that no longer exists (a crashed or out-of-band-cleaned-up prior run) — the subsequent git worktree add -b fails and the whole checkup dies.

Reproduction

from pathlib import Path
import subprocess, shutil, tempfile
from pdd.agentic_checkup_orchestrator import _setup_worktree

d = Path(tempfile.mkdtemp())
def git(*a): return subprocess.run(["git", *a], cwd=d, capture_output=True, text=True)
git("init", "-q"); git("config","user.email","t@x"); git("config","user.name","t")
(d/"f.txt").write_text("x\n"); git("add","."); git("commit","-q","-m","init")

# Stale branch: registered to a worktree dir that is then removed out of band.
stale = d/"old-wt"
git("worktree","add","-q","-b","checkup/issue-7", str(stale), "HEAD")
shutil.rmtree(stale)

print(_setup_worktree(d, issue_number=7, quiet=True, resume_existing=False))

Actual:

(None, "Failed to create worktree: Preparing worktree (new branch 'checkup/issue-7')\nfatal: a branch named 'checkup/issue-7' already exists")

(git branch -D checkup/issue-7 first fails with Cannot delete branch 'checkup/issue-7' checked out at '<gone path>', but that failure is swallowed.)

Expected: the stale branch is reset and a fresh worktree is created; the checkup proceeds.

Root cause

pdd/agentic_checkup_orchestrator.py _setup_worktree, step "Handle existing branch":

_delete_branch(git_root, branch_name)   # return value ignored
has_branch = False                       # assumes success unconditionally

There is also no git worktree prune to clear the stale worktree registration that blocks the delete.

Proposed fix

  1. git worktree prune before handling the branch, so a branch left registered to a removed worktree directory can be deleted/recreated.
  2. Check _delete_branch's return value; only set has_branch = False on success.
  3. If the branch is still undeletable after pruning (genuinely checked out in a live worktree), return a clear, actionable error instead of the cryptic Failed to create worktree.

Acceptance criteria

  • A stale checkup/issue-N branch from a removed worktree dir no longer fails checkup — the branch is reset and a fresh worktree is created.
  • A plain leftover branch (not checked out) still works.
  • A branch genuinely checked out in another live worktree yields a clear, actionable error (which worktree to remove), not Failed to create worktree: a branch named ... already exists.
  • Regression test covering all three cases against a real git repo.

🤖 Filed from Claude Code.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions