# For agents

Give an agent this prompt.

```text
Run npx whatfrees --json. Consider only safe entries for automatic cleanup. Leave orphan entries for a person to inspect and approve.
For a safe worktrees entry, run npx whatfrees check --json <path> right before deletion. Use the entry's own command only if the check exits with 0 and still reports safe; otherwise skip it.
Read https://whatfrees.loadsheet.dev/guide/agents first.
```

## The loop

### Scan

```sh
whatfrees --json
```

Read `entries`. They are sorted by `bytes.frees`, biggest first. `disk[].free` is the free space before you start.

### Choose

Only a `safe` entry can proceed without manual review:

| Verdict | What to do |
| --- | --- |
| `safe` | Go on to the next step |
| `orphan` | Leave it for a person to inspect and approve. A worktree without Git metadata may still hold the only copy of a file |
| `ask` | Show the person the `what`, the `evidence` and the size, and let them decide |
| `unsaved` | Never delete. The work exists nowhere else |
| `live` | Never delete. Something uses it |
| `unchecked` | Never delete. whatfrees couldn't tell |

### Check again

For a `safe` entry from the `worktrees` source, run this right before the delete, not at the start of the session. `whatfrees check` only knows Git checkouts and the agents' leftovers; the pnpm store is always `ask`.

```sh
whatfrees check --json /home/you/.codex/worktrees/9f2c/app
```

Go on only if the check exits with 0 and the JSON `verdict` is still `safe`. Skip the entry on any error or missing verdict. If it is now `orphan`, leave it for manual review even if the exit code is 0.

### Run the command

Run the entry's `command` as it is. It is a quoted POSIX command built from a short list of known verbs, so a path with spaces or `$(...)` in it can't become a second command. If the entry has a `then`, run it after, for the bytes to come back.

Don't add `--force`. Don't swap in another command.

### Confirm

Run `whatfrees` again. `disk[].free` should have grown by about the `frees` you counted. Report both numbers.

## Rules

- whatfrees deletes nothing. The agent does, and the agent is responsible for the delete.
- A `safe` verdict is true at the time of the scan. A person or another agent may start work afterwards. That is why `whatfrees check` comes right before each delete.
- Don't pass `--all` to find more to delete. It adds entries that are in use or small, and `live` stays `live`.
- When `partial` is `true`, sizes are lower bounds or missing. Run the first `next` command to get a longer budget, or pass `--exact`.
- A source in `skipped` has no entries in this report. It doesn't mean the thing is empty.
- Don't read the text report to parse it. Use `--json` and the fields in [the JSON report](https://whatfrees.loadsheet.dev/reference/report).

## In code

The same loop in a script.

```ts
import { check, scan } from "whatfrees";

const report = await scan();
for (const entry of report.entries) {
  if (entry.verdict !== "safe") continue;
  if (entry.source === "worktrees") {
    if (entry.path === undefined) continue;
    const answer = await check(entry.path);
    if (answer.verdict !== "safe") continue;
  }
  console.log(entry.command);
}
```

whatfrees gives the command. Running it is up to you. See [the JavaScript API](https://whatfrees.loadsheet.dev/reference/api).

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://whatfrees.loadsheet.dev/raw/guide.md)
- [Installation](https://whatfrees.loadsheet.dev/raw/guide/installation.md)
- [How it stays fast](https://whatfrees.loadsheet.dev/raw/guide/speed.md)
- [Verdicts](https://whatfrees.loadsheet.dev/raw/guide/verdicts.md)
- [Sources](https://whatfrees.loadsheet.dev/raw/guide/sources.md)
- [whatfrees check](https://whatfrees.loadsheet.dev/raw/guide/check.md)
- [For agents](https://whatfrees.loadsheet.dev/raw/guide/agents.md) (this page)

All pages: [llms.txt](https://whatfrees.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/whatfrees/issues). Report a security vulnerability privately, never in a public issue.
