Skip to content

fix(cleanup-worktree): resolve the branch ref once — documented glob form never worked, shorthand silently skipped branch deletes - #840

Merged
admarble merged 2 commits into
mainfrom
feature/838-fix-cleanup-worktree-merge-check-queries-the-r
Jul 29, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/838-fix-cleanup-worktree-merge-check-queries-the-r

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

cleanup-worktree.sh matched the worktree by literal substring and then handed the caller's raw argument to every downstream consumer. Two defects, same three lines.

1. The documented invocation never worked. fullsolve/SKILL.md:676, exec/SKILL.md:524 and docs/troubleshooting.md:46 all prescribe cleanup-worktree.sh feature/<issue-number>-*. The matcher compared the * as a literal character, so that form exited with ❌ Error: Worktree not found. 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-...'

2. Downstream consumers got the shorthand, not the ref. gh pr list --head, git branch -D and git push origin --delete all match exactly:

$ gh pr list --head "817"                             --state merged --jq '.[0].state'
                                                        # (empty) => "PR not merged"
$ gh pr list --head "feature/817-…-ins"               --state merged --jq '.[0].state'
MERGED

So the merge check reported "not merged" for a merged PR on every shorthand invocation. Worse, the two branch deletes are || true-suppressed, so they failed invisibly — the script printed ✅ Cleanup complete! while leaving both local and remote branches in place.

Both were observed live during #817's post-merge cleanup: the not-merged warning fired ~90s after the PR merged, and the local branch survived and needed a manual git branch -D.

Why the merge-check symptom matters beyond a false alarm

#750 built that gate precisely because agents were piping echo y | past it. A warning that fires on every invocation carries no signal and makes --yes/--force the mandatory incantation — which re-arms the exact bypass reflex #750 removed. The gate was functionally back to advisory, with no code regression to point at.

Change

Resolve path and ref in one pass, then use $RESOLVED_BRANCH everywhere downstream. The awk is replaced with bash case, so glob matching is native and no regex-escaping dance is needed — both accepted forms collapse into one test:

case "$_wt_branch" in
    *"$BRANCH_NAME"* | $BRANCH_NAME)   # quoted = substring; unquoted = glob

while IFS= read -r preserves whitespace in paths (#575's hardening, covered by a dedicated test). mapfile is deliberately avoided — macOS ships bash 3.2.

Usage/--help and the header comment now document all three accepted forms, since previously only the full branch name was shown and the other two were undiscoverable.

Test plan

New __tests__/cleanup-worktree-branch-resolution.integration.test.ts drives the real script against a real throwaway git repo with a real worktree and a real bare origin, with gh stubbed on PATH to emulate --head's exact-match semantics. Every assertion is parametrized over all three input forms, so a fix handling only one of them fails.

#750's protections are asserted in both directions: an unmerged PR still refuses non-interactively, and still skips the remote delete under --yes.

  • 16 tests pass
  • bash -n syntax check
  • --help output verified
  • Full __tests__/ sweep: 63 files, 1148 tests, 0 failures

Mutation record (per the CLAUDE.md gate-test rule)

Each mutation applied to the committed fix, then reverted and re-confirmed green:

Mutation Result
(baseline) 0 failed / 16 passed
M1 · drop the | $BRANCH_NAME glob arm 5 failed — every glob-form test
M2 · merge check back to --head "$BRANCH_NAME" 6 failed
M3 · git branch -D "$BRANCH_NAME" 2 failed — the two non-literal forms (the literal form correctly still passes, since there BRANCH_NAME == RESOLVED_BRANCH)

Note

The new test is subprocess- and git-heavy (~30s added to the integration project). It is parametrized 3 forms × 5 behaviors because the ACs require exactly that matrix; if the integration suite's runtime becomes a problem, collapsing the three merged-path assertions into one per form is the obvious trim.

Closes #838

admarble added 2 commits July 28, 2026 01:16
…ented glob form match

cleanup-worktree.sh matched the worktree by literal substring and then handed
the caller's RAW argument to every downstream consumer. Two defects, same three
lines:

1. The documented invocation never worked. fullsolve/SKILL.md:676,
   exec/SKILL.md:524 and docs/troubleshooting.md:46 all prescribe
   `cleanup-worktree.sh feature/<issue-number>-*`. The matcher compared the
   `*` literally, so that form died with "Worktree not found" — only a full
   literal branch name resolved.

2. Downstream consumers got the shorthand, not the ref. `gh pr list --head`,
   `git branch -D` and `git push origin --delete` all match EXACTLY:

     gh pr list --head 817          -> (empty)  => "PR not merged"
     gh pr list --head feature/817-...-ins -> MERGED

   The merge check therefore reported "not merged" for a merged PR on every
   shorthand invocation. Worse, `git branch -D` and `git push origin --delete`
   are `|| true`-suppressed, so they failed INVISIBLY: the script printed
   "Cleanup complete!" while leaving both branches behind. Observed live during
   #817's post-merge cleanup, which required a manual `git branch -D`.

The merge-check symptom matters beyond the false alarm: a warning that fires on
every invocation carries no signal and makes --yes/--force the mandatory
incantation, which re-arms the exact bypass reflex #750 removed. #750 built
that gate; this silently disarmed it.

Both are fixed by resolving path AND ref in one pass. The awk is replaced with
bash `case`, so glob matching is native (no regex-escaping dance) and both
accepted forms are one test:

    *"$BRANCH_NAME"*   quoted   -> literal substring  (`838`)
    $BRANCH_NAME       unquoted -> shell glob         (`feature/838-*`)

`while IFS= read -r` preserves whitespace in paths (#575's hardening — covered
by a dedicated test). `mapfile` is avoided: macOS ships bash 3.2.

New __tests__/cleanup-worktree-branch-resolution.integration.test.ts drives the
real script against a real throwaway repo with a real worktree and a real bare
origin, with `gh` stubbed on PATH to emulate --head's exact-match semantics.
Every assertion is parametrized over all three input forms, so a fix that only
handles one of them fails. #750's protections are asserted in both directions:
an unmerged PR still refuses non-interactively, and still skips the remote
delete under --yes.

16 tests pass.
… usage/--help

The shorthand and glob forms both resolve now, but usage said only
`<branch-name>` and showed a single full-name example — so the affordances stay
undiscovered and the docs keep implying the full name is required. Header
comment and print_usage list all three, kept in sync as the file already
requires.
@admarble
admarble merged commit 65a32eb into main Jul 29, 2026
5 checks passed
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.

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

1 participant