Skip to content

Commit 707118f

Browse files
perf(server): poll quiet pull requests less and revalidate stacks
Every server re-read every open linked pull request each minute, and refetched its stack over REST whenever the summary changed. Across a fleet sharing one account that alone spent more than GitHub's hourly GraphQL quota. An open pull request nobody has updated for 30 minutes is now read every five minutes; requested refreshes, watches and `gh pr merge` still read it at once. The stack read revalidates its last answer with an ETag, so an unchanged stack comes back as a free 304. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 806b539 commit 707118f

4 files changed

Lines changed: 129 additions & 6 deletions

File tree

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ describe("PullRequestSyncReactor", () => {
643643
yield* TestClock.setTime(Date.parse(NOW));
644644
const fixture = yield* makeHarness({
645645
snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(42)] })]),
646+
summary: (input) => Effect.succeed(makeSummary(input, { updatedAt: NOW })),
646647
});
647648

648649
yield* Effect.gen(function* () {
@@ -855,6 +856,39 @@ describe("PullRequestSyncReactor", () => {
855856
),
856857
);
857858

859+
it.effect("reads a quiet open pull request every five minutes until it changes", () =>
860+
Effect.scoped(
861+
Effect.gen(function* () {
862+
yield* TestClock.setTime(Date.parse(NOW));
863+
const updatedAt = yield* Ref.make("2026-08-27T00:00:00.000Z");
864+
const fixture = yield* makeHarness({
865+
snapshot: makeSnapshot([makeThread("quiet", { pullRequests: [makeLink(5)] })]),
866+
summary: (input) =>
867+
Ref.get(updatedAt).pipe(Effect.map((updatedAt) => makeSummary(input, { updatedAt }))),
868+
});
869+
yield* Effect.gen(function* () {
870+
const reactor = yield* startAndSweep(fixture);
871+
const applySyncs = Effect.flatMap(Ref.get(fixture.syncCommands), (commands) =>
872+
Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands)),
873+
);
874+
yield* applySyncs;
875+
const reads = Ref.get(fixture.summaryCalls).pipe(Effect.map((calls) => calls.length));
876+
assert.strictEqual(yield* reads, 1);
877+
for (let index = 0; index < 4; index += 1) yield* sweepAgain(fixture, reactor);
878+
assert.strictEqual(yield* reads, 1);
879+
880+
// Someone comments: the five-minute read sees it, and the next sweep reads it again.
881+
yield* Ref.set(updatedAt, "2026-08-28T12:04:00.000Z");
882+
yield* sweepAgain(fixture, reactor);
883+
assert.strictEqual(yield* reads, 2);
884+
yield* applySyncs;
885+
yield* sweepAgain(fixture, reactor);
886+
assert.strictEqual(yield* reads, 3);
887+
}).pipe(Effect.provide(fixture.layer));
888+
}),
889+
),
890+
);
891+
858892
it.effect("preserves a refresh requested while an older host read is in flight", () =>
859893
Effect.scoped(
860894
Effect.gen(function* () {
@@ -1116,13 +1150,15 @@ describe("PullRequestSyncReactor", () => {
11161150
yield* TestClock.setTime(Date.parse(NOW));
11171151
const fixture = yield* makeHarness({
11181152
snapshot: makeSnapshot([
1119-
makeThread("first", { pullRequests: [makeLink(7, { state: "open" })] }),
1120-
makeThread("second", { pullRequests: [makeLink(8, { state: "open" })] }),
1153+
makeThread("first", { pullRequests: [makeLink(7, { state: "open", updatedAt: NOW })] }),
1154+
makeThread("second", {
1155+
pullRequests: [makeLink(8, { state: "open", updatedAt: NOW })],
1156+
}),
11211157
makeThread("third", {
11221158
pullRequests: [
11231159
makeLink(
11241160
9,
1125-
{ state: "open" },
1161+
{ state: "open", updatedAt: NOW },
11261162
{ host: "forge.example", url: "https://forge.example/owner/repository/pulls/9" },
11271163
),
11281164
],

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ import * as ProjectionStore from "./ProjectionStore.ts";
3737
import { isTerminalRunStatus } from "./ThreadManagementService.ts";
3838

3939
const SLOW_SYNC_INTERVAL_MS = 15 * 60 * 1_000;
40+
// Every server polls every open linked pull request, and most of them sit untouched for days.
41+
// One nobody has updated for QUIET_AFTER_MS is read on the quiet cadence instead of each sweep;
42+
// requested refreshes, watches and `gh pr merge` still read it at once.
43+
const QUIET_AFTER_MS = 30 * 60 * 1_000;
44+
const QUIET_SYNC_INTERVAL_MS = 5 * 60 * 1_000;
4045
const PULL_REQUEST_CLOSE_COMMAND = /\b(?:gh\s+pr|glab\s+mr)\s+(?:merge|close)\b/u;
4146

4247
const isPullRequestProviderError = Schema.is(PullRequestProviderError);
@@ -177,9 +182,17 @@ export const make = Effect.gen(function* () {
177182
) {
178183
return false;
179184
}
180-
if (active.some((entry) => entry.link.snapshot?.state === "open")) return true;
181185
const last = lastSyncedAt.get(key);
182-
return last === undefined || nowMs - last >= SLOW_SYNC_INTERVAL_MS;
186+
const sinceLastMs = last === undefined ? Number.POSITIVE_INFINITY : nowMs - last;
187+
const open = active.filter((entry) => entry.link.snapshot?.state === "open");
188+
if (open.length > 0) {
189+
const quiet = open.every((entry) => {
190+
const updatedAt = entry.link.snapshot?.updatedAt;
191+
return updatedAt != null && nowMs - Date.parse(updatedAt) >= QUIET_AFTER_MS;
192+
});
193+
return !quiet || sinceLastMs >= QUIET_SYNC_INTERVAL_MS;
194+
}
195+
return sinceLastMs >= SLOW_SYNC_INTERVAL_MS;
183196
};
184197

185198
const logSkipped =

‎packages/source-control-github/src/server/GitHubPullRequestApi.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,46 @@ layer("GitHubPullRequestApi.layer", (it) => {
11191119
}),
11201120
);
11211121

1122+
it.effect("revalidates a polled stack, per credential, so an unchanged one costs no quota", () =>
1123+
Effect.gen(function* () {
1124+
const listing = encodeJson([
1125+
{
1126+
number: 4,
1127+
url: "https://api.github.com/repos/acme/poll/stacks/4",
1128+
base: { ref: "main" },
1129+
pull_requests: [
1130+
{ number: 9, head: { ref: "feat/nine" }, state: "open", merged_at: null },
1131+
],
1132+
},
1133+
]);
1134+
const read = Effect.flatMap(GitHubPullRequestApi.GitHubPullRequestApi, (cli) =>
1135+
cli.getPullRequestStack({
1136+
cwd: "/w",
1137+
repository: "acme/poll",
1138+
host: "github.com",
1139+
number: 9,
1140+
}),
1141+
);
1142+
mockedExecute.mockReturnValueOnce(
1143+
Effect.succeed(output(listing, false, false, { etag: 'W/"v1"' })),
1144+
);
1145+
const first = yield* read;
1146+
mockedExecute.mockReturnValueOnce(Effect.succeed(output("", false, false, {}, 304)));
1147+
const second = yield* read;
1148+
mockedCredential.mockImplementationOnce(() =>
1149+
Effect.succeed({ token: Redacted.make("other"), fingerprint: "github.com:other" }),
1150+
);
1151+
mockedExecute.mockReturnValueOnce(Effect.succeed(output(listing)));
1152+
yield* read;
1153+
1154+
assert.deepStrictEqual(second, first);
1155+
expect(callAt(0)).not.toHaveProperty("ifNoneMatch");
1156+
expect(callAt(1)).toMatchObject({ ifNoneMatch: 'W/"v1"' });
1157+
// Another account never revalidates the first account's answer.
1158+
expect(callAt(2)).not.toHaveProperty("ifNoneMatch");
1159+
}),
1160+
);
1161+
11221162
it.effect("fetches layer titles only when the caller asks for stack details", () =>
11231163
Effect.gen(function* () {
11241164
const minimal = {

‎packages/source-control-github/src/server/GitHubPullRequestApi.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,39 @@ export const make = Effect.gen(function* () {
11481148
),
11491149
);
11501150

1151+
// The last body and ETag of each polled REST read, so an unchanged answer comes back as a
1152+
// 304, which GitHub does not charge. Keyed by the credential the request is pinned to, so a
1153+
// validator never revalidates another account's body.
1154+
const validators = new Map<string, { readonly etag: string; readonly body: string }>();
1155+
const revalidatedRestRead = Effect.fn("GitHubPullRequestApi.revalidatedRestRead")(function* <A>(
1156+
input: Parameters<typeof restRead<A>>[0],
1157+
) {
1158+
const host = GitHubRequestPolicy.normalizeHost(input.host);
1159+
const { token, fingerprint } = yield* api.credential(host);
1160+
const credential = { host, token, credentialFingerprint: fingerprint };
1161+
const key = `${fingerprint}\0${host}\0${input.path}`;
1162+
const previous = validators.get(key);
1163+
const response = yield* api
1164+
.rest({
1165+
host,
1166+
operation: input.operation,
1167+
path: input.path,
1168+
...(previous === undefined ? {} : { ifNoneMatch: previous.etag }),
1169+
})
1170+
.pipe(Effect.provideService(GitHubRequestPolicy.PinnedGitHubCredential, credential));
1171+
if (response.status === 304 && previous !== undefined) {
1172+
return yield* decodeWith(input.cwd, input.operation, input.decode, previous.body);
1173+
}
1174+
const decoded = yield* decodeWith(input.cwd, input.operation, input.decode, response.body);
1175+
const etag = response.headers["etag"]?.trim();
1176+
validators.delete(key);
1177+
if (etag) {
1178+
if (validators.size >= 512) validators.delete(validators.keys().next().value!);
1179+
validators.set(key, { etag, body: response.body });
1180+
}
1181+
return decoded;
1182+
});
1183+
11511184
const diffFilesPage = (input: {
11521185
readonly cwd: string;
11531186
readonly repository: string;
@@ -1915,7 +1948,8 @@ export const make = Effect.gen(function* () {
19151948

19161949
getPullRequestStack: (input) => {
19171950
const { owner, name } = parseRepositorySelector(input.repository);
1918-
return restRead({
1951+
// Polled by the pull request sync for every linked pull request, so revalidate it.
1952+
return revalidatedRestRead({
19191953
cwd: input.cwd,
19201954
host: input.host,
19211955
operation: "getPullRequestStack",

0 commit comments

Comments
 (0)