Repository navigation
Description
Summary
templates/scripts/cleanup-worktree.sh aborts after git worktree remove if the caller's cwd was inside the worktree being deleted. The remaining steps (delete local branch, delete remote branch, update main) silently fail because the shell's cwd no longer exists.
Repro
cd ../worktrees/feature/<N>-... # enter the worktree
./scripts/dev/cleanup-worktree.sh feature/<N>-...Output (real, from #551 fullsolve cleanup on 2026-05-03):
📂 Removing worktree...
🌿 Deleting local branch...
☁️ Deleting remote branch...
📥 Updating main branch...
fatal: Unable to read current working directory: No such file or directory
After this, the local branch and remote branch are still present (the git branch -D and git push origin --delete calls fail because the underlying git couldn't read cwd, and 2>/dev/null || true silently swallowed the error). User must manually:
git -C <main-repo> branch -D feature/<N>-...
git -C <main-repo> push origin --delete feature/<N>-...Root Cause
templates/scripts/cleanup-worktree.sh line 76 (git worktree remove "$WORKTREE_PATH" --force) removes the directory the shell is currently in. Subsequent git calls on lines 80, 84, 88 inherit a now-broken cwd. With set -e at line 7, the script exits as soon as any git command fails — but only the silently-suppressed 2>/dev/null || true ones don't propagate, while git checkout main on line 88 (no error suppression) does abort.
Net result: lines 80 and 84 print their banner but the underlying commands fail; line 88 surfaces the cwd error and the script exits.
Proposed Fix
Capture the main worktree path at the start and cd there before the destructive git worktree remove:
BRANCH_NAME=$1
+
+# Resolve main worktree (first entry in `git worktree list`) so all subsequent
+# git commands run from a stable cwd even after the target worktree is deleted.
+MAIN_WORKTREE=$(git worktree list | head -1 | awk '{print $1}')
...
# Remove worktree
echo -e "${BLUE}📂 Removing worktree...${NC}"
+cd "$MAIN_WORKTREE"
git worktree remove "$WORKTREE_PATH" --forceAlternatively, reorder the script so worktree removal is the last step (delete branches and update main first, then git worktree remove). This is cleaner but changes user-visible ordering.
Notes
templates/scripts/cleanup-worktree.shis the canonical source perreference_scripts_layout.md.scripts/dev/cleanup-worktree.shis a symlink andscripts/dev/is gitignored — onlytemplates/scripts/needs to be patched.- Adding
cd "$MAIN_WORKTREE"is also defensive against an alternative repro: caller in any sub-directory of the worktree (not just the worktree root).
Acceptance Criteria
- AC-1: Running
cleanup-worktree.sh <branch>from inside the worktree being deleted completes all 5 steps (worktree remove, local branch delete, remote branch delete, checkout main, fetch+pull) withoutUnable to read current working directoryerrors. - AC-2: After the script exits,
git worktree listdoes not include the deleted worktree, andgit branch --list <branch>andgit ls-remote origin <branch>both return empty. - AC-3: Behavior unchanged when called from the main repo root (existing happy path).
Activity
admarble commentedon May 6, 2026
Plan Complete
AC Checklist
Unable to read current working directoryerror)git worktree listexcludes the deleted path;git branch --list <branch>andgit ls-remote origin <branch>both emptyVerification
cd <worktree>; cleanup-worktree.sh <branch>— all 5 steps succeedgit worktree list/git branch --list/git ls-remoteall confirm clean statecd <main-repo>; cleanup-worktree.sh <branch>— output and behavior unchangedImplementation Plan
Phase 1: Patch
templates/scripts/cleanup-worktree.shgit worktree list --porcelainafter line 16.cd "\$MAIN_WORKTREE"before line 76 (git worktree remove ... --force).scripts/cleanup-worktree.shis a symlink → no second edit needed.Phase 2: Verify via
bash -nsyntax check + read-back.Design Decisions
head|awk: robust against paths with spaces, documented machine-readable interface.cdlate: keeps user-visible step order identical to AC-3.Recommended Workflow
spec → exec → qa(no/test, quality loop disabled).Ready to implement.
admarble commentedon May 6, 2026
QA Review
AC Coverage (live-verified)
cd ../worktrees/test-cleanup-575 && cleanup-worktree.sh test-cleanup-575→ exit 0, all 5 steps complete, noUnable to read current working directoryerrorgit worktree list | grep -c test-cleanup-575= 0;git branch --list test-cleanup-575empty;git ls-remote origin test-cleanup-575emptycleanup-worktree.sh test-cleanup-575b→ exit 0, output identical to pre-patchQuality Metrics
templates/scripts/cleanup-worktree.sh)Code Review
MAIN_WORKTREEearly viagit worktree list --porcelain(machine-readable, not the formatted form).cd "$MAIN_WORKTREE"happens between the "Removing worktree..." banner and the destructivegit worktree remove, preserving the user-visible step ordering for AC-3.set -eensures hard fail ifcdever fails.MAIN_WORKTREEis guaranteed non-empty by line 84 sincegit worktree listalready executed successfully on lines 29/34.Self-Evaluation
awk '$2'. Pre-existing limitation in line 34 (worktree-path lookup), not introduced here. Sequant's own worktree naming convention never produces such paths.Verdict: READY_FOR_MERGE
admarble commentedon May 6, 2026
/fullsolve Complete
Issue: #575
Status: READY_FOR_MERGE
PR: #579
Summary
templates/scripts/cleanup-worktree.sh, +9/-0)Iterations
Risk Assessment
awk '$2'; pre-existing limitation on line 34, not introduced here. Sequant's own worktree naming never produces such paths.templates/scripts/*.sh— repo has none. AC-1 repro is the test.Ready for human review and merge.
fix(#575): pin cwd before exec-agents loop, parse porcelain for white…
Merge origin/main into feature/575-...
fix(#575): cleanup-worktree.sh aborts when invoked from inside the wo…
fix(cleanup-worktree): resolve the branch ref once — documented glob …
fix(cleanup-worktree): resolve the branch ref once, and let the docum…