Skip to content

Serve command lists from the last shell env; stop phone command prefetch - #6219

Merged
ymichael merged 1 commit into
mainfrom
bb/cheap-first-command-list-no-phone-prefetch-thr_wueryp4vjz
Oct 26, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/cheap-first-command-list-no-phone-prefetch-thr_wueryp4vjz

Conversation

@ymichael

@ymichael ymichael commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

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

The daemon's provider.list_commands and provider.usage now use the last
resolved login-shell environment and refresh it in the background, as
thread start does since #4022. Model listing, provider health and
installation status/run still wait for a fresh environment once the 10s
cache expires, because their answers depend on the current PATH. The
first resolution after daemon start still blocks.

The composer no longer prefetches the command catalog when focus enters
it on coarse pointers. Opening a thread focuses its composer, so every
thread open on a phone requested the catalog. Commands now load on the
first / or $ query and show the existing "Searching commands…" state
until they arrive. The focus listener, the prefetch request path and its
query-options helper are removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 29d757b into main Oct 26, 2026
50 checks passed
@ymichael
ymichael deleted the bb/cheap-first-command-list-no-phone-prefetch-thr_wueryp4vjz branch October 26, 2026 17:33
ymichael added a commit that referenced this pull request Oct 29, 2026
…6571)

## Human comments

## What was wrong

Since #6219, `providerUsage` refreshes the login-shell env with
`allowStale: true`, so the refresh can land while the usage request is
still running. When PATH changed, `RuntimeManager.replaceBaseShellEnv`
shut down the provider maintenance runtime immediately, including one
with a request in flight, and that request failed with `Provider "codex"
is not running`. With fnm, PATH changes on every probe (`eval "$(fnm
env)"` mints a new `fnm_multishells/<pid>_<ms>` dir), so every usage
call on that host failed. Fixes #6533.

## What changed

`apps/host-daemon/src/runtime-manager.ts`:

- The maintenance runtime now lives in a slot that counts the requests
using it, including requests still waiting for it to start. When the
shell env changes or a provider update invalidates it, a busy slot is
retired: new requests get a fresh runtime, and the old runtime shuts
down when its last request finishes. Idle slots still shut down
immediately, and `shutdownAll` still stops every slot right away, busy
or retired. This replaces the old pending/generation/active-count
fields.
- `shellEnvEquals` ignores the `fnm_multishells/<id>` segment of PATH
entries, so per-shell fnm drift no longer counts as an env change for
maintenance or environment runtimes (the trigger behind #1156).

No wire changes, so there is no `HOST_DAEMON_PROTOCOL_VERSION` bump.

## How you verified

- New tests in `runtime-manager.test.ts`:
- A busy maintenance runtime survives a PATH change, concurrent requests
get the fresh runtime, and the old runtime shuts down after its request
resolves.
- An fnm-multishell-only PATH change keeps the runtime, while a real
PATH change still replaces it.
- `shutdownAll` stops a retired runtime that still has a request in
flight.
- The first two tests fail against the old `runtime-manager.ts` and pass
with the fix.
- The existing pending-turn eviction test used fnm paths to force an
eviction; it now uses plain PATH changes. The stale-creation race test
now asserts through the public API instead of a removed private field.
- `pnpm exec turbo run typecheck test lint --filter=@bb/host-daemon`:
839 tests passed.

Fixes #6533

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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