Skip to content

fix(finishing-a-development-branch): handle worktree CWD cleanup ordering#391

Closed
tartansandal wants to merge 1 commit intoobra:mainfrom
tartansandal:fix/worktree-cwd-cleanup
Closed

fix(finishing-a-development-branch): handle worktree CWD cleanup ordering#391
tartansandal wants to merge 1 commit intoobra:mainfrom
tartansandal:fix/worktree-cwd-cleanup

Conversation

@tartansandal
Copy link
Copy Markdown

@tartansandal tartansandal commented Jan 31, 2026

Summary

  • Rewrite Step 5 (Cleanup Worktree) with correct ordering: move CWD out first, remove worktree, prune refs, then delete branch
  • Move branch deletion out of Option 1/4 inline code into Step 5 (which already handles it)
  • Add cross-session plan guidance: warn that the executing session's CWD is inside the worktree
  • Add two new Common Mistakes: "Removing worktree while CWD is inside it" and "Deleting branch before removing worktree"

Context

Discovered while using the skill to merge a feature branch from a git worktree. The previous Step 5 didn't account for:

  1. CWD inside worktree: After git worktree remove, the shell's working directory no longer exists. Every subsequent command fails with "Path does not exist" — unrecoverable in the current shell.
  2. Branch deletion ordering: git branch -d fails if the branch is still checked out in a worktree. The worktree must be removed first.
  3. Cross-session plans: When the skill generates a plan for another session to execute (e.g. via executing-plans), the executing session's CWD is inside the worktree. The plan must warn about this.

Test Plan

  • Subagent test: direct execution from inside worktree (Option 1 merge) — agent followed correct ordering
  • Subagent test: plan generation for cross-session execution — plan included CWD warning and prefixed all commands with cd <main-repo>

Summary by CodeRabbit

  • Documentation
    • Clarified steps for finishing development branches using git worktrees, emphasizing the correct order of operations for cleanup
    • Added explicit guidance on directory context requirements and potential issues when removing worktrees
    • Enhanced Quick Reference table and included rationale blocks explaining worktree removal sequencing

✏️ Tip: You can customize this high-level summary in your review settings.

…ring

Step 5 (Cleanup Worktree) now documents the correct ordering:
1. Move CWD out of worktree before removal
2. Remove worktree before deleting branch
3. Prune stale worktree refs

Without this, removing a worktree while the shell CWD is inside it
makes every subsequent command fail with 'Path does not exist' —
an unrecoverable state. Similarly, git refuses to delete a branch
that is checked out in a worktree.

Also adds guidance for cross-session plans where the executing
session's CWD will be inside the worktree.

Adds two new Common Mistakes entries for these failure modes.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 31, 2026

📝 Walkthrough

Walkthrough

A documentation file for finishing a development branch is updated to clarify worktree cleanup procedures, reorganizing Steps 4 and 5 with explicit sequencing requirements, rationale blocks explaining common pitfalls, and warnings about working directory positioning relative to worktree removal.

Changes

Cohort / File(s) Summary
Worktree Cleanup Documentation
skills/finishing-a-development-branch/SKILL.md
Updated Step 4 options to clarify switching to base branch in main repo (not worktree); Step 5 rewritten to emphasize correct ordering—move working directory out of worktree before removal. Added explicit rationale blocks and warnings about directory positioning, updated Quick Reference table, and refined textual guidance throughout (+31/-13 lines).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

Suggested reviewers

  • yoavsion

Poem

🐰 A branch completed, time to clean,
Step out before removing the scene,
Order matters in the git ballet,
Worktree removed, the branch goes away,
Garden tidy—all is well! 🌿

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing worktree CWD cleanup ordering in the finishing-a-development-branch skill documentation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@skills/finishing-a-development-branch/SKILL.md`:
- Around line 130-160: The doc is inconsistent: "Step 5: Cleanup Worktree" now
says cleanup applies to Options 1, 2, 4 but the Quick Reference table and the
“Automatic worktree cleanup” common-mistakes section still imply a different
behavior for Option 2; update the Quick Reference table entry for Option 2 to
indicate that worktree cleanup is part of the process (matching "Options 1, 2,
4"), and revise the “Automatic worktree cleanup” mistake text to state the new
policy (cleanup occurs for Options 1, 2, 4; Option 3 keeps the worktree), add
the explicit warning text required for generated plans about CWD being inside
the worktree and that plans must either start with "cd <main-repo>" or note that
worktree removal will invalidate the shell.
- Around line 185-191: Replace the two bolded section titles "**Removing
worktree while CWD is inside it**" and "**Deleting branch before removing
worktree**" with proper ATX headings using "### Removing worktree while CWD is
inside it" and "### Deleting branch before removing worktree" in SKILL.md to
satisfy MD036; ensure the rest of the paragraph content under each heading
remains unchanged and that any references to these titles (e.g., in links or
TOC) are updated if necessary.

Comment thread skills/finishing-a-development-branch/SKILL.md
Comment thread skills/finishing-a-development-branch/SKILL.md
@obra obra added claude-code Claude Code (Anthropic CLI) issues bug Something isn't working labels Feb 5, 2026
rahulsc added a commit to rahulsc/superpowers that referenced this pull request Mar 1, 2026
Delegate creation to Claude Code's built-in EnterWorktree tool.
Add optional worktree skip, state.yml tracking, team mode with
per-implementer worktrees, and QA worktree strategy.

Drops: directory selection logic (.worktrees/ vs worktrees/),
gitignore verification (native tool handles), auto-commit of
.gitignore changes without user consent.

Keeps: CLAUDE.md preference check, setup command auto-detection,
baseline test verification, project setup auto-detection.

Addresses Finding X, obra#583, obra#574, obra#371, obra#348, obra#299, obra#279, obra#238,
obra#186, obra#167, #5, PR obra#483, PR obra#391.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rahulsc added a commit to rahulsc/superpowers that referenced this pull request Mar 1, 2026
Add explicit cd-to-repo-root before any worktree removal, using
worktree.main.repo_root from state.yml. Running git worktree remove
from inside the worktree itself causes CWD to vanish mid-command.

Move team shutdown to Step 1 of main flow (not just Team Context
section) — specialists must be shut down before merging.

Add state.yml write on completion: phase:idle, plan.status:executed,
clear worktree.* and team entries so future sessions know no active
work is in progress.

Add native EnterWorktree cleanup awareness — check if worktree still
exists before manual removal, since native tool may auto-clean.

Replace "Type 'discard' to confirm" with AskUserQuestion approach.

Fix Option 2 worktree cleanup contradiction — quick reference table
now correctly shows Option 2 keeps worktree (no cleanup); prose
updated to match.

Addresses obra#167, PR obra#391, Findings D, X.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rahulsc added a commit to rahulsc/superpowers that referenced this pull request Mar 1, 2026
Delegate creation to Claude Code's built-in EnterWorktree tool.
Add optional worktree skip, state.yml tracking, team mode with
per-implementer worktrees, and QA worktree strategy.

Drops: directory selection logic (.worktrees/ vs worktrees/),
gitignore verification (native tool handles), auto-commit of
.gitignore changes without user consent.

Keeps: CLAUDE.md preference check, setup command auto-detection,
baseline test verification, project setup auto-detection.

Addresses Finding X, obra#583, obra#574, obra#371, obra#348, obra#299, obra#279, obra#238,
obra#186, obra#167, #5, PR obra#483, PR obra#391.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rahulsc added a commit to rahulsc/superpowers that referenced this pull request Mar 1, 2026
Add explicit cd-to-repo-root before any worktree removal, using
worktree.main.repo_root from state.yml. Running git worktree remove
from inside the worktree itself causes CWD to vanish mid-command.

Move team shutdown to Step 1 of main flow (not just Team Context
section) — specialists must be shut down before merging.

Add state.yml write on completion: phase:idle, plan.status:executed,
clear worktree.* and team entries so future sessions know no active
work is in progress.

Add native EnterWorktree cleanup awareness — check if worktree still
exists before manual removal, since native tool may auto-clean.

Replace "Type 'discard' to confirm" with AskUserQuestion approach.

Fix Option 2 worktree cleanup contradiction — quick reference table
now correctly shows Option 2 keeps worktree (no cleanup); prose
updated to match.

Addresses obra#167, PR obra#391, Findings D, X.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rahulsc added a commit to rahulsc/superpowers that referenced this pull request Mar 10, 2026
Delegate creation to Claude Code's built-in EnterWorktree tool.
Add optional worktree skip, state.yml tracking, team mode with
per-implementer worktrees, and QA worktree strategy.

Drops: directory selection logic (.worktrees/ vs worktrees/),
gitignore verification (native tool handles), auto-commit of
.gitignore changes without user consent.

Keeps: CLAUDE.md preference check, setup command auto-detection,
baseline test verification, project setup auto-detection.

Addresses Finding X, obra#583, obra#574, obra#371, obra#348, obra#299, obra#279, obra#238,
obra#186, obra#167, #5, PR obra#483, PR obra#391.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rahulsc added a commit to rahulsc/superpowers that referenced this pull request Mar 10, 2026
Add explicit cd-to-repo-root before any worktree removal, using
worktree.main.repo_root from state.yml. Running git worktree remove
from inside the worktree itself causes CWD to vanish mid-command.

Move team shutdown to Step 1 of main flow (not just Team Context
section) — specialists must be shut down before merging.

Add state.yml write on completion: phase:idle, plan.status:executed,
clear worktree.* and team entries so future sessions know no active
work is in progress.

Add native EnterWorktree cleanup awareness — check if worktree still
exists before manual removal, since native tool may auto-clean.

Replace "Type 'discard' to confirm" with AskUserQuestion approach.

Fix Option 2 worktree cleanup contradiction — quick reference table
now correctly shows Option 2 keeps worktree (no cleanup); prose
updated to match.

Addresses obra#167, PR obra#391, Findings D, X.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@obra obra added skills Skill system, development, and individual skills worktrees Git worktree management labels Mar 23, 2026
@arittr
Copy link
Copy Markdown
Collaborator

arittr commented Apr 28, 2026

Thanks for the PR. The substantive cleanup-ordering work here has been superseded by #1121, now merged into dev.

The current finishing-a-development-branch flow detects the environment, avoids cleaning up worktrees for PR creation, moves out of the worktree before removal, removes/prunes worktrees before branch deletion, and documents the ordering pitfalls.

Closing this as superseded by the broader worktree rototill.

@arittr arittr closed this Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working claude-code Claude Code (Anthropic CLI) issues skills Skill system, development, and individual skills worktrees Git worktree management

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants