Skip to content

Read each git repo fact once per inspect call and check dirtiness with -unormal - #7100

Merged
ymichael merged 1 commit into
mainfrom
bb/follow-up-8-lighter-dirty-check-and-single-repo-thr_x5t864zbae
Nov 5, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/follow-up-8-lighter-dirty-check-and-single-repo-thr_x5t864zbae

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

This is the rest of finding #8 in the Nov 4 hot-path audit; #7067 did the first part. Two problems remained:

  • Dirty check lists every untracked file. hasUncommittedChanges (packages/environment-provider-host/src/workspace-git.ts) ran git status --untracked-files=all, which lists every file inside untracked folders. Every caller only needs yes/no:

    • host.inspect_git_source → hasUncommittedChanges: boolean → the branch picker's dirty flag
    • project-checkout inspectCheckout → server.ts if (inspection.hasUncommittedChanges)
    • the assertSwitchable guard in attachCheckout
  • The same repo questions are asked several times per call. host.inspect_git_source (apps/host-daemon/src/command-handlers/host-branches.ts) asked:

    • "is this a repo" 4 times (detectGitRepoKind, then again inside getCheckoutRef, readDefaultBranchRefs and hasRef)
    • --git-common-dir, --absolute-git-dir and --git-dir once each, as separate processes
    • "does origin/main exist", then rev-parsed main and origin/main again for the relation check

    The project-checkout inspect and attach paths (plugins/environment-project-checkout/host/checkout.ts) had the same pattern. On ~/Projects/bb each git process costs about 15 ms.

What changed

  • --untracked-files=normal for the status read (readPorcelainStatus). It gives the same yes/no: an untracked folder shows as one dir/ entry, and empty or ignored-only folders are still hidden. The same read checks for conflicts during a rebase, merge, cherry-pick or revert. Conflicts only involve tracked files, so the untracked mode doesn't change that answer.
  • New readGitRepo(cwd) → GitRepo | null. It runs one rev-parse --is-inside-work-tree --is-bare-repository --git-dir --absolute-git-dir --git-common-dir and returns { path, kind, gitDir, commonDir, isLinkedWorktree }:
    • gitDir and commonDir are resolved exactly as getAbsoluteGitDir and getGitCommonDir resolve them.
    • isLinkedWorktree uses the same --git-dir "/worktrees/" test as the old detectLinkedWorktree, which this deletes along with its @bb/host-workspace export.
    • detectGitRepoKind and ensureGitRepo now use it; ensureGitRepo returns the GitRepo.
  • These functions now take the GitRepo instead of re-detecting:
    • readCheckoutRef(repo) (getCheckoutRef(cwd) keeps its detect-then-read behaviour)
    • getWorkspaceGitOperation(repo) (uses repo.gitDir)
    • refreshRemoteBranches(repo, …) (uses repo.commonDir)
    • readDefaultBranchRefs(repo)
  • readDefaultBranchRefs makes one for-each-ref call for refs/remotes/origin/HEAD plus refs/heads|refs/remotes/origin × main|master, with their oids and symref. It replaces symbolic-ref + for-each-ref + hasRef (detect + show-ref) + 2 × rev-parse. It queries more refs only when needed:
    • origin/HEAD names some other branch
    • neither main nor master exists locally
    • origin/HEAD is missing or dangling: it falls back to symbolic-ref, so a dangling origin/HEAD still picks the same local branch as before (new test)
  • Callers updated:
    • the inspect_git_source and inspect_git_checkout handlers
    • listBranchOptions
    • project-checkout inspectCheckout and attachCheckout: they read the repo once; locateBranch is one for-each-ref, and its result is passed from planSwitch to assertSwitchable instead of being looked up again
    • worktree resolveWorktreeBaseBranch: ensureGitRepo then readDefaultBranchRefs, the same process count as before
  • No wire change. Results are identical, so there is no HOST_DAEMON_PROTOCOL_VERSION bump. bb-environment-provider-host is internal, so there is no plugin SDK change.

How you verified

Benchmark on the real ~/Projects/bb (4,490 local branches, clean tree, git 2.50.1, on main = origin/main):

  • The script calls the real inspectHostGitSource handler and the project-checkout plugin's inspectCheckout and attachCheckout. The attachCheckout call targets the branch already checked out, so it runs plan + assert and exits as already-current without switching.
  • Git processes are counted with a logging git shim on PATH.
  • main (a detached origin/main worktree outside this checkout) and the branch alternated for 6 rounds of 20 calls each. Each round started with a warm-up call and an 8 s wait so the background remote fetch wasn't counted.
  • Every call returned the same result on main and on the branch.
  • Load average ranged from 4 to 22, and rounds 3–4 ran during a spike, so the table uses the median of the six per-round p50s.
main branch
inspectHostGitSource git spawns/call 15 5
inspectHostGitSource p50 (median of rounds) 182 ms 92 ms
per-round p50 177 / 174 / 661 / 295 / 187 / 177 95 / 186 / 403 / 89 / 88 / 89
inspectCheckout git spawns/call 6 4
inspectCheckout p50 (median of rounds) 87 ms 90 ms
per-round p50 86 / 84 / 570 / 100 / 87 / 87 86 / 516 / 365 / 87 / 87 / 94
attachCheckout (no-op) git spawns/call 10 7
attachCheckout p50 (median of rounds) 169 ms 104 ms
per-round p50 152 / 159 / 592 / 197 / 166 / 172 92 / 315 / 423 / 100 / 100 / 108

Git processes per inspectHostGitSource call on the branch:

  1. rev-parse (repo facts)
  2. symbolic-ref HEAD
  3. rev-parse --verify HEAD
  4. one for-each-ref
  5. status -unormal

inspectCheckout time doesn't change. Its two dropped rev-parse calls ran in parallel with status (~80 ms), which still sets its wall time; the change saves process CPU there, not latency.

-uall vs -unormal: the bb checkout was clean, so its numbers don't show the untracked-files saving. I measured it separately on a throwaway origin/main copy with 5,000 untracked files in 20 untracked folders, taking the median of 15 runs each:

  • -uall: 72 ms, 5,000 output lines
  • -unormal: 62 ms, 1 line

Same yes/no answer. The gap grows with untracked tree size.

Tests:

  • New: hasUncommittedChanges with nested untracked folders vs empty or ignored-only folders.
  • New: readDefaultBranchRefs with a dangling origin/HEAD.
  • The detectLinkedWorktree test became a readGitRepo test covering a linked worktree, an ordinary checkout, a bare root and a plain folder.
  • The existing readDefaultBranchRefs, getCheckoutRef, getWorkspaceGitOperation and project-checkout attach tests pass.
  • pnpm exec turbo run lint typecheck test --filter=bb-environment-provider-host --filter=@bb/host-workspace --filter=bb-plugin-environment-project-checkout --filter=bb-plugin-environment-git-worktree --filter=@bb/host-daemon --only: 12/12 tasks pass. CI runs the full suites.

Left out:

  • tryWithCheckoutMutationLock still runs its own rev-parse --absolute-git-dir, so attachCheckout makes 7 git calls rather than 6. The lock reads the git dir inside its admission lock, and the worktree plugin's teardown also uses it. Passing a known git dir in would change the shared lock API for one process.
  • readCheckoutRef stays 2 processes (symbolic-ref + rev-parse --verify HEAD, run in parallel). Folding HEAD into the repo rev-parse makes the whole command fail in an unborn repo, so it would need a fallback path.
  • Finding fix(agent-runtime): expose user/global ~/.claude settings to Claude SDK #8's "~4 repeated rev-parse checks" in other hasRef callers (worktree base-branch.ts, worktree.ts) are not on the inspect/attach paths, so they're unchanged.
  • The background remote refresh's fetchRemoteBranches still calls ensureGitRepo once per 30 s throttle window.

AGENT GENERATED

🤖 Generated with Claude Code

…h -unormal

host.inspect_git_source and the project-checkout inspect/attach paths
re-asked git whether the path is a repo, where its git dir and common
dir are, and whether each default-branch ref exists before rev-parsing
it again. One rev-parse now returns repo kind, git dir, common dir and
linked-worktree state; it is passed down as a GitRepo. One for-each-ref
reads origin/HEAD and the main/master refs with their oids. The dirty
check uses --untracked-files=normal, since every caller only needs
yes/no.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 87a6e85 into main Nov 5, 2026
48 checks passed
@ymichael
ymichael deleted the bb/follow-up-8-lighter-dirty-check-and-single-repo-thr_x5t864zbae branch November 5, 2026 17:54
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