Skip to content

Inspect the checkout once per new thread, and claim a warm worktree in one host call - #8121

Merged
ymichael merged 4 commits into
mainfrom
bb/new-worktree-thread-fewer-serial-host-calls-thr_cgsqayz78c
Nov 24, 2026
Merged

ymichael merged 4 commits into
mainfrom
bb/new-worktree-thread-fewer-serial-host-calls-thr_cgsqayz78c

Conversation

@ymichael

@ymichael ymichael commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Finding #8 of the server→daemon RPC audit. Two separate problems made creating a project-default worktree thread slow on a remote machine.

First, the create request inspected the project checkout twice, one after the other. resolveProjectDefaultThreadEnvironment called host.inspect_git_checkout to choose the worktree provider. Provider placement then called resolveGitCheckoutAvailability, which sent the same RPC for the same path again. Its dedupe only covers calls that are in flight at the same moment, and these two run in sequence.

Second, the worktree plugin's warm-pool handoff made separate plugin host calls in a row: readRemoteBase, then checkPoolMember, then fastForward when the member was behind the remote base, then nameHandedOffBranch.

What changed

  • Server: one checkout inspection per create request. createGitCheckoutInspector(deps) (services/environments/provider-availability.ts) memoizes host.inspect_git_checkout per (host, path) for one request.
    • thread-create.ts creates one and passes it to both resolveCreateThreadEnvironment and resolveThreadEnvironmentPlacement. Placement's validateProviderSelection reuses the result through gitCheckoutAvailability().
    • Every other completeProviderSelection caller gets a fresh inspector, so its behavior is unchanged.
    • The TTL-cached machine-availability path keeps its in-flight dedupe.
  • Worktree plugin: one claimPoolMember host call replaces readRemoteBase, checkPoolMember and fastForward (host/handoff.ts, contract.ts, warm-pools.ts, server.ts).
    • The claim reads the remote base. When the server passes the base from an earlier claim, the claim of the next candidate reuses it instead.
    • It then runs the same check under the ref lock, and fast-forwards the member when it is behind the base.
    • The server still owns reservation, discard and adopt policy. It still reserves one member at a time, so two creates running at once still get different members.
    • Failure handling, because the claim can now move the checkout:
      • A member whose fast-forward fails is refused and discarded. Before, the create failed after adopt.
      • If adopt fails after the claim moved the member, the member is discarded. Its setup ran against the old commit, so it must not go back into the pool.
      • A claim call that throws (disconnect, abort, or still running after 15 minutes) discards the member, because it may have moved.
    • Folder worktrees (from Let worktree threads branch the project's other git repositories, set them up and keep them warm #8065, merged in): once the main worktree's claim succeeds, each folder worktree is claimed with claimPoolMember too, all at once, reusing each folder's remote base for the next member. This replaces the separate readRemoteBase, checkPoolMember and fastForward calls per folder. A member whose folder claim is busy, timed out or has no readable base is kept only when none of its worktrees moved; otherwise it is deleted.
    • Time limits: reading the base and checking the member get 15 s together, as before. The server passes that budget as checkTimeoutMs and the host applies it to those steps only. When it runs out the claim returns timed-out, the member is kept and a fresh worktree is built. The fast-forward is not under that budget: the call gets the create's 15-minute timeout, the limit the fast-forward had before.
      • A remote base that can't be read returns base-unavailable. The member is kept and a fresh worktree is built, as before.
    • nameHandedOffBranch stays a separate call. It needs branchName(), which waits for the thread title to settle.
  • Docs: plugins/environment-git-worktree/README.md, docs/worktrees.md, and the experimental_ adopt entry in docs/api_to_audit.md now describe the new order.
  • No daemon wire change. claimPoolMember is a plugin host method, and its code ships to the daemon in the plugin artifact. Core protocol commands are untouched, so HOST_DAEMON_PROTOCOL_VERSION is not bumped.

How you verified

Real servers, main vs this branch, same input.

  • Setup: pnpm start for main (40b8939) and for this branch, each on its own /tmp data dir. Each server had a second enrolled daemon behind a TCP proxy that adds 100 ms each way (200 ms RTT). The proxy decodes the daemon WebSocket frames and logs each host-rpc.request/response.
  • The project's only checkout was on that remote machine, with a warm pool of size 1.
  • Each round waited for a ready member, then POST /api/v1/threads with environment: {type: "project-default"}, and polled until the environment was ready.
  • 8 rounds per case, with main and branch interleaved.
  • "Behind base" means a new commit was pushed to the remote just before the create, so the member had to fast-forward and run setup again.
200 ms RTT remote machine main branch
POST /threads response, median (min–max) 470 ms (458–477) 240 ms (236–252)
host RPCs awaited in the create request 2 (inspect_git_checkout ×2) 1
environment ready, member at base 1458 ms (1425–1585) 1056 ms (1018–1170)
environment ready, member behind base 2044 ms (2002–2225) 1396 ms (1378–1653)
warm-claim host calls before adopt (at base / behind) 2 / 3 1 / 1
all host RPCs from create to ready (at base / behind) 8 / 9 6 / 6

Sequences seen in the proxy log. A background provider.list_models refresh is left out because the request doesn't await it.

  • main, at base: inspect_git_checkout > inspect_git_checkout > readRemoteBase > checkPoolMember > nameHandedOffBranch > …
  • branch, at base: inspect_git_checkout > claimPoolMember > nameHandedOffBranch > …
  • main, behind: … readRemoteBase > checkPoolMember > fastForward > nameHandedOffBranch > environment.hook.run …
  • branch, behind: … claimPoolMember > nameHandedOffBranch > environment.hook.run …

On the local primary daemon (no proxy, 6 interleaved rounds), the create response went from a 56 ms to a 38 ms median. Time to ready was unchanged within noise: 206 vs 206 ms at base, 386 vs 374 ms behind base.

Tests

  • public-threads.project-default.test.ts now asserts that a branch checkout is inspected exactly once. It fails on main, which inspects twice, and passes here.
  • warm-pools.test.ts:
    • covers the new call order;
    • checks that a refused member's claim passes its base on to the next candidate, so the remote is read once;
    • checks that a member the claim moved is discarded when adopt is refused, and that a member whose claim timed out is discarded.
  • host.test.ts runs claimPoolMember against real git repos: usable, fetch then fast-forward, refused after a rewritten base, offline fallback, used placeholder, and busy lock.
    • It also checks that a check that runs out of time returns timed-out and leaves the member alone, and that a fast-forward slower than the check's budget still finishes. That second test fails when the fast-forward is put back under the check's budget.
  • Folder worktree, real server. A project with one extra repository and a warm pool that branches it. After a new commit landed only in that repository, the thread got the warm worktree, the folder claim moved the folder from 1b44b75 to 0d4e22c, setup ran again, and both worktrees were renamed to the thread's branch. The steps shown match main's wording.
  • Slow fast-forward, real server. A post-merge hook that sleeps 20 s stands in for a slow fast-forward in a large repository. Before the timeout fix, the create waited 15 s, the claim hit its deadline, the member was discarded and a fresh worktree was built. With it, the thread got the warm worktree after the 20 s fast-forward ("Moved the warm worktree from 3eb71f2 to 22a1ba1; setup runs again"), and no member was discarded.
  • Commands:
    • pnpm exec turbo run typecheck lint lint:type-aware --filter=@bb/server --filter=bb-plugin-environment-git-worktree
    • vitest run for the 5 affected server test files (204 tests)
    • the full worktree plugin suite (222 tests)

AGENT GENERATED

🤖 Generated with Claude Code

…rktree in one host call

Thread create now shares one host.inspect_git_checkout result between the
project-default policy and provider placement. The worktree plugin's
claimPoolMember reads the remote base, checks the member and fast-forwards it
in a single plugin host call, replacing readRemoteBase, checkPoolMember and
fastForward.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/new-worktree-thread-fewer-serial-host-calls-thr_cgsqayz78c branch from 2b0fd10 to 9262874 Compare November 24, 2026 20:51
ymichael and others added 3 commits November 24, 2026 14:14
Merging the warm-pool handoff into one claimPoolMember call put the
fast-forward under the 15 s handoff timeout that used to cover only
reading the base and checking the member. A fast-forward that took
longer, as in a large repository or one with Git LFS, cut the claim off,
discarded the member and built a slower fresh worktree instead.

The host now applies the 15 s budget, passed by the server as
checkTimeoutMs, to reading the base and checking the member only. When
that runs out the claim returns timed-out and the member stays in the
pool, as it did before the merge. The call itself gets the create's
15-minute timeout, so the fast-forward has the time it had before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Main (#8065) gave warm worktrees folder worktrees, each read, checked and
fast-forwarded with separate readRemoteBase, checkPoolMember and
fastForward calls. Those calls no longer exist on this branch, so each
folder worktree is now claimed with claimPoolMember too, all at once
once the main worktree's claim succeeds, reusing each folder's remote
base for the next member.

Because a claim can move a checkout, a member whose folder claim is
busy, timed out, or has no readable base is kept only when none of its
worktrees moved; otherwise it is deleted. A folder claim that throws
deletes the member, and so does a refused adopt after any worktree moved.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit af43d56 into main Nov 24, 2026
54 checks passed
@ymichael
ymichael deleted the bb/new-worktree-thread-fewer-serial-host-calls-thr_cgsqayz78c branch November 24, 2026 23:35
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