Skip to content

Read only base commits that can match when comparing a branch to its base - #7079

Merged
ymichael merged 3 commits into
mainfrom
bb/merge-base-check-reads-every-behind-commit-thr_jdbshu29pv
Nov 5, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/merge-base-check-reads-every-behind-commit-thr_jdbshu29pv

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The merge-base status (Git panel's "ahead/behind", committed changes) decides which branch commits are already in the base: cherry-picks via readPatchEquivalentCommits and squash merges via computeSquashMerge. Both called readCommitChanges on every behind commit, i.e. git log --raw over up to 1,000 base commits in 256-commit batches. Only base commits that touch the branch's paths can match, because a cherry-pick needs the same changed-path set and a squash needs every branch path. The per-commit cache lives in the git host worker and is lost on its idle stop, so every cold read paid the full cost. Finding #12 of the hot-path overreach audit (thr_384rg6d3n9 perf-audit.md). This still held on origin/main 7775a0f.

What changed

plugins/git/host/commit-cache.ts

  • New readCommitsTouchingPaths: a single git log --no-walk=unsorted --stdin --name-only --format=%x01%H -- :(literal)<path>… over the behind commits. It returns those with at least one name under their header, i.e. those touching a given path. It reads the path-limited diff output, not git's commit pruning, because git 2.55.0 (the version on CI's Linux and Windows runners) ignores the pathspec when deciding which --no-walk commits to show. Reproduced with a source build of 2.55.0; 2.50, 2.54 and 2.56 prune correctly. Both forms cost the same, ~230 ms vs ~700 ms for the full --raw read over 1,000 behind commits. Commits already in the --raw cache are classified in memory. Commits that match nothing are cached per (commit, path), so a warm recompute (HEAD or base moved) adds no process. Pathspecs over 16 KiB fall back to the old full read (Windows command-line limit).
  • readPatchEquivalentCommits reads the ahead side first. It then filters the behind side by one path per ahead commit (its longest; a matching commit must touch all of its paths) and reads --raw only for the matches.

plugins/git/host/workspace-reads.ts

  • computeSquashMerge filters the scanned behind commits by the longest required branch path (all branch paths when none qualify) before reading changes. The two checks usually pick the same path, so the squash check hits the per-path cache instead of re-filtering.

turbo.json

  • bb-plugin-bb--git#test now depends on //#ensure-native-modules, like the other plugin test tasks that use the SDK's fake plugin host (which opens better-sqlite3). This was a latent gap. Windows CI only passed while another affected package pulled the native install into the same job. With the git plugin as the only affected package, every server.test.ts case failed with "Could not locate the bindings file".

No wire, protocol or SDK changes. No visible behaviour change. The only theoretical difference: cherry-pick pairing used whitespace-stripped path keys, so two files whose names differ only in whitespace no longer count as the same file.

How you verified

Benchmark on real data. Real ~/Projects/bb worktrees (4.4k branches, 110 worktrees, 408k loose objects, 647 packs). A fresh WorkspaceReads.getStatus({ mergeBaseBranch: "origin/main" }) per process (cold worker), then a second read with a different maxFiles (warm recompute). Main and branch interleaved, 7 rounds each. Git time comes from GIT_TRACE2_EVENT. Load average fell from ~50 to ~8 during the run. Medians:

worktree behind / ahead cold getStatus main → branch commit-read git time main → branch warm recompute main → branch
timeline-history 1206 / 1 (equivalent) 2200 → 985 ms 1320 → 363 ms 536 → 542 ms
thr_r5v7pku3ib 1000 / 1 1921 → 1006 ms 1190 → 380 ms 600 → 663 ms
pr4502-repair 692 / 14 1461 → 1172 ms 734 → 504 ms 620 → 616 ms
bb-pr4-git-reads 535 / 34 1201 → 1198 ms 600 → 583 ms 551 → 509 ms

Re-traced at load ~7, bb-pr4-git-reads went from 1977 → 1317 ms cold (commit reads ~1.2 s → ~0.65 s). The remaining cold time is log --left-right and the two ... diffs, which are out of scope here.

The filter alone, wall/CPU against the full --raw read of the same behind commits (low-load samples):

  • 1000 behind: raw 1262/1195 ms vs filter 310/305 ms. It keeps 13 of 1000 commits.
  • 692 behind: raw 764/750 ms vs filter 307/299 ms. It keeps 46.
  • 535 behind: raw 757/741 ms vs filter 316/310 ms. It keeps 70.

Filtering by every ahead path instead of one per commit kept 61–280 commits and was no faster end to end, so I switched to one path per commit.

Tests

  • New workspace-status-cache.test.ts case: a cherry-picked file named picked dir/[1] picked.txt (space and glob characters, which checks the literal pathspec; valid on Windows) plus 20 unrelated base commits. Status is unchanged (1 ahead, 20 behind, squash check complete), and --raw reads cover 3 commits. On origin/main the same test reads 23 and fails (expected 23 to be 3).
  • pnpm exec turbo run test --filter=bb-plugin-bb--git -- host/workspace-status-cache.test.ts host/workspace-reads.test.ts host/git.test.ts: 60 passed. Full --filter=bb-plugin-bb--git suite: 277 passed.
  • pnpm exec turbo run typecheck lint --filter=bb-plugin-bb--git: passed.

AGENT GENERATED

🤖 Generated with Claude Code

…base

The merge-base status read `--raw` for every commit the branch was behind
on each cold read, though only base commits touching the branch's paths can
be cherry-picks or squash merges of it. Filter the behind commits with a
literal pathspec (one path per ahead commit, one required path for the
squash check) and read changes only for the matches. Commits that match no
path are cached per path so warm recomputes stay process-free.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/merge-base-check-reads-every-behind-commit-thr_jdbshu29pv branch from 5b8f96a to 745a217 Compare November 5, 2026 00:45
…ning

Git 2.55.0, which CI's Linux and Windows runners ship, ignores the pathspec
when deciding which --no-walk commits to show, so the filter matched every
behind commit. Read --name-only output instead: the diff is still
path-limited, so a commit touches the paths when any name follows its
header. Also use a test file name that is valid on Windows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/merge-base-check-reads-every-behind-commit-thr_jdbshu29pv branch from 6de6afe to 96793a5 Compare November 5, 2026 00:50
The git plugin's server tests open a better-sqlite3 database through the
SDK's fake plugin host, but its test task never depended on
//#ensure-native-modules. Windows CI only passed when another affected
package pulled that task into the same job; with the git plugin alone, all
server tests failed with "Could not locate the bindings file".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit cc85f36 into main Nov 5, 2026
52 checks passed
@ymichael
ymichael deleted the bb/merge-base-check-reads-every-behind-commit-thr_jdbshu29pv branch November 5, 2026 18:20
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