Repository navigation
Speed up thread creation by inspecting only the git checkout - #7058
Merged
Merged
Conversation
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]>
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
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/threadsreturns, so the delay is that request. Since #3393 the request runs the daemon'shost.inspect_git_sourceagainst 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 againstorigin, runsgit 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 showPOST /api/v1/threadsat 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
host.inspect_git_checkout({ path }→{ checkout }): repo-kind detection and the checkout ref only. No branch walk, nogit status, no fetch. Wire change:HOST_DAEMON_PROTOCOL_VERSION260 → 261.resolveGitCheckoutAvailability(therequires.gitCheckoutcheck at create time and in the per-machine availability probe) uses the new command. Result and refusal messages are unchanged.thread-default-policy.ts): abranchcheckout 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.{ kind: "default" }falls back to the last fetched default branch with a "may be out of date" notice. The pinnedorigin/mainname used to fail instead.host.inspect_git_sourcefor the callers that still need it (composer branch picker, project routes, project-default fallback, attached-environment branch refresh).readDefaultBranchRefsused to list every local branch (4,445 on the bb checkout: ~140 ms, 2 s once under load) to pick origin's HEAD branch, thenmain, thenmaster, then the first branch by name. A newreadPreferredLocalDefaultBranchinpackages/environment-provider-host/src/workspace-git.tsasksfor-each-reffor only those candidate refs and matches names exactly, sincefor-each-refpatterns also matchmain/*. It lists one branch with--count=1only when no candidate exists.readOriginDefaultBranchreuses the origin HEAD already read instead of runningsymbolic-refagain. Output is unchanged.How you verified
pnpm start:worktree) with a project rooted at~/Projects/bb, 10 creates each viaPOST /api/v1/threads(managed worktree, scheduled send):inspect_git_source): 345–420 ms, first create 1.5 s~/Projects/bb(3 runs each):readDefaultBranchRefs300–360 ms → 181–209 ms; fullhost.inspect_git_sourcework 310–380 ms → 195–230 ms.packages/environment-provider-host/test/workspace-git.test.ts: origin's HEAD branch wins overmain; amain/featurebranch is not mistaken formain; with nomainormaster, the first branch by name is picked. The existing equal/behind/ahead/diverged/no-origin cases still pass.inspect_git_sourceand 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-worktreeapps/host-daemon/test/command/host-branches-dispatch.test.ts,packages/host-daemon-contract(all),packages/environment-provider-host(all), andapps/servertest/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🤖 Generated with Claude Code