Expose storage usage and safe persisted age or size retention

This commit is contained in:
2026-11-06 21:34:55 +00:00
parent aa19c82156
commit 6abc309009
19 changed files with 1309 additions and 12 deletions
+57
View File
@@ -1183,3 +1183,60 @@ continues to its next stage or terminal, and its workspace is released. The
server applies this at startup and every 30 seconds, so a run parked before an
upgrade recovers after the next restart. A native session larger than the 8 MiB
export bound (typically inline image payloads read by a tool) is always partial.
### Storage overview and retention (application RPC)
The authenticated application uses `get_storage` to read a `storage` reply.
`storage.total_bytes` sums logical regular-file lengths, deduplicated by inode;
it is not allocated filesystem space or the size of the whole disk. Categories
cover database/journals, run artifacts/exports, chat attachments, server-owned
Pi session files, other data under the database directory, linked worktrees,
and known project workspaces. Symlink targets are excluded. Unknown external
harness histories and repositories without a recorded workspace are outside
this inventory. `measurement` is `logical_file_bytes`; `partial`, category
errors and warnings explicitly mark incomplete scans. Repository identities
are remembered so removing run history cannot make sibling worktrees disappear
from the next measurement.
`set_storage_policy` requires `storage_policy`:
```json
{"enabled":false,"max_age_days":30,"max_bytes":10737418240,"include_worktrees":false}
```
Zero disables the corresponding limit. Days must be 0–36500 and bytes 0–2^50.
Automatic cleanup requires at least one limit. It is disabled by default,
persisted server-side, and runs hourly while enabled; neither startup nor saving
a policy performs cleanup. Age expiry and size pressure are independent triggers:
expired eligible data is selected, then oldest eligible data until the size target
is met or only protected data remains. Run age is completion time (creation only
when no completion time exists); worktree age is the latest measured modification.
Database pages are not vacuumed on the live service, so a size target is best effort.
`preview_storage_cleanup` optionally accepts that policy and returns
`storage_cleanup_preview` with `storage_cleanup`: policy, total/reclaimable bytes,
target status, candidate/protected counts and items. Each item identifies its kind,
id, path, measured bytes, reason and protected flag. A preview performs no cleanup.
The result contains an opaque `preview_token` valid for ten minutes.
`apply_storage_cleanup` requires the same `storage_policy` and top-level
`storage_cleanup_token`; it returns `storage_cleanup_applied` with removed count,
measured reduction and any refusal/errors. Tokens are one-use; expired or changed
policies require another preview. Apply never adds candidates that appeared after
the confirmed preview, and revalidates every candidate.
Cleanup removes eligible completed run records and their server-owned files via
the existing transactional deletion path, never definitions or chat history.
Queued/running/waiting, unconfirmed-cleanup, active/exporting or retry-dependent
runs remain protected. Worktrees are opt-in and must be verified linked trees,
clean, unlocked and fully merged into local main. Primary/main checkouts, any
recorded operator-chat directory, active-run directory, live server-data roots,
unmerged work, and trees containing another registered checkout are retained.
Branches are never deleted and Git removal never uses force. New chat/workspace
admission is serialized with the short final removal gate.
Storage work runs beside the WebSocket read loop with one bounded worker; busy
requests fail promptly instead of blocking chat prompts or permissions. A pass
has a 20-second context, bounded Git output/time and file traversal, and at most
100 deletion candidates. Partial scans cannot claim that the storage target has
been reached. Cleanup refusals and limits remain visible rather than discarding
protected data to satisfy a quota.