Skip to content

Cache branch picker ref listings, fetch only origin when a picker opens, and keep results while searching - #7074

Merged
ymichael merged 3 commits into
mainfrom
bb/branch-picker-re-reads-all-refs-and-fetches-per-thr_fbmhvzsajp
Nov 5, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/branch-picker-re-reads-all-refs-and-fetches-per-thr_fbmhvzsajp

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Finding #3 of the Nov 4 hot-path audit. The Git plugin's branches RPC (plugins/git/host.ts) called listBranchOptions with remoteRefresh: "background". So every call listed all of refs/heads and refs/remotes, in two for-each-ref processes using %(refname:short). It also started a throttled git fetch --all --prune. Calls come on every debounced keystroke in a merge-base picker, and on every ref change while a Diff tab is visible: the Diff tab keeps the query live with loadOptions: isLive. A fetch that moves a ref invalidates the refs tag, which refetches branches, which starts the next fetch once the 30 s throttle lapses. The caller only needs about 50 names that match a query. This still held on origin/main at f3ccc8b.

What changed

  • packages/environment-provider-host/src/branch-options.ts: the parsed ref listing is cached per git common dir, so all worktrees of a repo share one entry (LRU of 8). The cache key is a stat fingerprint: inode and mtime of packed-refs, plus every directory under refs/heads and refs/remotes. Git writes refs by lock-and-rename, so any ref add, update or delete changes one of those. Fingerprints with a timestamp less than 2 s old are treated as racy and skip the cache, like git's racy-index rule. Reftable repos always skip it. Searches filter the cached list in memory. Core's host.list_branch_options uses the same function, so the project picker gets the cache too.
  • Git plugin: the branches host read now uses remoteRefresh: "none". A new fetchRemoteBranches RPC (UI → server → host) starts the existing throttled background fetch (refreshRemoteBranches). The merge-base pickers call it when they open. bb git branches calls it before listing (best effort), so the CLI behaves as before. Skill and README updated.
  • fetchRemoteBranches (branch-options.ts) now runs git fetch --prune --quiet <remote> for origin, or for the only remote when there is no origin. If a repo has several remotes and none is named origin, it fetches nothing. This is the one fetch path behind the Git plugin's pickers, core's project branch picker (host.inspect_git_source and host.list_branch_options, both the background refresh and the blocking Refresh) and thread placement, so the change applies to all of them. The 30 s throttle per git common dir is unchanged.
  • Two visible behaviour changes:
    1. A visible Diff tab no longer fetches remotes by itself. Remotes are fetched when a branch picker opens, from bb git branches, and by core's existing paths.
    2. Branches on remotes other than origin (or the single remote) no longer refresh from the pickers, including core's project picker and its Refresh. Run git fetch <remote> to update them.
  • No HOST_DAEMON_PROTOCOL_VERSION bump and no plugin SDK change. The only new wire surface is the Git plugin's own RPC method. If an older plugin host lacks it, the UI ignores the error and the CLI still lists.
  • plugins/git/ui/queries.ts: useGitBranches keeps the last loaded results, for the same environment and selected branch, until the next search answers. Each search is a new query keyed by the typed text, so the list used to go empty on every keystroke and show "Loading branches…". With searches now taking 50–100 ms, that showed as a flicker. The picker already filters as you type, so the old results narrow immediately and are then replaced.

How you verified

Real data: ~/Projects/bb (4,476 local branches, 2,672 remote refs, 123 worktrees, 17 remotes). Load average was 15–45 from other agents, so main and branch runs were interleaved. The harness imports the real listBranchOptions from a git archive copy of origin/main and from this branch. A PATH shim counts git spawns and turns git fetch into a no-op. Scripts and raw output are outside the checkout.

Search keystrokes (10 queries after the first call, per process, 4 interleaved rounds each):

git processes per keystroke keystroke median per round keystroke range first (cold) call
main 5 (2× for-each-ref over 7.1k refs, symbolic-ref, 2× rev-parse) 546, 746, 307, 237 ms 177–2,920 ms 337–1,573 ms
branch 2 (rev-parse ×2) 102, 84, 50, 64 ms 46–204 ms 378–811 ms

Fetch cost on this repo while it still had 18 remotes (17 of them contributor forks, since removed): git fetch --all --prune --dry-run took 147 s wall (13.8 s user, 38 s sys). git fetch origin --prune --dry-run took 7.5 s. The background fetch is killed at 5 s, so on main a visible Diff tab could restart a mostly wasted 5 s fetch --all every 30 s whenever refs moved. On the branch, branches reads start no fetches (see the test below). A 10-minute ps sample of the live machine saw one daemon-side fetch --all. No Diff-tab loop was running then, so I have no live before/after for the loop; the audit's 87 slow calls and the reflog alignment are the evidence for it.

for-each-ref format cost, same repo, 5 runs each: %(refname:short) 382–624 ms, %(refname:lstrip=2) 190–277 ms, %(refname) 210–252 ms. The format is unchanged here; this is context for why the listing is slow.

What fetch --all does when the 5 s background kill hits it. I tested a fixture repo with local bare remotes. Remote new branches were added and stale branches deleted on each remote after the first fetch. "Slow" remotes sleep 8 s in uploadpack. The "blackhole" remote is ssh://[email protected], which never answers. I called refreshRemoteBranches(repo, {}, "background"):

remotes (config order) main: fetch --all --prune this branch
alpha, beta, origin, zeta (all fast) finished in 155 ms; all four updated and pruned 64 ms; origin updated and pruned; others untouched
alpha, beta (slow), origin killed at 5,035 ms; alpha updated and pruned; beta and origin left as before (stale kept, new missing) 71 ms; origin updated and pruned
alpha, beta (blackhole), origin killed at 5,037 ms; alpha updated; origin left as before 73 ms; origin updated and pruned
origin, zeta (slow) killed at 5,044 ms; origin updated and pruned; zeta left as before 59 ms; origin updated and pruned
upstream only n/a 60 ms; upstream updated and pruned
alpha, zeta (no origin) n/a skipped; nothing fetched

fetch --all fetches remotes one at a time in config order. When killed, every remote before the first slow one is fully updated and pruned. The slow one and every remote after it keep their old refs. There are no partial updates and no leftover .lock files, because the kill takes the whole process group. Every failed background attempt also marks the repo for a retry, so the next refresh within 30 s runs again. So with one slow or unreachable remote listed before origin, the 5 s kill meant origin never refreshed.

Tests (each fails before the fix and passes after; checked by reverting):

  • packages/environment-provider-host/test/branch-options-cache.test.ts ages the ref store and then:

    • creates a loose branch
    • deletes a packed-only branch
    • fetches a new remote
    • creates a branch from another worktree of the same repo

    In each case the next listing must reflect the change. A spawn-count test checks that a repeat search runs no for-each-ref. With a constant fingerprint, the four freshness tests fail.

  • plugins/git/host.test.ts: branches reads never reach the remote's upload-pack, and fetchRemoteBranches does. This fails when branches goes back to remoteRefresh: "background".

  • packages/environment-provider-host/test/workspace-git-fetch.test.ts, both interactive and background:

    • with remotes alpha, origin and zeta, only origin is fetched and pruned
    • a lone non-origin remote is fetched
    • several remotes with no origin are skipped

    On the old fetch --all code, the multi-remote and skip cases fail.

  • plugins/git/server/cli.test.ts updated for the CLI's fetch call.

  • plugins/git/ui/queries.test.tsx: while the next search is in flight, the previous branches stay rendered. Fails without the fix.

  • Real app (pnpm start:worktree, fixture repo with 4,400 local and 2,500 remote branches): a headless browser typed in the composer's merge-base picker while a requestAnimationFrame probe counted frames showing "Loading branches". On main: 133/310 and 119/310. With the skeleton fix: 0/311 and 0/311. In the same app, opening the picker fetched a branch added to the bare origin, and it showed up in search about 2 s after opening. With the Diff tab open, three local ref changes over 26 s fetched nothing.

  • pnpm exec turbo run typecheck --filter=bb-plugin-bb--git --filter=bb-environment-provider-host --filter=@bb/host-daemon

  • pnpm exec turbo run typecheck lint test --filter=bb-plugin-bb--git (35 files) and pnpm exec turbo run test --filter=bb-environment-provider-host (5 files)

  • pnpm exec turbo run test --filter=@bb/host-daemon -- test/command/host-branches-dispatch.test.ts, --filter=@bb/server -- test/public/public-project-workspace-routing.test.ts and --filter=bb-plugin-bb-guide

  • lint clean on changed files. CI runs the full suites.

🤖 Generated with Claude Code

AGENT GENERATED

… opens

The Git plugin's branches RPC listed every local and remote ref on each
debounced keystroke and started `git fetch --all --prune` on every call.
The Diff tab keeps that query live, so every ref change refetched it, and
the fetch's own ref writes kept the loop going.

- Core's branch listing caches the parsed refs per git common dir, keyed by
  a stat fingerprint of packed-refs and the refs/heads and refs/remotes
  directories. Refs written within the last 2 seconds, and reftable repos,
  skip the cache.
- The plugin's branches read no longer fetches. A new fetchRemoteBranches
  RPC starts the throttled background fetch; the branch pickers call it when
  they open, and `bb git branches` still calls it before listing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oads

Each search in the Git plugin's branch pickers is a new query keyed by the
typed text, so the list emptied on every keystroke and the picker showed its
loading skeleton until the response arrived. useGitBranches now returns the
last loaded branches for the same environment and selected branch until the
new search answers; the picker filters them as you type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael ymichael changed the title Cache branch picker ref listings and fetch remotes only when a picker opens Cache branch picker ref listings, fetch remotes only when a picker opens, and keep results while searching Nov 5, 2026
The background and blocking branch refresh ran `git fetch --all --prune`.
It walks remotes in config order, and the 5 s background kill stops it at
the first slow or unreachable remote, so remotes listed after it (often
origin) never refreshed. fetchRemoteBranches now fetches origin, falls back
to the single remote when there is no origin, and skips repositories with
several remotes and none named origin. The Git plugin's pickers, core's
project branch picker and thread placement all share this path. The 30 s
throttle per common dir is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael ymichael changed the title Cache branch picker ref listings, fetch remotes only when a picker opens, and keep results while searching Cache branch picker ref listings, fetch only origin when a picker opens, and keep results while searching Nov 5, 2026
@ymichael
ymichael merged commit b9ea287 into main Nov 5, 2026
49 checks passed
@ymichael
ymichael deleted the bb/branch-picker-re-reads-all-refs-and-fetches-per-thr_fbmhvzsajp branch November 5, 2026 17:50
ymichael added a commit that referenced this pull request Nov 5, 2026
…hRemoteBranches (#7132)

## What was wrong

Main is red at 87a6e85. #7074 (b9ea287) and #7100 (87a6e85)
merged minutes apart, and each passed CI on its own base:

- #7074 added `getGitCommonDir` calls to
`packages/environment-provider-host/src/branch-options.ts`. It also
added the Git plugin's `fetchRemoteBranches` RPC, which calls
`refreshRemoteBranches(path, …)`.
- #7100 removed the `getGitCommonDir` import from `branch-options.ts`
and changed `refreshRemoteBranches` to take a `GitRepo` instead of a
path.

On main this gives:
- `Lint and typecheck`: `src/branch-options.ts(191,27): error TS2304:
Cannot find name 'getGitCommonDir'`.
- `ReferenceError: getGitCommonDir is not defined` in `packages-other`,
`packages-host`, `plugins-2` and `integration` (Linux and Windows).

Once the import is back, the Git plugin's `fetches remote branches when
asked, never on a branch read` test still fails. The plugin passes a
path string where `refreshRemoteBranches` now reads `repo.commonDir`, so
no fetch ever starts. Opening a merge-base picker would not fetch
remotes.

## Change

- `branch-options.ts`: import `getGitCommonDir` again.
- `plugins/git/host.ts`: `fetchRemoteBranches` resolves the repo with
`readGitRepo` and skips non-repos, the same way the daemon's
`host.inspect_git_source` caller does after #7100. It then calls
`refreshRemoteBranches(repo, …)`.

## Verification

Against origin/main 87a6e85 plus this change:

- `turbo run typecheck` for `bb-environment-provider-host`,
`bb-plugin-bb--git` and `@bb/host-daemon` passes.
- `bb-environment-provider-host` tests: 48/48 pass, including
`branch-options-cache.test.ts`, which failed on main.
- `bb-plugin-bb--git` tests: 278/278 pass. `host.test.ts > fetches
remote branches when asked` fails on main and fails with the import fix
alone. It passes with this change, and also passed at b9ea287.
- `@bb/host-daemon` `test/command/host-branches-dispatch.test.ts`: 21/21
pass. Several failed on main.
- `tests/integration` `fake/smoke/environment-actions.test.ts`: 5/5
pass. It failed on main.

> 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