Skip to content

Return branch and upstream from github-pr-status views instead of a separate upstream call - #7071

Merged
ymichael merged 1 commit into
mainfrom
bb/pr-discovery-per-environment-git-fan-out-thr_xxncinuj9y
Nov 5, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/pr-discovery-per-environment-git-fan-out-thr_xxncinuj9y

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Finding #11 of the Nov 4 hot-path overreach audit, still present on origin/main (7775a0f). Every github-pr-status load ran prepareLoad, which made a separate upstream host call per environment: rev-parse --is-inside-work-tree, symbolic-ref, rev-parse --verify @{u}. Only after that came the batched views call. The server used the result just to decide "read the known PR URL, or rediscover because the branch moved?" and to record the upstream OID that later ref events compare against. On a branch discovery, views then read the branch again and ran git config --get-regexp twice: getPullRequestTarget, then resolveBranchReadTarget, with overlapping patterns. That is 7 git processes and N + 1 host calls for N environments. It runs on every load: sidebar row watches, polls, refresh, and turn ends.

What changed

All changes are in plugins/github-pr-status.

  • Host contract: each views read takes known: { url, branchName } | null instead of url. Each result is { lookup, refs: { oid, branchName } | null }.
  • Host: views reads the branch and upstream OID once per read. It reads the known URL only while the workspace is still on known.branchName, and otherwise rediscovers from the current branch. A failed ref read now fails only that read, as the separate upstream call used to.
  • Server: prepareLoad no longer calls the host. It records the branch and OID from each views result. Settled PRs still rediscover. upstream is still used for ref events (inspectRefs), unchanged.
  • Discovery: reads branch config with one git config --get-regexp covering both pattern sets. act uses the same read.
  • Docs: one README sentence describing the views result.

There is no user-visible behaviour change. No HOST_DAEMON_PROTOCOL_VERSION bump: the daemon fetches the plugin's host bundle by digest from the server, so the plugin's server and host halves always ship together. No plugin SDK change.

Left out on purpose: the audit's "skip turn-end discovery when the upstream OID is unchanged". bb worktrees are created --no-track, so a git push without -u leaves @{u} unset, and PRs created through MCP tools or on github.com don't move it either. For an environment without a PR, turn end is the main point where such a PR gets discovered, so skipping it would make PRs show up late. That would be a visible change, so it is not in this PR.

How you verified

Benchmark. Real ~/Projects/bb checkout (4,482 branches; 117 worktrees on a branch), main vs this branch, alternating runs. The server plugin runs in the SDK's createFakePluginHost, and host calls go to the real host entry through experimental_createHostEntryHarness against real worktrees. A local gh stub answers GraphQL so the network doesn't hide the local cost. Git processes were counted with a PATH shim in a separate run. The harness calls host methods in process, so it leaves out the per-call server→daemon→worker round trip, which this change also removes. Load average was 33–55 throughout.

117 envs, one pullRequests / refresh per env main branch
Cold discovery: host calls 117 upstream + 1 views 1 views
Cold discovery: git processes 819 (7/env) 468 (4/env)
Cold discovery: wall time, median of 6 9,265 ms (6,367–10,637) 5,087 ms (4,242–6,295)
Known-PR re-read: host calls 117 upstream + 1 views 1 views
Known-PR re-read: git processes 351 (3/env) 351 (3/env)
Known-PR re-read: wall time, median of 6 3,745 ms (3,042–4,635) 4,122 ms (2,893–4,970)

The known-PR re-read does the same git work in one host call instead of 118. Its in-process wall time is within noise.

Tests and checks.

  • Plugin tests: vitest run in plugins/github-pr-status, 176 passed.
  • pnpm exec turbo run lint typecheck --filter=bb-plugin-bb--github-pr-status: clean.
  • New host test: a known URL is read by URL on its branch and rediscovered by branch elsewhere, and both return the branch and upstream OID. It fails if the branch check is dropped.
  • New server test: a known-PR poll is a single views call carrying known, and a ref event reloads only when the upstream OID moved. It fails if the OID from views isn't recorded.

AGENT GENERATED

🤖 Generated with Claude Code

…eparate upstream call

Every PR load first made an `upstream` host call per environment (detect,
symbolic-ref, rev-parse @{u}) and then the batched `views` call, which for a
branch discovery read the branch again and ran `git config --get-regexp`
twice. Each read in `views` now returns the workspace's branch and upstream
OID, the server passes the known PR URL with the branch it was found on, and
the host rediscovers when the branch moved. A discovery reads branch config
once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit e6b0c77 into main Nov 5, 2026
48 checks passed
@ymichael
ymichael deleted the bb/pr-discovery-per-environment-git-fan-out-thr_xxncinuj9y branch November 5, 2026 00:56
ymichael added a commit that referenced this pull request Nov 5, 2026
## Human comments

## What was wrong

Merge, Mark ready and Convert to draft did two lookups to find a pull
request bb already had. First, `requireActionTarget` always ran an
uncached branch discovery (`loadPullRequest(env, null, true)`: git
config reads, then a GraphQL search by head branch). Then `act` ran `gh
pr merge` with no selector, after reading the branch config again. With
no selector, gh resolves the PR from the branch itself: five more git
processes (`remote -v`, two `config --get-regexp`, `symbolic-ref`,
`rev-parse @{push}`) and its own GraphQL search by branch. Each git
process is slow in `~/Projects/bb` (about 4.5k branches, a 10k-line
`.git/config`). This is finding #13 in the Nov 4 hot-path audit ("Merge:
precheck by URL and pass it to gh").

## What changed

Only `plugins/github-pr-status`. This is rebased on #7071, which made
`views` take `known: { url, branchName }` and return the workspace's
`refs`.

- `server.ts`: the action precheck passes the PR URL the store already
holds as `known`. It still bypasses caches, so state and mergeability
are checked fresh. The precheck now gets `refs` back from its own
`views` result. It then rediscovers by branch when the PR has merged or
closed, or when its `headRefName` isn't `refs.branchName` from that same
read. A stale stored URL is therefore never acted on. For example, after
a branch switch, a refused Mark ready updates the plugin's branch map
without touching the stored URL, and the next Merge would otherwise act
on the old branch's PR. A fork PR whose upstream branch has a different
name never matches the local branch, so it always rediscovers. That
costs main's lookup plus one URL read, and still acts on the right PR.
With no stored URL, the precheck discovers by branch, as before. `act`
receives the URL of the PR that passed the precheck.
- `contract.ts` / `host.ts` / `host/git-host.ts`: `act` takes a required
`url` and runs `gh pr merge|ready <url> …`. The host no longer reads the
current branch or the upstream config for actions.
`runPullRequestActionForCurrentBranch` becomes `runPullRequestAction`.
- One visible change: an action from a detached workspace now fails in
the precheck with "No pull request found" instead of the host's "Cannot
update pull request from a detached workspace".
- No `HOST_DAEMON_PROTOCOL_VERSION` bump and no plugin SDK change. `act`
is this plugin's own host contract, and the server ships the host
artifact for each plugin generation.
- README sentence on actions updated.

## How you verified

**Real data**: the `~/Projects/bb` worktree for this PR's own branch
(open PR #7070), measured on origin/main after #7071 against this
branch. The plugin's real host entry ran under
`experimental_createHostEntryHarness`, making the same `views` → `act`
calls the server makes for a merge. A `gh` shim on `PATH` rewrote `gh pr
merge|ready <args>` into `gh pr view <same selector> --json <the fields
gh pr merge fetches>`. That exercises gh's PR finder without merging.
The merge mutation itself is the same on both sides and is not in these
numbers. Three interleaved rounds of 15 per side ran at load average
8–11.

| per merge | main (after #7071) | branch |
|---|---|---|
| git processes | 12 | 3 |
| gh processes (GraphQL round trips) | 2 | 2 |
| precheck p50 (n=45) | 853 ms | 767 ms |
| act, excluding the mutation, p50 (n=45) | 614 ms | 433 ms |
| total p50 / mean (n=45) | 1494 / 1510 ms | 1188 / 1252 ms |

Per-round total p50, main → branch: 1552 → 1242, 1466 → 1170, 1424 →
1188 ms.

What remains is GitHub latency: one GraphQL read for the precheck plus
gh's own read before the mutation.

**Tests**: `pnpm exec turbo run test typecheck lint
--filter=bb-plugin-bb--github-pr-status` passes (179 tests). New server
tests use a fake host that applies the real `views` rule: URL read only
when `known.branchName` matches the workspace branch.
- A merge after a load prechecks with `known` set to the stored URL and
branch, and passes that URL to `act`.
- When the stored PR has closed and the branch has a newer PR, the newer
PR's URL is used.
- Branch switch, then a refused Mark ready, then Merge: the stored URL
points at an open PR for another branch, so the action must go to the
current branch's PR. Without the head-branch check this test fails,
because it acts on pull/6 instead of pull/7.
- Host tests assert `gh pr ready|merge <url>` args, including from a
fork-tracking workspace.

> 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