Repository navigation
Read only the threads a storage report needs, and leave empty thread folders out of it - #7099
Merged
ymichael merged 2 commits intoNov 5, 2026
Conversation
The storage-retention report, scan and worktree-cleanup retry paged through every thread (about 4,500 here) to classify storage entries and find which retired worktrees are still occupied. Reports now read storage-entry threads by ID and threads in retired or linked environments by environment; scans and retries read only threads in retired environments. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The scan now records whether each thread-storage folder is empty. The report, Remove orphans and Clear archived files skip empty folders, so they are not counted, looked up, or treated as orphans. Empty entries stay in the cached scan because archive cleanup uses it to find the machine for a thread without an environment. Linked threads are sorted newest first so their order no longer depends on which read returned them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/follow-up-14-storage-scan-reads-only-needed-envi-thr_yygu5b55r6
branch
from
November 5, 2026 18:11
40397a9 to
95d9422
Compare
ymichael
deleted the
bb/follow-up-14-storage-scan-reads-only-needed-envi-thr_yygu5b55r6
branch
November 5, 2026 18:16
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
Follow-up to #7072 for finding #14 of the Nov 4 hot-path overreach audit. The storage-retention plugin read every thread (
threads.listpaged 500 at a time, about 4,500 rows here) in three places:hosts/hostRPCs (rendered on every Storage page refresh),startScan,retryWorktreeCleanup.The threads it actually uses are a much smaller set:
.bb-deventries, 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-storagehold 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/hostsat 1,821 ms. Its 500-rowGET /api/v1/threadspages take a median of 174 ms each (32 slow-request samples inserver.19x/20x.log).What changed
plugins/storage-retention/src/storage.ts,sdk-data.ts:threads.list({ threadIds })(existing 200-ID chunks). It reads threads in retired environments and in environments matching linked paths withthreads.list({ environmentId }), one indexed call per environment. Everything downstream (classification, leftover detection, linked threads) runs unchanged over that set.readLeftoverEnvironments). They don't page any more.leftoverEnvironmentsbecameretiredEnvironments.linkedThreadstakes a precomputedthreadLink(environment IDs plus encoded thread ID). With the thread list gone, the report context shared across hosts now only sharesprojects.list.Empty folders (second commit):
disk-usage.ts) flags each per-child directory that has no entries (empty), with oneopendir+readper folder. It's a real emptiness check, not "0 bytes", becausedureports an empty folder as 4 KiB on Linux.clearThreadStorage). Dropping them would send those threads into its hourly "location unavailable" retry; 187 such threads have empty folders here.emptyflag. They read as non-empty, so the report is unchanged until the machine is rescanned.linkedThreadssorts newest first (createdAt, thenid) 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.measurechildren gain a requiredemptyfield. The host worker ships in the same plugin bundle, as with Skip git status on bb-owned worktrees during storage scans #7072'sownedPaths.No SDK change.
threads.listalready 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 indexedthreads_environment_archived_deleted_idxlookup. AnenvironmentIdslist 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 fromorigin/main, from this PR's first commit (54d913c), and from the PR head. The fake SDK'sthreads.list/environments.list/projects.listcall the server's own route functions (listThreadsWithPendingInteractionState+toThreadListEntryResponses,listEnvironments+toEnvironmentResponses) against the live~/.bb/bb.dbopened 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 ownmeasureDiskUsage. That's read-only:duplus one directory read per folder. The other two machines use the plugin's cached scans (122 / 43 entries), which have noemptyflag. Medians of 9 runs, 3 alternating rounds:hosts()threads.listthreads.listcallsOf 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 foldersdumeasures at 0 bytes on this Mac.Reports:
emptyflag): byte-identical.largestThreads, leftover and external worktrees, developer storage, and linked threads. Only the counts that included empty folders change: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):threads.listthrows on an unfiltered call and honours thethreadIds/environmentIdfilters. 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:threadIdslookups ask for onlythr_kept;disk-usage.test.tsadds an empty folder to the per-child test, which runs against realdu,duwith one child per batch, and the walker. It expectsempty: truefor that folder andempty: falsefor folders and files with content.readThreadsInEnvironmentsreading nothing fails the new test (env_busyreported).discardfails the Clear-archived-files assertion.threads.listcall-count assertion from the reconnect-rescan test.scannedHostIdsalready asserts that only the reconnected machine is rescanned.Left out
readEnvironmentsstill pages environments per host. That route already leaves out destroyed environments (~60 rows here, 1.5 ms).service.tscandidates) still pages every thread. It selects threads by age across all projects, which is a different query and not this finding.🤖 Generated with Claude Code