Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

docs: the reviewed cleanup, its routes, its subcommands

lmliheng7b40696541be
diff --git a/changelog/unreleased/2026-10-10-storage-cleanup.md b/changelog/unreleased/2026-10-10-storage-cleanup.md
new file mode 100644
index 00000000..a6bbe89d
--- /dev/null
+++ b/changelog/unreleased/2026-10-10-storage-cleanup.md
@@ -0,0 +1,26 @@
+# A cleanup that moves what a person approved, and only that
+
+- **Date:** 2026-10-10
+- **Type:** feature
+- **Scope:** `core`, `server`, `cli`, `web`, `docs`
+- **Issue:** [#17](https://github.com/lmliheng/Adelie/issues/17)
+
+[中文版](2026-10-10-storage-cleanup.zh.md)
+
+The [storage ledger](2026-10-10-storage-ledger.md) could say what the data root held. This is the step that acts on it, in the order the design fixed: scan → a person reviews → a person approves. `scan` writes a **bill**; entries are ticked on that bill; `apply` moves exactly those entries into the data root's own trash, where they can be put back. Nothing runs on a timer, no threshold acts by itself, and nothing is ever unlinked except by a purge a person asked for.
+
+## Details
+
+- **Every write is gated by the cleanup mode, which is off until somebody turns it on.** `GET|PUT /api/admin/storage/settings` holds it (`{enabled, trashTtlDays, pins}`); `penguin storage mode on|off` and a switch on the Settings page's Storage tab are the two ways to move it. While it is off, scanning, pinning, applying, restoring and purging all answer `409 storage_mode_off` and the ledger stays readable, so an install that never turns it on behaves exactly as the previous release did. One write runs at a time; a second answers `409 storage_busy`.
+- **A bill is a file, and it is what gets approved.** `POST /api/admin/storage/plans` writes `<root>/storage/plans/<planId>.json` and moves nothing; the id begins with the local time it was written. Each entry carries its class, bytes, file count, last change, the rules it matched, a size-and-mtime `fingerprint`, and whether this version may move it. The bill's own fingerprint covers all of them, and its life is 24 hours.
+- **Only temporary Workspaces are executable in this release.** `executableClasses` is the bill's own answer, so the classes that may be cleaned later are already visible on it: Session drafts, Traces, tool environments and the database stay on the bill marked *report only*, and naming one is refused with `409 class_not_executable` rather than quietly skipped. Database maintenance is not in this release (see below).
+- **`apply` needs the bill's id, its fingerprint, and the paths.** There is no "all" and no class to name, which is why neither the route nor `penguin storage apply` has a `--yes` and why a run that selects nothing is refused before it reaches the server. A path that is not on that bill (`400 unknown_path`), a pinned one (`409 pinned_path`), a bill already used (`409 plan_used`), one past its day (`409 plan_expired`) and one whose fingerprint does not match (`409 plan_stale`) each stop the run and say so.
+- **The disk is re-checked, as a batch, an instant before anything moves.** Every selected entry is re-resolved inside the root and re-measured: a tree whose size or modification time moved is `409 plan_stale`, one a Session has started using again is `409 entry_still_live`, one that is no longer a real directory inside the root is `409 path_not_allowed`. The check is all-or-nothing, so a refusal names every path that caused it, and a person answers it by scanning again.
+- **A move is a rename into `<root>/.trash/<timestamp>/items/<original path>`, never a delete.** The trash entry gets a `manifest.json` beside its items, and every move is written to `<root>/logs/storage-gc.jsonl` *before* it happens. `penguin storage trash` lists the batches, `trash restore <id>` renames the items back and skips whatever is in the way (`target_exists`, `not_in_trash`) instead of overwriting it, and `trash purge [<id>]` is the only deletion — one batch by id, or the batches past `trashTtlDays` (14 by default) when no id is given, and it can only reach inside `.trash`.
+- **A pin keeps an entry out of the bill entirely.** `POST /api/admin/storage/plans/:planId/pin`, or the pin toggle on the Storage page. A pinned path stays off every later bill and `apply` refuses it even when it is named by hand; turning the mode on or off and every pin and unpin land in the same audit log.
+- **The Settings page gains the review loop.** The Storage page keeps its report and adds the mode switch and trash retention, a Scan button, the bill's entries with the executable ones ticked and a checkbox that stays under the person's control, a per-entry pin, the move step, and the trash with its restore and purge, so the whole sequence can be walked without a terminal.
+- **Docs.** `/docs/cli § penguin storage` and the [server API reference](/server-api) document the subcommands, every route, the error codes and the review loop; both language files were updated.
+
+## What comes later
+
+Reclaiming the space a purge frees at the filesystem level (the database's own `VACUUM`, and the classes that are report-only today) is a separate change: it is a maintenance window rather than a reviewed move, and it is not needed while the database is not what fills the root.
diff --git a/changelog/unreleased/2026-10-10-storage-cleanup.zh.md b/changelog/unreleased/2026-10-10-storage-cleanup.zh.md
new file mode 100644
index 00000000..74f64e05
--- /dev/null
+++ b/changelog/unreleased/2026-10-10-storage-cleanup.zh.md
@@ -0,0 +1,26 @@
+# 只搬走人批准过的东西的清理
+
+- **Date:** 2026-10-10
+- **Type:** feature
+- **Scope:** `core`, `server`, `cli`, `web`, `docs`
+- **Issue:** [#17](https://github.com/lmliheng/Adelie/issues/17)
+
+[English](2026-10-10-storage-cleanup.md)
+
+[存储台账](2026-10-10-storage-ledger.zh.md)已经能说清数据根里有什么,这一版是照着它动手的那一步,顺序就是设计里定死的:扫描 → 人过目 → 人批准。`scan` 写出一份**账单**;人在账单上勾选条目;`apply` 把勾中的条目搬进数据根自己的回收站,还能原地放回。没有任何东西挂在定时器上,没有任何阈值会自己动手,除非有人要求清理,否则没有任何东西会被 unlink。
+
+## 细节
+
+- **所有写操作都由「清理模式」把关,而它默认关闭。** 模式存在 `GET|PUT /api/admin/storage/settings`(`{enabled, trashTtlDays, pins}`);`penguin storage mode on|off` 与设置页「存储」标签上的开关是唯一的两种改法。模式关闭时,扫描、pin、搬移、还原、清理回收站一律返回 `409 storage_mode_off`,而台账照旧可读——所以从没打开过它的安装,行为与上一版完全一致。同一时刻只有一个写操作,第二个返回 `409 storage_busy`。
+- **账单是一个文件,被批准的正是它。** `POST /api/admin/storage/plans` 写出 `<root>/storage/plans/<planId>.json`,不动任何别的东西;id 以写入时的本地时间开头。每条记录带上类别、字节数、文件数、最后改动时间、命中的规则、由大小与修改时间算出的 `fingerprint`,以及这一版能不能搬它。账单自己的指纹覆盖全部条目,寿命 24 小时。
+- **这一版只有临时工作区可执行。** `executableClasses` 由账单自己给出,所以以后才允许清理的类别已经先摆在账单上:会话草稿、轨迹、工具环境与数据库留在账单上标着**仅报告**,点名它们会被 `409 class_not_executable` 拒绝,而不是被悄悄跳过。数据库维护不在这一版里(见下)。
+- **`apply` 需要账单 id、它的指纹,以及路径。** 没有「全部」,也不能点名类别——所以路由与 `penguin storage apply` 里都没有 `--yes`,一个什么都没选的运行在到达服务端之前就被拒。不在该账单上的路径(`400 unknown_path`)、被 pin 的路径(`409 pinned_path`)、已用过的账单(`409 plan_used`)、超过一天的账单(`409 plan_expired`)、指纹不符的账单(`409 plan_stale`),每一条都会中止运行并说明原因。
+- **真正搬动之前的一瞬,整批重新核对磁盘。** 每个选中的条目都会重新解析到根目录内、重新测量:大小或修改时间变过的目录树返回 `409 plan_stale`,被 Session 重新用上的返回 `409 entry_still_live`,不再是根内真实目录的返回 `409 path_not_allowed`。这道核对是全有或全无,因此拒绝时会点名所有导致拒绝的路径,人的回应就是重新扫一次。
+- **搬移是重命名进 `<root>/.trash/<时间戳>/items/<原路径>`,绝不是删除。** 回收站条目旁边有一份 `manifest.json`,而且每一次搬移都在发生**之前**写进 `<root>/logs/storage-gc.jsonl`。`penguin storage trash` 列出各批次,`trash restore <id>` 把条目重命名回原处、遇到挡路的东西就跳过(`target_exists`、`not_in_trash`)而不覆盖,`trash purge [<id>]` 是唯一的删除——带 id 删那一批,不带 id 只删超过 `trashTtlDays`(默认 14 天)的批次,而且只能触及 `.trash` 内部。
+- **pin 让一个条目完全不进账单。** `POST /api/admin/storage/plans/:planId/pin`,或「存储」页上的 pin 开关。被 pin 的路径从此不出现在任何后续账单上,即便被人手动点名 `apply` 也会拒绝;模式的开关、每一次 pin 与 unpin 都写进同一份审计日志。
+- **设置页补上了这套审核闭环。** 「存储」页保留原来的报告,并加上模式开关与回收站保留期、扫描按钮、账单条目(可执行的默认勾选、勾选权始终在人手上)、逐条 pin、搬移步骤,以及带还原与清理的回收站——整条链路不打开终端也能走完。
+- **文档。** `/docs/cli § penguin storage` 与服务端 API 参考记录了这些子命令、每个路由、错误码与审核闭环,中英两份都已更新。
+
+## 之后要做的
+
+把清理回收站真正释放的空间在文件系统层面还回去(数据库自己的 `VACUUM`,以及今天仍为「仅报告」的那些类别)是另一笔改动:那更像一次维护窗口,而不是一次经审核的搬移;在数据库不是占满数据根的主因之前,它也还用不上。
diff --git a/packages/docs/content/cli.en.md b/packages/docs/content/cli.en.md
index 7f40e833..25ec6e4e 100644
--- a/packages/docs/content/cli.en.md
+++ b/packages/docs/content/cli.en.md
@@ -259,18 +259,25 @@ penguin cost --from 2026-08-01 --to 2026-08-25 --by agent
## penguin storage
-Shows what occupies the data root (`ADELIE_HOME`, `~/.adelie/data` by default): one row per class of data, then the entries a person could clean up, then the environments that look like the same toolchain installed twice.
+Shows what occupies the data root (`ADELIE_HOME`, `~/.adelie/data` by default): one row per class of data, then the entries a person could clean up, then the environments that look like the same toolchain installed twice. The bare command is the report, spelled short.
```bash
-penguin storage [options]
+penguin storage [options] # the report
+penguin storage scan # write a bill
+penguin storage plan [<planId>] # read one back, or list the recent ones
+penguin storage apply <planId> (--path <path> … | --all) # move the entries you name
+penguin storage trash [list]
+penguin storage trash restore <id>
+penguin storage trash purge [<id>]
+penguin storage mode [on|off]
```
| Option | Description | Default |
| --- | --- | --- |
-| `--top <n>` | How many candidates to print; `0` prints every one. | 20 |
-| `--json` / `--server <url>` | See [Global conventions](#global-conventions). | — |
+| `--top <n>` | How many candidates the report prints; `0` prints every one. | 20 |
+| `--json` / `--server <url>` | See [Global conventions](#global-conventions). Every subcommand takes both. | — |
-**This command is read-only, and it has no flag that could change that**: it reports, it never moves or deletes a file, and nothing in Adelie runs a cleanup on a timer.
+**The report is read-only.** It prints, it never moves or deletes a file, and nothing in Adelie runs a cleanup on a timer.
The class column accounts for every byte under the root. User data — Agent State, Project config, the Workspaces you chose, vaults, plugins, benchmarks, snapshots — is listed but is never a cleanup candidate; the derived classes are the ones that can be: temporary Workspaces, Session drafts, Traces, tool environments, the trash, the database. The candidate list names the rule each row matched: `empty` or `unreferenced` (a temporary Workspace no Session points at), `session gone` (drafts of a deleted Session), `idle` (silent past a threshold), `over budget` (a size cap evicted it, oldest first). Every threshold defaults to off except the temporary-Workspace idle rule (30 days), so a fresh install reports an empty candidate list rather than a to-do list.
@@ -284,6 +291,30 @@ penguin storage --top 50
penguin storage --json
```
+### The reviewed cleanup
+
+Everything below the report is a deliberate act, and the server refuses all of it until somebody turns the **cleanup mode** on — `penguin storage mode on`, or the switch on the Settings page's Storage tab. While it is off, the report is all this command can do and every write answers `409 storage_mode_off`; while a cleanup is running, another one answers `409 storage_busy`. The mode is off on a machine that never turned it on, and nothing turns it on by itself.
+
+`scan` writes a bill — a plan — under the data root's `storage/plans/` and moves nothing. A bill lists each candidate with its class, size, file count, last-change time, the rule it matched and its path, and marks every entry `move` or `report only`: only temporary Workspaces are executable in this version, and the rest are on the bill so a person can see them and deal with them by hand. The bill carries a fingerprint of its entries and a 24-hour life; acting on it, or any entry changing size or modification time, invalidates it.
+
+`apply` needs the bill's id **and** the paths to move, which is why there is no `--yes` anywhere in this command and no "clean whatever you find": a run naming neither `--path` nor `--all` is refused before the server is even asked. `--all` means every *executable* entry on that bill, never a report-only one. So the sequence is always read the bill, name what should go, then run it.
+
+What a run actually does, per entry, re-checked at that moment: a bill past its day, an entry whose size or modification time has drifted, a path that has gone missing, a temporary Workspace a Session has started using again, a class this version only reports, and a path pinned on the bill each stop the whole run and name themselves (`409 plan_expired`, `plan_stale`, `entry_still_live`, `class_not_executable`, `pinned_path`) instead of being cleared. Nothing is deleted: the entries that pass are renamed into the data root's `.trash/<timestamp>/` with a `manifest.json` beside them, and each one is written to `logs/storage-gc.jsonl` before it moves.
+
+`trash` is where a move lands. `list` (the default) shows each batch with its items and marks the ones past the retention period; `restore <id>` renames the items back where they came from and skips any whose original path is occupied again; `purge [<id>]` is the only deletion in the whole design and is spelled out separately — with an id it removes that batch, without one it removes only the batches past the retention period (14 days by default, set under `mode` or on the Settings page).
+
+An entry can be pinned to keep it off the bill entirely, from the pin button on the Settings page's Storage tab; `mode` prints how many paths are pinned. A pinned path is refused by `apply` even when it is named by hand.
+
+```bash
+penguin storage mode on # deliberately open the cleanup mode
+penguin storage scan # write a bill; nothing has moved yet
+penguin storage plan # the recent bills, newest first
+penguin storage plan 2026-10-10-13-05-22-4f2a
+penguin storage apply 2026-10-10-13-05-22-4f2a --path 'agents/helper/workspaces/tmp-8f3c'
+penguin storage trash # what a move landed in
+penguin storage trash restore 20261010-130621
+```
+
## penguin schedule
Lists and manages the Project's scheduled tasks.
diff --git a/packages/docs/content/cli.zh.md b/packages/docs/content/cli.zh.md
index 629f3d5f..9d755a0f 100644
--- a/packages/docs/content/cli.zh.md
+++ b/packages/docs/content/cli.zh.md
@@ -259,18 +259,25 @@ penguin cost --from 2026-08-01 --to 2026-08-25 --by agent
## penguin storage
-查看数据根(`ADELIE_HOME`,默认 `~/.adelie/data`)被什么占满:先是每一类数据一行,然后是可供人清理的条目,最后是看起来同一套工具链装了多份的环境。
+查看数据根(`ADELIE_HOME`,默认 `~/.adelie/data`)被什么占满:先是每一类数据一行,然后是可供人清理的条目,最后是看起来同一套工具链装了多份的环境。不带子命令时就是报告本身。
```bash
-penguin storage [选项]
+penguin storage [选项] # 报告
+penguin storage scan # 出一份账单
+penguin storage plan [<planId>] # 读回一份,或列出最近几份
+penguin storage apply <planId> (--path <路径> … | --all) # 搬走你点名的条目
+penguin storage trash [list]
+penguin storage trash restore <id>
+penguin storage trash purge [<id>]
+penguin storage mode [on|off]
```
| 选项 | 说明 | 默认 |
| --- | --- | --- |
-| `--top <n>` | 打印多少条候选;`0` 表示全部。 | 20 |
-| `--json` / `--server <url>` | 见[全局约定](#全局约定)。 | — |
+| `--top <n>` | 报告打印多少条候选;`0` 表示全部。 | 20 |
+| `--json` / `--server <url>` | 见[全局约定](#全局约定)。每个子命令都接受这两个。 | — |
-**本命令只读,也没有任何能让它改变这一点的开关**:它只报告,从不移动或删除文件,Adelie 里也没有任何定时清理在跑。
+**报告本身只读。** 它只打印,从不移动或删除文件,Adelie 里也没有任何定时清理在跑。
类别一列覆盖数据根下的每一个字节。用户资产——Agent State、Project 配置、你自己指定的 Workspace、密钥库、插件、基准、快照——会被列出,但永远不会成为清理候选;可能成为候选的是派生数据:临时工作区、会话草稿、轨迹、工具环境、回收站、数据库。候选清单会写明每一行命中的规则:`empty` 空目录、`unreferenced` 无引用(没有任何会话指向的临时工作区)、`session gone` 会话已删除(草稿所属的会话不存在了)、`idle` 静默超期、`over budget` 超出预算(按最旧优先被体积上限选出)。除了临时工作区的静默规则(30 天)之外,所有阈值默认关闭——因此刚装好的机器看到的是一份空清单,而不是一份待办。
@@ -284,6 +291,30 @@ penguin storage --top 50
penguin storage --json
```
+### 人工审核的清理
+
+报告之外的一切都是刻意的动作,而且在有人打开**清理模式**之前,服务端会拒绝全部写操作——`penguin storage mode on`,或设置页「存储」标签里的开关。模式关闭时,报告就是这条命令能做的全部,任何写操作都返回 `409 storage_mode_off`;一次清理正在跑时,另一次返回 `409 storage_busy`。没被人打开过的机器上模式就是关着的,也没有任何东西会自己打开它。
+
+`scan` 会在数据根的 `storage/plans/` 下写出一份账单(plan),不移动任何东西。账单逐条列出候选:类别、体积、文件数、最后改动时间、命中的规则与路径,并给每条标记 `move`(可搬)或 `report only`(仅报告):这一版只有临时工作区可执行,其余留在账单上供人看见并手动处理。账单带有自身条目的指纹与 24 小时寿命;一旦被使用过,或任何条目的大小/修改时间变了,它就失效。
+
+`apply` 需要账单 id **和**要搬的路径——所以这条命令里没有任何 `--yes`,也没有「看见什么就清什么」:既不给 `--path` 也不给 `--all` 的运行会在请求服务端之前就被拒绝。`--all` 指的是账单上所有**可执行**条目,从不包含仅报告的那些。所以顺序永远是:读账单、点名、再执行。
+
+真正执行时,逐条当场复核:账单过期、条目大小或修改时间漂移、路径已消失、临时工作区被某个会话重新用上、此类在这一版只报告、以及账单上被 pin 住的路径——任何一条都会中止整批运行并点名自己(`409 plan_expired`、`plan_stale`、`entry_still_live`、`class_not_executable`、`pinned_path`),而不是被顺手清掉。没有任何删除:通过的条目被重命名进数据根的 `.trash/<时间戳>/`,旁边留一份 `manifest.json`,并且每条在搬动之前先写进 `logs/storage-gc.jsonl`。
+
+`trash` 就是搬移的落点。`list`(默认)列出每一批及其条目,并标出超过保留期的批次;`restore <id>` 把条目重命名回原处,原路径已被占用的则跳过;`purge [<id>]` 是整个设计里唯一的删除,且单独成词——带 id 删那一批,不带 id 只删超过保留期的批次(默认 14 天,由 `mode` 与设置页设定)。
+
+条目也可以被 pin 住,从而完全不进账单(设置页「存储」标签上的 pin 按钮);`mode` 会打印当前 pin 了几条路径。被 pin 的路径即便被人手动点名,`apply` 也会拒绝。
+
+```bash
+penguin storage mode on # 刻意打开清理模式
+penguin storage scan # 出一份账单;此时什么都还没动
+penguin storage plan # 最近的几份账单,最新在前
+penguin storage plan 2026-10-10-13-05-22-4f2a
+penguin storage apply 2026-10-10-13-05-22-4f2a --path 'agents/helper/workspaces/tmp-8f3c'
+penguin storage trash # 搬移落到了哪里
+penguin storage trash restore 20261010-130621
+```
+
## penguin schedule
列出并管理 Project 的定时任务。
diff --git a/packages/docs/content/server-api.en.md b/packages/docs/content/server-api.en.md
index e70cfa6e..3b5d02ba 100644
--- a/packages/docs/content/server-api.en.md
+++ b/packages/docs/content/server-api.en.md
@@ -175,13 +175,23 @@ On PUT, fields the request omits keep their value, `null` or `""` clears one, an
## Storage Ledger (admin only)
-What the data root holds, class by class, and which entries a person could review.
+What the data root holds, class by class, which entries a person could review, and the reviewed cleanup that reads it. The report is always available; every write answers `409 storage_mode_off` until the cleanup mode is on.
| Method | Path | Description |
| --- | --- | --- |
| GET | `/api/admin/storage` | The storage ledger: `{report: {root, scannedAt, totalBytes, classes, candidates, sharedEnvGroups, disk, unreadable}}` |
-
-**Nothing here cleans up.** The route reads the root and answers; it takes no query parameter, has no PUT or DELETE beside it, and no part of the server acts on a report by itself — every removal in this design is a decision a person takes, and a report is what they take it against. A stale report cannot cause one either: `referenced` is recomputed from live state when a cleanup would run, so an entry that came alive in between is not acted on.
+| GET | `/api/admin/storage/settings` | The cleanup mode: `{settings: {enabled, trashTtlDays, pins}}` |
+| PUT | `/api/admin/storage/settings` | Sets any of `enabled`, `trashTtlDays` (a whole number of days, 1–365) and `pins`, and answers with the stored settings. A PUT naming one bad field writes nothing. |
+| GET | `/api/admin/storage/plans` | The twenty most recent bills, newest first: `{plans: [{id, createdAt, expiresAt, entries, totalBytes, appliedAt, expired, …}]}` |
+| POST | `/api/admin/storage/plans` | Scans the data root and writes a bill: `201` with `{plan}`. Writes no other file. |
+| GET | `/api/admin/storage/plans/:planId` | One bill: `{plan}` |
+| POST | `/api/admin/storage/plans/:planId/pin` | Keeps one entry out of every future bill: `{path, pinned}` (`pinned` defaults to `true`) answers with the settings. A path that is not inside the root is `400 invalid_pin`. |
+| POST | `/api/admin/storage/apply` | Moves the approved entries into the trash: `{planId, fingerprint, paths}` answers with `{planId, trashId, moved, failed, freedBytes}`. |
+| GET | `/api/admin/storage/trash` | What a run moved: `{entries: [{id, createdAt, planId, bytes, files, items, expired}], ttlDays}` |
+| POST | `/api/admin/storage/trash/restore` | Puts one batch back: `{id}` answers with `{id, restored, skipped, remaining}`. |
+| POST | `/api/admin/storage/trash/purge` | The one deletion: `{id}` removes that batch; an empty body removes every batch past the retention. Answers `{purged: [{id, bytes, files}]}`. |
+
+**Only a person starts anything here.** No route runs on a timer and none accepts a policy: a caller cannot widen what a scan considers, and cannot ask for "everything unreferenced" — the unit of approval is a specific bill. `GET /api/admin/storage` reads the root and answers and takes no query parameter, and the entries of a bill are re-measured and re-checked against live state when a run starts, so a stale bill cannot cause a move: a tree that grew, was touched or came alive in between stops the run instead of being half-cleaned. One write runs at a time; a second answers `409 storage_busy`.
`root` is the real path that was walked and `scannedAt` is the instant it was observed (ISO), so the report describes that moment rather than the moment it is read. `totalBytes` is the sum of every class, so the classification accounts for the whole root; each class carries its byte, file and entry counts plus how many of its entries are candidates and how many bytes they hold. The classes are `protected` (user data: Agent State, Project configuration, a Workspace the user chose, user vaults, plugins, benchmarks, snapshots), `tmp_workspaces`, `session_drafts`, `traces`, `shared_env`, `trash`, `database` and `other`.
@@ -189,6 +199,16 @@ What the data root holds, class by class, and which entries a person could revie
Every threshold is off by default except the temporary-Workspace idle rule: the ledger's value is that it names references, budgets and duplicate environments, not that it guesses at timestamps.
+### A bill, and what a run does with it
+
+A bill is a plan file under `<root>/storage/plans/<planId>.json`, with an id that starts in the local timestamp it was written (`2026-10-10-13-05-22-4f2a`). It carries one entry per candidate — `{path, class, bytes, files, lastModifiedAt, rules, fingerprint, executable}` — a fingerprint over all of them, and a 24-hour life. `executable` is what this version may move; the classes it may act on are listed in the bill's own `executableClasses`, and today that is `tmp_workspaces` alone — the other classes are on the bill so a person can see them and deal with them by hand.
+
+`apply` is the only route in this group that changes the data root, and it needs three things: the bill's id, that bill's `fingerprint`, and the paths to move. There is no "all" and no class to name, so what moves is exactly what somebody ticked on a bill they were reading. A run with an empty `paths` is `400 nothing_selected`; a path that is not an entry of that bill is `400 unknown_path`; one this version only reports is `409 class_not_executable`; one outside what its class may touch is `400 path_not_allowed`; one that is pinned is `409 pinned_path`. A bill that was already used is `409 plan_used`, one past its day `409 plan_expired`, one whose fingerprint does not match `409 plan_stale`. Each entry is then re-resolved and re-measured: gone or changed is `409 plan_stale`, a Workspace a Session has started using again is `409 entry_still_live`, and a path that is no longer a directory inside the root is `409 path_not_allowed`. The batch is validated as a whole before a single move, so a refusal names every path that caused it.
+
+Nothing is ever unlinked. A passing entry is renamed into `<root>/.trash/<timestamp>/items/<original path>`, with a `manifest.json` beside it, and the move is written to `<root>/logs/storage-gc.jsonl` before it happens — so a run that dies partway is explainable and a rerun converges. The same log records the mode being turned on or off and every pin and unpin, which is what answers "why is this entry not on the bill any more" months later.
+
+`trash` lists those batches; `restore` renames their items back and skips (`target_exists`, `not_in_trash`) whatever is in the way rather than overwriting it, removing the batch once nothing but empty directories is left. `purge` is the one deletion, it needs the cleanup mode like the rest, and it can only reach inside `<root>/.trash`: with an id it removes exactly that batch, without one only the batches past `trashTtlDays` (14 by default).
+
## Machines (admin only)
Installs this server's build on other hosts over ssh and manages the connections to them.
diff --git a/packages/docs/content/server-api.zh.md b/packages/docs/content/server-api.zh.md
index 7b37f35f..e4c94222 100644
--- a/packages/docs/content/server-api.zh.md
+++ b/packages/docs/content/server-api.zh.md
@@ -175,13 +175,23 @@ PUT 时,请求省略的字段保持原值,`null` 或 `""` 清除该字段,
## 存储台账(仅管理员)
-数据根目录里有什么,按类别列出,以及哪些条目值得人工过目。
+数据根目录里有什么,按类别列出,哪些条目值得人工过目,以及读取它的那套「人工审核的清理」。报告随时可读;在清理模式打开之前,所有写操作都返回 `409 storage_mode_off`。
| 方法 | 路径 | 说明 |
| --- | --- | --- |
| GET | `/api/admin/storage` | 存储台账:`{report: {root, scannedAt, totalBytes, classes, candidates, sharedEnvGroups, disk, unreadable}}` |
-
-**这个接口不做任何清理。** 它只读取根目录并返回结果:不接受任何查询参数,旁边没有 PUT 也没有 DELETE,服务器也不会自行依据报告采取动作——这套设计里每一次删除都由人决定,报告只是决定的依据。过期的报告也不会导致删除:真正执行时 `referenced` 会按当时的活性状态重新计算,期间重新活跃起来的条目不会被处理。
+| GET | `/api/admin/storage/settings` | 清理模式:`{settings: {enabled, trashTtlDays, pins}}` |
+| PUT | `/api/admin/storage/settings` | 设置 `enabled`、`trashTtlDays`(整天数,1–365)、`pins` 中的任意项,返回存储后的设置。只要有一个字段不合法,整个 PUT 不写入任何东西。 |
+| GET | `/api/admin/storage/plans` | 最近二十份账单,最新在前:`{plans: [{id, createdAt, expiresAt, entries, totalBytes, appliedAt, expired, …}]}` |
+| POST | `/api/admin/storage/plans` | 扫描数据根并写出一份账单:`201` 加 `{plan}`。不写任何其他文件。 |
+| GET | `/api/admin/storage/plans/:planId` | 读一份账单:`{plan}` |
+| POST | `/api/admin/storage/plans/:planId/pin` | 让某个条目从此不进任何账单:`{path, pinned}`(`pinned` 默认为 `true`)返回设置。路径不在数据根内时返回 `400 invalid_pin`。 |
+| POST | `/api/admin/storage/apply` | 把批准过的条目搬进回收站:`{planId, fingerprint, paths}` 返回 `{planId, trashId, moved, failed, freedBytes}`。 |
+| GET | `/api/admin/storage/trash` | 搬移落到了哪里:`{entries: [{id, createdAt, planId, bytes, files, items, expired}], ttlDays}` |
+| POST | `/api/admin/storage/trash/restore` | 把一批放回原处:`{id}` 返回 `{id, restored, skipped, remaining}`。 |
+| POST | `/api/admin/storage/trash/purge` | 唯一的删除:`{id}` 删掉那一批;请求体为空则只删超过保留期的批次。返回 `{purged: [{id, bytes, files}]}`。 |
+
+**这里的一切都由人启动。** 没有任何路由挂在定时器上,也没有任何路由接受「策略」:调用方既不能放宽一次扫描的判定范围,也不能要求「把所有无引用的都清掉」——批准的粒度就是一份具体的账单。`GET /api/admin/storage` 只读取根目录并返回,不接受任何查询参数;真正执行时会按当时的活性状态重新测量并复核账单里的每个条目,因此过期的账单不会导致搬移:期间长大了、被改动过或重新活跃起来的目录树会中止这次运行,而不是被清掉一半。同一时刻只有一个写操作在跑,第二个返回 `409 storage_busy`。
`root` 是实际遍历的真实路径,`scannedAt` 是观测发生的那一刻(ISO),因此报告描述的是那一刻而不是被读取的那一刻。`totalBytes` 是各类别之和,也就是说这套分类覆盖整个根目录;每个类别带有自己的字节数、文件数、条目数,以及其中有多少条目是候选、共占多少字节。类别有 `protected`(用户数据:Agent State、Project 配置、用户自选的 Workspace、用户密钥库、插件、Benchmark、快照)、`tmp_workspaces`、`session_drafts`、`traces`、`shared_env`、`trash`、`database` 和 `other`。
@@ -189,6 +199,16 @@ PUT 时,请求省略的字段保持原值,`null` 或 `""` 清除该字段,
除临时 Workspace 的静默规则外,所有阈值默认关闭:这份台账的价值在于点出引用、体积上限和重复环境,而不在于拿时间戳去猜。
+### 账单,以及一次运行对它做了什么
+
+账单是数据根下 `storage/plans/<planId>.json` 中的一份文件,id 以写入时的本地时间开头(`2026-10-10-13-05-22-4f2a`)。它为每个候选带一条记录——`{path, class, bytes, files, lastModifiedAt, rules, fingerprint, executable}`——外加覆盖全部条目的指纹与 24 小时寿命。`executable` 表示这一版能不能搬它;可执行的类别列在账单自己的 `executableClasses` 里,目前只有 `tmp_workspaces`——其余类别留在账单上是让人看见并手动处理。
+
+`apply` 是这一组路由里唯一改动数据根的接口,它需要三样东西:账单 id、该账单的 `fingerprint`,以及要搬的路径。没有「全部」也不能点名类别,所以搬走的正是有人在账单上勾选的那些。`paths` 为空返回 `400 nothing_selected`;不是该账单条目的路径返回 `400 unknown_path`;这一版只报告的类别返回 `409 class_not_executable`;超出该类别可触及范围的返回 `400 path_not_allowed`;被 pin 住的返回 `409 pinned_path`。已用过的账单返回 `409 plan_used`,超过一天返回 `409 plan_expired`,指纹不符返回 `409 plan_stale`。随后每个条目会被重新解析、重新测量:已消失或已变动返回 `409 plan_stale`,被 Session 重新用上的 Workspace 返回 `409 entry_still_live`,不再是数据根内目录的路径返回 `409 path_not_allowed`。整批会在搬动任何一条之前先整体校验,所以拒绝时会点名所有导致拒绝的路径。
+
+任何东西都不会被 unlink。通过的条目被重命名进 `<root>/.trash/<时间戳>/items/<原路径>`,旁边留一份 `manifest.json`,并且这次搬移在发生之前就写进 `<root>/logs/storage-gc.jsonl`——所以中途死掉的运行有据可查,重跑也会收敛。同一份日志还记录清理模式的开关、每一次 pin 与 unpin,这正是几个月后回答「这个条目为什么不在账单上了」的依据。
+
+`trash` 列出这些批次;`restore` 把其中的条目重命名回原处,遇到挡路的东西就跳过(`target_exists`、`not_in_trash`)而不是覆盖,等到只剩空目录时整批一并删除。`purge` 是唯一的删除,和其他写操作一样需要清理模式,而且只能触及 `<root>/.trash` 内部:带 id 就删那一批,不带 id 只删超过 `trashTtlDays`(默认 14 天)的批次。
+
## 机器(仅管理员)
通过 ssh 在其他主机上安装本服务器的构建,并管理与这些主机的连接。