Skip to content

Speed up thread creation by inspecting only the git checkout - #7058

Merged
ymichael merged 2 commits into
mainfrom
bb/thread-create-checkout-inspection
Nov 4, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/thread-create-checkout-inspection

Conversation

@ymichael

@ymichael ymichael commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

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

Every thread create with a worktree environment ran host.inspect_git_source
against the project's main checkout before inserting the thread row. The
availability check reads only the checkout ref, but the full inspection also
walks every local branch to resolve default-branch refs, runs git status,
checks for in-progress operations and linked worktrees, and starts a
background fetch. On the bb checkout (4,445 local branches, 110 worktrees)
that took 345-420 ms per create, 1.5 s cold, and production logs show
POST /api/v1/threads at a 412 ms median, 2.3 s p90 and 5.5 s max.

Add host.inspect_git_checkout, which runs repo-kind detection and the checkout
ref only. Git-checkout availability uses it. Project-default resolution uses
it too: a branch checkout goes to the worktree provider with the default base
branch, which the plugin already resolves when it creates the worktree, and
any other checkout falls back to the full inspection so the worktree versus
project-checkout choice is unchanged. Bumps HOST_DAEMON_PROTOCOL_VERSION to
261.

On the same dev build against the bb checkout, POST /api/v1/threads drops
from 345-420 ms to 53-67 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
readDefaultBranchRefs listed every local branch to pick the default: origin's
HEAD branch, then main, then master, then the first branch by name. On the bb
checkout that is 4,445 branches, 140 ms per call and up to 2 s under load,
and it runs on every host.inspect_git_source (branch picker, project routes,
attached-environment branch refresh). Ask for the candidate refs directly and
list one branch only when none exists. Also reuse the origin HEAD already read
instead of reading it again.

host.inspect_git_source on the bb checkout drops from 310-380 ms to
195-230 ms. Output is unchanged, so no wire change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael ymichael changed the title Inspect only the checkout before creating a worktree thread Speed up thread creation by inspecting only the git checkout Nov 4, 2026
@ymichael
ymichael merged commit 1a8de2d into main Nov 4, 2026
51 checks passed
@ymichael
ymichael deleted the bb/thread-create-checkout-inspection branch November 4, 2026 23:20
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