Repository navigation
Support environment cleanup recovery on macOS - #7520
Merged
Merged
Conversation
`bb environment cleanup <id> --recover` refused on every non-Linux host because the daemon's workspace process guard only read /proc. On macOS, environments stuck in "Needs recovery" could never be cleaned up. The daemon now inspects same-user processes on darwin with /bin/ps and /usr/sbin/lsof (cwd and txt descriptors, which include mmapped files), canonicalizes the workspace with realpath, and matches it against lsof's escaped names. Same-user processes lsof cannot read are reported as unreadable unless they have exited or run under another effective UID (setuid processes such as Terminal's login). A missing, failing, or timed-out ps/lsof returns supported: false. The result shape and wire contract are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Nov 13, 2026
…, plain explanations (#7521) ## 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](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael
added a commit
that referenced
this pull request
Nov 30, 2026
…7854) ## Human comments ## What was wrong `bb environment cleanup <id> --recover`, and the storage plugin's automatic recovery, refuse while any same-user process cannot be inspected ([environment-engine.ts#L1145-L1150](https://github.com/get-bb/bb/blob/e6f3032d1898b1404f1facf4baaf75da69e28da3/apps/server/src/services/environments/environment-engine.ts#L1145-L1150)). The Linux scan treats a process as same-user when any of its four UIDs matches. It also counts every `cwd` or `maps` read error as unreadable ([workspace-processes.ts#L53-L98](https://github.com/get-bb/bb/blob/e6f3032d1898b1404f1facf4baaf75da69e28da3/apps/host-daemon/src/workspace-processes.ts#L53-L98)). Every Linux login session has such processes. `systemd --user`, `(sd-pam)`, `sshd-session` and `gpg-agent` are non-dumpable, so their `cwd` cannot be read without privileges. Setuid helpers such as `fusermount3` match through their real UID. On this machine (Fedora, one SSH session) recovery is refused for 6 such processes with nothing in the workspace, so it can never pass (#7435). A second gap in the same scan: a process in another mount namespace, such as a rootless container, reports its `cwd` and mapped files as paths inside that namespace. The scan compared them with the host workspace path, so a container that bind-mounts the workspace was not found. ## What changed `apps/host-daemon/src/workspace-processes.ts`, Linux only: - **Another effective UID:** an unreadable process running under another effective UID counts as another user's process, as on macOS since #7520 (`fusermount3`, a `sudo` process). - **Own mount namespace:** an unreadable process in the daemon's own mount namespace is not a blocker. A process shares the namespace when the ID of its root mount equals the daemon's. Its `mountinfo` is readable even when its `cwd` is not. - **Another mount namespace:** the process is checked through its own `mountinfo`. - Paths are resolved to a filesystem device and a path within that filesystem, then compared with the workspace's location. - A readable process blocks when its `cwd` or a mapped file resolves into the workspace. - An unreadable process blocks when one of its mounts exposes the workspace, a directory inside it, or a parent directory. - **Fallback:** if the daemon's own namespace link or `mountinfo` cannot be read or parsed, the previous rules apply. - `bb-guide-environments.md` describes the Linux rules and the gap below. No wire change: the result shape and its meaning ("processes that may be using the workspace" and "processes that could not be ruled out") are unchanged, so `HOST_DAEMON_PROTOCOL_VERSION` is not bumped. The server is unchanged. **Decision for review: the same-namespace exception.** A non-dumpable program running inside the workspace in the daemon's own mount namespace is not detected. One example is `ssh-agent -D` started from a shell there; the shell that started it is still detected. This is the Linux counterpart of the `sudo` gap accepted for macOS in #7520. Without the exception, recovery cannot pass on any Linux machine with a login session. Not changed: the issue's nested "Needs recovery: Needs recovery: ..." message. On current main I found no path that re-wraps a stored "Needs recovery" message. Every write that moves an exhausted environment out of `failed` needs states that do not occur together, or resets the retry budget first, as `--recover` does. The issue was seen on 0.48.1, before #7725 reworked the teardown retry schedule. Its 46,759-attempt count is also bounded since #7725. ## How you verified - New real-process tests in `apps/host-daemon/src/workspace-processes-linux.test.ts`: - "does not refuse for a non-dumpable process in the daemon's own mount namespace" (`prctl(PR_SET_DUMPABLE, 0)` through python3). **On main:** the PID is in `unreadablePids`. - "finds a process in another mount namespace that works in the workspace through a bind mount" (`unshare -Urm`, `mount --bind`, `cd` into the bind target). **On main:** not found. - "ignores a process in another mount namespace that does not work in the workspace": passes on main and here. - The `unshare` cases skip where unprivileged user namespaces are blocked, which may include GitHub's Ubuntu runners. - New mocked cases in `workspace-processes.test.ts`, both failing on main: - unreadable processes with realistic `mountinfo`: same namespace, a container bind-mounting the workspace, one bind-mounting a parent, one that does not reach it, and an unreadable `mountinfo`; - an unreadable process under another effective UID. - The existing Linux tests are unchanged and pass. `pnpm exec turbo run test --filter=@bb/host-daemon` passes (890 tests). `typecheck lint lint:type-aware` pass for `@bb/host-daemon`, and `@bb/templates` tests pass. - Live on this machine (Fedora 44, xfs, SELinux enforcing, one SSH session), calling the real `inspectWorkspaceProcesses` on a real bb worktree: | Situation | main | This branch | |---|---|---| | Nothing in the workspace | `unreadablePids` with 6 PIDs: systemd, sd-pam, sshd-session, gpg-agent and 2 rootful container processes | `{ pids: [], unreadablePids: [] }` | | A process with its cwd in the workspace | found | found | | Rootless podman container with `-v <workspace>:/app -w /app` | `pids: []` (missed) | container PID in `pids` | | Container without that mount | ignored | ignored | - Not run: the full `tests/qa/scripts/environment-recovery.py` flow, which needs the host-care plugin. Fixes #7435 Fixes #8328 > AGENT GENERATED --------- Co-authored-by: Michael Yong <[email protected]>
scowalt
pushed a commit
to scowalt/bb
that referenced
this pull request
Dec 1, 2026
…et-bb#7854) ## Human comments ## What was wrong `bb environment cleanup <id> --recover`, and the storage plugin's automatic recovery, refuse while any same-user process cannot be inspected ([environment-engine.ts#L1145-L1150](https://github.com/get-bb/bb/blob/c113593252d5bdd55b39718a1cadaa6fa09aaac3/apps/server/src/services/environments/environment-engine.ts#L1145-L1150)). The Linux scan treats a process as same-user when any of its four UIDs matches. It also counts every `cwd` or `maps` read error as unreadable ([workspace-processes.ts#L53-L98](https://github.com/get-bb/bb/blob/c113593252d5bdd55b39718a1cadaa6fa09aaac3/apps/host-daemon/src/workspace-processes.ts#L53-L98)). Every Linux login session has such processes. `systemd --user`, `(sd-pam)`, `sshd-session` and `gpg-agent` are non-dumpable, so their `cwd` cannot be read without privileges. Setuid helpers such as `fusermount3` match through their real UID. On this machine (Fedora, one SSH session) recovery is refused for 6 such processes with nothing in the workspace, so it can never pass (get-bb#7435). A second gap in the same scan: a process in another mount namespace, such as a rootless container, reports its `cwd` and mapped files as paths inside that namespace. The scan compared them with the host workspace path, so a container that bind-mounts the workspace was not found. ## What changed `apps/host-daemon/src/workspace-processes.ts`, Linux only: - **Another effective UID:** an unreadable process running under another effective UID counts as another user's process, as on macOS since get-bb#7520 (`fusermount3`, a `sudo` process). - **Own mount namespace:** an unreadable process in the daemon's own mount namespace is not a blocker. A process shares the namespace when the ID of its root mount equals the daemon's. Its `mountinfo` is readable even when its `cwd` is not. - **Another mount namespace:** the process is checked through its own `mountinfo`. - Paths are resolved to a filesystem device and a path within that filesystem, then compared with the workspace's location. - A readable process blocks when its `cwd` or a mapped file resolves into the workspace. - An unreadable process blocks when one of its mounts exposes the workspace, a directory inside it, or a parent directory. - **Fallback:** if the daemon's own namespace link or `mountinfo` cannot be read or parsed, the previous rules apply. - `bb-guide-environments.md` describes the Linux rules and the gap below. No wire change: the result shape and its meaning ("processes that may be using the workspace" and "processes that could not be ruled out") are unchanged, so `HOST_DAEMON_PROTOCOL_VERSION` is not bumped. The server is unchanged. **Decision for review: the same-namespace exception.** A non-dumpable program running inside the workspace in the daemon's own mount namespace is not detected. One example is `ssh-agent -D` started from a shell there; the shell that started it is still detected. This is the Linux counterpart of the `sudo` gap accepted for macOS in get-bb#7520. Without the exception, recovery cannot pass on any Linux machine with a login session. Not changed: the issue's nested "Needs recovery: Needs recovery: ..." message. On current main I found no path that re-wraps a stored "Needs recovery" message. Every write that moves an exhausted environment out of `failed` needs states that do not occur together, or resets the retry budget first, as `--recover` does. The issue was seen on 0.48.1, before get-bb#7725 reworked the teardown retry schedule. Its 46,759-attempt count is also bounded since get-bb#7725. ## How you verified - New real-process tests in `apps/host-daemon/src/workspace-processes-linux.test.ts`: - "does not refuse for a non-dumpable process in the daemon's own mount namespace" (`prctl(PR_SET_DUMPABLE, 0)` through python3). **On main:** the PID is in `unreadablePids`. - "finds a process in another mount namespace that works in the workspace through a bind mount" (`unshare -Urm`, `mount --bind`, `cd` into the bind target). **On main:** not found. - "ignores a process in another mount namespace that does not work in the workspace": passes on main and here. - The `unshare` cases skip where unprivileged user namespaces are blocked, which may include GitHub's Ubuntu runners. - New mocked cases in `workspace-processes.test.ts`, both failing on main: - unreadable processes with realistic `mountinfo`: same namespace, a container bind-mounting the workspace, one bind-mounting a parent, one that does not reach it, and an unreadable `mountinfo`; - an unreadable process under another effective UID. - The existing Linux tests are unchanged and pass. `pnpm exec turbo run test --filter=@bb/host-daemon` passes (890 tests). `typecheck lint lint:type-aware` pass for `@bb/host-daemon`, and `@bb/templates` tests pass. - Live on this machine (Fedora 44, xfs, SELinux enforcing, one SSH session), calling the real `inspectWorkspaceProcesses` on a real bb worktree: | Situation | main | This branch | |---|---|---| | Nothing in the workspace | `unreadablePids` with 6 PIDs: systemd, sd-pam, sshd-session, gpg-agent and 2 rootful container processes | `{ pids: [], unreadablePids: [] }` | | A process with its cwd in the workspace | found | found | | Rootless podman container with `-v <workspace>:/app -w /app` | `pids: []` (missed) | container PID in `pids` | | Container without that mount | ignored | ignored | - Not run: the full `tests/qa/scripts/environment-recovery.py` flow, which needs the host-care plugin. Fixes get-bb#7435 Fixes get-bb#8328 > AGENT GENERATED --------- Co-authored-by: Michael Yong <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 returnedsupported: 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.realpathand matched against lsof's escaped names.ps/lsofreturnssupported: false, as before.HOST_DAEMON_PROTOCOL_VERSIONisn't bumped. An older daemon on macOS still reportssupported: falseand the server still refuses.--recoverhelp text matches.bb-guide-environments.mdandtests/qa/scripts/environment-recovery.md.Decision for review: the effective-UID exception. Every Terminal tab keeps a root-owned
loginprocess whose cwd lsof can't read. Without the exception, recovery would always be refused while Terminal is open. The cost is that asudoprocess 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 realps/lsofagainst spawned processes. It covers: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":
--recoverwas refused with "workspace processes remain".bb thread stop, recovery removed the checkout and the environment was destroyed.🤖 Generated with Claude Code