Skip to content

Read only the checkout when refreshing a reused environment's branch - #7103

Merged
ymichael merged 1 commit into
mainfrom
bb/follow-up-6-cheap-branch-refresh-for-reused-chec-thr_442866cf7g
Nov 5, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/follow-up-6-cheap-branch-refresh-for-reused-chec-thr_442866cf7g

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Sometimes an environment provider answers attach with a path that an existing ready environment already holds. This happens for threads in the project's own checkout, or for a worktree thread pointed at an existing worktree. Core then adopts that row (bindEnvironmentPath) and calls refreshAttachedEnvironmentBranch before the first turn. That function used host.inspect_git_source with remoteRefresh: "background". The daemon then ran:

  • a full git status --porcelain=v1 --untracked-files=all
  • the default-branch ref reads (symbolic-ref origin/HEAD, for-each-ref, show-ref, rev-parse ×2)
  • the operation and linked-worktree probes
  • a background git fetch --all --prune

The refresh only needs the branch currently checked out. Finding #6 in the Nov 4 hot-path audit.

What changed

  • apps/server/src/services/threads/thread-environment-placement.ts: refreshAttachedEnvironmentBranch now calls host.inspect_git_checkout (repo check, symbolic-ref HEAD, rev-parse HEAD) and records only branchName. No fetch.
  • packages/db/src/data/environments.ts: recordEnvironmentCurrentBranch no longer accepts defaultBranch, because its only caller no longer passes it.
  • No wire change: host.inspect_git_checkout already exists, so HOST_DAEMON_PROTOCOL_VERSION is unchanged.

Left out, and why:

  • Refreshing defaultBranch on reuse. The row keeps the value set at provisioning. The daemon workspace watch reports defaultBranch changes while the environment is watched, and the git plugin reads the default branch live (workspaceDefaultBranch before the row value). Re-reading it here would need the extra ref reads or a new daemon field. The old code also wrote the current branch into defaultBranch when no default could be found.
  • The refresh runs twice per thread. On main, every probe in the trace appears twice at the same timestamps, including two concurrent git status runs. The branch build still shows two copies of the checkout read (2 × symbolic-ref + rev-parse). The fix for that is in the advancement scheduling and is out of scope here. At about 8 ms per process it is now cheap.
  • Live measurement of the existing-worktree case. It uses the same function. I measured only the project-checkout case.

How you verified

Measured on the real ~/Projects/bb (4,489 local branches, 1 remote). Two source servers, origin/main fa30b0a7dc and this branch, each on a fresh data dir and run at the same time. GIT_TRACE2_EVENT recorded every git process. Each sample creates a thread with environment: {type: "provider", environmentProviderId: "project-checkout"} (what the composer sends) against an existing ready project-checkout row. Wall time runs from the thread's created_at to its turn/started event. There were 10 interleaved pairs, with a 16 s gap so main's background fetch finished before the next sample.

main branch
git processes before the first turn 34–37 12
git status -uall before the first turn 2 (60–920 ms each) 0
create → provisioning done (median) 365 ms 155.5 ms
create → turn started (median, min–max) 410 ms (352–1,219) 196.5 ms (184–584)
fetch --all --prune started 6 / 10 (others hit the 30 s throttle) 0 / 10

When the fetch ran on main, it took about 3.3 s in the background, plus a rev-list --objects --all child (~1 s) and maintenance run --auto. Load average during the series fell from 31 to 4. The branch outliers (338 ms, 584 ms) came at the highest load.

Branch correctness, live on the branch server: in a scratch repo project I created thread 1, ran git switch -c feature/live-check, then created thread 2. The reused row changed to branch_name = feature/live-check and default_branch stayed main.

Tests:

  • apps/server test/threads/environment-providers.test.ts: the reused-row refresh test now asserts that exactly one host.inspect_git_checkout runs, that no host.inspect_git_source runs, and that defaultBranch is preserved. This fails on main. The ordering test and the merge-base reuse tests now observe the checkout read.
  • apps/server test/threads/, test/environments/, test/public/public-threads.project-default.test.ts, test/public/public-project-workspace-routing.test.ts: 678 passed.
  • packages/db test/data/environments.test.ts: 16 passed.
  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/db: passed.

AGENT GENERATED

🤖 Generated with Claude Code

When a provider answers attach with a path an existing ready environment
already holds, core adopts that row and refreshes its branch before the
first turn. The refresh used host.inspect_git_source with a background
remote refresh: a full status -uall, default-branch ref reads, and
fetch --all --prune. It only needs the current branch, so it now uses
host.inspect_git_checkout and leaves defaultBranch to provisioning and
the workspace watch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit c26a707 into main Nov 5, 2026
32 checks passed
@ymichael
ymichael deleted the bb/follow-up-6-cheap-branch-refresh-for-reused-chec-thr_442866cf7g branch November 5, 2026 17:36
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