Skip to content

Storage retention: reliable cleanup status, bulk cleanup by staleness, plain explanations - #7521

Merged
ymichael merged 5 commits into
mainfrom
bb/fix-storage-retention-plugin-errors-thr_qjiray8j3c
Nov 13, 2026
Merged

ymichael merged 5 commits into
mainfrom
bb/fix-storage-retention-plugin-errors-thr_qjiray8j3c

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Cleaning up storage on the Storage page produced confusing toasts and errors, and the page often showed stale data:

  • "Failed to fetch" on cleanups that worked. Row cleanups were followed by polling a status RPC every 500 ms. A brief bb Connect tunnel drop failed a poll, and the page reported the whole cleanup as failed while it kept running. A reload also lost track of the cleanup.
  • Toasts nobody asked for. With "Delete development data when its checkout is removed" on, bb rescans every hour and on reconnect, and every one of those scans showed a toast.
  • Wrong totals. When a deletion couldn't measure what it freed, it counted as 0 bytes.
  • Races. Removing one worktree while a bulk removal ran made the two race.
  • Trash that wouldn't delete. Folders with read-only directories failed to delete.
  • Retry gave up early. Retrying stuck checkouts stopped at the first failure.
  • Unhelpful states.
    • Leftover worktrees showed "Needs recovery … run bb environment cleanup … --recover".
    • Archived threads whose checkout another thread still used failed to clear with no explanation.
    • Worktrees created outside BB were sorted by size, which doesn't help decide what's safe to delete.
    • Each worktree had to be removed one row at a time.
  • Kept checkouts reported as errors. When Clean up kept a checkout on purpose because it had uncommitted changes, it showed an error toast: "Couldn't clean up checkouts · 1 checkout kept: Kept because …".

What changed

All changes are in plugins/storage-retention. No core or Plugin SDK changes.

  • Row cleanups follow realtime updates instead of polling.
    • The server lists running and recently finished row cleanups (kept for 10 minutes) in the hosts response, and publishes changed on every transition.
    • The page waits by watching that list and refreshes when the realtime connection comes back.
    • Rows are marked busy from the list, so a cleanup started before a reload or on another device still shows as busy.
    • The entryCleanup RPC and its retry are removed.
  • Bulk cleanup.
    • Worktrees created outside BB: a "Not used in" filter (1 week, 2 weeks, 30 days, 90 days, any time). "Last used" is the latest of git metadata times, the mtimes of changed files, and activity in any thread using the worktree. One confirmation removes everything matching, with "Remove all N" or "Keep the K with changes".
    • Development instances: a "Remove all" button per group, plus an "Archived threads" group.
    • Leftover worktrees: one "Clean up" button.
    • Per-row actions still work.
    • CLI: bb storage cleanup --kind worktrees --include-uncommitted and --kind externalWorktrees --unused-for <days>.
  • Automatic recovery. Every hour, leftover checkouts whose teardown was interrupted are recovered if nothing is running in them and they have no uncommitted changes. Each row explains in plain words why a checkout was kept.
  • Kept is not failed. Clean up results now separate kept checkouts from real failures. It's an error only when nothing was removed or kept and something actually failed.
  • Backfill. Turning on "Delete thread storage on archive" now also clears threads that were archived before, and the hourly run does the same.
  • Explanations.
    • Archived threads whose checkout is still used by another thread say which thread, and "Clear files" is disabled.
    • Worktrees left behind by removed BB environments are labelled as such.
  • Toast and total fixes.
    • Only scans the user requested produce toasts.
    • An unmeasured deletion makes the freed total unknown instead of counting as 0.
    • Single worktree removal is disabled while a bulk removal runs.
    • Trash with read-only directories is deleted after making entries owner-writable. Symbolic links are never followed.
    • Retrying stuck checkouts continues past failures.

On macOS, interrupted checkouts are kept with "BB can't check for running programs yet; update BB on this machine" until #7520 ships. That PR adds the macOS process check that recovery needs.

How you verified

  • pnpm exec turbo run typecheck test lint --filter=bb-plugin-bb--storage-retention: 103 tests pass and lint is clean. New tests cover:
    • src/leftover-cleanup.test.ts: a kept checkout finishes normally, and only all-failed retries fail.
    • app.test.tsx:
      • a completion notice lost while disconnected is picked up on reconnect; the test fails with the reconnect refresh removed
      • a row cleanup started before the page loaded shows as busy
      • bulk removal by staleness, optionally keeping worktrees with changes
      • removing a whole development group
      • unsolicited scans not toasting
      • unknown freed space in combined toasts
      • single removal disabled during a bulk removal
    • src/storage.test.ts:
      • row cleanup state and change notices in the hosts response
      • interrupted checkout recovery and the explanations for kept ones
      • the archived-storage backfill
    • src/git-worktrees.test.ts: the last-used date and checkout inspection.
    • src/remove-tree.test.ts: read-only trash, and symbolic links left untouched.
  • Manual checks in an isolated dev instance (pnpm start:worktree) with sample worktrees aged from 2 to 200 days, on desktop, tablet (820 px) and phone (390 px) widths:
    • Staleness bulk removal: "Keep the 2 with changes" removed 3 and reported "Removed 3 worktrees · 1.9 GB freed".
    • The explanation on a checkout that's still in use.
    • Clean up on a checkout with 3 uncommitted changes showed a normal completion toast, kept the checkout on disk, and the row read "Kept because it has 3 uncommitted changes".
    • Row "Clear files" finished with a success toast and no polling.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 4 commits November 13, 2026 09:51
…tuck trash

- Keep polling a row cleanup through brief network drops instead of
  reporting "Failed to fetch" for work that completes.
- Toast scan completion and failure only for scans started from the page;
  hourly and reconnect scans stay silent.
- Omit freed space from a combined toast when any completion has no size,
  instead of showing "0 B freed".
- Disable single worktree removal while a bulk worktree removal runs.
- Make owner-read-only directories writable before deleting trash, so
  removed storage no longer reappears as orphaned on the next scan.
- Retry every leftover worktree even when one needs manual recovery, and
  report the ones that could not be retried.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y, plain explanations

- Date worktrees created outside BB by when they were last used (latest
  git activity, uncommitted edits, or linked thread activity) and remove
  everything unused for a chosen period in one confirmation, optionally
  keeping ones with uncommitted changes. Row-by-row removal stays.
- Remove a whole group of development instances at once, with instances
  whose threads are all archived in their own group.
- Recover leftover checkouts whose cleanup was interrupted, hourly and from
  Clean up, when they have no uncommitted changes; one confirmation removes
  the rest. Each row says in plain words why it is kept.
- Turning on "Delete thread storage on archive" also clears threads that
  were already archived, and scans keep that up to date.
- Explain when an archived thread can't be cleared because another thread
  still uses a checkout inside its storage, label worktrees left over from
  removed BB environments, and count every leftover checkout per project.
- Report freed space for orphan, development and worktree cleanups, and
  treat worktrees that are already gone as removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and stop reporting kept checkouts as failures

Single-row cleanups (clearing a thread's files, removing a development
instance) were followed by polling a per-cleanup status RPC every 500 ms.
A dropped bb Connect tunnel made a poll fail, which the page reported as
"Failed to fetch" even though the cleanup kept running, and a reload lost
track of the cleanup entirely.

The server now lists in-flight and recently finished row cleanups in the
storage list response and publishes a change notice on every transition.
The page waits for a cleanup by watching that list, refreshes after the
realtime connection comes back, and marks rows busy from the list, so a
cleanup started before a reload or on another device still shows as busy.
The polling RPC and its transient-failure retry are removed.

Clean up on leftover worktrees reported checkouts it deliberately kept
(uncommitted changes, programs still running) as an error toast reading
"1 checkout kept: Kept because ...". Kept checkouts are now reported
separately from real failures and the cleanup finishes normally.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…f on Windows

On Windows, chmod only toggles the read-only bit, so a directory chmodded
to 0o555 reads back as 0o444 and the fixed expectation failed. The test
now records the outside directory's mode before removing the tree and
checks it is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 998880b into main Nov 13, 2026
32 checks passed
@ymichael
ymichael deleted the bb/fix-storage-retention-plugin-errors-thr_qjiray8j3c branch November 13, 2026 18:53
ymichael added a commit that referenced this pull request Nov 13, 2026
## Human comments

## What was wrong

`bb environment cleanup <id> --recover`, and the storage plugin's
automatic recovery, refused on every non-Linux machine. The daemon's
check for programs still running in a workspace
(`inspectWorkspaceProcesses`) only read `/proc`. On macOS it returned
`supported: false`, so the server refused with "process inspection
requires Linux". Worktrees whose teardown was interrupted stayed in
"Needs recovery" on Macs, and nothing in the UI or CLI could remove
them.

## What changed

- `apps/host-daemon/src/workspace-processes.ts`: on darwin, finds
same-user processes with `/bin/ps`, then reads their cwd, txt and mapped
files with `/usr/sbin/lsof`.
- The workspace path is canonicalized with `realpath` and matched
against lsof's escaped names.
- Same-user processes that lsof can't read are reported as unreadable,
which refuses recovery, unless they exited or run under another
effective UID.
- A missing, failing or timed-out `ps`/`lsof` returns `supported:
false`, as before.
- The result shape and the wire contract are unchanged, so
`HOST_DAEMON_PROTOCOL_VERSION` isn't bumped. An older daemon on macOS
still reports `supported: false` and the server still refuses.
- The server refusal now reads "process inspection is unavailable on
this host (supported on Linux and macOS)", and the `--recover` help text
matches.
- Docs: `bb-guide-environments.md` and
`tests/qa/scripts/environment-recovery.md`.

**Decision for review: the effective-UID exception.** Every Terminal tab
keeps a root-owned `login` process whose cwd lsof can't read. Without
the exception, recovery would always be refused while Terminal is open.
The cost is that a `sudo` process running inside the workspace isn't
detected, though the shell that started it still is.

This is independent of #7521, which makes the storage plugin's
leftover-worktree cleanup use this recovery path. Without this change,
that cleanup keeps Mac checkouts and tells the user to update BB.

## How you verified

- `workspace-processes-darwin.test.ts` (new) runs real `ps`/`lsof`
against spawned processes. It covers:
- processes whose cwd or mapped executable is inside the canonicalized
workspace
  - an unused workspace
  - lsof-escaped names, without matching sibling paths
- refusing rather than reporting a clean workspace when lsof is
unavailable
- live unreadable same-user processes, while ignoring exited, zombie and
other-user setuid processes

These tests are skipped on non-darwin runners, so Linux CI won't run
them. They pass locally on macOS.
- `pnpm exec turbo run typecheck test --filter=@bb/host-daemon
--filter=@bb/cli`: 874 daemon tests and 937 CLI tests pass.
- Manual check in an isolated dev instance on macOS, with an environment
forced into "Needs recovery":
- With a Codex session still running in the workspace, `--recover` was
refused with "workspace processes remain".
- After `bb thread stop`, recovery removed the checkout and the
environment was destroyed.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant