Clean closed issue worktrees

From agentskillexchange/skills · View interactive version

Clean closed issue worktrees is an AI agent skill from the GitHub repository agentskillexchange/skills. Safely audits and removes Git worktrees linked to closed GitHub or GitLab issues with a mandatory scan-confirm-execute protocol and deterministic local validation.

How to use this skill

Skill file

Clean Closed Issue Worktrees

Clean completed worktrees through a mandatory scan-confirm-execute protocol. Match the language of all user-facing questions, reports, warnings, and results to the user's current language. Preserve commands, paths, branch names, and provider field names verbatim.

Resolve relative resource paths in this file from the skill directory. Before invoking the bundled script, resolve scripts/worktree_cleanup.py to an absolute path so the command does not depend on the target repository's working directory.

Safety contract

Phase 1: scan and propose

  1. Identify the repository named by the supplied GitHub/GitLab URL and match it to an exact local remote. Do not assume the remote is origin or the default branch is main/master. If matching is ambiguous, ask the user.
  2. Before browser use, look for a purpose-built provider skill, connector, or MCP. Then try an already authenticated gh/glab, then the official read-only API for public repositories. Use a browser MCP or built-in browser only as the last fallback. If all routes fail, ask the user for access or a closed-issue export. Read provider-access.md when selecting or using a provider route.
  3. Extract candidate issue numbers from local branch names and closing commit messages, then verify each exact item against the provider. Do not treat the first page or first 100 results as exhaustive. Respect explicit filters in the supplied list URL.
  4. Query harness task/session state when tools expose it. Read harness-detection.md for Codex, Claude Code, and unknown harness handling.
  5. Run the local inventory script from a directory outside every removal candidate:

```bash

python3 <skill-root>/scripts/worktree_cleanup.py scan \

--repo /absolute/path/inside/repository \

--baseline <matched-remote>/<default-branch> \

--json-out "$TEMP_DIR/scan.json" \

--stdout none

```

  1. Classify every registered worktree:
  1. Report exact paths, issue/PR/MR links and states, branch/detached state, dirty status, harness status, commit retention, ignored-path risks, per-worktree size, and the total estimated reclaimable space. Call directory-size totals estimated reclaimable space, not exact filesystem savings.
  2. Ask one decision at a time when material choices are missing, provide a recommended answer, and look up discoverable facts instead of asking. For the final confirmation, identify the exact batch and state the default recommendation to retain branches.

Mapping confidence

Strong evidence is one of:

Directory numbers, title similarity, ordinary Ref #1459, multiple matches, or a mismatched repository are not strong evidence.

Ignored local content

git status can be clean while ignored files would still be deleted. The script reports ignored top-level paths without reading their contents.

Phase 2: confirm and execute

Do not enter this phase until the user has seen Phase 1 results and explicitly selected exact targets and branch behavior.

  1. Read evidence-schema.md. Create the normalized selection and plan only in a system temporary directory. Do not add them to the target repository.
  2. If a selected detached HEAD has no retaining ref, offer a backup branch first. Creating it is a separate write and must be included in the user's explicit approval. Use worktree-cleanup/backup-YYYYMMDD-<short-sha> and never overwrite an existing ref.
  3. Create the immutable plan. The script refuses locally unsafe selections:

```bash

python3 <skill-root>/scripts/worktree_cleanup.py create-plan \

--repo /absolute/path/inside/repository \

--selection "$TEMP_DIR/selection.json" \

--output "$TEMP_DIR/plan.json"

```

  1. Immediately before execution, re-query every exact issue/PR/MR and harness task state. Abort if an issue reopened, a PR/MR is no longer authoritative, or a task became active.
  2. Execute only with the exact plan_id shown in the confirmation. The script rechecks the whole batch before the first mutation and aborts if HEAD, branch, dirty state, ignored paths, retaining refs, baseline, lock state, registration, path resolution, or repository identity changed:

```bash

python3 <skill-root>/scripts/worktree_cleanup.py execute \

--plan "$TEMP_DIR/plan.json" \

--confirm-plan <exact-plan-id> \

--delete-plan-on-success

```

  1. When branch deletion was explicitly selected, require a baseline and permit only git branch -d. Squash/rebase branches with unique commits remain preserved unless the user separately approves a backup workflow.
  2. Verify that removed directories and Git registrations are gone, retained branches/backups exist, protected worktrees are unchanged, and report the estimated space reclaimed plus any failures. Delete temporary artifacts after success; export Markdown/JSON only when the user requests a saved audit record.

Prunable metadata

Never treat prunable as permission. Report it separately. The bundled script intentionally refuses prunable metadata; use a separate exact, user-confirmed recovery or prune workflow after verifying the closed issue and missing directory.

Publication and portability

The bundled script requires Python 3.9+ and Git. Provider and harness access remains outside the script so the same skill can run in Codex, Claude Code, and other agent environments without reading credential stores or browser cookies.

Installation

No source-backed install or usage instructions could be extracted automatically. Review the upstream project before running this skill in a sensitive workflow.

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.