Worktree lifecycle

From event4u-app/agent-config · View interactive version

Worktree lifecycle is an AI agent skill from the GitHub repository event4u-app/agent-config. Use when governing a worktree across its whole life — scope-lock declaration, merge-readiness status, scoped verification, and safe cleanup that refuses while unique unmerged commits exist.

How to use this skill

Skill file

worktree-lifecycle

Governance layer for worktrees that already exist (or are about to).

Creation mechanics — directory convention, ignore-safety, clean

baseline — live in CODE0

and are referenced, never restated here. This skill owns everything

after the spawn: what the worktree is allowed to touch, when it is

merge-ready, and when it may be removed.

When to use

ahead/behind, verification evidence

Do NOT use when:

(Iron Law: no worktree without verified ignore + clean baseline)

CODE0

selects the mode; this skill governs the worktrees a chosen mode uses

The Iron Law

EVERY GOVERNED WORKTREE DECLARES ITS SCOPE. NO REMOVAL WHILE
UNIQUE UNMERGED COMMITS EXIST. INHERITED COMMITS ARE NEVER DROPPED.

Procedure

1. Decide isolation

Isolate when any holds; otherwise stay on the current branch

(per the Do-NOT list in using-git-worktrees):

Then create via CODE0

(or the host primitive — § Host-native mapping below), declare the

scope lock (§ 2), keep status honest (§ 3), and gate removal (§ 4).

2. Scope lock

At worktree start, write a .worktree-scope.md note at the worktree

root and keep it untracked via .git/info/exclude (shared exclude —

one line .worktree-scope.md, no commit needed):

## Scope lock

The lock is the disjointness contract between parallel worktrees. A

diff that leaves the owned paths is scope creep — stop and surface,

per scope-control. Check mechanically:

git diff --name-only "$(git merge-base HEAD <base>)"..HEAD
# every path must match an `owns:` entry

3. Status / merge-ready checklist

A worktree is merge-ready only when ALL hold:

  1. Clean tree — git status --porcelain is empty.
  2. Scope lock respected — changed files ⊆ owns: paths (command above).
  3. Verification evidence attached — the scoped probe for the

declared change ran fresh and passed (per verify-before-complete);

record command + result tail in the status report. No fresh

evidence → not merge-ready, regardless of how the diff looks.

  1. Ahead/behind known — git fetch origin --quiet then

git rev-list --left-right --count HEAD...origin/<base>. Behind →

flag; divergent (both sides non-zero on a pushed branch) → route to

CODE0.

  1. No inherited-commit drops — commits on the branch that this

session did not author stay on the branch; never rebase-out,

reset-away, or exclude them (rule git-history-discipline,

shared-branch Iron Law).

4. Cleanup discipline

Removal is gated by the deterministic helper (tested on detached HEAD,

no remote, tag-only reachability, deleted remote branch, untracked

files, paths with spaces):

agent-config worktree:cleanup check <worktree-path>

Exit 0 → removal allowed; exit 1 → refuse, gates in order:

  1. Detached HEAD — no branch to judge reachability for; resolve

manually first.

  1. Unsaved work — git status --porcelain non-empty, untracked

files included (never answer with --force).

  1. Unique-commit check — commits reachable from the worktree branch

but from no other ref (branches, remotes, AND tags — a tag counts

as reachability). Non-empty → refuse removal; the branch holds

work that exists nowhere else. Surface the commit list and hand

back — merging or preserving them is the user's call

(git-history-discipline).

Then: remove, never delete — git worktree remove <path>, then

git worktree prune. Branch deletion is a separate, permission-gated

step (scope-control); never force-delete (-D) as part of cleanup.

Cross-worktree scope-lock overlaps are scanned via

worktree_cleanup_check scope-overlap (surfaced by /worktree status).

Whole-checkout sweeps. Under worktrees.cleanup: auto the proven set leaves

at /pr:merge and daily (ADR-295). git worktree prune only clears registrations whose

directory is gone. Classify the whole set in one pass:

agent-config worktree:cleanup inventory [repo] [--json|--plan]
agent-config worktree:cleanup apply [repo]   # removes the safe set only

apply runs only on the user's ask (/worktree cleanup --apply) or under

the user-set worktrees.cleanup: auto — never on the agent's initiative.

safe requires all of: on a branch, merged into the trunk, clean, inside a

conventional worktree root (.claude/worktrees/ or .worktrees/), and no git

activity for 48 h. review keeps its disqualifying reason so the next sweep

starts from a shorter list; live means another session may hold it. A

worktree outside the conventional roots is never safe — sitting beside the

repo it can be mistaken for a sibling package, so its removal stays a

judgement call. It is also the placement that loses the persistent shell

cwd on a host with a working-directory boundary — see

CODE0 § 2.

The mode reports only. --plan prints git worktree remove plus

git branch -d (never -D) for the safe set; **running it is a bulk deletion

needing the user's explicit this-turn approval**

(non-destructive-by-default), which a single earlier approval never covers.

Host-native mapping

| Host capability | Use |

|---|---|

| Claude Code EnterWorktree / ExitWorktree | Enter/leave a governed worktree in-session; scope lock is written right after enter |

| Claude Code subagent isolation: "worktree" | Dispatch a slice into its own auto-managed worktree; unchanged worktrees are auto-cleaned by the host — the unique-commit gate still applies to any it leaves behind |

| No worktree primitive (other hosts) | Degrade to plain git worktree add per CODE1 § Procedure — same scope lock, same gates |

The mapping changes only who creates the directory. Scope lock,

merge-ready checklist, and cleanup gates are host-independent.

Gotcha

the branch itself, so the naive check never fires. The

--exclude="refs/heads/<branch>" --all variant is also unreliable

when combined with --not (observed on git 2.39). The cleanup

helper above excludes the branch explicitly.

in .git/info/exclude; .gitignore would be a tracked change

outside the scope lock.

only covers worktrees with no changes; committed but unmerged work

still needs the unique-commit gate.

rev-list --left-right count; a cached view reports merge-ready on

a diverged branch.

Output format

  1. Worktree — path + branch, and the scope-lock owns: list
  2. State — clean/dirty, ahead/behind vs base, merge-ready verdict

with the failing checklist item named when not ready

  1. Evidence — verification command(s) run + result tail, or

"none attached" stated explicitly

  1. Next step — merge path, missing verification, or cleanup verdict

(allowed / refused with the unique-commit list)

Do NOT

git-history-discipline)

Handover

| Task | Skill / command |

|---|---|

| Creating the worktree (ignore-safety, baseline) | CODE0 |

| Divergence recovery, safe squash, PR flow | CODE0 |

| Finishing the branch | finishing-a-development-branch |

| Day-to-day operations | /worktree create · status · verify · cleanup |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.