Closeout

From dannys-code-corner/incan · View interactive version

Closeout is an AI agent skill from the GitHub repository dannys-code-corner/incan. Safely close out completed Incan work after a PR is merged by verifying merge status, syncing the base branch, removing task-owned local worktrees/assets, deleting merged local/remote branches, pruning refs, and reporting dirty or ambiguous leftovers. Use when the user says /closeout, asks to clean

How to use this skill

Skill file

Closeout — Incan Project

Purpose

/closeout cleans up local implementation state after a PR has merged. It is for removing task-owned worktrees, local branches, remote topic branches, stale refs, and temporary agent artifacts without touching unrelated user work.

This skill is intentionally conservative. A clean closeout is less important than not deleting active work.

Inputs

The user may provide:

If no target is provided, infer it from the current branch, current worktree path, recent PR context, or task-owned temp directory names. If inference is ambiguous, ask the user which PR or branch to close out.

Hard rules

Workflow

Step 1: Orient

Collect the current repository state:

git rev-parse --show-toplevel
git status --short --branch
git branch --show-current
git remote -v
git worktree list --porcelain

If the task references a PR, fetch its metadata through the GitHub connector when available. If using gh, be prepared for invalid local auth and fall back to GitHub connector data or plain git merge checks.

Record:

Step 2: Verify the PR is merged

Use the strongest available source:

  1. GitHub PR metadata: PR state is merged, with base branch and head branch recorded.
  2. Git ancestry after fetching: the target branch tip is an ancestor of the base branch.
  3. Local merged-branch status: git branch --merged <base> shows the target branch.

Recommended git fallback:

git fetch origin <base-branch> <target-branch>
git merge-base --is-ancestor <target-branch> origin/<base-branch>

GitHub merged status is authoritative for PR closeout when the target branch exactly matches the PR head branch. If GitHub reports the PR as merged but merge-base --is-ancestor fails, treat the branch as squash-merged or rebase-merged: record that ancestry did not prove the merge, then continue cleanup for the exact PR head branch only.

If the PR is open, closed-unmerged, unknown, or there is no reliable merged PR metadata and the branch is not an ancestor of the base branch, stop after reporting status. Do not clean up.

Step 3: Sync the base branch

Once merged, update the base branch in the main worktree if it is clean:

git fetch origin --prune
git switch <base-branch>
git pull --ff-only

If the base worktree is dirty, skip the switch/pull and report the dirty files. Cleanup of separate task worktrees can still proceed if those task worktrees are clean and merge status is confirmed.

Step 4: Build the cleanup inventory

Identify only task-owned candidates:

Use dry-run commands for asset cleanup:

git clean -nd
git status --short --ignored

Do not treat every untracked file as disposable. RFC drafts, docs changes, local notes, snapshots, and generated assets in the main worktree may be intentional user work.

Step 5: Remove clean task worktrees

For each candidate worktree:

  1. Check status inside that worktree.
  2. Confirm it is tied to the merged target branch.
  3. Ensure no other branch is actively checked out there.
  4. Remove it with git:
git -C <main-repo> worktree remove <worktree-path>

If removal fails because the worktree is dirty, do not force-remove it. Report the path and dirty files.

Exception: for task-owned worker worktrees created as scratch slices by ralph-loop or orchestrate-parallel-work, dirty status is expected after the merged orchestrator branch has integrated the work. If the user has confirmed closeout or says those workers should go, remove them with:

git -C <main-repo> worktree remove --force <worker-worktree-path>

Record the removed worker path and branch in the closeout report.

Step 6: Delete merged branches

Delete the local branch only after no worktree is using it:

git branch -d <target-branch>

Delete the remote topic branch only when it is the PR head branch and the PR is merged:

git push origin --delete <target-branch>

If git branch -d reports unmerged commits, check whether the branch is the exact head branch of a PR that GitHub reports as merged. If yes, treat it as squash/rebase-merged and delete the local branch with:

git branch -D <target-branch>

Do not use this exception for branches inferred only from names, branches without reliable merged PR metadata, branches that do not match the PR head, or branches still checked out in any worktree. In those cases, stop and report the exact git output unless the user explicitly confirms the commits are disposable.

For confirmed disposable worker branches whose worktrees were force-removed as scratch assets, delete with:

git branch -D <worker-branch>

Use this only for branches that match the closed task and were created as local worker slices, not for arbitrary feature branches.

Step 7: Clean task-local artifacts

Prefer worktree removal to manual file deletion. If artifacts remain outside the removed worktree, delete only files that are all of:

For ambiguous .agents/state/*, temp directories, or generated docs assets, ask before deletion unless they are inside the removed task worktree.

Step 8: Verify cleanup

Run:

git worktree list
git branch --list <target-branch>
git branch -r --list origin/<target-branch>
git status --short --branch

If the base worktree was updated, also verify it is on the expected branch and clean except for pre-existing unrelated user changes.

Output format

Report the closeout as:

## Closeout — <PR / branch / task>

### Removed
- <worktree / branch / remote ref / artifact>

### Kept
- <dirty or ambiguous item intentionally preserved>

### Verification
- `<command>` — <result>

### Blocked
- <only if something could not be safely cleaned>

If nothing was removed because the PR is not merged yet, say that directly and include the merge status source.

Common stop conditions

Stop and ask or report status when:

Escalation

If sandbox permissions block cleanup, request approval for the exact command and target. Do not request broad approval for destructive command families. Never suggest a persistent prefix rule for rm, forced deletion, or arbitrary shell execution.

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.