agentleFS
komlukkomluk/scaffolding/skills/worktree-management/SKILL.md

worktree-management

Skill15 starsChanged 2 months agoMITView on GitHub

Scaffolding worktree lifecycle, diagnostics, and recovery. TRIGGER when: debugging worktree isolation, recovering a stuck worktree, or managing task isolation. SKIP: ordinary branch/commit/merge operations (use git-operations).

  • Deletes or force-pushes

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are 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.

  • 3

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If a worktree has no new commits after an agent finishes, the changes are uncommitted — gitops must commit them BEFORE merge.

    line 20
  2. Very specific:

    When Agent Teams is enabled (CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1, see docs/agent-teams.md), multiple writer teammates may run …

    line 24
  3. Very specific:

    Per-teammate isolation: every writer gets its OWN worktree .scaffolding/worktrees/{task_id[:12]} AND its OWN unique SCAFFOLDING_TASK_ID.

    line 28
  4. Very specific:

    No two writers share a working tree or a /tmp/scaffolding-mtime-${TASK_ID}.json staleness store — sharing would trip …

    line 30
  5. Distinctive:

    After a merge or discard finishing action, clean up the worktree:

    line 100

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 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 Management Skill

Purpose

Scaffolding.tool-specific worktree lifecycle management, diagnostics, and recovery procedures.

Live worktrees: !git worktree list 2>/dev/null || true

CRITICAL: Only gitops Touches Git

Other agents (developer, architect) do NOT commit. The worktree flow is:

  1. Agent writes code/tests in worktree
  2. gitops commits, merges to main, pushes, cleans up

If a worktree has no new commits after an agent finishes, the changes are uncommitted — gitops must commit them BEFORE merge.

Parallel Teammates: Isolation + Serialized Merge

When Agent Teams is enabled (CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1, see docs/agent-teams.md), multiple writer teammates may run concurrently. The worktree model makes this safe ONLY if both hold:

  • Per-teammate isolation: every writer gets its OWN worktree .scaffolding/worktrees/{task_id[:12]} AND its OWN unique SCAFFOLDING_TASK_ID. No two writers share a working tree or a /tmp/scaffolding-mtime-${TASK_ID}.json staleness store — sharing would trip file-staleness-check.sh between them and produce unattributable interleaved changes.
  • Serialized merge: gitops remains the SOLE committer and merges worktree branches to main ONE AT A TIME through the merging→merged states below. Parallel work is allowed; parallel committing/merging is not — only one merge is ever in flight.

Worktree Lifecycle

create_worktree() --> agent executes (no commit) --> gitops: commit
    --> gitops: merge_to_main() --> gitops: push --> gitops: cleanup

States (WorktreeStatus enum)

StatusMeaning
readyCreated, waiting for task execution
mergingMerge in progress
mergedSuccessfully merged to main
conflictMerge conflict detected
cleanupBeing removed
removedFully cleaned up

File Locations

ResourcePath
Worktrees directory.scaffolding/worktrees/
Worktree metadata.scaffolding/worktrees/{task_id[:12]}/worktree.json
Branch namingscaffolding/{task_id[:12]}

Diagnostics

List All Worktrees

git worktree list
ls -la .scaffolding/worktrees/

Find Uncommitted Changes

for d in .scaffolding/worktrees/*/; do
  changes=$(cd "$d" && git status --porcelain 2>/dev/null | wc -l)
  if [ "$changes" -gt 0 ]; then
    echo "UNCOMMITTED: $d ($changes files)"
  fi
done

Recovery Procedures

Force Remove Corrupt Worktree

git worktree remove --force .scaffolding/worktrees/{task_id[:12]}
git branch -D scaffolding/{task_id[:12]}
rm -rf .scaffolding/worktrees/{task_id[:12]}
git worktree prune

Recover Uncommitted Work

cd .scaffolding/worktrees/{task_id[:12]}
git add -A && git stash
cd /project/root && git stash pop

Post-Finish Cleanup

After a merge or discard finishing action, clean up the worktree:

git worktree remove .scaffolding/worktrees/{task_id[:12]} 2>/dev/null
rm -rf .scaffolding/worktrees/{task_id[:12]}
git worktree prune

Note: Only needed for merge and discard actions. PR and keep actions leave the worktree intact.

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.