Repository navigation
Skip git status on bb-owned worktrees during storage scans - #7072
Merged
ymichael merged 2 commits intoNov 5, 2026
Merged
Conversation
The storage scan asked the host to run git status on every linked worktree, then discarded the bb-owned ones on the server. The server now sends the owned paths with listGitWorktrees, and the host leaves those worktrees out before inspecting any. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The host bundle's plugin SDK runtime does not export the host-path comparison helper, so the bundled build failed. The host now skips worktrees whose normalized path equals an owned path, and the server keeps its comparison-key filter as the authority. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
deleted the
bb/storage-scan-inspects-owned-worktrees-thr_2sv3xu3229
branch
November 5, 2026 00:50
ymichael
added a commit
that referenced
this pull request
Nov 5, 2026
…folders out of it (#7099) ## Human comments ## What was wrong Follow-up to #7072 for finding #14 of the Nov 4 hot-path overreach audit. The storage-retention plugin read **every thread** (`threads.list` paged 500 at a time, about 4,500 rows here) in three places: - the report behind the `hosts` / `host` RPCs (rendered on every Storage page refresh), - `startScan`, - `retryWorktreeCleanup`. The threads it actually uses are a much smaller set: - threads named by the machine's thread-storage entries (and large files), - threads in *retired* environments, used to decide whether a leftover worktree is still occupied, - threads linked to external worktrees and `.bb-dev` entries, found by environment path or by a thread ID encoded in the path. Most of those storage entries are **empty folders**. On this machine 1,546 of the 1,844 folders in `~/.bb/thread-storage` hold nothing. 1,157 were emptied when their thread was archived: "Delete storage on archive" discards the contents and recreates the folder (`recreate: true`). Another 350 were created at thread start and never written to. On the other two machines, 118 of 122 and 20 of 43 entries are 0 bytes. The report still looked up each of their threads, and it counted the folders as storage: 1,844 "threads with storage" here, where only 298 hold anything. On this machine the live server logged `POST /api/v1/plugins/bb--storage-retention/rpc/hosts` at **1,821 ms**. Its 500-row `GET /api/v1/threads` pages take a median of 174 ms each (32 slow-request samples in `server.19x/20x.log`). ## What changed `plugins/storage-retention/src/storage.ts`, `sdk-data.ts`: - **Report:** reads storage-entry, large-file and path-encoded threads by ID with `threads.list({ threadIds })` (existing 200-ID chunks). It reads threads in retired environments and in environments matching linked paths with `threads.list({ environmentId })`, one indexed call per environment. Everything downstream (classification, leftover detection, linked threads) runs unchanged over that set. - **Scan and cleanup retry:** read only threads in retired environments (`readLeftoverEnvironments`). They don't page any more. - The environment-only half of `leftoverEnvironments` became `retiredEnvironments`. `linkedThreads` takes a precomputed `threadLink` (environment IDs plus encoded thread ID). With the thread list gone, the report context shared across hosts now only shares `projects.list`. **Empty folders** (second commit): - The host scan (`disk-usage.ts`) flags each per-child directory that has no entries (`empty`), with one `opendir` + `read` per folder. It's a real emptiness check, not "0 bytes", because `du` reports an empty folder as 4 KiB on Linux. - The report, **Remove orphans** and **Clear archived files** skip empty entries. They aren't looked up, counted, offered for clearing, or labelled orphans. The cleanup actions' counts now match what the report shows. - Empty entries stay in the cached scan. Archive cleanup uses the scan to find the machine for a thread without an environment (`clearThreadStorage`). Dropping them would send those threads into its hourly "location unavailable" retry; 187 such threads have empty folders here. - Scans cached before this change have no `empty` flag. They read as non-empty, so the report is unchanged until the machine is rescanned. - `linkedThreads` sorts newest first (`createdAt`, then `id`) instead of following read order. That's the order main produced for this data, and it no longer changes with which read returned a thread. - The host contract's `measure` children gain a required `empty` field. The host worker ships in the same plugin bundle, as with #7072's `ownedPaths`. **No SDK change.** `threads.list` already has both targeted filters (`threadIds`, `environmentId`). The per-environment calls cover only retired worktrees awaiting teardown plus linked paths: 2 + a few on this machine, each an indexed `threads_environment_archived_deleted_idx` lookup. An `environmentIds` list filter would save a handful of sub-millisecond calls, which isn't worth an SDK bump. ## How you verified **Benchmark, real data.** I ran the plugin's actual `createStorage(...).hosts()` code from `origin/main`, from this PR's first commit (54d913c), and from the PR head. The fake SDK's `threads.list` / `environments.list` / `projects.list` call the server's own route functions (`listThreadsWithPendingInteractionState` + `toThreadListEntryResponses`, `listEnvironments` + `toEnvironmentResponses`) against the live `~/.bb/bb.db` **opened read-only**. Each response gets a JSON round trip. For input, I rescanned this machine's real `~/.bb/thread-storage` (32 GB, 1,844 folders) with each version's own `measureDiskUsage`. That's read-only: `du` plus one directory read per folder. The other two machines use the plugin's cached scans (122 / 43 entries), which have no `empty` flag. Medians of 9 runs, 3 alternating rounds: | `hosts()` | main | first commit | PR head | |---|---|---|---| | total | 197.9 / 200.6 / 197.0 ms | 71.2 / 71.9 / 70.8 ms | **25.4 / 25.8 / 25.5 ms** | | inside `threads.list` | 185 ms | 63 ms | 20 ms | | `threads.list` calls | 10 (full paging) | 24 | 16 | | thread rows served | 4,521 | 2,023 | **485** | | scan/retry thread read (2 retired envs) | 176 ms, 4,513 rows | 0.6 ms, 2 rows | 0.6 ms, 2 rows | Of the 485 rows, 298 are this machine's non-empty folders and 165 are the other two machines' cached entries. The rest are threads in retired or linked environments. **Scan time is unchanged:** 10.1 / 9.7 s for main's scanner and 9.9 / 10.3 s for the PR's over the same folder. The emptiness check is lost in `du`'s noise. Both find 1,844 entries, and the PR flags 1,546 empty, exactly the folders `du` measures at 0 bytes on this Mac. **Reports:** - main and the first commit: byte-identical for all 3 machines. - main and the PR head on a scan cached before this change (no `empty` flag): byte-identical. - main and the PR head on the fresh scan: the other two machines are byte-identical. On this machine every byte total is identical (active, archived, orphan, leftover, hidden, clearable, large files), and so are `largestThreads`, leftover and external worktrees, developer storage, and linked threads. Only the counts that included empty folders change: | this machine | main | PR head | |---|---|---| | threads with storage | 1,844 | 298 | | archived threads with storage | 1,805 | 271 | | archived files to clear (threads) | 1,651 | 209 | | hidden threads, active / archived | 4 / 20 | 0 / 0 | The harness has no HTTP hop and the server isn't under load, so absolute times are lower than live. Thread rows served for the report drop 89% (4,521 → 485). On the live server that should take the logged ~1.8 s to a few hundred milliseconds. That's an estimate; I didn't measure it on the live server. **Tests** (`pnpm exec turbo run test typecheck lint --filter=bb-plugin-bb--storage-retention`, 88 passing): - I replaced the test that pinned the old "one full thread listing for every host" design. In the new test, `threads.list` **throws on an unfiltered call** and honours the `threadIds` / `environmentId` filters. Real temp folders back the storage root: a folder with a file, an empty folder for an archived thread, and an empty folder with no thread. It checks four things: - the report counts only the non-empty folder; - neither empty folder is counted as archived or orphan; - `threadIds` lookups ask for only `thr_kept`; - a retired worktree held by an unarchived thread, which only the environment read finds, isn't reported as leftover. - In "never clears thread storage that holds a workspace", the archived thread already cleared on archive no longer shows as "archived files to clear". **Clear archived files** now reports 0 cleared, matching the report. - `disk-usage.test.ts` adds an empty folder to the per-child test, which runs against real `du`, `du` with one child per batch, and the walker. It expects `empty: true` for that folder and `empty: false` for folders and files with content. - **Mutation checks:** - `readThreadsInEnvironments` reading nothing fails the new test (`env_busy` reported). - Dropping the report's empty filter fails 2 tests. - Dropping the filter in `discard` fails the Clear-archived-files assertion. - I dropped the `threads.list` call-count assertion from the reconnect-rescan test. `scannedHostIds` already asserts that only the reconnected machine is rescanned. **Left out** - The ~300 non-empty storage-entry reads stay. The report classifies every non-empty entry (active, archived, hidden, clearable, orphan), so it needs each of those threads. - Empty folders aren't deleted. They take almost no space, and "Delete storage on archive" still recreates the folder on purpose. Whether it should stop recreating it is a separate question. - The other two machines' counts update on their next scan. - `readEnvironments` still pages environments per host. That route already leaves out destroyed environments (~60 rows here, 1.5 ms). - The retention-policy sweep (`service.ts` `candidates`) still pages every thread. It selects threads by age across all projects, which is a different query and not this finding. > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[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
A storage scan finds worktrees created outside bb. To do that, the server called the host's
listGitWorktreeswith only the repository paths. The host rangit status(plusrev-listfor detached HEADs) on every linked worktree, and the server then threw away the ones bb owns: live environment paths and project sources. On~/Projects/bbthat's 127git statusruns per scan, and 42 of them were for worktrees the caller ignored. This is finding #14 from the Nov 4 hot-path overreach audit. Two parts of that finding didn't hold or are out of scope here:GIT_OPTIONAL_LOCKS=0.hoststhread paging is a separate mechanism and is not touched.What changed
plugins/storage-retention/src/host-contract.ts:listGitWorktreesinput gainsownedPaths.plugins/storage-retention/src/git-worktrees.ts: the host skips any linked worktree whose normalized path exactly equals an owned path, before inspecting it. The host bundle's SDK runtime has no host-path comparison helper, and adding one would change plugin-build/SDK, so the host uses exact matching. An exact match can only skip paths the server also treats as owned.plugins/storage-retention/src/storage.ts: the server sends its owned path list and keeps its comparison-keyownedByBbfilter as the authority. A textual mismatch (different case, for example) costs only the old extragit status; it can never hide an external worktree.Users see the same external-worktree list; scans just do less work. This RPC is plugin-internal, and the server builds and ships the host bundle alongside it, so there is no
HOST_DAEMON_PROTOCOL_VERSIONbump and no plugin SDK change.How you verified
Real data. I ran
listGitWorktreesfrom main and from this branch directly against~/Projects/bb(4.4k branches, 128 worktrees), interleaved over 5 pairs. The owned paths came from the live~/.bb/bb.db(opened read-only; 61 environment and source paths, 42 of them worktrees of this repo). Agitshim on PATH counted spawns. Load average was 26–55 throughout (other threads' benchmarks were running), so treat wall times as noisy and spawn counts as exact.git status/ scanrev-list/ scanIn every pair, both versions produced the same set of external worktrees.
I re-ran 3 more interleaved pairs after the exact-match revision, at a quieter load of 4–10.
git statusdropped from 127 to 85 per scan (the same 42 owned worktrees skipped), the external sets were identical, and wall time went from 13195/13293/12592 ms to 8986/9530/9082 ms (median 13.2 s → 9.1 s).Tests. The existing real-git scan test in
storage.test.tsnow also checks that the host returns only the four external worktrees and never the bb-ownedmanagedone. With the host filter disabled it fails (expected [ …(5) ] to deeply equal [ …(4) ]).pnpm exec turbo run prepare:bundled typecheck lint --filter=bb-plugin-bb--storage-retention: pass (prepare:bundledis the host-bundle build that failed in CI on the first revision)vitest run src/storage.test.ts: 33/33 passCI runs the full suites.
🤖 Generated with Claude Code