@devries-cameron20 / t3code-1 / commits / 382bf4d906
Fork 0

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

fix(server): isolate pull request sync reads by project (#18854)

Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Southwell committed Oct 20, 2026 at 05:41 UTC 382bf4d90623b5a62b850bcd9224f14e7959f1a9
2 files changed +118 -18
apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts
+101 -8
@@ -499,7 +499,7 @@ describe("PullRequestSyncReactor", () => {
499 ),
500 );
501
502 - it.effect("asks the host once for a pull request shared by two threads", () =>
502 + it.effect("asks the host once for a pull request shared by two threads in one project", () =>
503 Effect.scoped(
504 Effect.gen(function* () {
505 yield* TestClock.setTime(Date.parse(NOW));
@@ -516,6 +516,7 @@ describe("PullRequestSyncReactor", () => {
516 yield* startAndSweep(fixture);
517
518 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
519 + assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 1);
520 const commands = yield* Ref.get(fixture.syncCommands);
521 assert.deepStrictEqual(
522 commands
@@ -531,6 +532,86 @@ describe("PullRequestSyncReactor", () => {
532 ),
533 );
534
535 + it.effect.each(["summary", "stack"] as const)(
536 + "isolates a failed project %s refresh and retries it after the other project succeeds",
537 + (operation) =>
538 + Effect.scoped(
539 + Effect.gen(function* () {
540 + yield* TestClock.setTime(Date.parse(NOW));
541 + const otherProject = ProjectId.make("other-project");
542 + const link = makeLink(42, { state: "merged", mergedAt: NOW });
543 + let failing = true;
544 + const failure = new PullRequestOperationError({ operation, detail: "checkout missing" });
545 + const fixture = yield* makeHarness({
546 + snapshot: makeSnapshot([
547 + makeThread("failing", { pullRequests: [link] }),
548 + makeThread("healthy", { projectId: otherProject, pullRequests: [link] }),
549 + makeThread("same-project", { projectId: otherProject, pullRequests: [link] }),
550 + ]),
551 + summary: (input) =>
552 + failing && input.projectId === PROJECT_ID && operation === "summary"
553 + ? Effect.fail(failure)
554 + : Effect.succeed(
555 + makeSummary(input, {
556 + state: "merged",
557 + mergedAt: NOW,
558 + title: input.projectId,
559 + }),
560 + ),
561 + stack: (input) =>
562 + failing && input.projectId === PROJECT_ID && operation === "stack"
563 + ? Effect.fail(failure)
564 + : Effect.succeed({
565 + id: input.projectId,
566 + number: 42,
567 + url: "https://github.com/owner/repository/stacks/42",
568 + base: "main",
569 + layers: [{ number: 42, headBranch: "feature", state: "merged" }],
570 + }),
571 + });
572 + yield* Effect.gen(function* () {
573 + const reactor = yield* startAndSweep(fixture);
574 + assert.deepStrictEqual(yield* Ref.get(fixture.summaryCalls), []);
575 + yield* reactor.requestSync(link);
576 + yield* Queue.take(fixture.snapshotReads);
577 + yield* reactor.drain;
578 + const commands = yield* Ref.get(fixture.syncCommands);
579 + assert.sameDeepMembers(
580 + commands.map((command) => [
581 + command.threadId,
582 + command.snapshot.title,
583 + command.stack?.id,
584 + ]),
585 + [
586 + ["healthy", otherProject, otherProject],
587 + ["same-project", otherProject, otherProject],
588 + ],
589 + );
590 + assert.sameMembers(
591 + (yield* Ref.get(fixture.summaryCalls)).map((call) => call.projectId),
592 + [PROJECT_ID, otherProject],
593 + );
594 + assert.sameMembers(
595 + (yield* Ref.get(fixture.stackCalls)).map((call) => call.projectId),
596 + operation === "stack" ? [PROJECT_ID, otherProject] : [otherProject],
597 + );
598 + yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands));
599 + failing = false;
600 + yield* sweepAgain(fixture, reactor);
601 + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 3);
602 + assert.strictEqual(
603 + (yield* Ref.get(fixture.summaryCalls)).at(-1)?.projectId,
604 + PROJECT_ID,
605 + );
606 + const retried = (yield* Ref.get(fixture.syncCommands)).at(-1);
607 + assert.strictEqual(retried?.threadId, ThreadId.make("failing"));
608 + assert.strictEqual(retried?.snapshot.title, PROJECT_ID);
609 + assert.strictEqual(retried?.stack?.id, PROJECT_ID);
610 + }).pipe(Effect.provide(fixture.layer));
611 + }),
612 + ),
613 + );
614 +
615 it.effect("recovers the HTTP port when syncing an older Forgejo link", () =>
616 Effect.scoped(
617 Effect.gen(function* () {
@@ -580,11 +661,11 @@ describe("PullRequestSyncReactor", () => {
661 ),
662 );
663
583 - it.effect("refreshes closed links through the reactor's project after reopening elsewhere", () =>
664 + it.effect("refreshes closed links through each project after reopening elsewhere", () =>
665 Effect.scoped(
666 Effect.gen(function* () {
667 yield* TestClock.setTime(Date.parse(NOW));
587 - const stale = yield* Ref.make(true);
668 + const invalidated = yield* Ref.make<ReadonlyArray<ProjectId>>([]);
669 const fixture = yield* makeHarness({
670 snapshot: makeSnapshot([
671 makeThread("first", { pullRequests: [makeLink(42, { state: "closed" })] }),
@@ -594,17 +675,21 @@ describe("PullRequestSyncReactor", () => {
675 }),
676 ]),
677 invalidate: ({ reference }) =>
597 - reference?.projectId === makeProject().id && reference.host === "github.com"
598 - ? Ref.set(stale, false)
678 + reference !== undefined
679 + ? Ref.update(invalidated, (projects) => [...projects, reference.projectId])
680 : Effect.void,
681 summary: (input) =>
601 - Ref.get(stale).pipe(
602 - Effect.map((cached) => makeSummary(input, { state: cached ? "closed" : "open" })),
682 + Ref.get(invalidated).pipe(
683 + Effect.map((projects) =>
684 + makeSummary(input, {
685 + state: projects.includes(input.projectId) ? "open" : "closed",
686 + }),
687 + ),
688 ),
689 });
690 yield* Effect.gen(function* () {
691 const reactor = yield* startAndSweep(fixture);
607 - assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
692 + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2);
693 yield* reactor.requestSync({
694 host: "github.com",
695 repository: "owner/repository",
@@ -612,6 +697,14 @@ describe("PullRequestSyncReactor", () => {
697 });
698 yield* Queue.take(fixture.snapshotReads);
699 yield* reactor.drain;
700 + assert.sameMembers(
701 + [...(yield* Ref.get(invalidated))],
702 + [PROJECT_ID, ProjectId.make("second-project")],
703 + );
704 + assert.sameMembers(
705 + (yield* Ref.get(fixture.stackCalls)).map((call) => call.projectId),
706 + [PROJECT_ID, ProjectId.make("second-project")],
707 + );
708 const commands = yield* Ref.get(fixture.syncCommands);
709 yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands));
710 const snapshot = yield* Ref.get(fixture.snapshots);
apps/server/src/orchestration-v2/PullRequestSyncReactor.ts
+17 -10
@@ -152,9 +152,10 @@ export const make = Effect.gen(function* () {
152 const crypto = yield* Crypto.Crypto;
153
154 const lastSyncedAt = new Map<string, number>();
155 - const requested = new Map<string, number>();
156 - let requestGeneration = 0;
157 - // Requested keys wait in `requested` for one queued sweep, so a burst of links (an agent
155 + const requested = new Set<string>();
156 + // Requests arriving during a host read wait for the next sweep.
157 + const pendingRequests = new Set<string>();
158 + // PR keys wait in `pendingRequests` for one queued sweep, so a burst of links (an agent
159 // linking dozens of pull requests) is read together and shares the summary batches.
160 let requestedSweepQueued = false;
161 const retryStacks = new Set<string>();
@@ -195,13 +196,20 @@ export const make = Effect.gen(function* () {
196 const groups = new Map<string, Array<LinkEntry>>();
197 for (const thread of threads) {
198 for (const link of visibleThreadPullRequests(thread.pullRequests ?? [])) {
198 - const key = threadPullRequestKeyOf(link);
199 + // Reads depend on the project's checkout and credentials, including their failures.
200 + const key = `${thread.projectId}\0${threadPullRequestKeyOf(link)}`;
201 const entries = groups.get(key) ?? [];
202 entries.push({ thread, link });
203 groups.set(key, entries);
204 }
205 }
206
207 + // A host-level refresh reaches every project, each retaining its own pending retry.
208 + for (const [key, entries] of groups) {
209 + if (pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) requested.add(key);
210 + }
211 + pendingRequests.clear();
212 +
213 for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key);
214 for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key);
215 for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key);
@@ -278,19 +286,19 @@ export const make = Effect.gen(function* () {
286 repository: first.link.repository,
287 number: first.link.number,
288 };
281 - const generation = requested.get(key);
289 + const refreshRequested = requested.has(key);
290 // A held merged summary is served without asking the host, so a snapshot still missing its
291 // merge time has to drop it or the slow-cadence re-read would hand back the same gap.
292 const missingMergedAt = entries.some(
293 (entry) => entry.link.snapshot?.state === "merged" && entry.link.snapshot.mergedAt == null,
294 );
287 - if (generation !== undefined || missingMergedAt) {
295 + if (refreshRequested || missingMergedAt) {
296 yield* pullRequests.invalidate({ reference: ref });
297 }
298 const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false });
299 const fields = snapshotFieldsOf(summary);
300 const needsStack =
293 - generation !== undefined ||
301 + refreshRequested ||
302 retryStacks.has(key) ||
303 entries.some(
304 (entry) =>
@@ -330,8 +338,7 @@ export const make = Effect.gen(function* () {
338 }
339 // The host answered, so the cadence clock ticks even if a dispatch below is rejected.
340 lastSyncedAt.set(key, nowMs);
333 - // A refresh requested while the host read was in flight belongs to the next sweep.
334 - if (requested.get(key) === generation) requested.delete(key);
341 + requested.delete(key);
342 yield* Effect.forEach(
343 entries,
344 (entry) =>
@@ -473,7 +480,7 @@ export const make = Effect.gen(function* () {
480
481 const requestSync: PullRequestSyncReactor["Service"]["requestSync"] = (key) =>
482 Effect.suspend(() => {
476 - requested.set(threadPullRequestKeyOf(key), ++requestGeneration);
483 + pendingRequests.add(threadPullRequestKeyOf(key));
484 if (requestedSweepQueued) return Effect.void;
485 requestedSweepQueued = true;
486 return worker.enqueue("requested");