Repository navigation
Serve project commands from a stale-while-revalidate live command cache - #6210
Merged
ymichael merged 1 commit intoOct 26, 2026
Merged
Conversation
GET /api/v1/projects/:id/commands waited on provider.list_commands every call. On the host daemon that maintenance request re-resolves the login shell environment once its 10s cache lapses (0.6-2.2s), restarts the provider bridge after 60s idle (~480ms), and re-probes Claude Code (~190ms). The native-root scan, skills and skill catalog take under 25ms. The server now caches each live command answer per host, provider and cwd. It serves the cached list for 60s, then keeps serving it while a background refresh runs, and sends a provider-commands-changed host change when the answer differs so open composers refetch. Failed refreshes keep the previous list; a failed first load is not cached. The disk scan and skill catalog still run on every request. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
deleted the
bb/speed-up-project-commands-endpoint-thr_jq77xpw3ws
branch
October 26, 2026 17:33
ymichael
added a commit
that referenced
this pull request
Oct 26, 2026
…tch (#6219) ## Human comments ## What was wrong Every new thread gets its own worktree, so the first command-list lookup for a thread always reaches the machine, even with the server-side cache in #6210. On the machine, `provider.list_commands` ran through `withMaintenanceRuntime`, which waited for a fresh login-shell environment whenever the cached one was more than 10s old. That wait is 0.6–2.2s depending on the shell. #4022 made thread start use the last resolved environment and refresh it in the background, but kept every maintenance request blocking, so command listing still paid for the login shell. Separately, `shouldPrefetchCommandCatalog` prefetched the catalog whenever focus entered a composer on a coarse pointer. Opening a thread focuses its composer, so every thread open on a phone sent this request, even when nobody typed a command. ## What changed **Daemon (`apps/host-daemon/src/app.ts`).** `withMaintenanceRuntime` now takes the shell-environment refresh mode explicitly at each call site: | Request | Shell env | Why | | --- | --- | --- | | `provider.list_commands` | last resolved, refreshed in background | Command names don't depend on a just-changed PATH, and the thread that uses them already starts on the last resolved environment (#4022). | | `provider.usage` | last resolved, refreshed in background | Quota/plan display, polled and cached for 10s on the server. Errors aren't cached there, so if a stale PATH ever causes one, the next poll runs on the refreshed environment. | | `provider.list_models` | fresh | The server clears the model-catalog failure right after a successful CLI install and fetches again. A stale PATH there would record a failure that sticks for the catalog's failure TTL. The catalog is persisted, prewarmed and served stale-while-revalidate on the server, so the wait rarely reaches a user. | | `provider.health` | fresh | This is the "is the CLI installed and signed in" answer shown after install/login. It must see the current PATH. | | `provider.installation.status` / `.run` | fresh | Unchanged from #4022: they must see a just-installed CLI on PATH. | The first resolution after daemon start still blocks for every request. The cache only serves a stale environment once one has been resolved. No wire fields change, so there is no `HOST_DAEMON_PROTOCOL_VERSION` bump. **App.** The coarse-pointer focus prefetch is gone, along with everything that existed only for it: - the `focusin` listener in `composer-root.tsx`; - `prefetchCommands` / `useComposerCommandPrefetch` in `composer-search.tsx`; - `composerFocused`, `shouldPrefetchCommandCatalog` and `prefetchCommandCatalog` in `useCommandSuggestions.ts`; - `projectCommandsQueryOptions`, now inlined back into `useProjectCommands`. The catalog loads on the first `/` or `$` query. On first open the menu shows what it already has (prompt-action commands such as `plan` and `goal`) with the refreshing spinner, then fills in the catalog. With nothing to show yet, it shows the existing "Searching commands…" row. `docs/api_to_audit.md` (composer search contract) now says that commands load on the first command query rather than when focus enters the composer. This is independent of #6210 and based on main. The two together make the first lookup in a new worktree cheap: #6210 serves repeat lookups from the server, and this change removes the login-shell wait from the misses. Remaining cost on a miss: the maintenance runtime still restarts after 60s idle, plus Claude Code's startup check (about 350ms total in the measurements below). One race existed before this change and is unchanged: if a background refresh finds a *different* PATH while a listing request is in flight, `replaceBaseShellEnv` shuts down the maintenance runtime under that request. The shell env holds only PATH and bb's own variables, so this only happens when PATH actually changes. ## How you verified - `apps/host-daemon/src/app.test.ts`: a table test runs each of the six maintenance RPCs with an expired shell env and a pending login-shell resolution. `list_commands` and `usage` answer on the last resolved env without waiting. `list_models`, `health`, `installation.status` and `installation.run` don't reach the runtime until the fresh env resolves, and then run on it. A second test checks that `list_commands` still waits for the first resolution after daemon start. Flipping `listCommands` to blocking, or `providerHealth` to stale, fails the matching case. - `apps/app/src/hooks/useCommandSuggestions.dom.test.tsx`: the real hook makes no catalog request before a trigger, reports `isLoading` while the first request is pending, then returns the catalog. - `pnpm exec turbo run test --filter=@bb/host-daemon`: 823 passed, 12 skipped. - `pnpm exec turbo run test --filter=@bb/app -- src/hooks/useCommandSuggestions src/components/promptbox src/components/plugin/PluginNewThreadComposer`: 599 passed. - `pnpm exec turbo run typecheck --filter=@bb/app --filter=@bb/host-daemon` and `pnpm exec turbo run lint --filter=@bb/app` (0 errors) passed. - Phone-width check on a `pnpm start:worktree` build: Chrome at 390×844 with `pointer: coarse` emulated. Tapping into the new-thread composer sent no `/commands` request. Typing `/` sent one request; the menu showed the prompt-action commands with the spinner, then the full catalog when the (artificially delayed 1.5s) response landed. **Measurements.** `GET /api/v1/projects/:id/commands?provider=claude-code` on a `pnpm start:worktree` instance (main without #6210, so every call reaches the daemon). This machine's login-shell resolution takes about 570ms. Before = this branch with `listCommands` switched back to `allowStale: false`, otherwise identical build. | Call | Before | After | | --- | --- | --- | | +15s (env stale, runtime warm) | 602ms, 603ms | 32ms, 34ms | | +70s (env stale, runtime idle-restarted) | 936ms, 913ms | 357ms, 342ms | > 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
GET /api/v1/projects/:id/commandstook 1.0–1.3s on cold calls (10 times in an hour on a real server, once per thread view), which delayed the composer. The route ran four lookups in parallel. I timed each one with temporary logs on a dev server: the native-root scan, workspace and shared skills, and the skill catalog took under 25ms combined. All the remaining time went to theprovider.list_commandshost RPC. That RPC uses the daemon's maintenance runtime, which (since #4022) waits for a fresh login-shell environment once its 10s cache expires. It also restarts the provider bridge after 60s idle, and Claude Code re-probes its initialization. The server cached none of this, so every view outside those short windows paid the full cost again.Measured on a dev server, personal project, Claude Code:
What changed
apps/server/src/services/providers/provider-live-commands.ts: a cache of live provider commands keyed by host, provider and cwd, held inAppDeps.providerLiveCommandsnext toproviderNativeRoots.provider-commands-changedhost change. The app's realtime registry maps it to the project command queries, so an open composer refetches.listProviderLiveCommandsin place of the removedlistProviderLiveCommandsBestEffort.HOST_CHANGE_KINDSgainsprovider-commands-changed. Clients parse change kinds leniently, so older clients ignore it. This is a server/app change only: the daemon wire is untouched, so there is noHOST_DAEMON_PROTOCOL_VERSIONbump.docs/provider-bridge-protocol.md(Command catalog) describes the caching.The first view of a new cwd still waits for the live answer (about 0.7–2s, as before), so
bb project commandsand the SDK still get a complete list on first call. Making that miss faster means letting maintenance requests use a stale shell environment in the daemon, which #4022 chose not to do. I left that for a separate change.How you verified
test/services/providers/provider-live-commands.test.tscovers four cases: a failed refresh keeps the old list and the next read retries; a failed first load is not cached; concurrent first reads share one load; one refresh per stale window, with no change signal when the list is unchanged. The last test first failed on a real race: the in-flight marker was cleared before the refreshed list was stored, which allowed a duplicate refresh. The implementation now stores before clearing.test/public/public-project-commands.test.tshas a new route-level case. A second request inside the window makes noprovider.list_commandsRPC but still shows a newly scanned skill. After the window, the cached list is served, one refresh runs,provider-commands-changedis pushed, and the next request returns the new command.pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/app --filter=@bb/integration-testspassed.pnpm exec turbo run lint --filter=@bb/apppassed.pnpm exec turbo run test --filter=@bb/domainpassed. Targeted@bb/app(realtime-cache-effects.test.ts) and@bb/server(hub.test.ts,public-project-*) tests passed.pnpm exec turbo run test --filter=@bb/server: 3684 passed and 1 failed. The failure waspublic-thread-timeline-scrollback.test.tsunder parallel load; it passes when run alone.🤖 Generated with Claude Code