Skip to content

Bound thread-storage listings and notify only the changed thread - #7089

Merged
ymichael merged 2 commits into
mainfrom
bb/thread-storage-change-per-thread-thr_wxx5y5j3nk
Nov 5, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/thread-storage-change-per-thread-thr_wxx5y5j3nk

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Perf audit finding #10 (thread-storage file list). Two separate costs:

1. Every unqueried listing walked the whole tree. The thread-storage panel asks for the first 1,000 files with no query (apps/server/src/routes/threads/data.ts → host.list_files, limit: 1000). On the daemon, listHostFiles/listHostPaths always called listWorkspacePaths, which walks the whole tree (11–17k files in real thread-storage dirs). finalizeListedPaths then keeps only limit + 1 entries. The full listing is cached only when the walk took ≥500 ms, so most refetches while an agent writes pay for the full walk again.

2. Every storage write refetched the lists of every thread in the environment. When a file changed in a thread's storage, the daemon sent environment-change with change: "thread-storage-changed" and only the environment id (apps/host-daemon/src/app.ts), although its watcher already knew the thread. The server broadcast that to every connected client. The app then refetched the storage queries of every cached thread in that environment (dirtyThreadStorageQueriesForEnvironment). So one agent writing reports or screenshots made every open thread in the same environment re-list its own storage, on every client.

What changed

Bounded unqueried listings (daemon only). When a listing has no query, the walk stops once it has limit + 1 paths. The walk is depth-first in readdir order, so the bounded walk returns the same prefix in the same order as the full one, and truncated is unchanged. Queried listings still walk, cache and rank the full tree.

The change is daemon-local: there are no wire changes, no HOST_DAEMON_PROTOCOL_VERSION bump and no plugin SDK change. The same path serves every unqueried host.list_files/host.list_paths caller (workspace, project, environment, skills), and each gets identical results.

The one part of finding #10 left out is always caching the full listing with watcher invalidation.

Thread-targeted storage notifications.

  • Daemon (HOST_DAEMON_PROTOCOL_VERSION 261 → 262): sends a new thread-storage-change { threadId } message. thread-storage-changed is removed from the daemon's environment-change kinds. The thread-storage watch target drops environmentId, because this message was its only use.
  • Server: notifyDaemonThreadStorageChange checks that the thread's environment belongs to the sending host and isn't destroyed. It then calls hub.notifyThread(threadId, ["storage-changed"]). storage-changed is detail-only, like history-compacted, so it goes only to that thread's thread-detail subscribers and not to the thread list. It doesn't re-resolve watch targets.
  • App: a storage-changed thread rule (debounced) refetches that thread's storage files, paths and previews. The environment rule, collectCachedThreadIdsForEnvironment and the getCachedThreadIdsForEnvironment context are deleted. A released thread-detail watch now catches up by re-marking that thread's storage, not the whole environment's.
  • Domain / plugin SDK: storage-changed is added to THREAD_CHANGE_KINDS and thread-storage-changed is removed from ENVIRONMENT_CHANGE_KINDS. These unions are part of the plugin SDK's realtime types (ChangedMessage), so the SDK goes 0.6.124 → 0.6.125. In-repo, the git plugin listed the old kind only to keep its caches, and the github-pr-status test listed it as a no-op. Both are updated.

Why the protocol bump and SDK change can't be avoided: the server can't tell from the environment id which thread's storage changed, so the daemon message needs the thread id. Clients can only be told which thread changed through the realtime change message type, and the plugin SDK publishes that type.

How you verified

Bounded listings

The benchmark calls listHostFiles with the panel's exact command (limit: 1000, includeHidden: false, respectGitIgnore: false, default excludes) on the six largest real dirs in ~/.bb/thread-storage. Each call runs in a fresh process, 5 runs per dir. Main and branch use the same checkout with only the daemon files swapped. Medians:

thread storage files walked on main main wall / CPU (ms) branch wall / CPU (ms)
thr_cbwjth4yt7 11,596 317.6 / 194.9 9.1 / 15.2
thr_n6cyn6sk25 16,894 233.9 / 226.8 9.8 / 16.1
thr_7csui6fdj4 16,394 245.0 / 219.8 12.6 / 18.2
thr_gtbhvapit2 12,569 71.1 / 94.7 13.3 / 19.4
thr_272ubxihy7 (active, being written) 10,985 123.5 / 122.6 47.2 / 53.9
thr_y6xhxw8t8w 4,229 20.7 / 34.4 12.3 / 17.4

Parity: the response hash (files + truncated) matches main for every static dir. thr_272ubxihy7 is a live thread and its hash changes between runs on either build. Frozen copies of it and thr_cbwjth4yt7 give identical hashes on main and branch (8e315bfdd95a, d4f45e09ef0b).

Tests: a new case in workspace-path-list.test.ts checks that an unqueried host.list_paths at a limit returns exactly the full walk's first entries with truncated: true, reads fewer directories than the full walk, and returns truncated: false when the limit equals the tree size. Locally workspace-path-list.test.ts and file-list.test.ts pass (32 tests), and @bb/host-daemon typecheck passes. CI runs the full suites.

Thread-targeted notifications

Real running servers, main vs branch. I ran source servers from origin/main and from this branch with pnpm start on fresh data dirs, not a copy of ~/.bb. Each had one project and two threads in one environment: A, where files are written, and B, the viewer. Three headless Chrome instances each held one foreground page on the real app: A with the right panel open, B with the right panel open, and home. I wrote 20 files into A's thread storage, one per second, and counted /thread-storage/files requests and storage-changed WebSocket frames per page.

page main: storage list requests main: frames branch: storage list requests branch: frames
A (written to, panel open) 20 20 20 40
B (same environment, panel open) 20 20 0 0
home (no thread open) 0 20 0 0
  • On main, B re-listed its own unchanged storage on every write to A, and every connected client received every change.
  • On the branch, only A refetches. A's page has two sockets subscribed to its thread detail: the app's own and the SDK realtime client plugins use. So it receives two frames per write, but still makes one request.

Tests:

  • internal-environment-change.test.ts: thread-storage-change notifies only the changed thread with storage-changed, ignores the same message from another host, and never sends an environment change.
  • realtime-cache-effects.test.ts: a storage-changed change refetches the changed thread's storage preview and not a sibling thread's in the same environment.
  • Contract tests cover the new message and confirm the old environment kind is rejected.
  • Updated: watch-manager, watch-interests, freeze-fence, daemon-message-fence and contract fixtures.

Commands:

  • pnpm exec turbo run typecheck --filter='...[origin/main]': 117/117
  • turbo run lint on the same filter: 57/57
  • Targeted vitest runs for host-daemon, server, host-daemon-contract, domain, app, git and github-pr-status
  • check-plugin-sdk-version.mjs

CI runs the full suites.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits November 4, 2026 17:18
The daemon reported a thread-storage write as an environment change with
only the environment id. The server broadcast it to every client, and the
app refetched the storage lists of every cached thread in that environment.

The daemon now sends thread-storage-change with the thread id (protocol
262). The server checks the thread belongs to the sending host and sends a
thread-level storage-changed change to that thread's detail subscribers
only, and the app refetches that thread's storage queries. The thread
storage watch target no longer carries an environment id, and
thread-storage-changed is no longer an environment change kind.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
host.list_files and host.list_paths without a query walked the whole
tree and then kept the first limit + 1 entries. The walk now stops once
it has limit + 1 paths, which is the same prefix in the same order, so
results and truncation are unchanged. Queried listings still walk and
cache the full tree for ranking.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael ymichael changed the title Notify only the changed thread when its storage changes Bound thread-storage listings and notify only the changed thread Nov 5, 2026
@ymichael
ymichael merged commit da62923 into main Nov 5, 2026
52 checks passed
@ymichael
ymichael deleted the bb/thread-storage-change-per-thread-thr_wxx5y5j3nk branch November 5, 2026 17:49
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