TypeScript
perf(server): read quiet pull requests every five minutes (#22073)
Julius Marminge8dec2ffef970
diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.tsindex 9418de49d9f..364ff7d09ba 100644--- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts+++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts@@ -643,6 +643,7 @@ describe("PullRequestSyncReactor", () => {yield* TestClock.setTime(Date.parse(NOW));const fixture = yield* makeHarness({snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(42)] })]),+ summary: (input) => Effect.succeed(makeSummary(input, { updatedAt: NOW })),});yield* Effect.gen(function* () {@@ -855,6 +856,39 @@ describe("PullRequestSyncReactor", () => {),);+ it.effect("reads a quiet open pull request every five minutes until it changes", () =>+ Effect.scoped(+ Effect.gen(function* () {+ yield* TestClock.setTime(Date.parse(NOW));+ const updatedAt = yield* Ref.make("2026-08-27T00:00:00.000Z");+ const fixture = yield* makeHarness({+ snapshot: makeSnapshot([makeThread("quiet", { pullRequests: [makeLink(5)] })]),+ summary: (input) =>+ Ref.get(updatedAt).pipe(Effect.map((updatedAt) => makeSummary(input, { updatedAt }))),+ });+ yield* Effect.gen(function* () {+ const reactor = yield* startAndSweep(fixture);+ const applySyncs = Effect.flatMap(Ref.get(fixture.syncCommands), (commands) =>+ Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands)),+ );+ yield* applySyncs;+ const reads = Ref.get(fixture.summaryCalls).pipe(Effect.map((calls) => calls.length));+ assert.strictEqual(yield* reads, 1);+ for (let index = 0; index < 4; index += 1) yield* sweepAgain(fixture, reactor);+ assert.strictEqual(yield* reads, 1);++ // Someone comments: the five-minute read sees it, and the next sweep reads it again.+ yield* Ref.set(updatedAt, "2026-08-28T12:04:00.000Z");+ yield* sweepAgain(fixture, reactor);+ assert.strictEqual(yield* reads, 2);+ yield* applySyncs;+ yield* sweepAgain(fixture, reactor);+ assert.strictEqual(yield* reads, 3);+ }).pipe(Effect.provide(fixture.layer));+ }),+ ),+ );+it.effect("preserves a refresh requested while an older host read is in flight", () =>Effect.scoped(Effect.gen(function* () {@@ -1116,13 +1150,15 @@ describe("PullRequestSyncReactor", () => {yield* TestClock.setTime(Date.parse(NOW));const fixture = yield* makeHarness({snapshot: makeSnapshot([- makeThread("first", { pullRequests: [makeLink(7, { state: "open" })] }),- makeThread("second", { pullRequests: [makeLink(8, { state: "open" })] }),+ makeThread("first", { pullRequests: [makeLink(7, { state: "open", updatedAt: NOW })] }),+ makeThread("second", {+ pullRequests: [makeLink(8, { state: "open", updatedAt: NOW })],+ }),makeThread("third", {pullRequests: [makeLink(9,- { state: "open" },+ { state: "open", updatedAt: NOW },{ host: "forge.example", url: "https://forge.example/owner/repository/pulls/9" },),],diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.tsindex 398b788c03b..2f2e982dd3f 100644--- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts+++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts@@ -37,6 +37,11 @@ import * as ProjectionStore from "./ProjectionStore.ts";import { isTerminalRunStatus } from "./ThreadManagementService.ts";const SLOW_SYNC_INTERVAL_MS = 15 * 60 * 1_000;+// Every server polls every open linked pull request, and most of them sit untouched for days.+// One nobody has updated for QUIET_AFTER_MS is read on the quiet cadence instead of each sweep;+// requested refreshes, watches and `gh pr merge` still read it at once.+const QUIET_AFTER_MS = 30 * 60 * 1_000;+const QUIET_SYNC_INTERVAL_MS = 5 * 60 * 1_000;const PULL_REQUEST_CLOSE_COMMAND = /\b(?:gh\s+pr|glab\s+mr)\s+(?:merge|close)\b/u;const isPullRequestProviderError = Schema.is(PullRequestProviderError);@@ -177,9 +182,17 @@ export const make = Effect.gen(function* () {) {return false;}- if (active.some((entry) => entry.link.snapshot?.state === "open")) return true;const last = lastSyncedAt.get(key);- return last === undefined || nowMs - last >= SLOW_SYNC_INTERVAL_MS;+ const sinceLastMs = last === undefined ? Number.POSITIVE_INFINITY : nowMs - last;+ const open = active.filter((entry) => entry.link.snapshot?.state === "open");+ if (open.length > 0) {+ const quiet = open.every((entry) => {+ const updatedAt = entry.link.snapshot?.updatedAt;+ return updatedAt != null && nowMs - Date.parse(updatedAt) >= QUIET_AFTER_MS;+ });+ return !quiet || sinceLastMs >= QUIET_SYNC_INTERVAL_MS;+ }+ return sinceLastMs >= SLOW_SYNC_INTERVAL_MS;};const logSkipped =