Skip to content

fix(#844): anchor cleanup-worktree branch matching + ambiguity gate - #865

Merged
admarble merged 2 commits into
mainfrom
feature/844-cleanup-worktree-unanchored-substring-matcher-can-
Jul 29, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/844-cleanup-worktree-unanchored-substring-matcher-can-

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

cleanup-worktree.sh resolved its <branch> argument with an unanchored substring match, first-match-wins, no ambiguity check — so 838 matched feature/1838-other-work and (since #838 made local+remote deletion work off the resolved ref) tore down the wrong branch on both.

This replaces the single case "$_wt_branch" in *"$BRANCH_NAME"* | $BRANCH_NAME) arm with a tiered resolver, and makes any 2+-way match fatal:

  1. exact — branch == argument (guards coexisting feature/838-fix / feature/838-fix-more).
  2. anchored — branch's last path segment == argument; plus, for all-digit arguments, the sequant issue-number shapes parseIssueNumberFromBranch already recognizes (feature/<N>-*, feature/<N>, issue-<N>, <N>-*). So 838 anchors to the issue-number position and never matches 1838.
  3. glob — only when the argument itself contains */?/[; keeps the documented, quoted feature/<issue>-* form working, now behind the ambiguity gate.

Any tier yielding 2+ candidates prints every candidate (branch + path) to stderr and exits non-zero instead of silently deleting the first. Candidate collection excludes the main worktree (so 'feature/*'/'*' can't resolve to and remove it) and exec-agent-* sub-worktree branches. bash 3.2-safe (no arrays/mapfile; process substitution keeps the match counters in the current shell).

Only templates/scripts/cleanup-worktree.sh is edited — scripts/cleanup-worktree.sh and scripts/dev/cleanup-worktree.sh are symlinks to it. The documented skill/doc call sites all use the feature/<issue>-* glob form, which still resolves to one branch behind the gate, so no SKILL.md sync is required.

AC Verification

AC Description Status Evidence
AC-1 Bare number anchors (838 ≠ feature/1838-other-work) ✅ anchored tier; test AC-1: a bare number anchors
AC-2 Multi-match → exit non-zero listing every candidate ✅ ambiguity gate; test AC-2/AC-4 (bare number)
AC-3 Over-broad glob ('feature/*') hits the same check ✅ glob tier + gate; tests AC-3/AC-4 (glob) and AC-3: a glob matching exactly one
AC-4 Multi-match fixture per documented input form ✅ new #844 describe block: bare number, glob, full-name (exact-wins)

Mutation verification (gate-test discipline)

Both new gate claims were mutation-verified:

  • Mutation A — anchoring: re-added an unanchored substring match to the anchored tier → only AC-1: a bare number anchors failed (900 mis-matched feature/1900-other → ambiguous, no resolution). Restored → green.
  • Mutation B — ambiguity gate: replaced the -gt 1 fatal branch with first-match-wins → exactly AC-2/AC-4 (bare number) and AC-3/AC-4 (glob) failed, while AC-3: a glob matching exactly one branch still resolves stayed green (correct scoping). Restored → green.

Test plan

Closes #844

Replace the unanchored substring matcher (first-match-wins, no ambiguity
check) with a tiered resolver: exact, anchored (last path segment / sequant
issue-number shapes), then quoted-glob. A bare number anchors to the
issue-number position so 838 no longer matches feature/1838-other-work, and
any tier with 2+ matches exits non-zero listing every candidate instead of
tearing down the first. Adds multi-match regression fixtures per documented
input form.

Closes #844

Entire-Checkpoint: 4af007c1f6ae
@admarble
admarble merged commit e32e21b into main Jul 29, 2026
5 checks passed
@admarble
admarble deleted the feature/844-cleanup-worktree-unanchored-substring-matcher-can- branch July 29, 2026 16:33
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.

cleanup-worktree: unanchored substring matcher can delete the wrong branch (838 matched feature/1838-other-work)

1 participant