@devries-cameron20 / t3code Mirror · read-only / commits / 91f55d90e9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 18:30 UTC

fix(server): PR sync reads share GitHub requests again on busy servers (#28422)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Victor committed Nov 12, 2026 at 15:10 UTC 91f55d90e95149e9b988ef7abe37d02232b1a570
5 files changed +189 -20
apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts
+106
@@ -171,6 +171,7 @@ const missingSummary = Effect.fail(
171
172 interface HarnessOptions {
173 readonly invalidate?: PullRequestService.PullRequestService["Service"]["invalidate"];
174 + readonly batchesReads?: (input: PullRequestRef) => boolean;
175 readonly snapshot: TestShellSnapshot;
176 readonly summary?: (
177 input: PullRequestRef,
@@ -228,6 +229,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options:
229 const layerDependencies = Layer.mergeAll(
230 Layer.mock(PullRequestService.PullRequestService)({
231 withSharedProjectReads: (effect) => effect,
232 + batchesReads: (input) => Effect.succeed(options.batchesReads?.(input) ?? true),
233 summary,
234 stack,
235 invalidate: options.invalidate ?? (() => Effect.void),
@@ -1224,6 +1226,110 @@ describe("PullRequestSyncReactor", () => {
1226 ),
1227 );
1228
1229 + it.effect("reads hosts that batch all at once and other hosts at the old sweep width", () =>
1230 + Effect.scoped(
1231 + Effect.gen(function* () {
1232 + yield* TestClock.setTime(Date.parse(NOW));
1233 + const numbers = Array.from({ length: 60 }, (_, index) => index + 1);
1234 + const hostOf = (number: number) => (number <= 30 ? "github.com" : "gitlab.example.com");
1235 + const batches = (input: PullRequestRef) => input.host === "github.com";
1236 + const batched = yield* Ref.make(0);
1237 + const unbatched = yield* Ref.make(0);
1238 + const started = yield* Queue.unbounded<void>();
1239 + const release = yield* Deferred.make<void>();
1240 + const fixture = yield* makeHarness({
1241 + snapshot: makeSnapshot(
1242 + numbers.map((number) =>
1243 + makeThread(`thread-${number}`, {
1244 + pullRequests: [makeLink(number, null, { host: hostOf(number) })],
1245 + }),
1246 + ),
1247 + ),
1248 + batchesReads: batches,
1249 + summary: (input) => {
1250 + const inFlight = batches(input) ? batched : unbatched;
1251 + return Ref.update(inFlight, (count) => count + 1).pipe(
1252 + Effect.andThen(Queue.offer(started, undefined)),
1253 + Effect.andThen(Deferred.await(release)),
1254 + Effect.andThen(Ref.update(inFlight, (count) => count - 1)),
1255 + Effect.as(makeSummary(input)),
1256 + );
1257 + },
1258 + });
1259 + yield* Effect.gen(function* () {
1260 + const reactor = yield* PullRequestSyncReactor.PullRequestSyncReactor;
1261 + yield* reactor.start();
1262 + yield* Deferred.succeed(fixture.activation, undefined);
1263 + yield* Queue.takeN(started, 55);
1264 + assert.strictEqual(yield* Ref.get(batched), 30);
1265 + assert.strictEqual(yield* Ref.get(unbatched), 25);
1266 + yield* Deferred.succeed(release, undefined);
1267 + yield* reactor.drain;
1268 + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, numbers.length);
1269 + }).pipe(Effect.provide(fixture.layer));
1270 + }),
1271 + ),
1272 + );
1273 +
1274 + it.effect("reads a member a sibling flags while the sweep is full", () =>
1275 + Effect.scoped(
1276 + Effect.gen(function* () {
1277 + yield* TestClock.setTime(Date.parse(NOW));
1278 + const stackOf = (layers: ReadonlyArray<number>) => ({
1279 + id: "9",
1280 + number: 9,
1281 + url: "https://github.com/owner/repository/stack/9",
1282 + base: "main",
1283 + layers: layers.map(stackLayer),
1284 + });
1285 + // #50 is merged and not due until #1's stack read shows it a different layer list.
1286 + const merged = makeLink(
1287 + 50,
1288 + { state: "merged", mergedAt: NOW },
1289 + { stack: { kind: "native", ...stackOf([50]) } },
1290 + );
1291 + const started = yield* Ref.make(0);
1292 + const full = yield* Deferred.make<void>();
1293 + const synced1 = yield* Deferred.make<void>();
1294 + const fixture = yield* makeHarness({
1295 + snapshot: makeSnapshot([
1296 + // Another host takes 24 of the 25 shared slots and #1 the last one.
1297 + ...Array.from({ length: 24 }, (_, index) =>
1298 + makeThread(`thread-${index + 2}`, {
1299 + pullRequests: [makeLink(index + 2, null, { host: "gitlab.example.com" })],
1300 + }),
1301 + ),
1302 + makeThread("thread-1", { pullRequests: [makeLink(1)] }),
1303 + makeThread("thread-50", { pullRequests: [merged] }),
1304 + ]),
1305 + batchesReads: () => false,
1306 + // #1 reads once all 25 slots are taken; the other 24 hold theirs until #1 has synced.
1307 + summary: (input) =>
1308 + Ref.updateAndGet(started, (count) => count + 1).pipe(
1309 + // Forked, so #1 resumes only after the sweep has handed out its next slot.
1310 + Effect.tap((count) =>
1311 + count === 25 ? Effect.forkChild(Deferred.succeed(full, undefined)) : Effect.void,
1312 + ),
1313 + Effect.andThen(
1314 + input.number === 50
1315 + ? Effect.void
1316 + : Deferred.await(input.number === 1 ? full : synced1),
1317 + ),
1318 + Effect.as(makeSummary(input)),
1319 + ),
1320 + stack: (input) => Effect.succeed(input.number === 1 ? stackOf([1, 50]) : null),
1321 + onSync: (command) =>
1322 + command.number === 1 ? Deferred.succeed(synced1, undefined) : Effect.void,
1323 + });
1324 + yield* Effect.gen(function* () {
1325 + yield* startAndSweep(fixture);
1326 + const read = (yield* Ref.get(fixture.summaryCalls)).map((ref) => ref.number);
1327 + assert.include(read, 50);
1328 + }).pipe(Effect.provide(fixture.layer));
1329 + }),
1330 + ),
1331 + );
1332 +
1333 it.effect("skips the stack read for a pull request the summary places in no stack", () =>
1334 Effect.scoped(
1335 Effect.gen(function* () {
apps/server/src/orchestration-v2/PullRequestSyncReactor.ts
+66 -20
@@ -183,6 +183,19 @@ interface LinkEntry {
183 readonly link: ThreadPullRequestLink;
184 }
185
186 +const referenceOf = ({ thread, link }: LinkEntry) => ({
187 + projectId: thread.projectId,
188 + host: normalizeThreadPullRequestKey(link).host,
189 + repository: link.repository,
190 + number: link.number,
191 +});
192 +
193 +/** Pauses and batching both belong to a project's host. */
194 +const pauseKeyOf = (entry: LinkEntry) => {
195 + const { projectId, host } = referenceOf(entry);
196 + return `${projectId}\0${host}`;
197 +};
198 +
199 /** Another layer of the stack `url` belongs to, in its host's canonical shape. */
200 function siblingChangeRequestUrl(url: string, number: number): string | null {
201 if (!Number.isSafeInteger(number) || number < 1) return null;
@@ -478,12 +491,7 @@ export const make = Effect.gen(function* () {
491
492 const syncGroup = Effect.fnUntraced(function* (key: string, entries: ReadonlyArray<LinkEntry>) {
493 const first = entries[0]!;
481 - const ref = {
482 - projectId: first.thread.projectId,
483 - host: normalizeThreadPullRequestKey(first.link).host,
484 - repository: first.link.repository,
485 - number: first.link.number,
486 - };
494 + const ref = referenceOf(first);
495 const refreshRequested = requested.has(key);
496 // A held merged summary is served without asking the host, so a snapshot still missing its
497 // merge time has to drop it or the slow-cadence re-read would hand back the same gap.
@@ -612,25 +620,63 @@ export const make = Effect.gen(function* () {
620 return Effect.void;
621 }),
622 );
623 + const pools = {
624 + // Wider than one summary batch, so reads that finish their cache checks at different
625 + // times still share batches.
626 + batched: { width: 100, reads: yield* Semaphore.make(100) },
627 + // One read per pull request, often a CLI process, so these keep the narrower width.
628 + unbatched: { width: 25, reads: yield* Semaphore.make(25) },
629 + };
630 + const isPaused = (pauseKey: string, atMs: number) => (pausedUntil.get(pauseKey) ?? 0) > atMs;
631 + // Judged once a slot is free, so a stack retry a sibling flags meanwhile still counts, and
632 + // pauses set during the sweep apply at once.
633 + const readIfDue = (
634 + key: string,
635 + entries: ReadonlyArray<LinkEntry>,
636 + pauseKey: string,
637 + reads: Semaphore.Semaphore,
638 + ) =>
639 + reads.withPermits(1)(
640 + Effect.suspend(() =>
641 + isDue(key, entries, nowMs)
642 + ? Clock.currentTimeMillis.pipe(
643 + Effect.flatMap((startedAtMs) =>
644 + isPaused(pauseKey, startedAtMs) ? Effect.void : readGroup(key, entries, pauseKey),
645 + ),
646 + )
647 + : Effect.void,
648 + ),
649 + );
650 + const groupsByHost = Map.groupBy(
651 + Array.from(groups).filter(([key]) => scope === "all" || requested.has(key)),
652 + ([, entries]) => pauseKeyOf(entries[0]!),
653 + );
654 yield* Effect.forEach(
616 - groups,
617 - ([key, entries]) => {
618 - if (!((scope === "all" || requested.has(key)) && isDue(key, entries, nowMs))) {
655 + groupsByHost,
656 + ([pauseKey, hostGroups]) => {
657 + // A host paused or with nothing due skips the lookup. Stack retries only flag groups on
658 + // the flagging group's own host, so nothing on it becomes due this sweep.
659 + if (
660 + isPaused(pauseKey, nowMs) ||
661 + !hostGroups.some(([key, entries]) => isDue(key, entries, nowMs))
662 + ) {
663 return Effect.void;
664 }
621 - const first = entries[0]!;
622 - const pauseKey = `${first.thread.projectId}\0${normalizeThreadPullRequestKey(first.link).host}`;
623 - // Check pauses at each read so changes during the sweep apply immediately.
624 - return Clock.currentTimeMillis.pipe(
625 - Effect.flatMap((startedAtMs) =>
626 - (pausedUntil.get(pauseKey) ?? 0) > startedAtMs
627 - ? Effect.void
628 - : readGroup(key, entries, pauseKey),
629 - ),
665 + return pullRequests.batchesReads(referenceOf(hostGroups[0]![1][0]!)).pipe(
666 + Effect.flatMap((batches) => {
667 + const { width, reads } = batches ? pools.batched : pools.unbatched;
668 + return Effect.forEach(
669 + hostGroups,
670 + ([key, entries]) => readIfDue(key, entries, pauseKey, reads),
671 + {
672 + concurrency: width,
673 + discard: true,
674 + },
675 + );
676 + }),
677 );
678 },
632 - // Match summary batch width so GitHub reads coalesce into one request.
633 - { concurrency: 25, discard: true },
679 + { concurrency: "unbounded", discard: true },
680 );
681 for (const [reason, { count, key }] of skips) {
682 yield* Effect.logWarning("pull request sync skipped", { count, key, reason });
apps/server/src/pullRequest/PullRequestService.ts
+14
@@ -200,6 +200,8 @@ export class PullRequestService extends Context.Service<
200 input: PullRequestRef,
201 options?: { readonly recoverTransientFailure?: boolean },
202 ) => Effect.Effect<PullRequestSummary, PullRequestError>;
203 + /** Whether the reference's host batches summary reads; false when it cannot be resolved. */
204 + readonly batchesReads: (input: PullRequestRef) => Effect.Effect<boolean>;
205 readonly stack: (
206 input: PullRequestRef,
207 options?: { readonly includeDetails?: boolean },
@@ -536,6 +538,7 @@ function withRateLimitBackoff(
538 ...(api.readsPublicHostWithoutCheckout === undefined
539 ? {}
540 : { readsPublicHostWithoutCheckout: api.readsPublicHostWithoutCheckout }),
541 + ...(api.batchesReads === undefined ? {} : { batchesReads: api.batchesReads }),
542 getViewer:
543 options?.viewerAllowsPause === true
544 ? interactive("getViewer", api.getViewer)
@@ -3074,6 +3077,16 @@ export const make = Effect.gen(function* () {
3077 );
3078 };
3079
3080 + const batchesReads: PullRequestService["Service"]["batchesReads"] = Effect.fn(
3081 + "PullRequestService.batchesReads",
3082 + )(
3083 + function* (input) {
3084 + const project = yield* requireProject(input);
3085 + return project.api.batchesReads === true;
3086 + },
3087 + Effect.orElseSucceed(() => false),
3088 + );
3089 +
3090 const stack: PullRequestService["Service"]["stack"] = (input, options) =>
3091 persistedRead(
3092 input,
@@ -3850,6 +3863,7 @@ export const make = Effect.gen(function* () {
3863 return yield* listStats({ ...input, refs: refs.flatMap(Option.toArray) });
3864 }),
3865 summary: credentialCached(summary),
3866 + batchesReads,
3867 stack: credentialCached(stack),
3868 subscribeMerges: PubSub.subscribe(mergedPullRequests).pipe(
3869 Effect.map((subscription) => Stream.fromSubscription(subscription)),
packages/source-control-core/src/server/PullRequestProvider.ts
+2
@@ -318,6 +318,8 @@ export interface PullRequestProviderApi {
318 * definition's `publicHost` can be read from a project with no checkout of its repository.
319 */
320 readonly readsPublicHostWithoutCheckout?: boolean;
321 + /** Concurrent summary reads share requests, so callers may keep many in flight. */
322 + readonly batchesReads?: boolean;
323 /**
324 * Absent means every host detected as this kind is read. A host it refuses is listed as one that
325 * cannot be browsed here, and nothing is sent for it.
packages/source-control-github/src/server/GitHubPullRequestProvider.ts
+1
@@ -399,6 +399,7 @@ export const make = Effect.gen(function* () {
399 capabilities: CAPABILITIES,
400 unavailableRequirement: UNAVAILABLE_REQUIREMENT,
401 readsPublicHostWithoutCheckout: true,
402 + batchesReads: true,
403 mergeMessageRewrite: removeAgentCredits,
404 getRoutingIdentity: (input) =>
405 cli.getRoutingIdentity(input).pipe(Effect.mapError(fail("routeIdentity"))),