Repository navigation
perf: batch the dev fleet prune preview's pr lookups - #31165
Conversation
The prune preview asked GitHub once or twice per worktree: a PR-status lookup on a cache miss, then a fresh head-OID check for every merged or closed head. Its network cost grew with the fleet. The preview now fetches every head's upstream PRs up front, one `gh api graphql` request per 50 heads, built from the same pullRequests(headRefName:) connection `gh pr list --head` queries. Each verdict rules on its head's rows exactly as on its own query. A failed batch and a PR found only in a legacy repo still take the per-head path, and the removal re-check never reads the batch. Closes #11529
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the batch only replaces the upstream lookups in the preview. Legacy-repo lookups, heads whose batch failed, and the removal re-check all still query GitHub per head, so the destructive path's freshness holds. Notes
[DESIGN-REVIEWED] 4e0497a Description read[DESCRIPTION-READ] 66f8e35a93f6756ddc6df95247fdf58ef54b18599100573e8a3b44fc361878cd That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing to check. What this change shipsInventory (6 items) — 6 justifiedIntent: let operators of large Dev Fleets preview a prune without spending one or two GitHub round trips per worktree; an ADDITION (perf), tied to #11529.
[FIRST-PRINCIPLES-REVIEWED] 4e0497a Description read[DESCRIPTION-READ] 1b7863ed263166b795bc732e3db06fd258b5b5457f157bd8adfef1fe1db35806 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
buluoray
left a comment
There was a problem hiding this comment.
🤖 Kiro Crew Auto-Pipeline [operator: buluoray]:
APPROVE. No behaviour defect found on 4e0497a.
What it does for users: the Dev Fleet prune preview now asks GitHub once per 50 worktrees instead of once or twice per worktree. A large fleet's preview spends far less rate limit and has more room inside the 30 s proxy budget. Every verdict code and the response shape stay the same.
Same per-PR result as the old per-PR calls:
- The batch connection asks for the same fields
_pr_query_onerequests (number, state, url, isDraft, title, body, headRefOid). It uses the same states, newest-first CREATED_AT order, and a row cap of_PR_HEAD_LOOKUP_LIMIT + 1(worktree_ops.py:255). Sorows[0]is the rowgh pr list --limit 1returned (fleet_state.py:157), and the cached payload is built by the same_pr_payloadon a copy. _fetch_pr_head_oidapplies its unchanged saturation, OPEN-refusal and MERGED rules to the batch rows (fleet_state.py:235, 269). Because the cap is 11 rows, a saturated head still fails closed.- The rows are only used when the lookup repo is the upstream repo (fleet_state.py:235). A PR found in a legacy repo still gets its own query.
- No removal path passes rows. The per-target re-verdict in the prune workers (worktree_ops.py:446) and every
_fetch_pr_head_oidcall in worktree_ops_removal.py still query fresh, which matches the spec paragraph. - New test
test_prune_candidates_batches_pr_lookups_and_keeps_per_head_verdictscompares batched and per-head verdicts on five heads. One of them is a reused head with a newer MERGED PR over an older OPEN one. - Mutation: reading
rows[-1]instead ofrows[0]at fleet_state.py:157 reddens that test.
Partial failure and pagination:
- A chunk whose
gh api graphqlexits non-zero is skipped (worktree_ops.py:293). So is a chunk whose JSON is missingrepositoryor any alias, or where either is null (worktree_ops.py:298). - The
found.updatedict is built in full before it is applied. A malformed chunk therefore contributes no heads at all, and each of its heads falls back to its own per-head query. - Pagination: 50 heads per request, chunked in order. The test pins three calls for five heads at batch size 2.
Tests: 50 passed across test_dev_fleet_server_coverage_prune.py and test_dev_fleet_git_facts_cache.py. CI shows 77 success and 16 skipped, PR Readiness passes, and the head merges cleanly into main. Every bot lane passes; the UX lane skipped because no UI changed.
Source-only:
- That
gh pr list --head <b> --state allqueries the samepullRequests(headRefName:)connection. I took this from the author's manual comparison and from gh's documented list path, and did not run it against the live API. - That
gh api graphqlexits non-zero on a GraphQLerrorspayload. If it exited 0, the missing or null alias would still drop the chunk through the KeyError/TypeError branch.
Follow-ups (non-blocking):
- Coverage gap: the failed-batch fallback is not pinned. Turning a failed chunk into "no PRs for these heads" at worktree_ops.py:293 leaves all 20 prune tests green. In production that would rule every head in the chunk
freshinstead of querying it alone. A test with one failing chunk, asserting per-headgh pr listcalls for exactly that chunk's heads, would pin it. - The chunks run one after another, each with a 15 s timeout. A fleet of 101 or more heads against a hanging GitHub could spend 45 s in the batch before falling back. Running the chunks under the existing
_PRUNE_CONCURRENCYbound would keep the worst case at one timeout.
Problem / Motivation
Goal: The Dev Fleet prune preview asks GitHub a fixed few times per 50 worktrees instead of once or twice for every worktree.
The prune preview (
prune-candidates) looked up each worktree's pull request on its own: agh pr liststatus lookup on a cache miss, then a freshgh pr listhead-OID check for every merged or closed head. #11450 made those calls concurrent, but a fleet of 100+ worktrees still costs 100+ GitHub round trips per preview.Why it matters
Every preview, and every auto-prune pass, spends GitHub rate limit and time in proportion to the fleet, on exactly the large fleets the preview exists to clean up, inside a 30 s proxy budget.
Not a goal
gh pr listindex: measured on the issue, it is always truncated on a large repository.What changed (motivation → approach → change)
Each prune verdict made its own GitHub calls, so the preview's cost grew with the fleet. A capped repo-wide PR list cannot replace them: on a repository with thousands of PRs the page is always full, and a truncated index can mis-rule a reused branch name.
So the batch is keyed on the fleet's own heads.
_prune_candidates(dev_fleet/worktree_ops.py) first calls the new_fetch_pr_rowsbeside it: onegh api graphqlrequest per 50 heads, each head an aliasedpullRequests(headRefName:)connection with the same states, newest-first order and saturation-sentinel row cap thatgh pr list --head <b> --state allqueries. Branch names travel as GraphQL variables, never inside the query text.Each verdict hands its head's rows to the lookups it already makes in
dev_fleet/fleet_state.py._pr_status_cachedand_fetch_pr_statusread the newest row instead of querying the upstream repo;_fetch_pr_head_oidapplies its unchanged saturation, OPEN-refusal and MERGED rules to the rows. A head whose batch request failed, and a PR found only in a legacy repo, still take the per-head query, and no removal path passes rows. The dev-fleet spec's scan paragraph describes the batch.Backwards compatibility
Compatible: the preview's response and every verdict code are unchanged, and each new parameter defaults to the per-head behaviour every other caller keeps.
Removes nothing: no route, field, verdict or check goes away; the per-head query stays for every caller that passes no rows.
Tests
test_prune_candidates_batches_pr_lookups_and_keeps_per_head_verdicts(new): five worktrees (merged, closed, open, a reused head with a newer MERGED over an older OPEN PR, and no PR) with the batch size set to 2 cost exactly threegh api graphqlcalls and nogh pr list, and every verdict equals the one the per-headgh pr listpath gives for the same PRs._prune_candidatesfakes and the full-head-OID test take the new rows argument, and the five candidate-split tests stub the batch so nothing reaches gh; the TTL test's status-fetch fake intest_dev_fleet_git_facts_cache.pytakes it too.-n0and 10 at-n 4 --dist loadgroup --max-worker-restart=2besidetest_dev_fleet_server_coverage.pyandtest_dev_fleet_git_facts_cache.py, and both changed files' 50 tests passed in a shuffled order (seed 8829). With the production change reverted it fails: the preview makes per-headgh pr listcalls instead of threegh api graphqlcalls.Manual verification
Ran the batch query against this repository with
gh api graphqlfor three heads (main, a merged PR's branch, a branch with no PR) and compared each head's rows withgh pr list --head <b> --state all: the same PRs in the same newest-first order, and no rows for the missing head.gh api graphqlexits 1 on a GraphQL error, which the batch treats as a failed request.Screenshots / video
N/A — backend only, no user-visible change.
Related Issues
Closes #11529
Sibling sweep
N/A — a perf change, not a fix or revert.
Pattern harvest
N/A — a perf change, not a fix or revert.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)