Skip to content

Read git status for the operation check only when an operation marker exists - #7067

Merged
ymichael merged 1 commit into
mainfrom
bb/duplicate-git-status-in-inspecthostgitsource-thr_2cb7i36jqd
Nov 5, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/duplicate-git-status-in-inspecthostgitsource-thr_2cb7i36jqd

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

getWorkspaceGitOperation ran git --no-optional-locks status --porcelain=v1 --untracked-files=all on every call, even though it only uses that output for hasConflicts, and it returns hasConflicts only when a rebase/merge/cherry-pick/revert marker exists in the git dir. All three callers also run the same status themselves to get hasUncommittedChanges, so every call did two full status walks in parallel:

  • host.inspect_git_source (apps/host-daemon/src/command-handlers/host-branches.ts), used by the branch picker and project routes
  • inspectCheckout, and the attachCheckout readiness check, in plugins/environment-project-checkout/host/checkout.ts

This is finding #8 in the Nov 4 hot-path audit, which looked for hot paths doing more work than their caller needs (after 1a8de2d). The duplicate is still present on origin/main 7775a0f.

What changed

packages/environment-provider-host/src/workspace-git.ts: getWorkspaceGitOperation checks the marker files first and reads status only when it finds one. In the usual case, with no operation in progress, each caller now runs one status instead of two. During an operation the result is computed exactly as before. The return values don't change, so the wire format is the same: no HOST_DAEMON_PROTOCOL_VERSION bump and no plugin SDK change.

How you verified

Benchmark on the real ~/Projects/bb checkout (4,477 local branches, 123 worktrees, clean tree, git 2.50.1). The script calls the real inspectHostGitSource handler and the plugin's inspectCheckout, counting git processes through a logging git shim on PATH. It ran main then branch, alternating for 6 rounds of 20 calls each, after one warm-up call and an 8 s wait so the background remote refresh wasn't included. The host was loaded (load average 15–28), so the absolute times are noisy, but the branch was faster than main in every round.

main branch
inspectHostGitSource git spawns/call 17 (2 × status) 16 (1 × status)
inspectHostGitSource p50, median of 6 rounds 305 ms 216 ms
inspectHostGitSource per-round p50 381 / 259 / 219 / 237 / 381 / 352 279 / 188 / 203 / 228 / 303 / 205
inspectCheckout git spawns/call 7 (2 × status) 6 (1 × status)
inspectCheckout p50, median of 6 rounds 131 ms 93 ms
inspectCheckout per-round p50 167 / 96 / 135 / 142 / 128 / 110 99 / 83 / 93 / 92 / 127 / 81

On its own, a single status on this checkout takes about 80 ms. The two calls ran concurrently and competed for the same index and worktree scan, so the saving is larger than one call running alone would suggest.

Targeted tests: pnpm exec turbo run typecheck test --filter=bb-environment-provider-host --filter=@bb/host-workspace --filter=bb-plugin-environment-project-checkout passed: 12/12 tasks, and the existing getWorkspaceGitOperation tests (no operation, merge conflicts, rebase conflicts) pass. CI runs the full suites.

AGENT GENERATED

🤖 Generated with Claude Code

… exists

getWorkspaceGitOperation ran `git status --untracked-files=all` on every call
to compute hasConflicts, which is only returned when a rebase/merge/
cherry-pick/revert marker exists. Its callers (host.inspect_git_source and the
project-checkout plugin's inspect and attach checks) also run the same status
for hasUncommittedChanges, so each call ran two full status walks.

Check the marker files first and read status only when one exists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 2d912ea into main Nov 5, 2026
88 of 89 checks passed
@ymichael
ymichael deleted the bb/duplicate-git-status-in-inspecthostgitsource-thr_2cb7i36jqd branch November 5, 2026 01:04
ymichael added a commit that referenced this pull request Nov 5, 2026
…h -unormal (#7100)

## 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 #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](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