worktree-cleanup

worktree-cleanup is a command for Claude Code from christopher-buss/bedrock. It costs 21 tokens per session (2,983 once invoked), scanned D, a copy of worktree-cleanup, MIT.

Clean up merged worktrees by verifying PR/issue status, consolidating settings, and removing stale worktrees.

Command for Claude Code

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/christopher-buss/bedrock/worktree-cleanup
Clone the repo
git clone --depth 1 https://github.com/christopher-buss/bedrock

Made for: Claude Code.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for worktree-cleanup

README.md
[![agentmods](https://agentmods.dev/badge/commands/christopher-buss/bedrock/worktree-cleanup.svg)](https://agentmods.dev/commands/christopher-buss/bedrock/worktree-cleanup)
Your own site
<a href="https://agentmods.dev/commands/christopher-buss/bedrock/worktree-cleanup"><img src="https://agentmods.dev/badge/commands/christopher-buss/bedrock/worktree-cleanup.svg" alt="Measured on agentmods" height="20"></a>
Per session 21 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,983 The whole file, excluding the scripts and references it only reads on demand.
Security scan D 2 findings. Scan, not verified.
Origin 91% copy Near-identical to another mod in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00021 $0.02983
Opus 5 $0.00010 $0.01491
Sonnet 5 $0.00004 $0.00597
Haiku 4.5 $0.00002 $0.00298

Measured 2d ago against content hash 8c0871823155, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade D, and why

worktree-cleanup scanned grade D with 2 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Hidden instructionshighPrompt injection

Directives inside HTML comments, invisible characters or bidirectional overrides are read by the model and not by the person reviewing the file.

<!-- prettier-ignore-start -->

Reads agent configuration directoriesmediumAgent snooping

.claude/, .codex/, .gemini/ hold keys, settings and other credentials a mod has no legitimate need for.

<read_worktree_settings>{worktree_path}/.claude/settings.local.json</read_worktree_settings>
Origin

This is a copy

91% identical to worktree-cleanup — 85 lines differ, which has more behind it and is treated as the original. This page carries a canonical link to it rather than competing with it.

.claude/commands/worktree-cleanup.md · 240 lines

How it starts

The opening of the file, as written. The whole thing — 240 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Worktree Cleanup

Clean up merged worktrees by finding the oldest merged branch, consolidating settings, and removing stale worktrees.

Additional info: $ARGUMENTS

<current_state> Current branch: git branch --show-current Current worktrees: git worktree list </current_state>

<execution_steps> <step_0> Detect git hosting provider <detect_provider> <check_remote_url>git remote get-url origin</check_remote_url> <identify_host> - github.com → Use gh CLI - gitlab.com → Use glab CLI - Other → Ask user which CLI tool to use </identify_host> <store_as>$GIT_HOST_CLI (e.g., "gh", "glab")</store_as> </detect_provider>

Detect git hosting provider and select appropriate CLI tool for PR verification </step_0>

<step_1> Determine default branch and verify we're on it <find_default_branch> git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@' git remote show origin | grep 'HEAD branch' | cut -d: -f2 | tr -d ' ' <store_as>$DEFAULT_BRANCH (typically "main" or "master")</store_as> </find_default_branch> <check_current_branch>git branch --show-current</check_current_branch> Current branch must equal $DEFAULT_BRANCH <error_if_not_default>Exit with error: "This command must be run from the default branch ($DEFAULT_BRANCH)"</error_if_not_default> Ensure we're consolidating to the default branch worktree </step_1>

<step_2> Get list of all worktrees git worktree list --porcelain <parse_output>Extract worktree paths and branch names</parse_output> <exclude_default>Filter out the default branch worktree from cleanup candidates</exclude_default> Identify all worktrees that could potentially be cleaned up </step_2>

<step_3> Find oldest worktree by directory age <get_worktree_ages> <detect_platform>uname -s (returns "Darwin" for macOS, "Linux" for Linux)</detect_platform> <command_macos>git worktree list | grep -v "[$DEFAULT_BRANCH]" | awk '{print $1}' > /tmp/worktrees-$$.txt && while IFS= read -r path; do echo "$(/usr/bin/stat -f '%Sm' -t '%Y-%m-%d %H:%M' "$path" 2>/dev/null)|$path"; done < /tmp/worktrees-$$.txt | sort; rm -f /tmp/worktrees-$$.txt</command_macos> <command_linux>git worktree list | grep -v "[$DEFAULT_BRANCH]" | awk '{print $1}' > /tmp/worktrees-$$.txt && while IFS= read -r path; do echo "$(stat -c '%y' "$path" 2>/dev/null | cut -d. -f1)|$path"; done < /tmp/worktrees-$$.txt | sort; rm -f /tmp/worktrees-$$.txt</command_linux> List all worktrees sorted by directory modification time (oldest first) <critical_notes> - Replace $DEFAULT_BRANCH with value from step_1 (e.g., "main" or "master") - grep "[branch]" matches branch name in brackets, not paths containing the word - Temp file approach avoids subshell parsing issues with piped while-loops - /usr/bin/stat on macOS avoids homebrew stat conflicts </critical_notes> <expected_output_format>YYYY-MM-DD HH:MM|/full/path/to/worktree (oldest first)</expected_output_format> </get_worktree_ages> <filter_recent> <exclude_new>For worktrees created within the last 24 hours, let user know that this worktree might not be worth cleaning</exclude_new> <get_current_time>date +"%Y-%m-%d %H:%M"</get_current_time> </filter_recent> <select_oldest> <extract_branch_name>Parse branch name from oldest worktree path</extract_branch_name> <important_note>DO NOT use "git branch --merged" to check merge status - it's unreliable</important_note> <proceed_to_pr_check>Move directly to step 4 to verify PR merge status instead</proceed_to_pr_check> </select_oldest> Identify oldest worktree candidate - actual merge verification happens via PR/MR in next step </step_3>

Read the full file on GitHub · 240 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 240 lines · 21 tokens per session scan D 8c0871823155

Subscribe to this mod's changes

worktree-cleanup is a command published in the GitHub repository christopher-buss/bedrock (14 stars, last pushed today), licensed MIT. It adds 21 tokens to every session and 2,983 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it D with 2 findings (hidden instructions, reads agent configuration directories). It is 91% identical to worktree-cleanup, differing in 85 lines, and is treated as a copy.