/mac-agent-hygiene:mac-agent-hygienefrom MSApps Mac Agent Hygiene
Frees disk space on Macs running Claude Code agents by removing stale git worktrees, Xcode DerivedData, Playwright browser caches, and leftover plugin clones. Provides a plan-confirm-apply workflow via MCP tools and a daily LaunchAgent.
Ships in MSApps Mac Agent Hygiene. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
A Mac that runs Claude Code agents all day fills its disk in places no cleanup app looks: linked git worktrees that nobody removed, leftover plugin-install clones, browser-profile caches, Xcode build output. On one 926 GB machine these were 200+ GB while Downloads and Documents together held 2 GB. This skill, the bundled MCP server and the daily LaunchAgent exist so that never happens again on any Mac that installs the plugin.
Run the four MCP tools in this order. Never skip the plan.
hygiene_status — read the snapshot: disk free, DerivedData size, leftover plugin clones,
Playwright profiles (how many, how many locked / in use), worktrees total and stale, whether Xcode
is building, whether the LaunchAgent is loaded, and the last log lines.hygiene_plan — dry run. Every candidate comes back with remove or keep, its size, and a
reason. Read the keep reasons as carefully as the remove ones: dirty-uncommitted-work,
unpushed-commits, a-process-is-inside, profile-in-use, xcode-building are the routine
working. The plan returns a plan_id valid for 30 minutes.hygiene_apply with that plan_id and confirm: true — each item is re-checked at apply
time by the same function that planned it; anything that changed since the plan is refused, not
removed. Report removed / refused / MB freed from the result.hygiene_schedule action: "install" once per Mac — installs the LaunchAgent
com.msapps.mac-agent-hygiene (default 04:30 daily); it copies hygiene.sh to
~/.local/share/mac-agent-hygiene/ so the job survives plugin updates — re-run install after an
update. action: "status" later. The scheduled run
is exactly plan + apply with no human in the loop, which is why the eligibility rules below
are conservative by construction.Without the MCP server (plain shell): bash scripts/hygiene.sh status | plan | run and
bash scripts/install-launchd.sh install | status | uninstall.
| Category | Removed | Kept |
|---|---|---|
derived-data | ~/Library/Developer/Xcode/DerivedData/* | everything, while xcodebuild, Xcode or XCBBuildService is running |
plugin-clones | ~/.claude/plugins/cache/temp_git_* older than 24 h | younger clones (an install may still be using them) |
browser-caches | inside each ~/.cache/playwright-* profile, ONLY: Default/Cache, Default/Code Cache, Default/GPUCache, Default/Service Worker/CacheStorage + ScriptCache, Dawn / Graphite / shader caches, BrowserMetrics* | Cookies, Local Storage, IndexedDB, Login Data, Local State, Preferences, Session Storage, Network — the profile IS a login; any profile with a SingletonLock or open files is skipped whole |
model-caches | the directories in HYG_MODEL_CACHES (default ~/.cache/whisper, ~/.cache/codex-runtimes), idle > HYG_MODEL_CACHE_AGE_DAYS (7) | when a process has them open or they were used this week |
worktrees | a linked worktree under HYG_WT_ROOT (default ~/code) — a direct child matching HYG_WT_GLOB (default *wt-*), or up to HYG_WT_DEPTH (3) levels below a top-level dir in HYG_WT_NESTED (default wt _worktrees, i.e. ~/code/wt/{sup,ref,pipe}-*/... and ~/code/_worktrees/...) — that is untouched > HYG_WT_AGE_DAYS (7), is nobody's cwd, has an empty git status --porcelain (or only untracked agent scaffolding, PROMPT.md / docs/qa/<card>/… for the card id its branch card/<id>-… or folder name declares, archived first and restored if the remove fails: reason *-scaffold-only), is not locked (git worktree lock),, and whose HEAD is on a remote-tracking ref, equals the head of a merged PR (gh pr list --state merged), or sits on the branch of a merged PR whose commits (gh pr view --json commits) match every local-only commit subject (rebased / force-pushed heads) | dirty (dirty-uncommitted-work: a tracked or staged change, or an untracked file outside the scaffold shapes), locked, unpushed, unmerged, in-use, recently touched, or a full clone (.git is a directory, not a file) |
The decision lives in one shell function per category, used by plan and by apply. A gh failure
makes the merged-PR branch of the worktree check return empty, so the worktree is kept — the rail
fails toward keeping. Paths outside $HOME are refused at apply time regardless of category.
Things this skill never does: sudo; anything under /System, /Library, ~/Library/Application Support, keychains, Mail, ~/.ssh; deleting a SingletonLock; killing a process; removing a
worktree with --force.
Optional file ~/.config/mac-agent-hygiene/config.env, sourced by the script:
HYG_WT_ROOT="$HOME/code" # where agent worktrees live
HYG_WT_GLOB="*wt-*" # how they are named
HYG_WT_NESTED="wt _worktrees" # top-level dirs whose nested worktrees are agent-owned ("" = off)
HYG_WT_DEPTH=3 # how deep under HYG_WT_ROOT to look
HYG_WT_AGE_DAYS=7
HYG_MODEL_CACHES="$HOME/.cache/whisper $HOME/.cache/codex-runtimes"
HYG_MODEL_CACHE_AGE_DAYS=7 # model caches go when not MODIFIED this long (a read does not count)
HYG_WT_MERGED_PR_CHECK=1 # 0 = pushed-only (no gh call)
HYG_SCAFFOLD_ARCHIVE="$HOME/.local/state/mac-agent-hygiene/scaffold-archive" # agent scaffolding is tarred here first
Environment variables win over the config file (an MCP worktree_age_days override applies to that
run only). "Untouched" means no regular file inside the worktree (outside .git) is newer than the
age. "Clean" is git status --porcelain empty, so gitignored files go with the worktree.
The browser-cache allowlist is fixed in code on purpose. The LaunchAgent carries an explicit PATH
(/usr/bin:/bin:/usr/sbin:/sbin plus the Homebrew bin that holds gh) because launchd does not
inherit the shell's; every system binary in the scripts is called by absolute path for the same
reason. Temp files use mktemp with a ${TMPDIR:-/tmp} template.
keep is a measurement, not a failure. Report the top keep reasons so the user knows what is
holding space on purpose (for example 80 dirty or unpushed worktrees).~/.claude/projects are pruned by Claude Code itself via
cleanupPeriodDays in ~/.claude/settings.json (default 30). hygiene_status reports the current
value; recommend 14 when the directory is large. Never edit that file from this skill.~/Library/Logs/mac-agent-hygiene.log for the daily apply done line.df -h / is printed in every apply summary.references/safety-model.md — why each rule exists, with the measurements behind it (the
playwright-profile login trap, the launchd PATH trap, the mktemp/TMPDIR trap, the
worktree .git-is-a-file test, and the SingletonLock age rule).scripts/hygiene.sh — status | plan | apply <planfile> | runscripts/install-launchd.sh — install [--hour H --minute M] | status | uninstallscripts/launchagent.plist.template — rendered by the installerserver/index.js — the MCP server (zero dependencies, stdio)Related plugin in the same marketplace: mac-disk-cleaner (interactive disk exploration and
one-off cache cleaning for any Mac). This plugin is the scheduled, agent-aware routine.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
disk-cleanup, mac-hygiene, worktree-cleanup, launchagent, mcp-tools
npx claudepluginhub msapps-mobile/claude-plugins --plugin mac-agent-hygieneOther plugins' skills for the same job