Repository navigation
Description
Summary
cleanup-worktree.sh resolves the worktree by substring match but feeds the caller's raw argument to gh pr list --head, which requires an exact branch ref. Every invocation form except a full literal branch name therefore reports ⚠️ Warning: PR for this branch is not merged — including the form the skills and docs tell you to use — no matter what the PR's real state is.
The gate this defeats is the one #750 was filed to build.
Repro (PR #832 for #817, squash-merged, verified state=MERGED)
$ gh pr list --head "817" --state merged --json state --jq '.[0].state'
# (empty) → "PR not merged"
$ gh pr list --head "feature/817-*" --state merged --json state --jq '.[0].state'
# (empty) → would say "not merged"
$ gh pr list --head "feature/817-feat-run-ready-gate-run-the-post-qa-ready-gate-ins" --state merged ...
MERGED # only an exact ref works
Observed live: ./scripts/cleanup-worktree.sh 817 printed the not-merged warning and Non-interactive context and PR not merged — pass --yes or --force to proceed. Exiting without changes. for a PR that had merged ~90 seconds earlier.
Correction (added after implementing). I originally wrote that the documented
feature/<issue>-*form "reports PR not merged". That is wrong, and understates the problem: the glob never reaches the merge check at all. The worktree lookup is a literal substring search, so the*is compared as a character and the script exits earlier with❌ Error: Worktree not found for branch: feature/817-*. Verified against the live repo:input '833' -> resolved: 'feature/833-fix-cli-bare-parseint-...' input 'feature/833-*' -> <NONE — Worktree not found> input 'feature/833-fix-cli-bare-parseint-…' -> resolved: 'feature/833-fix-cli-bare-parseint-...'So there are two defects here, not one: (a) the documented glob invocation has never worked, and (b) the shorthand form resolves but then leaks the raw argument downstream. Both live in the same three lines and are fixed together.
Additionally: the silent branch-delete failures
The raw argument doesn't only reach gh. It also reaches (:184, :192):
git branch -D "$BRANCH_NAME" 2>/dev/null || true
git push origin --delete "$BRANCH_NAME" 2>/dev/null || trueBoth match exactly, and both are || true-suppressed — so on a shorthand they fail invisibly and the script still prints ✅ Cleanup complete! while leaving the local and remote branches in place. Observed live: after cleanup-worktree.sh 817 reported success, feature/817-… was still present and needed a manual git branch -D.
This is the more damaging half. The merge-check false alarm is at least visible.
Root Cause
templates/scripts/cleanup-worktree.sh — the worktree lookup is deliberately fuzzy (index($0, target) > 0, :105-115), which is why a shorthand finds the worktree at all:
WORKTREE_PATH=$(git worktree list --porcelain | awk -v target="$BRANCH_NAME" '
/^branch refs\/heads\// { sub(...); if (index($0, target) > 0) { print path; exit } }')…but the merge check twenty lines later reuses the unresolved argument (:129):
PR_STATUS=$(gh pr list --head "$BRANCH_NAME" --state merged --json number,state --jq '.[0].state' 2>/dev/null || echo "")--head is an exact-match filter. A substring (817) or a glob (feature/817-*) matches no head ref, PR_STATUS is empty, and [ "$PR_STATUS" != "MERGED" ] takes the not-merged branch. The awk resolver already walks past the real branch ref — it just discards it.
Why this matters (it re-arms the reflex #750 removed)
The failure is fail-safe in direction — it warns when it shouldn't, never the reverse — so nothing is destroyed. The damage is behavioral:
- The warning is unconditional for every documented invocation, so it carries zero signal.
fullsolve/SKILL.md:676,exec/SKILL.md:524, anddocs/troubleshooting.md:46all prescribe./scripts/cleanup-worktree.sh feature/<issue-number>-*, which can never satisfy the check. - It trains
--yes/--forceas the default incantation. fix(cleanup-worktree): deletes remote branch before PR is merged — closes the open PR unmerged #750's whole thesis was that agents pipeecho y |to get past this exact prompt, and that the gate is worthless if bypassing it is routine. fix(cleanup-worktree): deletes remote branch before PR is merged — closes the open PR unmerged #750 replacedecho y |with real flags — but a warning that always fires makes reaching for those flags mandatory, so the gate is functionally back to advisory. - Remote-branch deletion is always skipped (:189-194 gates on the same
PR_STATUS), so the post-merge teardown the script exists to perform never completes on the documented path without--delete-remote/--force.
.claude/scripts/cleanup-worktree.sh (the divergent copy #750 flagged) no longer exists, and scripts/ is a symlink to templates/scripts/, so there is exactly one file to fix.
Proposed Fix (directional)
Have the awk resolver emit the resolved branch ref alongside the path, and use that for the gh query:
read -r WORKTREE_PATH RESOLVED_BRANCH < <(git worktree list --porcelain | awk -v target="$BRANCH_NAME" '
/^worktree / { sub(/^worktree /, ""); path = $0 }
/^branch refs\/heads\// { sub(/^branch refs\/heads\//, "");
if (index($0, target) > 0) { print path, $0; exit } }')
PR_STATUS=$(gh pr list --head "$RESOLVED_BRANCH" --state merged ...)Resolution already succeeds for every accepted input form, so this makes the merge check as permissive as the lookup it follows. (Paths with whitespace need a different field split than read -r a b — worth handling, since the porcelain parser was hardened for exactly that in #575.)
Acceptance Criteria
- The merge check queries the resolved branch ref, not the raw argument, so
817,feature/817-*, and the full branch name all produce the samePR_STATUS. - Invoking with a shorthand or glob against a merged PR completes teardown with no warning and no
--yes(the fix(cleanup-worktree): deletes remote branch before PR is merged — closes the open PR unmerged #750 happy path short-circuits the confirm gate entirely). - Invoking against a genuinely unmerged PR still warns and still skips the remote delete — the fix(cleanup-worktree): deletes remote branch before PR is merged — closes the open PR unmerged #750 protection is preserved, not weakened, for all three input forms.
- Whitespace-containing worktree paths still resolve correctly (do not regress fix(cleanup-worktree): aborts mid-way when invoked from inside the worktree being deleted #575's porcelain parsing).
- A test covers all three input forms against both merged and unmerged PR states, with
ghstubbed. - Mutation-verified per the repo's gate-test rule: revert the resolution fix, confirm exactly the new test fails, record the result.
Out of scope
Changing #750's gating semantics or the --yes/--force/--delete-remote flag surface; the fuzzy worktree lookup itself (it is the affordance that makes shorthands work and should stay).
References
templates/scripts/cleanup-worktree.sh:105-115(fuzzy resolve),:129(exact-match query),:132-152(confirm gate),:189-194(remote-delete gate)- fix(cleanup-worktree): deletes remote branch before PR is merged — closes the open PR unmerged #750 — built the gate this silently disarms; fix(cleanup-worktree): aborts mid-way when invoked from inside the worktree being deleted #575 — hardened the same porcelain parsing
- Callers that use the always-failing form:
{.claude,templates,}/skills/fullsolve/SKILL.md:676,skills/exec/SKILL.md:524,docs/troubleshooting.md:46 - Observed during feat(run): --ready-gate — run the post-QA ready gate inside
sequant run(automates the any-gaps/fix-gaps second look, stops at human merge) #817 / PR feat(#817): --ready-gate — run the post-QA ready gate insidesequant run#832 post-merge cleanup
Activity
fix: close three pre-2.10.0 gaps found by release verification (#851)