Skip to content

Memoize provider.list_models on the server and key follow-up execution options by host (B6) - #1885

Merged
SawyerHood merged 4 commits into
bb/mobile-perf/thread-open-query-gating-reviewfrom
bb/mobile-perf/list-models-cache
Aug 19, 2026
Merged

SawyerHood merged 4 commits into
bb/mobile-perf/thread-open-query-gating-reviewfrom
bb/mobile-perf/list-models-cache

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 19, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

Every execution-options read (each thread open, window focus, reconnect) sent a fresh provider.list_models RPC to the host, and the host spawned a provider CLI per request (measured 1-3 s). Nothing cached the answer on the server, daemon, or bridge. The request carried the environment's cwd although the Claude Code, Codex, and ACP bridges ignore it, so every environment probed separately. On the client, the follow-up composer keyed its query by environmentId, so each thread in a different environment on the same machine issued its own request. On mobile this put a CLI spawn on the critical path of every thread open. (Mobile perf sweep finding B6.)

What changed

  • apps/server: listProviderModelsMemoized runs the probe through a new createAsyncTtlMemo (services/lib/async-ttl-memo.ts) held on lifecycleDedupers.providerModelList (10 min TTL, in-flight dedupe, failures never stored). The key is [hostId, daemonSessionId, providerRegistrationRevision, command], so a daemon reconnect or a plugin reload re-probes at once. The probe bypasses the memo only while no daemon session is registered.
  • packages/domain: providerModelCatalogDependsOnWorkspace(providerId) states which first-party bridges read the workspace path (only Pi). The server sends cwd only for those; host-scoped catalogs share one memo entry across environments.
  • apps/app: component-local composers (ThreadDetailPromptArea, EmbeddedThreadChat via PluginThreadChat) receive the environment's host and resolveThreadCreationProviderRouting routes by hostId when the selected provider's catalog is host-scoped. Pi and unknown providers keep environment routing.
  • plugins/provider-claude-code: model/list goes through createClaudeCodeBridgeModelListMemo (2 min window plus in-flight dedupe; failures not kept) so bursts share one CLI spawn.

How you verified

  • New tests: apps/server/test/services/async-ttl-memo.test.ts (TTL expiry, in-flight sharing, rejection not stored, clear); apps/server/test/system/execution-options.test.ts "model probe memo" block (one probe without cwd across two environments plus explicit host; Pi keeps cwd and keys per workspace; two concurrent readers share one held RPC; a failed probe is retried and a reconnected daemon session re-probes); apps/app/src/hooks/useThreadCreationOptions.test.tsx (claude-code component-local routes by host, pi routes by environment); plugins/provider-claude-code/src/bridge/__tests__/model-list-memo.test.ts (shared probe, window expiry, failure retry). Updated provider-routing.test.ts and public-threads.defaults.test.ts for the host-scoped Codex probe (no cwd, single probe).
  • pnpm exec turbo run typecheck --force for @bb/domain, @bb/server, @bb/app, bb-plugin-provider-claude-code: pass. lint for @bb/app: 0 errors, 147 warnings (same as baseline). Targeted vitest runs above: server 54/54, app 134/134, claude plugin 264/264. Full @bb/server suite: 1735 passed, 2 unrelated failures (file-mode umask assertion in internal-skill-trees, timeout in plugin-service under load).

Fixes: part of the mobile / iOS Safari performance program (verified sweep report in the bb thread; no single issue).

Stack context

Layer 6 of 22 in the bb/mobile-perf/* stack (bottom → top: quick wins first, big rocks last).

  • Prerequisite (layer below): bb/mobile-perf/thread-open-query-gating-review (Cut per-thread-open requests behind the surfaces that need them (B9-B13, B16, C5) #1884).
  • Next layer: bb/mobile-perf/workspace-status-and-pr-cache (Dedupe and briefly cache workspace status and pull-request reads (B7, B8) #1886).
  • Audit findings addressed: see title IDs. Review: approved-with-fixes; 1 review fix commit(s).
  • Reviewer notes / follow-ups: Minor (not fixed, low value): on plugin surfaces (PluginThreadChat) the environment is fetched separately from the thread, so the follow-up execution-options query can flip its key | Design note: host-scoped catalogs are reused for up to 10 min (server) + 2 min (Claude bridge) after an account/CLI change on the same daemon session; the curated Claude catalog is
  • Wire/contract: No schema or protocol change. HOST_DAEMON_PROTOCOL_VERSION not bumped (still 135). The only wire-visible difference: the server now omits the already-optional cwd field from provider.list_models for claude-code, codex, and acp-* providers (verified their bridges ignore it: Claude model/list params are z.object({}), Codex handleModelList(id) takes no params, ACP builds its list from the l

AGENT GENERATED: by Claude Opus 5

@SawyerHood SawyerHood changed the title bb/mobile perf/list models cache Memoize provider.list_models on the server and key follow-up execution options by host (B6) Aug 19, 2026
@SawyerHood
SawyerHood force-pushed the bb/mobile-perf/list-models-cache branch from afa1327 to 33f28ea Compare August 19, 2026 07:50
@SawyerHood
SawyerHood marked this pull request as ready for review August 19, 2026 08:04
@SawyerHood
SawyerHood force-pushed the bb/mobile-perf/list-models-cache branch from 33f28ea to ce98ee5 Compare August 19, 2026 15:59
SawyerHood and others added 4 commits August 19, 2026 16:03
Every execution-options read (each thread open, window focus, and
reconnect) sent a fresh provider.list_models RPC to the host, and each
one spawned a provider CLI (measured 1-3 s per request). Nothing cached
the answer on the server, the daemon, or the bridge, and the request
carried the environment's cwd even though the Claude Code, Codex, and
ACP bridges ignore it, so every environment probed separately.

Add an in-process TTL memo (10 min) with in-flight dedupe for the model
probe. The key carries the host, the daemon session serving it (a
reconnected daemon re-probes), the provider registration revision (a
plugin reload re-probes), and the full command. Failures are never
memoized. The cwd now rides only for workspace-scoped catalogs; the new
@bb/domain helper providerModelCatalogDependsOnWorkspace states which
first-party bridges read it (only Pi), so host-scoped catalogs share one
entry across environments.

Co-Authored-By: Claude <[email protected]>
The follow-up composer keyed its execution-options query by
environmentId, so every thread in a different environment on the same
machine issued its own request (staleTime 60 s, refetched on focus and
reconnect) even though the Claude Code, Codex, and ACP catalogs do not
depend on the workspace.

Pass the environment's host into the component-local composers
(ThreadDetailPromptArea, EmbeddedThreadChat via PluginThreadChat) and
route by hostId when the selected provider's catalog is host-scoped, so
threads across environments share one cached query. Workspace-scoped
(Pi) and unknown providers keep the environment routing so the server
can pass the workspace path through.

Co-Authored-By: Claude <[email protected]>
model/list spawned a Claude CLI on every request. Wrap
listClaudeCodeBridgeModels in a per-bridge memo (in-flight dedupe plus a
2 min window; failures are not kept) so bursts of picker, thread-open,
and reconnect asks share one probe. The window stays short because the
server memoizes on top of this and the bridge outlives server restarts.

Co-Authored-By: Claude <[email protected]>
…hook

The memo never forwards an env to the probe, so the injectable list
function should not advertise one.

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood force-pushed the bb/mobile-perf/list-models-cache branch from ce98ee5 to 0af949b Compare August 19, 2026 16:13
@SawyerHood
SawyerHood merged commit 189be3e into main Aug 19, 2026
12 checks passed
@SawyerHood
SawyerHood deleted the bb/mobile-perf/list-models-cache branch August 19, 2026 16:23
SawyerHood added a commit that referenced this pull request Aug 19, 2026
…13, B16, C5) (#1884)

## What was wrong

Opening a thread on a phone issued ~17 requests. Several of them served
surfaces that were not on screen: the thread-storage file list
(`host.list_files`, limit 1000) for the secondary panel, the forks list
for the info tab, 50 full prompt-history entries that only ArrowUp
consumes, a child-thread subset that the sidebar bootstrap already
holds, a duplicate `GET /threads?projectId=` for worktree-reuse options,
and a 200-thread `@`-mention candidate list that duplicates the sidebar
cache. The first timeline window also always used the 20-segment server
default on a 390 px viewport (B9, B10, B11, B12, B13, B16, C5).

## What changed

- The thread-storage file list loads only while the secondary panel is
open or a storage tab exists.
- The child-thread subset and the `@`-mention candidates derive from the
cached sidebar bootstrap (via `select` with structural sharing) and fall
back to the network only when the project is absent from the cache. On
cold deep-link opens the hooks wait for the in-flight bootstrap instead
of racing it.
- The forks list query is enabled only while the secondary panel is
open.
- Prompt history (thread and project) is enabled on fine pointers, or on
coarse pointers after the first non-IME keydown, so iPad + keyboard
still works.
- The worktree-reuse options in the new-thread composer derive from the
sidebar bootstrap the composer already observes.
- The `/` command catalog is prefetched on the first composer focus on
coarse pointers (`retry: false`).
- Compact viewports request `segmentLimit=8` for the first timeline
window and on the full-refetch path; the server timeline cache is keyed
by segment limit so deltas keep working.

## How you verified

- New tests: query gates (enabled/disabled per surface), fallback vs
derived child/mention lists, in-flight bootstrap wait,
viewport-dependent `segmentLimit` including the delta path, IME handling
for the keyboard-seen flag, focus-time command prefetch (coarse) vs none
(fine).
- `pnpm exec turbo run typecheck --filter=@bb/app`, `pnpm exec turbo run
lint --filter=@bb/app`, targeted `pnpm exec turbo run test
--filter=@bb/app -- src/hooks/queries src/components/promptbox
src/views/thread-detail`.
- Measured on the seeded perf fixture at 390 px: thread-open API
requests 17 → 8 (with the other layers of this stack).

Fixes: part of the mobile / iOS Safari performance program (verified
sweep report in the bb thread; no single issue).


## Stack context

Layer 5 of 22 in the `bb/mobile-perf/*` stack (bottom → top: quick wins
first, big rocks last).
- Prerequisite (layer below): `bb/mobile-perf/ws-liveness-and-resume`
(#1883).
- Next layer: `bb/mobile-perf/list-models-cache` (#1885).
- Audit findings addressed: see title IDs. Review: approved-with-fixes;
2 review fix commit(s).
- Reviewer notes / follow-ups: The requested branch
bb/mobile-perf/thread-open-query-gating is checked out in worktree
/home/sawyer/projects/bb/.claude/worktrees/wf_dec7ce2a-e37-8 and still
points at dde6cdd w | B16 prefetch nuance (not a bug):
useProjectCommands keeps staleTime 0, so on coarse-pointer devices the
first '/' still issues a background refetch after the focus-time
prefetch. T | B12 nuance (by design, matches the audit's proposed fix):
on an iPad with a hardware keyboard the very first keydown enables the
prompt-history fetch, so an ArrowUp pressed as the | Derived
child/mention lists depend on the sidebar bootstrap being kept fresh by
another active observer (AppLayout's useSidebarNavigation, always
mounted). If a sibling package (e.

> AGENT GENERATED: by Claude Code (claude-mangosteen-eap)

---------

Co-authored-by: Sawyer Hood <[email protected]>
Co-authored-by: Claude <[email protected]>
SawyerHood added a commit that referenced this pull request Aug 19, 2026
… B8) (#1886)

## What was wrong

`GET /environments/:id/status` and `GET /environments/:id/pull-request`
forwarded every request to the host daemon. Each status probe spawns 4-5
git subprocesses (measured 240-344 ms); each pull-request probe spawns
`gh pr view`. A thread open, sidebar rows and a phone returning to the
foreground ask for the same environment inside the same second, and the
client refetches status on every `work-status-changed` batch. One
editing turn produced a stream of duplicate probes on the host (audit
findings B7, B8).

## What changed

- New `WorkspaceReadCaches`
(`apps/server/src/services/environments/workspace-read-cache.ts`) on
`AppDeps`: a per-environment in-flight dedupe plus a short TTL cache, 3
s for `workspace.status`, 10 s for `workspace.pull_request`.
- The two read routes go through the cache. Overlapping reads share one
daemon RPC. Repeated reads inside the TTL are served from memory. The
response shape is unchanged.
- Freshness follows the daemon events the client already refetches on.
Any environment change except record-only `metadata-changed` /
`thread-storage-changed` drops the cached value and detaches any
in-flight probe for that environment (a probe that started before the
change may have read the pre-change tree). Host connect/disconnect drops
that host's entries. `metadata-changed` is ignored because the status
probe itself records the observed branch mid-flight.
- Cache keys include the workspace context and the merge-base branch, so
a re-provisioned environment or a different merge base never reads a
stale entry. Mutation routes (commit, squash-merge, PR actions) keep
probing directly.
- No server/daemon wire change.

## How you verified

- `apps/server/src/services/environments/workspace-read-cache.test.ts`:
in-flight sharing, TTL expiry with a fake clock, key separation, no
caching of rejections, in-flight detachment on invalidation (late result
does not overwrite), host-scoped invalidation, both caches dropped on
`work-status-changed` / `git-refs-changed`, kept across
`metadata-changed` / `thread-storage-changed`, dropped on host events.
- `apps/server/test/environments/workspace-read-cache-routes.test.ts`:
two concurrent status GETs produce one `workspace.status` daemon
command; a third GET is served without a daemon round trip;
`?mergeBaseBranch=` is a separate probe; `work-status-changed` forces a
fresh probe. Same for `/pull-request` with `git-refs-changed`. These
fail before the change (the third request blocks on an unanswered second
daemon command).
- `pnpm exec turbo run typecheck --filter=@bb/server` passes; related
suites (`test/public`, `test/hosts`, `test/internal`,
`test/environments`, `test/ai/commit-message.test.ts`,
`src/services/environments`) pass except one pre-existing
umask-dependent assertion in `internal-skill-trees.test.ts` (file mode
0664 vs 0644, unrelated).

Fixes: part of the mobile / iOS Safari performance program (verified
sweep report in the bb thread; no single issue).


## Stack context

Layer 7 of 22 in the `bb/mobile-perf/*` stack (bottom → top: quick wins
first, big rocks last).
- Prerequisite (layer below): `bb/mobile-perf/list-models-cache`
(#1885).
- Next layer: `bb/mobile-perf/search-fts` (#1887).
- Audit findings addressed: see title IDs. Review: approved.
- Reviewer notes / follow-ups: B8 client half (drop PR key from the
work-status-changed handler, refetchOnWindowFocus:true, ~30 s
pending-checks poll) is not in this package by design; until it lands,
agent edit | Remote-only PR changes (e.g. `gh pr create` from a shell
with no local ref change) can be up to 10 s stale; unsubscribed
environments up to 3 s stale for status. Both are within wh |
apps/server/src/routes/environments.ts has a pre-existing prettier drift
(assertCanMarkPullRequestReady / assertCanConvertPullRequestToDraft)
that predates this branch; not touched
- Wire/contract: None. No server<->daemon message, command, result, or
HOST_DAEMON_PROTOCOL_VERSION change. HTTP responses for /status and
/pull-request are byte-identical in shape; only freshness/dedupe
changed. New internal AppDeps member `workspaceReadCaches`
(server-only).

> AGENT GENERATED: by Claude Code (claude-mangosteen-eap)

Co-authored-by: Claude <[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