Skip to content

Commit 12198f9

Browse files
t3dotggclaude
andcommitted
fix(server): background GitHub reads share batched GraphQL documents
Branch PR discovery ran one `gh pr list --head` per branch each minute and again for every settled branch at startup. Lookups on one repository now share an aliased GraphQL document (100 heads cost 1 point), resolved with gh's own base-repository rule and falling back to `gh pr list` whenever gh could pick differently. PR link syncs queued one sweep per link, so a burst never formed a summary batch. Requested keys now wait for one sweep. Summaries on github.com carry stack membership, so the sync skips the REST stack read for pull requests GitHub places in no stack. The REST `gh api rate_limit` probe is gone. Its GraphQL entry disagreed with GraphQL's own `rateLimit` and, carrying a later reset, shadowed the in-band readings. The budget now takes one GraphQL `rateLimit` reading per host and credential at each reset (at most every ten minutes), and GraphQL documents keep it current in between. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a3abb52 commit 12198f9

15 files changed

Lines changed: 1039 additions & 213 deletions

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as Logger from "effect/Logger";
1313
import * as Option from "effect/Option";
1414
import * as PlatformError from "effect/PlatformError";
1515
import * as References from "effect/References";
16+
import * as Result from "effect/Result";
1617
import * as Schema from "effect/Schema";
1718
import * as Scope from "effect/Scope";
1819
import { TestClock } from "effect/testing";
@@ -32,6 +33,7 @@ import {
3233
TextGenerationError,
3334
} from "@t3tools/contracts";
3435
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
36+
import { decodeGitHubPullRequestListJson } from "../sourceControl/gitHubPullRequests.ts";
3537
import * as GitLabCli from "../sourceControl/GitLabCli.ts";
3638
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
3739
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
@@ -510,6 +512,30 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
510512
return {
511513
service: {
512514
execute,
515+
// The fake answers the CLI shape, so batched lookups read it the way the fallback does.
516+
listPullRequestsByHead: (input) =>
517+
execute({
518+
cwd: input.cwd,
519+
args: [
520+
"pr",
521+
"list",
522+
"--head",
523+
input.headSelector,
524+
"--state",
525+
input.state,
526+
"--limit",
527+
String(input.limit),
528+
"--json",
529+
"number,title,url,baseRefName,headRefName,state,isDraft,mergedAt,closedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
530+
],
531+
}).pipe(
532+
Effect.map((result) => {
533+
const raw = result.stdout.trim();
534+
if (raw.length === 0) return [];
535+
const decoded = decodeGitHubPullRequestListJson(raw);
536+
return Result.isSuccess(decoded) ? decoded.success : [];
537+
}),
538+
),
513539
listOpenPullRequests: (input) =>
514540
execute({
515541
cwd: input.cwd,

‎apps/server/src/orchestration/PullRequestSyncReactor.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,88 @@ describe("PullRequestSyncReactor", () => {
754754
),
755755
);
756756

757+
it.effect("reads a burst of requested links in one sweep", () =>
758+
Effect.scoped(
759+
Effect.gen(function* () {
760+
yield* TestClock.setTime(Date.parse(NOW));
761+
const reading = yield* Deferred.make<void>();
762+
const release = yield* Deferred.make<void>();
763+
const fixture = yield* makeHarness({
764+
snapshot: makeSnapshot([]),
765+
summary: (input) =>
766+
Effect.gen(function* () {
767+
if (input.number === 1) {
768+
yield* Deferred.succeed(reading, undefined);
769+
yield* Deferred.await(release);
770+
}
771+
return makeSummary(input);
772+
}),
773+
});
774+
yield* Effect.gen(function* () {
775+
const reactor = yield* startAndSweep(fixture);
776+
yield* Ref.set(
777+
fixture.snapshots,
778+
makeSnapshot(
779+
[1, 2, 3, 4].map((number) =>
780+
makeThread(`thread-${number}`, { pullRequests: [makeLink(number)] }),
781+
),
782+
),
783+
);
784+
const request = (number: number) =>
785+
reactor.requestSync({ host: "github.com", repository: "owner/repository", number });
786+
yield* request(1);
787+
yield* Deferred.await(reading);
788+
yield* Effect.forEach([2, 3, 4], request, { discard: true });
789+
yield* Deferred.succeed(release, undefined);
790+
yield* reactor.drain;
791+
// One sweep for the first link and one for the three that arrived while it read.
792+
assert.strictEqual(yield* Queue.size(fixture.snapshotReads), 2);
793+
assert.deepStrictEqual(
794+
(yield* Ref.get(fixture.summaryCalls)).map((ref) => ref.number).toSorted(),
795+
[1, 2, 3, 4],
796+
);
797+
}).pipe(Effect.provide(fixture.layer));
798+
}),
799+
),
800+
);
801+
802+
it.effect("skips the stack read for a pull request the summary places in no stack", () =>
803+
Effect.scoped(
804+
Effect.gen(function* () {
805+
yield* TestClock.setTime(Date.parse(NOW));
806+
const fixture = yield* makeHarness({
807+
snapshot: makeSnapshot([
808+
makeThread("unstacked", { pullRequests: [makeLink(1)] }),
809+
makeThread("stacked", { pullRequests: [makeLink(2)] }),
810+
makeThread("unknown", { pullRequests: [makeLink(3)] }),
811+
]),
812+
summary: (input) =>
813+
Effect.succeed(
814+
makeSummary(
815+
input,
816+
input.number === 1
817+
? { stack: null }
818+
: input.number === 2
819+
? { stack: { number: 9, position: 1, size: 2, base: "main" } }
820+
: {},
821+
),
822+
),
823+
});
824+
yield* Effect.gen(function* () {
825+
yield* startAndSweep(fixture);
826+
assert.deepStrictEqual(
827+
(yield* Ref.get(fixture.stackCalls)).map((ref) => ref.number).toSorted(),
828+
[2, 3],
829+
);
830+
assert.deepStrictEqual(
831+
(yield* Ref.get(fixture.syncCommands)).map((command) => command.number).toSorted(),
832+
[1, 2, 3],
833+
);
834+
}).pipe(Effect.provide(fixture.layer));
835+
}),
836+
),
837+
);
838+
757839
it.effect("polls open pull requests on settled threads every fifteen minutes", () =>
758840
Effect.scoped(
759841
Effect.gen(function* () {

‎apps/server/src/orchestration/PullRequestSyncReactor.ts‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export const make = Effect.gen(function* () {
133133
const lastSyncedAt = new Map<string, number>();
134134
const requested = new Map<string, number>();
135135
let requestGeneration = 0;
136+
// Requested keys wait in `requested` for one queued sweep, so a burst of links (an agent
137+
// linking dozens of pull requests) is read together and shares the summary batches.
138+
let requestedSweepQueued = false;
136139
const retryStacks = new Set<string>();
137140

138141
const isDue = (key: string, entries: ReadonlyArray<LinkEntry>, nowMs: number): boolean => {
@@ -151,7 +154,8 @@ export const make = Effect.gen(function* () {
151154
<E>(cause: Cause.Cause<E>): Effect.Effect<void, E> =>
152155
Cause.hasInterruptsOnly(cause) ? Effect.failCause(cause) : Effect.logWarning(message, fields);
153156

154-
const sweep = Effect.fn("PullRequestSyncReactor.sweep")(function* (requestedKey?: string) {
157+
/** `requested` reads only keys asked for through `requestSync`; `all` is the periodic pass. */
158+
const sweep = Effect.fn("PullRequestSyncReactor.sweep")(function* (scope: "all" | "requested") {
155159
const threads = yield* snapshots.listThreadsWithPullRequests();
156160
const now = yield* DateTime.now;
157161
const nowMs = DateTime.toEpochMillis(now);
@@ -250,22 +254,31 @@ export const make = Effect.gen(function* () {
250254
retryStacks.has(key) ||
251255
entries.some(
252256
(entry) =>
253-
entry.link.snapshot === null || !snapshotFieldsEqual(entry.link.snapshot, fields),
257+
entry.link.snapshot === null ||
258+
!snapshotFieldsEqual(entry.link.snapshot, fields) ||
259+
(summary.stack !== undefined &&
260+
(entry.link.stack?.number ?? null) !== (summary.stack?.number ?? null)),
254261
);
255-
const fetchedStack = needsStack
256-
? yield* pullRequests.stack(ref, { includeDetails: false }).pipe(
257-
Effect.map((stack) => ({
258-
stack: stack === null ? null : ({ kind: "native", ...stack } as const),
259-
})),
260-
Effect.catchCauseIf(
261-
(cause) => !Cause.hasInterruptsOnly(cause),
262-
() =>
263-
Effect.logWarning("pull request stack lookup failed", {
264-
key,
265-
}).pipe(Effect.as(null)),
266-
),
267-
)
268-
: null;
262+
// A summary that says the pull request is in no stack, for links that hold none, already
263+
// answers what the stack read would.
264+
const knownUnstacked =
265+
summary.stack === null && entries.every((entry) => entry.link.stack === null);
266+
const fetchedStack = !needsStack
267+
? null
268+
: knownUnstacked
269+
? { stack: null }
270+
: yield* pullRequests.stack(ref, { includeDetails: false }).pipe(
271+
Effect.map((stack) => ({
272+
stack: stack === null ? null : ({ kind: "native", ...stack } as const),
273+
})),
274+
Effect.catchCauseIf(
275+
(cause) => !Cause.hasInterruptsOnly(cause),
276+
() =>
277+
Effect.logWarning("pull request stack lookup failed", {
278+
key,
279+
}).pipe(Effect.as(null)),
280+
),
281+
);
269282
if (needsStack) {
270283
if (fetchedStack === null) {
271284
retryStacks.add(key);
@@ -296,7 +309,7 @@ export const make = Effect.gen(function* () {
296309
yield* Effect.forEach(
297310
groups,
298311
([key, entries]) =>
299-
(requestedKey === undefined || requestedKey === key) && isDue(key, entries, nowMs)
312+
(scope === "all" || requested.has(key)) && isDue(key, entries, nowMs)
300313
? syncGroup(key, entries).pipe(
301314
Effect.catchCause(logSkipped("pull request sync skipped", { key })),
302315
)
@@ -307,8 +320,12 @@ export const make = Effect.gen(function* () {
307320
);
308321
});
309322

310-
const worker = yield* makeDrainableWorker((key: string | undefined) =>
311-
sweep(key).pipe(Effect.catchCause(logSkipped("pull request sync sweep failed", {}))),
323+
const worker = yield* makeDrainableWorker((scope: "all" | "requested") =>
324+
Effect.suspend(() => {
325+
// Requests from here on queue another sweep; the ones already recorded are read by this.
326+
if (scope === "requested") requestedSweepQueued = false;
327+
return sweep(scope);
328+
}).pipe(Effect.catchCause(logSkipped("pull request sync sweep failed", {}))),
312329
);
313330

314331
const start: PullRequestSyncReactor["Service"]["start"] = Effect.fn(
@@ -322,17 +339,18 @@ export const make = Effect.gen(function* () {
322339
);
323340
yield* forkParked(
324341
Effect.gen(function* () {
325-
yield* worker.enqueue(undefined);
342+
yield* worker.enqueue("all");
326343
yield* worker.drain;
327344
}).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.asVoid),
328345
);
329346
});
330347

331348
const requestSync: PullRequestSyncReactor["Service"]["requestSync"] = (key) =>
332349
Effect.suspend(() => {
333-
const syncKey = threadPullRequestKeyOf(key);
334-
requested.set(syncKey, ++requestGeneration);
335-
return worker.enqueue(syncKey);
350+
requested.set(threadPullRequestKeyOf(key), ++requestGeneration);
351+
if (requestedSweepQueued) return Effect.void;
352+
requestedSweepQueued = true;
353+
return worker.enqueue("requested");
336354
});
337355

338356
return { start, drain: worker.drain, requestSync } satisfies PullRequestSyncReactor["Service"];

‎apps/server/src/orchestration/ThreadPullRequestReactor.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,9 @@ export const make = Effect.gen(function* () {
348348
}).pipe(Effect.tap(() => Effect.sync(() => failBackfill(group)))),
349349
),
350350
),
351-
{ concurrency: 8, discard: true },
351+
// Wide enough that a sweep's GitHub branch lookups reach GitHubCli together and share one
352+
// GraphQL document, instead of one `gh pr list` per branch.
353+
{ concurrency: 32, discard: true },
352354
);
353355
});
354356

‎apps/server/src/pullRequest/GitHubPullRequestCli.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,12 @@ export const make = Effect.gen(function* () {
18211821
const batchable = entries.filter(
18221822
(entry) => buildPullRequestSummariesGraphQlQuery([entry.request]) !== null,
18231823
);
1824-
const query = buildPullRequestSummariesGraphQlQuery(batchable.map((entry) => entry.request));
1824+
// Stack membership rides along where GitHub serves stacks, so the background sync can skip
1825+
// the REST stack read for pull requests that are in none.
1826+
const query = buildPullRequestSummariesGraphQlQuery(
1827+
batchable.map((entry) => entry.request),
1828+
first.request.host === "github.com",
1829+
);
18251830
const batched =
18261831
query === null
18271832
? Effect.succeed(new Map<number, GitHubPullRequestSummary>())

‎apps/server/src/pullRequest/PullRequestProvider.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ export interface ProviderChangeRequestSummary {
116116
readonly reviewDecision?: PullRequestReviewDecision | null | undefined;
117117
readonly checksState?: PullRequestChecksState | null | undefined;
118118
readonly mergeability?: PullRequestMergeability | undefined;
119+
/**
120+
* The host-native stack the pull request sits in, from the same read. Null when the host says
121+
* it is in none; absent when the read did not ask.
122+
*/
123+
readonly stack?: PullRequestStackMembership | null | undefined;
119124
}
120125

121126
/** One layer of a host-native stack, bottom to top order is the array's. */

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@ export const make = Effect.gen(function* () {
15961596
...(changeRequest.mergeability === undefined
15971597
? {}
15981598
: { mergeability: changeRequest.mergeability }),
1599+
...(changeRequest.stack === undefined ? {} : { stack: changeRequest.stack }),
15991600
})),
16001601
);
16011602
}),

‎apps/server/src/pullRequest/gitHubPullRequestJson.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,5 +1934,43 @@ describe("batched pull request summaries", () => {
19341934
mergeability: "unknown",
19351935
additions: 0,
19361936
});
1937+
// The document did not ask about stacks, so the summary does not claim an answer.
1938+
expect(decoded.success.get(0)).not.toHaveProperty("stack");
1939+
});
1940+
1941+
it("reads stack membership only where the document asked for it", () => {
1942+
expect(
1943+
buildPullRequestSummariesGraphQlQuery([{ repository: "acme/web", number: 7 }], true),
1944+
).toContain("stack { number size baseRefName } stackEntry { position }");
1945+
expect(
1946+
buildPullRequestSummariesGraphQlQuery([{ repository: "acme/web", number: 7 }]),
1947+
).not.toContain("stack {");
1948+
const pullRequest = (number: number, stack: Record<string, unknown>) => ({
1949+
pullRequest: {
1950+
number,
1951+
title: "Stacked",
1952+
url: `https://github.com/acme/web/pull/${number}`,
1953+
headRefName: `feat/${number}`,
1954+
baseRefName: "main",
1955+
state: "OPEN",
1956+
updatedAt: "2026-08-24T00:00:00Z",
1957+
...stack,
1958+
},
1959+
});
1960+
const decoded = expectSuccess(
1961+
decodePullRequestSummariesJson(
1962+
JSON.stringify({
1963+
data: {
1964+
s0: pullRequest(7, { stack: null, stackEntry: null }),
1965+
s1: pullRequest(8, {
1966+
stack: { number: 3, size: 2, baseRefName: "main" },
1967+
stackEntry: { position: 2 },
1968+
}),
1969+
},
1970+
}),
1971+
),
1972+
);
1973+
expect(decoded.get(0)?.stack).toBeNull();
1974+
expect(decoded.get(1)?.stack).toEqual({ number: 3, size: 2, base: "main", position: 2 });
19371975
});
19381976
});

‎apps/server/src/pullRequest/gitHubPullRequestJson.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,8 @@ export function decodePullRequestStatsJson(
18521852
* shape as the search row where the two overlap: the checks arrive as GitHub's one-word rollup
18531853
* rather than the whole check list `gh pr view` hands back, which is what keeps a batch cheap.
18541854
*/
1855+
const STACK_MEMBERSHIP_SELECTION = "stack { number size baseRefName } stackEntry { position }";
1856+
18551857
const PULL_REQUEST_SUMMARY_SELECTION =
18561858
"number title url state isDraft mergeable reviewDecision additions deletions changedFiles " +
18571859
"updatedAt mergedAt closedAt headRefName baseRefName " +
@@ -1865,6 +1867,7 @@ const PULL_REQUEST_SUMMARY_SELECTION =
18651867
*/
18661868
export function buildPullRequestSummariesGraphQlQuery(
18671869
changeRequests: ReadonlyArray<{ readonly repository: string; readonly number: number }>,
1870+
includeStacks = false,
18681871
): string | null {
18691872
if (changeRequests.length === 0) return null;
18701873
const selections: string[] = [];
@@ -1874,7 +1877,7 @@ export function buildPullRequestSummariesGraphQlQuery(
18741877
if (!REPOSITORY_PART.test(owner) || !REPOSITORY_PART.test(name)) return null;
18751878
if (!Number.isSafeInteger(changeRequest.number) || changeRequest.number <= 0) return null;
18761879
selections.push(
1877-
` s${index}: repository(owner: "${owner}", name: "${name}") { pullRequest(number: ${changeRequest.number}) { ${PULL_REQUEST_SUMMARY_SELECTION} } }`,
1880+
` s${index}: repository(owner: "${owner}", name: "${name}") { pullRequest(number: ${changeRequest.number}) { ${PULL_REQUEST_SUMMARY_SELECTION}${includeStacks ? ` ${STACK_MEMBERSHIP_SELECTION}` : ""} } }`,
18781881
);
18791882
}
18801883
return `query PullRequestSummaries {\n${selections.join("\n")}\n}`;
@@ -1920,6 +1923,8 @@ export interface GitHubPullRequestSummary {
19201923
readonly reviewDecision: PullRequestReviewDecision | null;
19211924
readonly checksState: PullRequestChecksState | null;
19221925
readonly mergeability: PullRequestMergeability;
1926+
/** Null when GitHub says the pull request is in no stack; absent when the read did not ask. */
1927+
readonly stack?: PullRequestStackMembership | null;
19231928
}
19241929

19251930
/**
@@ -1965,6 +1970,7 @@ export function decodePullRequestSummariesJson(
19651970
}),
19661971
),
19671972
mergeability: toMergeability(pr.mergeable),
1973+
...(pr.stack === undefined ? {} : { stack: toStackMembership(pr) ?? null }),
19681974
});
19691975
}
19701976
return Result.succeed(summaries);

0 commit comments

Comments
 (0)