agentleFS
marcusgollmarcusgoll/Spec-Flow/.claude/skills/worktree-context/SKILL.md

worktree-context

Skill94 starsChanged 7 months agoMITView on GitHub

Provides centralized worktree handling for workflow commands, eliminating ~50 lines of inline code per command. Git worktrees enable parallel development by allowing multiple branches to be checked out simultaneously. This skill provides a single source of truth for worktree detection, creation, and context generation. Returns JSON with worktree details: Replace 50+ lines of inline worktree detection with this compact reference: When spawning worker agents that need to operate in a worktree: Before (50+ lines inline):

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When spawning worker agents that need to operate in a worktree:

    line 74
  2. Distinctive:

    When operating in worktrees, paths can get duplicated:

    line 166
  3. Very specific:

    Use relative-path to fix:

    line 170

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Worktree Context Skill

Provides centralized worktree handling for workflow commands, eliminating ~50 lines of inline code per command.

Purpose

Git worktrees enable parallel development by allowing multiple branches to be checked out simultaneously. This skill provides a single source of truth for worktree detection, creation, and context generation.

Quick Reference

# Check if in worktree
bash .spec-flow/scripts/bash/worktree-context.sh in-worktree && echo "yes" || echo "no"

# Get worktree info (JSON)
bash .spec-flow/scripts/bash/worktree-context.sh info

# Create worktree for feature
bash .spec-flow/scripts/bash/worktree-context.sh create "feature" "001-auth" "feature/001-auth"

# Generate Task() agent context block
bash .spec-flow/scripts/bash/worktree-context.sh context "/path/to/worktree"

Output Format

Info Command

Returns JSON with worktree details:

{
  "is_worktree": true,
  "worktree_path": "/path/to/worktrees/feature/001-auth",
  "root_path": "/path/to/main/repo",
  "branch": "feature/001-auth",
  "worktree_type": "feature",
  "worktree_slug": "001-auth"
}

Fields

FieldValuesDescription
is_worktreebooleanWhether currently in a worktree
worktree_pathstringAbsolute path to worktree
root_pathstringAbsolute path to main repository
branchstringCurrent git branch
worktree_typefeature, epicType of worktree
worktree_slugstringFeature/epic slug

Compact Pattern (v11.2)

In Command Process Section

Replace 50+ lines of inline worktree detection with this compact reference:

### Step N: Worktree Context (Optional)

**Check worktree using centralized skill** (see `.claude/skills/worktree-context/SKILL.md`):

1. Check preference: `bash .spec-flow/scripts/utils/load-preferences.sh --key "worktrees.auto_create" --default "true"`
2. Check if already in worktree: `bash .spec-flow/scripts/bash/worktree-context.sh in-worktree`
3. If NOT in worktree AND auto_create is true:
   - Create: `bash .spec-flow/scripts/bash/worktree-context.sh create "feature" "$SLUG" "$BRANCH"`
   - Store path in state.yaml: `yq eval ".git.worktree_path = \"$PATH\"" -i state.yaml`
4. For Task() agents, generate context: `bash .spec-flow/scripts/bash/worktree-context.sh context "$PATH"`

For Task() Agent Prompts

When spawning worker agents that need to operate in a worktree:

## Worktree Context

${WORKTREE_PATH ? `
**WORKTREE MODE (CRITICAL)**

Path: ${WORKTREE_PATH}

Execute this FIRST:
\`\`\`bash
cd "${WORKTREE_PATH}"
\`\`\`

All paths are relative to worktree root.
Git commits stay local to worktree branch.
Do NOT merge or push - orchestrator handles that.
` : `
**NORMAL MODE**
Working in main repository.
`}

Commands

CommandPurpose
in-worktreeCheck if in worktree (exit 0 = yes)
infoGet current worktree info (JSON)
rootGet main repository path
create TYPE SLUG BRANCHCreate new worktree
path SLUGGet worktree path for slug
context PATHGenerate Task() agent context block
run PATH CMDRun command in worktree
git PATH ARGSRun git command in worktree
merge SLUGMerge worktree branch to main
sync PATHSync state files from worktree
cleanup [--dry-run]Remove merged worktrees
relative-path PATHExtract worktree-relative path
extract-slug PATHExtract slug from any path format

Usage in Commands

/feature Command

Before (50+ lines inline):

### Step 1.1.5: Create Worktree (If Preference Enabled)

Check worktree context and preference:

\`\`\`bash
# Check if already in a worktree
IS_WORKTREE=$(bash .spec-flow/scripts/bash/worktree-context.sh in-worktree && echo "true" || echo "false")

# Check worktree auto-create preference
WORKTREE_AUTO=$(bash .spec-flow/scripts/utils/load-preferences.sh --key "worktrees.auto_create" --default "true" 2>/dev/null)
...
# 40 more lines of worktree handling
\`\`\`

After (compact pattern):

### Step 1.1.5: Create Worktree (If Preference Enabled)

**Handle worktree using centralized skill** (see `.claude/skills/worktree-context/SKILL.md`):

1. Check if worktree auto-create is enabled (preference: `worktrees.auto_create`)
2. If NOT in worktree AND enabled: Create via `worktree-context.sh create`
3. Store `worktree_path` in state.yaml for Task() agents
4. Generate context block for worker prompts

/implement Command (Worker Agents)

Include worktree context in worker prompts:

Task(worker):
  prompt: |
    ${WORKTREE_ENABLED ? generate_worktree_context(WORKTREE_PATH) : ""}

    Implement ONE feature from domain memory.
    ...

Path Handling

The Duplication Bug

When operating in worktrees, paths can get duplicated:

  • Bad: worktrees/epic/004-app/epics/004-app/epics/004-app
  • Good: epics/004-app

Use relative-path to fix:

bash .spec-flow/scripts/bash/worktree-context.sh relative-path "worktrees/epic/004-app/epics/004-app"
# Returns: epics/004-app

Extracting Slugs

Extract slug from any path format:

bash .spec-flow/scripts/bash/worktree-context.sh extract-slug "specs/001-auth/domain-memory.yaml"
# Returns: 001-auth

Related Files

  • .spec-flow/scripts/bash/worktree-context.sh - Main implementation (540 lines)
  • .spec-flow/scripts/bash/worktree-manager.sh - Worktree lifecycle management
  • docs/references/git-worktrees.md - Deep documentation

Benefits

BeforeAfter
50+ lines inline per command5-10 lines referencing skill
Duplicated across 3+ commandsSingle source of truth
Changes require multiple editsChanges require 1 skill edit
Worktree bugs scatteredWorktree bugs centralized

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.