Skip to content

fix(cleanup-worktree): merge check queries the raw argument, not the resolved branch — every documented invocation reports "PR not merged" #838

Closed

Description

@admarble

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 || true

Both 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:

  1. The warning is unconditional for every documented invocation, so it carries zero signal. fullsolve/SKILL.md:676, exec/SKILL.md:524, and docs/troubleshooting.md:46 all prescribe ./scripts/cleanup-worktree.sh feature/<issue-number>-*, which can never satisfy the check.
  2. It trains --yes/--force as 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 pipe echo 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 replaced echo y | with real flags — but a warning that always fires makes reaching for those flags mandatory, so the gate is functionally back to advisory.
  3. 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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions