agentleFS
karanmrnkaranmrn/kstack/skills/audit-agent-worktrees/SKILL.md

audit-agent-worktrees

Skill11 starsChanged 24 days agoMITView on GitHub

Audit old git worktrees and clones that coding agents left behind, and delete only the ones whose work is safe. Use before you remove agent worktrees, prune worktree pools, or reclaim disk space from old agent checkouts.

  • Commits and pushes

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Run these checks inside the worktree.

    line 29
  2. Very specific:

    Remove each safe worktree with its tool: git -C <repo> worktree remove <path>.

    line 51
  3. Very specific:

    Run git -C <repo> worktree prune to clear stale records.

    line 52

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

Audit agent worktrees

Agents leave many worktrees behind. Some hold the only copy of real work. This skill sorts each worktree into safe to delete, keep or ask, and deletes only the safe ones.

Unlanded work is work that is not on the default branch and not on a pushed branch. Never delete unlanded work without a human's explicit approval for that worktree.

1. Make the inventory

For each repository, list every worktree:

git -C <repo> fetch --all --prune
git -C <repo> worktree list --porcelain

Also find loose clones and copies outside git worktree list, for example under the agent tool's worktree directory. Record for each one: path, branch, HEAD commit, last change time, size (du -sh).

Skip any worktree that a running agent uses. Check for live processes with lsof +D <path> or the agent tool's own status command. A slot that looks unused is not proof that it is free.

Done when every worktree has one inventory row.

2. Classify each worktree

Run these checks inside the worktree. Each check gives evidence. Write the evidence into the row.

CheckCommandUnlanded work if
Uncommitted changesgit status --porcelain --untracked-files=alloutput is not empty (ignore build output that .gitignore covers)
Stashesgit stash lista stash entry belongs to this branch
Commits not on the default branchgit log --oneline origin/<default>..HEADthe list is not empty
Commits not pushedgit log --oneline @{upstream}..HEADthe list is not empty, or the branch has no upstream
Squash-mergedgh pr list --state merged --head <branch>no merged PR includes these commits
Open PRgh pr list --state open --head <branch>an open PR exists: keep the worktree

A squash merge or rebase merge changes commit hashes. So origin/<default>..HEAD can list commits that are already landed. If a merged PR exists for the branch, compare its final diff with the branch tip (git diff <merge-commit>^..<merge-commit> against git diff origin/<default>...HEAD) before you call the work landed.

Sort each row:

  • Safe to delete: clean tree, no own stash, and every commit is on the default branch, or on a pushed branch, or in a merged PR.
  • Keep: an open PR, a live process, or recent activity.
  • Ask: any unlanded work. Show the human the branch, the commits and the diff summary.

3. Back up, then delete

  1. Push any branch whose commits exist nowhere else: git push origin <branch>. If you cannot push, make a bundle: git bundle create <backup-dir>/<branch>.bundle origin/<default>..<branch>.
  2. Remove each safe worktree with its tool: git -C <repo> worktree remove <path>. Use the agent tool's own remove command when it manages a pool. Never use --force on a worktree that has unlanded work.
  3. Run git -C <repo> worktree prune to clear stale records.
  4. Record what you removed, the space it freed and every backup path.

Done when every row is deleted with evidence, kept with a reason, or waiting for a human answer.

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.