From worktree
**Status: Archived** -- This skill documents an iTerm-specific race condition that is no longer relevant. The worktree plugin no longer manages terminal tabs.
npx claudepluginhub manifoldlogic/claude-code-plugins --plugin worktreeThis skill uses the workspace's default tool permissions.
**Status: Archived** -- This skill documents an iTerm-specific race condition that is no longer relevant. The worktree plugin no longer manages terminal tabs.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Searches prompts.chat for AI prompt templates by keyword or category, retrieves by ID with variable handling, and improves prompts via AI. Use for discovering or enhancing prompts.
Checks Next.js compilation errors using a running Turbopack dev server after code edits. Fixes actionable issues before reporting complete. Replaces `next build`.
Status: Archived -- This skill documents an iTerm-specific race condition that is no longer relevant. The worktree plugin no longer manages terminal tabs.
This skill documents a subtle race condition that occurs when a shell script attempts to close its own iTerm tab after changing directories or removing the working directory. The problem: if iTerm updates the tab title based on the new current directory before iterm-close-tab.sh executes, the pattern matching will fail to find the tab.
The solution is to capture the tab pattern (title) BEFORE any directory changes or worktree removals, store it in a variable, and use that captured pattern when invoking iterm-close-tab.sh at the end of the script.
This pattern was identified during the WTMERGE ticket and should be adopted by other self-closing scripts like cleanup-worktree.sh.
Use this pattern when:
Do not use this pattern for:
Capture tab pattern EARLY in script execution:
# Capture the tab pattern BEFORE changing directory or removing worktree
# This must happen before any cd commands or crewchief worktree merge/remove
TAB_PATTERN="$REPO $WORKTREE_NAME"
debug "Captured tab pattern: $TAB_PATTERN"
The pattern should match the actual tab title format used by your iTerm configuration. In worktree scripts, the convention is "<repo> <worktree>".
Execute directory-changing or worktree-removing operations:
# Change to main worktree directory
cd "$MAIN_WORKTREE_PATH"
# OR: Remove the worktree entirely
crewchief worktree merge "$WORKTREE_NAME" # This removes the feature worktree directory
After this point, the shell's cwd may be invalid or different, and iTerm may update the tab title.
Use the captured pattern for tab close:
# Use the CAPTURED pattern, not a newly computed one
if "$ITERM_CLOSE_TAB_SCRIPT" --force "$TAB_PATTERN"; then
success "Tab closed"
else
warn "Could not close tab '$TAB_PATTERN'. Please close manually."
fi
Make tab close non-fatal:
# Tab close failure should not cause the entire operation to fail
# Use exit code 10 (success with warnings) if tab close fails after critical work succeeds
if [[ ${#WARNINGS[@]} -gt 0 ]]; then
exit 10 # Success with warnings
else
exit 0 # Complete success
fi
The capture location depends on when directory changes occur:
For merge-worktree.sh (cd happens mid-script):
# After argument parsing and validation, BEFORE cd to main worktree
WORKTREE_NAME="feature-x"
REPO="myproject"
# Capture here (before any directory changes)
TAB_PATTERN="$REPO $WORKTREE_NAME"
# ... confirmation prompt ...
# Now safe to change directory
cd "$MAIN_WORKTREE_PATH"
crewchief worktree merge "$WORKTREE_NAME"
# ... later ...
iterm-close-tab.sh --force "$TAB_PATTERN" # Uses captured pattern
For cleanup-worktree.sh (worktree removal happens mid-script):
# After argument parsing and validation, BEFORE worktree removal
TAB_PATTERN="$REPO $WORKTREE_NAME"
# ... confirmation prompt ...
# Worktree removal invalidates cwd
crewchief worktree remove "$WORKTREE_NAME"
# Tab close uses captured pattern
iterm-close-tab.sh --force "$TAB_PATTERN"
Script WITHOUT pattern capture:
cd "$MAIN_WORKTREE_PATH"
crewchief worktree merge "PANE-001"
# Compute pattern AFTER directory change - WRONG!
TAB_PATTERN="$REPO $WORKTREE_NAME"
# At this point, iTerm may have already updated tab title to reflect new cwd
# Pattern match may fail to find the tab
iterm-close-tab.sh --force "$TAB_PATTERN"
# Result: Tab not found, manual cleanup required
Timeline of events:
Script WITH pattern capture:
# Capture pattern BEFORE directory change - CORRECT!
TAB_PATTERN="$REPO $WORKTREE_NAME"
debug "Captured tab pattern: $TAB_PATTERN"
cd "$MAIN_WORKTREE_PATH"
crewchief worktree merge "PANE-001"
# Use captured pattern (not recomputed)
iterm-close-tab.sh --force "$TAB_PATTERN"
# Result: Tab closes successfully
Timeline of events:
Even with mitigation, tab close may fail due to timing or multiple tabs with similar names. The script should treat this as non-fatal:
WARNINGS=()
# ... capture pattern, do critical work ...
if "$ITERM_CLOSE_TAB_SCRIPT" --force "$TAB_PATTERN"; then
success "Tab closed"
else
warn "Could not close tab '$TAB_PATTERN'. Please close manually."
WARNINGS+=("Tab close failed for: $TAB_PATTERN")
fi
# Exit with success-with-warnings if tab close failed
if [[ ${#WARNINGS[@]} -gt 0 ]]; then
echo "Operation completed with warnings. Manual cleanup may be needed."
exit 10
else
exit 0
fi
Output:
[OK] Worktree merged successfully
[WARN] Could not close tab 'myproject PANE-001'. Please close manually.
Operation completed with warnings. Manual cleanup may be needed.
Exit code: 10 (success with warnings)
Even with this mitigation:
These limitations are acceptable because:
/workspace/.devcontainer/scripts/merge-worktree.sh lines 901-903 (capture), 962-975 (tab close)/workspace/_SPECS/claude-code-plugins/tickets/WTMERGE_worktree-merge-skill/planning/architecture.md Component 6 "Tab Close Race Condition"/workspace/.devcontainer/scripts/cleanup-worktree.sh (should adopt this pattern)plugins/iterm/skills/tab-management/scripts/iterm-close-tab.sh (the tab close script being invoked)