Skip to content

Defer environment availability checks to thread creation - #3393

Merged
ymichael merged 3 commits into
mainfrom
bb/speed-up-environment-picker-loading-thr_imfebhbg5w
Sep 10, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/speed-up-environment-picker-loading-thr_imfebhbg5w

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

New Thread loaded the provider registry and then fanned out one scoped environment-provider request per persistent machine. Each scoped request could inspect the machine's Git checkout and invoke plugin availability, while the client aggregation withheld the provider value until every machine answered. The slowest machine therefore gated the picker even though only one provider and machine would ultimately be selected.

What changed

  • Made environment-provider discovery structural only: project/projectless matching, source presence, and Git remote presence are resolved without contacting host daemons or invoking plugin availability. The existing nullable availability response remains null for compatibility.
  • Removed the app's per-machine provider-query fan-out. New Thread performs one provider discovery request and derives machine eligibility from already-loaded host and project-source data.
  • Moved Git checkout inspection and plugin availability to the selected provider and machine during thread creation, before thread or environment rows are inserted. Availability is checked afresh for every creation request.
  • Treats setup-required and unavailable results as selection rejections, and hook failures as provider failures, while preserving the submitted draft.
  • Removed picker and CLI presentation of preflight availability and updated the Plugin SDK contract, API audit, CLI guide, templates, and Plugin Guide.
  • No server/daemon wire fields changed, so HOST_DAEMON_PROTOCOL_VERSION is unchanged.

How you verified

  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/app --filter=@bb/cli --filter=@get-bb/plugin-sdk --filter=@bb/plugin-api-map --filter=@bb/templates
  • pnpm exec turbo run test --filter=@bb/server -- --run test/threads/environment-providers.test.ts — 39 passed
  • pnpm exec turbo run test --filter=@bb/app -- --run src/components/pickers/EnvironmentPicker.test.tsx src/components/plugin/PluginNewThreadComposer.test.tsx src/hooks/queries/environment-provider-queries.test.tsx src/views/RootComposeView.test.ts — 115 passed across the focused files
  • pnpm exec turbo run test --filter=@bb/cli -- --run src/__tests__/command-output/environment.test.ts src/__tests__/command-output/thread-spawn.test.ts — 70 passed
  • pnpm exec turbo run test --filter=@get-bb/plugin-sdk --filter=@bb/plugin-api-map --filter=@bb/templates — 367 passed
  • Re-ran @bb/templates after minimizing the guide-template diff — 44 passed
  • git diff --check
  • Started the source dev app and confirmed the app, server, and host-daemon health endpoints.

AGENT GENERATED

@ymichael
ymichael merged commit 40ed14c into main Sep 10, 2026
16 checks passed
@ymichael
ymichael deleted the bb/speed-up-environment-picker-loading-thr_imfebhbg5w branch September 10, 2026 05:39
SawyerHood added a commit that referenced this pull request Sep 10, 2026
…#3436)

## Human comments

## What was wrong

The prompt-box environment picker offered every environment provider on
every machine, so CoW copy showed up under Macs where its reflink probe
can never succeed. Per-machine plugin availability existed briefly
(#3227), but #3393 removed it from discovery because the app's
per-machine fan-out blocked the picker on the slowest machine's Git
inspection and plugin probe. That fix removed the information rather
than making it non-blocking: discovery became structural only,
availability is always null, and an unsupported choice now fails only at
thread creation.

## What changed

Server
(`apps/server/src/services/environments/provider-machine-availability.ts`,
new): a background per-machine availability probe. When a project's
providers are listed, each structurally eligible provider and connected
persistent machine is probed in the background (Git inspection when
`requires.gitCheckout`, then the plugin's `availability` hook), keyed by
provider, project, and machine. Listing never awaits a probe: it returns
the cached answer or null. Results are cached for ten minutes, cleared
when a plugin rechecks, and disconnected machines are never probed. When
an answer changes, the server emits a new
`environment-availability-changed` system change so clients refetch.

Contract (`packages/server-contract/src/api/system.ts`): each provider
now carries `machineAvailability`, a map from host id to `available`,
`setup-required`, `unavailable`, or null while unknown. `availability`
is filled from that map when `hostId` is given. Thread creation still
checks the selected provider and machine afresh, exactly as #3393 left
it.

App: `NewThreadComposer` lists providers for the current project and
derives each machine's rows from `machineAvailability`. The picker hides
providers a machine reports as unavailable, except the currently
selected one, which stays visible and disabled with its reason so the
selection never disappears; setup-required rows stay selectable with
their message; unknown rows show as before. The provider list is
remembered per project in localStorage through the existing last-known
cache and served as placeholder data, so the picker is already trimmed
on the first open after a reload. The realtime registry maps the new
system change to the provider query.

CLI: `bb environment providers --project <id> --machine <id>` prints
each provider's availability on that machine, and `--json` includes
`machineAvailability`. Guide template, bb-cli skill,
`docs/api_to_audit.md`, the Plugin Guide surface text, and the SDK doc
comment were updated to describe background probing.

No server/daemon wire fields changed, so `HOST_DAEMON_PROTOCOL_VERSION`
is unchanged.

## How you verified

- Server tests in
`apps/server/test/threads/environment-providers.test.ts` rewritten for
the new contract: listing answers null first, the background answer is
served on the next listing and `environment-availability-changed` fires
once; Git inspection runs in the background and a missing branch is
reported as unavailable; disconnected machines are not probed; unmet
structural requirements never probe; a plugin recheck invalidates the
cache and probes again; a throwing hook is reported as unavailable
without failing the listing; creation still checks afresh. 43 passed.
- App tests: picker hides unavailable rows, keeps the selected
unavailable row disabled with its reason, keeps setup-required
selectable with its message, hides per machine in the grouped menu;
provider query writes and serves the last-known cache and rejects a
corrupt entry; realtime registry covers the new system change.
- CLI test for the availability column with available, unavailable, and
unknown rows.
- `pnpm exec turbo run typecheck` for server, app, cli, domain,
server-contract, sdk, plugin-sdk, plugin-api-map, and templates passes.
- `pnpm exec turbo run test` for cli, server-contract, sdk, domain,
templates, plugin-api-map, and plugin-sdk passes. Full server and app
Vitest suites pass (the app's turbo test task fails before tests on this
machine because its PWA icon check needs a libvips module that isn't
installed, so the app suite was run with vitest directly).
- Manual, on the earlier client-only version of this change: a dev
server with the btrfs-cow plugin and two projects on one machine, one on
btrfs and one on tmpfs; the tmpfs picker omitted CoW copy and was
already trimmed on first open after a reload.

> AGENT GENERATED

Co-authored-by: Claude Fable 5.1 <[email protected]>
ymichael added a commit that referenced this pull request Nov 4, 2026
## Human comments

## What was wrong

New threads took noticeably long to show up in the sidebar after
submitting the composer. The app adds the thread to the sidebar as soon
as `POST /api/v1/threads` returns, so the delay is that request. Since
#3393 the request runs the daemon's `host.inspect_git_source` against
the project's main checkout before it inserts the thread row, to check
the checkout has a commit. That check only reads the checkout ref, but
the command also lists every local branch to resolve default-branch
refs, checks local against `origin`, runs `git status`, looks for
in-progress operations and linked worktrees, and starts a background
fetch. On the bb checkout (4,445 local branches, 110 worktrees) that is
about 15 git processes per create, and each gets slower when agents are
using git in the same repo. Production logs show `POST /api/v1/threads`
at a 412 ms median, 2.3 s p90 and 5.5 s max (Nov 3–4), and similar
numbers in October. The project-default path (Tasks, automations, CLI
spawns without an environment) ran the same full inspection a second
time to pin a base-branch name that the worktree plugin already resolves
when it creates the worktree.

## What changed

- New daemon command `host.inspect_git_checkout` (`{ path }` → `{
checkout }`): repo-kind detection and the checkout ref only. No branch
walk, no `git status`, no fetch. **Wire change:
`HOST_DAEMON_PROTOCOL_VERSION` 260 → 261.**
- `resolveGitCheckoutAvailability` (the `requires.gitCheckout` check at
create time and in the per-machine availability probe) uses the new
command. Result and refusal messages are unchanged.
- Project-default resolution (`thread-default-policy.ts`): a `branch`
checkout now goes to the worktree provider with `{ kind: "default" }`
instead of a server-pinned `{ kind: "named", name: "origin/main" }`, so
the plugin resolves the base when it creates the worktree. Any other
checkout (`detached`, `unborn`, `unknown`) falls back to the full
inspection, so the choice between a worktree and running in the project
checkout is unchanged.
- One side effect: when the base-branch fetch fails, `{ kind: "default"
}` falls back to the last fetched default branch with a "may be out of
date" notice. The pinned `origin/main` name used to fail instead.
- Worktree provisioning still fetches its own base branch, so dropping
the per-create background fetch does not make new worktrees staler.
- Cheaper `host.inspect_git_source` for the callers that still need it
(composer branch picker, project routes, project-default fallback,
attached-environment branch refresh). `readDefaultBranchRefs` used to
list every local branch (4,445 on the bb checkout: ~140 ms, 2 s once
under load) to pick origin's HEAD branch, then `main`, then `master`,
then the first branch by name. A new `readPreferredLocalDefaultBranch`
in `packages/environment-provider-host/src/workspace-git.ts` asks
`for-each-ref` for only those candidate refs and matches names exactly,
since `for-each-ref` patterns also match `main/*`. It lists one branch
with `--count=1` only when no candidate exists.
`readOriginDefaultBranch` reuses the origin HEAD already read instead of
running `symbolic-ref` again. Output is unchanged.

## How you verified

- Same dev build (`pnpm start:worktree`) with a project rooted at
`~/Projects/bb`, 10 creates each via `POST /api/v1/threads` (managed
worktree, scheduled send):
- before (availability via `inspect_git_source`): 345–420 ms, first
create 1.5 s
  - after: 53–67 ms
- Earlier per-step timing on the old code put ~99% of create time in
environment placement, specifically this inspection. The plugin
dispatch-hook pass was ~3 ms.
- Daemon inspection functions against `~/Projects/bb` (3 runs each):
`readDefaultBranchRefs` 300–360 ms → 181–209 ms; full
`host.inspect_git_source` work 310–380 ms → 195–230 ms.
- New tests in
`packages/environment-provider-host/test/workspace-git.test.ts`:
origin's HEAD branch wins over `main`; a `main/feature` branch is not
mistaken for `main`; with no `main` or `master`, the first branch by
name is picked. The existing equal/behind/ahead/diverged/no-origin cases
still pass.
- New daemon tests: the new command reports the checkout without
contacting the remote, and reports a non-repo path as unknown.
- Server tests updated or added: availability fixtures use the new
command; a branch project-default never calls `inspect_git_source` and
passes `{ kind: "default" }`; a detached checkout runs the full
inspection before choosing a worktree; zero-commit and non-Git
project-defaults still run in the project checkout.
- `pnpm exec turbo run typecheck --filter=@bb/server
--filter=@bb/host-daemon --filter=@bb/host-daemon-contract
--filter=bb-environment-provider-host
--filter=bb-plugin-environment-git-worktree`
- vitest:
`apps/host-daemon/test/command/host-branches-dispatch.test.ts`,
`packages/host-daemon-contract` (all),
`packages/environment-provider-host` (all), and `apps/server`
`test/threads/environment-providers.test.ts`,
`test/threads/thread-default-policy.test.ts`,
`test/public/public-threads.project-default.test.ts`,
`test/public/public-threads.named-base-branch.test.ts`,
`test/public/public-project-workspace-routing.test.ts`,
`test/services/plugins/plugin-sdk.test.ts`

> 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