The symptom
A 228 GB MacBook, running Codex and Claude Code side by side for development. One evening it had 5.8 GB free. Two hours later, 1.4 GB. Both tools were mid-task, and neither could be stopped.
The reflex is to start deleting. But on this machine both Codex and Claude Code had a history of deleting the wrong things, for the same reason each time: treating "temporary files" as "safe to delete". So the ground rules came first:
- Never touch any AI agent's own environment. `~/.codex`, `~/.claude` and the desktop app's data are measured, never modified.
- No wrong deletions. Remove only what one command can rebuild, and prove it is rebuildable first.
- Every step on record, recoverable. What was removed, why, and how to bring it back, all under version control.
Measure, don't guess
```
~/Developer 92 GB
~/Library/Application Support/Claude 12 GB untouched
~/.codex 7.5 GB untouched
~/.claude 2.5 GB untouched
old pnpm stores 2.4 GB
caches (huggingface, uv, npm) 5.8 GB
```
Of the 92 GB in `~/Developer`, rebuildable artifacts (`node_modules`, `.next`, `dist`, `.venv`) accounted for 60.9 GB.
One level down, a single repo had 53 git worktrees. Where they came from was clear:
Created by | Count |
|---|---|
Codex tasks (`~/Developer/.worktrees/`) | 31 |
Claude Code tasks (`firstsite-launch-*`) | 11 |
The Claude desktop app | 5 |
Deploy snapshots and scratch | 6 |
The repo itself is 108 MB and 1,191 files. The bloat isn't code. It is every worktree running its own `pnpm install` (962 MB) and its own `pnpm build` (0.5 to 1.6 GB). Of 111 local branches, only 2 had been merged into main. Neither tool removes a worktree when its task ends. This isn't a bug in the code. The workflow is missing a "clean up" step.
We are not alone in this. In openai/codex issues, one user reports `~/.codex/sessions` growing to 755 GiB (#34061), another that worktrees are never reclaimed and there is no prune command (#54891). The author of that issue makes the point well: `rm -rf ~/.codex/worktrees` is the natural reaction to seeing 3.8 GB, and it destroys uncommitted work.
The tool's rules
We wrote a Python script called devsweep. The rules matter more than the code:
Scan is the default. `scan` measures, classifies and writes a manifest. It changes no files.
`apply` acts only on what `scan` classified as rebuildable. It writes the manifest first, then acts. Without `--yes` it refuses.
Rebuildable requires evidence. `node_modules` needs a lockfile next to it, `.venv` a lock file or requirements, `target` a `Cargo.lock`. Each item records its rebuild command.
Anything in use is skipped. A project with a running process inside it (checked via `lsof` working directories), or with file changes in the last 48 hours, is left alone.
Agent environments are never a target. Hard-coded, reported by size only.
Worktrees are listed, never removed. The tool reports each worktree's dirty file count, whether it is merged, and whether it has unpushed commits, and marks the safe candidates. A person runs the removal.
Everything goes into git. `~/.devsweep` is itself a git repo, one commit per run. `ledger.jsonl` has one line per item: path, size, branch, dirty count, idle hours, rebuild command, and whether it was actually acted on.
The first test overturned the estimate
We tried deleting one 962 MB pnpm `node_modules` and compared `df` before and after.
Result: 0.
On macOS APFS, pnpm uses clones by default. The files in `node_modules` share physical blocks with the global store. `du` sees 962 MB. Deleting it reclaims only metadata. Of the original 60.9 GB estimate, 30.2 GB was clones like this: deleting them is wasted effort, plus a reinstall.
The tool gained a rule: pnpm `node_modules` are always skipped. Freed space is now measured as the `df` difference, and the manifest records both the `du` estimate and the `df` measurement. Estimates can be wrong. Measurements can't.
The real numbers
Action | Items | du estimate | df measured |
|---|---|---|---|
Delete one pnpm node_modules (experiment) | 1 | 962 MB | ≈ 0 |
Purge `.next` build output | 24 | 12.5 GB | 12.6 GB |
Purge npm `node_modules` | 39 | 17.6 GB | 17.0 GB |
Free space went from 1.4 GB to 31.4 GB. Not a single file under `~/.codex` or `~/.claude` was touched. The code and uncommitted files in all 53 worktrees are exactly as they were.
Keeping it from happening again
Three things, none of them code changes:
- Add a step at the end of every task: `git worktree remove <path>`, `git branch -d <branch>`.
- In pnpm projects, add `package-import-method=hardlink` to `.npmrc`. The space was shared already, but `du` will then report honestly.
- A launchd job runs `scan` every Monday morning. Scan only, never delete. Read the log, then decide.
Open source
The code, README and launchd configuration are here:
https://github.com/profitvisionlab/devsweep
MIT licensed. Three hundred lines of Python, no packages to install. If your machine also runs two or more AI agents side by side, run `scan` first, read the manifest, then decide whether to act.
