Skip to content

perf(source-control): index main's first-parent chain and batch pin reads in the post-merge check - #10406

Merged
kyle-sexton merged 2 commits into
mainfrom
perf/postmerge-ancestry-index
Oct 27, 2026
Merged

kyle-sexton merged 2 commits into
mainfrom
perf/postmerge-ancestry-index

Conversation

@kyle-sexton

@kyle-sexton kyle-sexton commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

No related issue: efficiency plan item 1 alternative (GitHub Support hub), per-tick post-merge check cost

Summary

babysit_postmerge.py check() runs as pr-merge's "Check earlier lane merges" step on every 15-minute post-merge-dispatch tick. In dispatch job 121959899053 that step took 543 s (01:46:45 to 01:55:47) over 154 merges and made 248 gh calls, 199 of them GraphQL. It made no compare REST calls, so every contains miss went to a local git merge-base --is-ancestor spawn.

A local run of the same check (--lane-merges-only, 154 merges, instrumented by a throwaway wrapper that is not committed) put the time here:

Cost Before After
_local_contains calls (each a git merge-base spawn before) 38,634 calls, 193.6 s 39,099 calls, under 0.05 s in total
pin read-back 150 GraphQL queries, 71.4 s 8 GraphQL queries, 7.7 s
whole check 781.7 s 518.6 s

Both runs gave the same verdict: readable, ok, 154 of 154 verified. The local run took 484-485 artifact-listing reads (about 166 s) because it had no lane records file. CI passes that file, so CI made 2 such reads. That cost exists only locally and this change leaves it alone.

Fix

  • Ancestry index. _Context._chain_contains reads git rev-list --first-parent of the first full-history clone's copy of the branch (refs/remotes/origin/<branch>, else refs/heads/<branch>, the same lookup _tip_bridge uses, now shared as _local_branch). It reads it once per check, through policy._git, so replace refs and graft files are ignored. If both commits are on that chain, the older one is the other's ancestor and the newer one is not. If either commit is off the chain (reachable only through a merge's second parent, or absent), the method returns None and today's merge-base path, bridge and compare fallback answer it. It never returns False for such a commit. With no branch, no clone, a shallow clone or an unreadable list, there is no index.
  • Batched pin reads. read_pins reads the first comment page of up to 20 pull requests in one aliased GraphQL query (PostMergeHeadPins), in the alias shape babysit_merge_skip.py uses. A thread with more pages continues one pull request at a time on the unchanged PIN_QUERY, and the batched page counts toward PIN_PAGE_LIMIT. read_pin is split into _pin_thread (page walk) and _pin_sha (evaluation), which both paths share, so an edited newest pin is still no pin. Any errors, or a pull request the response leaves out, raises PostMergeReadError, so the verdict is unreadable as before. pin_reader now returns a PinReader. check() calls its prefetch with exactly the merges whose pin it reads, fresh on every check. read_pin(alone=True) in the merge policy is unchanged.

Verification

  • pytest over plugins/source-control/skills/babysit-prs/scripts/tests: 3151 passed.
  • New FirstParentChain tests, on a fixture with --no-ff merges, second-parent-only commits and an unmerged commit. For every pair, the result equals git's own merge-base --is-ancestor. Pairs on the chain spawn no merge-base, and off-chain pairs do. The chain is read once. A graft file does not shape the chain. With no branch, no chain is read. Flipping the comparison to <= turns these tests red.
  • New test_babysit_postmerge_pins.py tests:
    • A batch over 25 pull requests makes 2 queries.
    • A 271-comment thread pages on its own, with cursors 100 and 200.
    • An edited pin reads as None.
    • The batched result equals read_pin for each pull request.
    • A pull request missing from the response raises. Making it return None instead turns this test red.
    • A response carrying errors raises.
    • A thread past the page limit reads as no pin.
    • A new prefetch reads afresh.
  • Check-level tests: the check reads its pins in one PostMergeHeadPins query, and a merge missing from the batch makes the verdict unreadable.
  • scripts/check-affected.sh --uncommitted: 12 passed, 5 skipped. check-fixture-git-isolation.sh and check-cant-fail-scan.sh pass. Ruff runs through scripts/run-ruff.sh.

Related

  • babysit_merge_skip.py, the aliasing pattern followed.
  • publish-promotion-evidence.mjs mirrors PIN_QUERY, which is unchanged.

🤖 Generated with Claude Code

kyle-sexton and others added 2 commits October 26, 2026 22:57
…eads in the post-merge check

`_Context.contains` answers two commits on the local clone's first-parent
chain of the branch from one `git rev-list --first-parent`, and spawns
`git merge-base --is-ancestor` only for a commit off that chain. The pin
read-back for every judged merge goes 20 pull requests to a GraphQL query
(`read_pins`), paging a thread over 100 comments on its own. Verdicts are
unchanged.

Measured on a local run of `--lane-merges-only` over 154 merges: the
merge-base spawns went from 38,634 calls taking 193.6 s to none, and the
pin reads from 150 queries taking 71.4 s to 8 taking 7.7 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton marked this pull request as ready for review October 27, 2026 03:24
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T03:28:36.366171Z 49fee9e Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

claude-review reviewed 49fee9ee6c9eb89c01e1dd8971cff33323e026ac: 0 finding(s), 0 as inline comments.

No findings. I read the full diff, the surrounding babysit_postmerge.py code, and policy._git and _graphql. I ran no tests, so the PR's test counts are author-claimed and I haven't verified them.

Ancestry index (_chain_contains).

  • Ordering is right: on a first-parent list, a later index is an older commit, so at >= of means "ancestor" and includes the equal case.
  • It answers only when both commits are on the chain. Anything else returns None, so merge-base, the bridge and the compare API still decide it. It never returns False for an off-chain commit.
  • The chain is read through policy._git, which passes --no-replace-objects. Graft files are ignored, and the graft-file test covers that case.
  • _local_branch reuses the _tip_bridge ref lookup. The new if not self.branch guard only skips a ref lookup that couldn't have matched.

Batched pin reads (read_pins, PinReader).

  • _pin_thread and _pin_sha keep the original page-walk and evaluation behaviour. The batched first page counts toward PIN_PAGE_LIMIT.
  • reviews is None on batched pages. Only the alone=True path reads it, and the batched path never sets alone.
  • _graphql raises on any errors and on missing data. A pull request the response leaves out raises PostMergeReadError, so the verdict stays unreadable.
  • check() prefetches exactly the merges it later judges (pinned_merges | optional_pins), so no extra pull request can make the read fail. prefetch runs inside the try that turns PostMergeReadError into an unreadable verdict.
  • PinReader falls back to a single read for any number it didn't prefetch, and prefetch replaces the cache, so a pin read is never stale across checks.

Tests. The new tests cover the risky edges:

  • every pair is compared against git's own merge-base --is-ancestor;
  • off-chain, unmerged, graft-file and no-branch cases;
  • the 25-pull-request batch, paging, an edited pin, a missing alias and an errors response.

Manifests and cost. The change touches no plugin manifest and uses a changelog fragment with bump: patch, which fits an internal performance change with unchanged verdicts. It spends no prepaid credit, adds no billed resource, and sets no hold.

babysit_postmerge.py is on the trusted-path list, so I read its whole change. The check keeps its fail-closed behaviour (unreadable rather than guessed), and nothing here widens what the post-merge gate accepts.

@github-actions

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 49fee9ee6c9eb89c01e1dd8971cff33323e026ac: 0 finding(s), 0 as inline comments.

No security findings in #10406.

critical-findings: 0

Scope. The change is in babysit_postmerge.py (a trusted path), a changeset file, and tests. I read its whole change and what it gates. No plugin manifest is touched, so there are no manifest <field> lines. No .github/ workflow or action changed. The PR description contained no embedded instructions.

What the trusted path gates. check() is the post-merge verdict on earlier lane merges. It decides ok, unverified or unreadable. The change speeds up two reads. It does not loosen the verdict logic.

Ancestry index (_chain_contains, _local_contains).

  • The answer comes from the index only when both commits are on the first-parent chain of the local branch copy. Then the older commit (larger index) is an ancestor of the newer one, and at >= of also covers the equal case. Git object IDs are content-addressed, so this is a fact about the commits whatever clone it was read from.
  • A commit off the chain, or absent from it, returns None and falls through to the existing merge-base, bridge and compare path. The index can never return False for such a commit, so it cannot turn a contained commit into a missing one, or the reverse.
  • The chain is used only if the list is all full SHAs and starts at the tip. A failed or partial rev-list leaves it empty and falls back to merge-base.
  • policy._git runs with --no-replace-objects, GIT_GRAFT_FILE=/dev/null and GIT_NO_LAZY_FETCH=1, so grafts and replace refs cannot reshape the chain. A test covers the graft case.
  • _local_branch is the old _tip_bridge lookup, extracted. It adds an empty-branch guard that returns None.

Batched pin reads (read_pins, _pin_thread, _pin_sha, PinReader).

  • The aliases use p{n} with n an int taken from merge numbers, so there is no query injection surface. Owner and repo go in as GraphQL variables.
  • A response with errors, a missing alias, a null pull request, or a number mismatch raises PostMergeReadError, which makes the verdict unreadable as before.
  • Pin evaluation is shared with read_pin through _pin_sha, so an edited newest pin is still no pin and the identity match is unchanged. The batch fragment omits reviews, but _pin_sha reads it only when alone=True. The batched path never sets that, and the merge-policy read_pin(alone=True) is untouched.
  • prefetch runs inside the try that turns read errors into an unreadable verdict. It covers exactly the merges whose pins are read, and each check replaces the cache. A merge not prefetched falls back to a single read.
  • One small behaviour change: pins are now read once before judging, not at each merge's judge time. An edit made between the batch read and the judgment would be seen one check later. I did not count it: it needs write access to the agent identity's comments, and the next 15-minute tick would catch it.

Spend. No billable change. The change reduces API and CPU use per tick.

@kyle-sexton
kyle-sexton added this pull request to the merge queue Oct 27, 2026
Merged via the queue into main with commit 9a96c8e Oct 27, 2026
102 of 103 checks passed
@kyle-sexton
kyle-sexton deleted the perf/postmerge-ancestry-index branch October 27, 2026 03:39
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