fix(server): background GitHub polling uses ~74% fewer calls with batched GraphQL (#14673)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-10-01 16:48:29 -07:00
1 parent a3abb52660
commit 10ac2f2ba4
15 files changed
+1039 -213

No files matched your search

@@ -1821,7 +1821,12 @@ export const make = Effect.gen(function* () {
const batchable = entries.filter(
(entry) => buildPullRequestSummariesGraphQlQuery([entry.request]) !== null,
);
const query = buildPullRequestSummariesGraphQlQuery(batchable.map((entry) => entry.request));
// Stack membership rides along where GitHub serves stacks, so the background sync can skip
// the REST stack read for pull requests that are in none.
const query = buildPullRequestSummariesGraphQlQuery(
batchable.map((entry) => entry.request),
first.request.host === "github.com",
);
const batched =
query === null
? Effect.succeed(new Map<number, GitHubPullRequestSummary>())
@@ -116,6 +116,11 @@ export interface ProviderChangeRequestSummary {
readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
readonly checksState?: PullRequestChecksState | null | undefined;
readonly mergeability?: PullRequestMergeability | undefined;
/**
* The host-native stack the pull request sits in, from the same read. Null when the host says
* it is in none; absent when the read did not ask.
*/
readonly stack?: PullRequestStackMembership | null | undefined;
}
/** One layer of a host-native stack, bottom to top order is the array's. */
@@ -1596,6 +1596,7 @@ export const make = Effect.gen(function* () {
...(changeRequest.mergeability === undefined
? {}
: { mergeability: changeRequest.mergeability }),
...(changeRequest.stack === undefined ? {} : { stack: changeRequest.stack }),
})),
);
}),
@@ -1934,5 +1934,43 @@ describe("batched pull request summaries", () => {
mergeability: "unknown",
additions: 0,
});
// The document did not ask about stacks, so the summary does not claim an answer.
expect(decoded.success.get(0)).not.toHaveProperty("stack");
});
it("reads stack membership only where the document asked for it", () => {
expect(
buildPullRequestSummariesGraphQlQuery([{ repository: "acme/web", number: 7 }], true),
).toContain("stack { number size baseRefName } stackEntry { position }");
expect(
buildPullRequestSummariesGraphQlQuery([{ repository: "acme/web", number: 7 }]),
).not.toContain("stack {");
const pullRequest = (number: number, stack: Record<string, unknown>) => ({
pullRequest: {
number,
title: "Stacked",
url: `https://github.com/acme/web/pull/${number}`,
headRefName: `feat/${number}`,
baseRefName: "main",
state: "OPEN",
updatedAt: "2026-08-24T00:00:00Z",
...stack,
},
});
const decoded = expectSuccess(
decodePullRequestSummariesJson(
JSON.stringify({
data: {
s0: pullRequest(7, { stack: null, stackEntry: null }),
s1: pullRequest(8, {
stack: { number: 3, size: 2, baseRefName: "main" },
stackEntry: { position: 2 },
}),
},
}),
),
);
expect(decoded.get(0)?.stack).toBeNull();
expect(decoded.get(1)?.stack).toEqual({ number: 3, size: 2, base: "main", position: 2 });
});
});
@@ -1852,6 +1852,8 @@ export function decodePullRequestStatsJson(
* shape as the search row where the two overlap: the checks arrive as GitHub's one-word rollup
* rather than the whole check list `gh pr view` hands back, which is what keeps a batch cheap.
*/
const STACK_MEMBERSHIP_SELECTION = "stack { number size baseRefName } stackEntry { position }";
const PULL_REQUEST_SUMMARY_SELECTION =
"number title url state isDraft mergeable reviewDecision additions deletions changedFiles " +
"updatedAt mergedAt closedAt headRefName baseRefName " +
@@ -1865,6 +1867,7 @@ const PULL_REQUEST_SUMMARY_SELECTION =
*/
export function buildPullRequestSummariesGraphQlQuery(
changeRequests: ReadonlyArray<{ readonly repository: string; readonly number: number }>,
includeStacks = false,
): string | null {
if (changeRequests.length === 0) return null;
const selections: string[] = [];
@@ -1874,7 +1877,7 @@ export function buildPullRequestSummariesGraphQlQuery(
if (!REPOSITORY_PART.test(owner) || !REPOSITORY_PART.test(name)) return null;
if (!Number.isSafeInteger(changeRequest.number) || changeRequest.number <= 0) return null;
selections.push(
` s${index}: repository(owner: "${owner}", name: "${name}") { pullRequest(number: ${changeRequest.number}) { ${PULL_REQUEST_SUMMARY_SELECTION} } }`,
` s${index}: repository(owner: "${owner}", name: "${name}") { pullRequest(number: ${changeRequest.number}) { ${PULL_REQUEST_SUMMARY_SELECTION}${includeStacks ? ` ${STACK_MEMBERSHIP_SELECTION}` : ""} } }`,
);
}
return `query PullRequestSummaries {\n${selections.join("\n")}\n}`;
@@ -1920,6 +1923,8 @@ export interface GitHubPullRequestSummary {
readonly reviewDecision: PullRequestReviewDecision | null;
readonly checksState: PullRequestChecksState | null;
readonly mergeability: PullRequestMergeability;
/** Null when GitHub says the pull request is in no stack; absent when the read did not ask. */
readonly stack?: PullRequestStackMembership | null;
}
/**
@@ -1965,6 +1970,7 @@ export function decodePullRequestSummariesJson(
}),
),
mergeability: toMergeability(pr.mergeable),
...(pr.stack === undefined ? {} : { stack: toStackMembership(pr) ?? null }),
});
}
return Result.succeed(summaries);