@devries-cameron20 / t3code-1 / commits / ffa39e2f04
Fork 0

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

perf(server): PR sync stops reading pull requests closed over a day ago (#30071)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Advait Paliwal committed Nov 3, 2026 at 15:45 UTC ffa39e2f0478da07675fe283cd56eb7c212174fb
2 files changed +102 -48
apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts
+88 -36
@@ -979,6 +979,49 @@ describe("PullRequestSyncReactor", () => {
979 ),
980 );
981
982 + it.effect("stops reading a pull request a day after it closed, until a refresh asks", () =>
983 + Effect.scoped(
984 + Effect.gen(function* () {
985 + yield* TestClock.setTime(Date.parse(NOW));
986 + const closed = (closedAt: string | null) => ({ state: "closed" as const, closedAt });
987 + const fixture = yield* makeHarness({
988 + snapshot: makeSnapshot([
989 + makeThread("closed", {
990 + pullRequests: [
991 + makeLink(1, closed("2026-08-20T00:00:00.000Z")),
992 + makeLink(2, closed("2026-08-26T00:00:00.000Z")),
993 + makeLink(3, closed("2026-08-28T11:00:00.000Z")),
994 + makeLink(4, closed(null)),
995 + ],
996 + }),
997 + ]),
998 + summary: (input) =>
999 + Effect.succeed(makeSummary(input, { state: "closed", closedAt: NOW })),
1000 + });
1001 + const reads = Ref.get(fixture.summaryCalls).pipe(
1002 + Effect.map((calls) => calls.map((call) => call.number).toSorted()),
1003 + );
1004 +
1005 + yield* Effect.gen(function* () {
1006 + const reactor = yield* startAndSweep(fixture);
1007 + // Closed within the day, or with no close time, keeps the fifteen-minute cadence.
1008 + assert.deepStrictEqual(yield* reads, [3, 4]);
1009 + for (let index = 0; index < 15; index += 1) yield* sweepAgain(fixture, reactor);
1010 + assert.deepStrictEqual(yield* reads, [3, 3, 4, 4]);
1011 +
1012 + yield* reactor.requestSync({
1013 + host: "github.com",
1014 + repository: "owner/repository",
1015 + number: 1,
1016 + });
1017 + yield* Queue.take(fixture.snapshotReads);
1018 + yield* reactor.drain;
1019 + assert.deepStrictEqual(yield* reads, [1, 3, 3, 4, 4]);
1020 + }).pipe(Effect.provide(fixture.layer));
1021 + }),
1022 + ),
1023 + );
1024 +
1025 it.effect("re-reads a merged pull request until the host supplies its merge time", () =>
1026 Effect.scoped(
1027 Effect.gen(function* () {
@@ -1579,46 +1622,55 @@ describe("PullRequestSyncReactor", () => {
1622 ),
1623 );
1624
1582 - it.effect("reads open links fresh as soon as a merge command ends, before its run ends", () =>
1583 - Effect.scoped(
1584 - Effect.gen(function* () {
1585 - yield* TestClock.setTime(Date.parse(NOW));
1586 - const state = yield* Ref.make<PullRequestSummary["state"]>("open");
1587 - const fixture = yield* makeHarness({
1588 - snapshot: makeSnapshot([
1589 - makeThread("agent", { pullRequests: [makeLink(7, { state: "open" })] }),
1590 - ]),
1591 - summary: (input) =>
1592 - Ref.get(state).pipe(
1593 - Effect.map((current) =>
1594 - makeSummary(input, current === "merged" ? { state: current, mergedAt: NOW } : {}),
1625 + it.effect.each(["open", "closed"] as const)(
1626 + "reads %s links fresh as soon as a merge command ends, before its run ends",
1627 + (initialState) =>
1628 + Effect.scoped(
1629 + Effect.gen(function* () {
1630 + yield* TestClock.setTime(Date.parse(NOW));
1631 + const state = yield* Ref.make<PullRequestSummary["state"]>(initialState);
1632 + const initialSnapshot = {
1633 + state: initialState,
1634 + closedAt: initialState === "closed" ? "2026-08-20T00:00:00.000Z" : null,
1635 + };
1636 + const fixture = yield* makeHarness({
1637 + snapshot: makeSnapshot([
1638 + makeThread("agent", { pullRequests: [makeLink(7, initialSnapshot)] }),
1639 + ]),
1640 + summary: (input) =>
1641 + Ref.get(state).pipe(
1642 + Effect.map((current) =>
1643 + makeSummary(
1644 + input,
1645 + current === "merged" ? { state: current, mergedAt: NOW } : initialSnapshot,
1646 + ),
1647 + ),
1648 ),
1596 - ),
1597 - });
1649 + });
1650
1599 - yield* Effect.gen(function* () {
1600 - const reactor = yield* startAndSweep(fixture);
1601 - yield* Ref.set(state, "merged");
1602 - yield* Ref.set(fixture.summaryCalls, []);
1651 + yield* Effect.gen(function* () {
1652 + const reactor = yield* startAndSweep(fixture);
1653 + yield* Ref.set(state, "merged");
1654 + yield* Ref.set(fixture.summaryCalls, []);
1655
1604 - yield* Queue.offer(
1605 - fixture.domainEvents,
1606 - commandRan(ThreadId.make("agent"), "gh pr merge 7"),
1607 - );
1608 - yield* Queue.take(fixture.snapshotReads);
1609 - yield* Queue.take(fixture.snapshotReads);
1610 - yield* reactor.drain;
1656 + yield* Queue.offer(
1657 + fixture.domainEvents,
1658 + commandRan(ThreadId.make("agent"), "gh pr merge 7"),
1659 + );
1660 + yield* Queue.take(fixture.snapshotReads);
1661 + yield* Queue.take(fixture.snapshotReads);
1662 + yield* reactor.drain;
1663
1612 - assert.deepStrictEqual(
1613 - (yield* Ref.get(fixture.syncCommands)).map((command) => [
1614 - command.number,
1615 - command.snapshot.state,
1616 - ]),
1617 - [[7, "merged"]],
1618 - );
1619 - }).pipe(Effect.provide(fixture.layer));
1620 - }),
1621 - ),
1664 + assert.deepStrictEqual(
1665 + (yield* Ref.get(fixture.syncCommands)).map((command) => [
1666 + command.number,
1667 + command.snapshot.state,
1668 + ]),
1669 + [[7, "merged"]],
1670 + );
1671 + }).pipe(Effect.provide(fixture.layer));
1672 + }),
1673 + ),
1674 );
1675
1676 it.effect.each([
apps/server/src/orchestration-v2/PullRequestSyncReactor.ts
+14 -12
@@ -44,6 +44,9 @@ const SLOW_SYNC_INTERVAL_MS = 15 * 60 * 1_000;
44 // requested refreshes, watches and a host CLI's merge command still read it at once.
45 const QUIET_AFTER_MS = 30 * 60 * 1_000;
46 const QUIET_SYNC_INTERVAL_MS = 5 * 60 * 1_000;
47 +// A pull request closed this long ago is rarely reopened, so the sweep stops reading it, as it
48 +// does merged ones. A reader that sees it open, or a requested refresh, still reads it at once.
49 +const CLOSED_FINAL_AFTER_MS = 24 * 60 * 60 * 1_000;
50
51 const isChangeRequestCloseCommand = (command: string) =>
52 sourceControlDefinitions.definitions.some(
@@ -351,13 +354,12 @@ export const make = Effect.gen(function* () {
354 if (entries.some((entry) => entry.link.snapshot === null)) return true;
355 const active = entries.filter((entry) => isUnsettled(entry.thread));
356 // Merged snapshots without mergedAt cannot settle threads; reread slowly until supplied.
354 - if (
355 - active.every(
356 - (entry) => entry.link.snapshot?.state === "merged" && entry.link.snapshot.mergedAt != null,
357 - )
358 - ) {
359 - return false;
360 - }
357 + const isFinal = ({ link: { snapshot } }: LinkEntry) =>
358 + snapshot?.state === "merged"
359 + ? snapshot.mergedAt != null
360 + : snapshot?.state === "closed" &&
361 + nowMs - Date.parse(snapshot.closedAt ?? "") >= CLOSED_FINAL_AFTER_MS;
362 + if (active.every(isFinal)) return false;
363 const last = lastSyncedAt.get(key);
364 const sinceLastMs = last === undefined ? Number.POSITIVE_INFINITY : nowMs - last;
365 const open = active.filter((entry) => entry.link.snapshot?.state === "open");
@@ -627,13 +629,13 @@ export const make = Effect.gen(function* () {
629
630 // Threads that ran a close command this run, with the commands already read for.
631 const closeCommands = new Map<ThreadId, Set<string>>();
630 - const refreshOpenLinks = (threadId: ThreadId) =>
632 + const refreshUnmergedLinks = (threadId: ThreadId) =>
633 projections.getThreadsWithPullRequests(threadId).pipe(
634 Effect.flatMap((threads) =>
635 Effect.forEach(
636 threads.flatMap((thread) =>
637 visibleThreadPullRequests(thread.pullRequests ?? []).filter(
636 - (link) => link.snapshot?.state === "open",
638 + (link) => link.snapshot?.state === "open" || link.snapshot?.state === "closed",
639 ),
640 ),
641 requestSync,
@@ -698,7 +700,7 @@ export const make = Effect.gen(function* () {
700 { discard: true },
701 );
702 // An agent can merge or close its pull request from a shell with its host's CLI, which
701 - // sends no merge notification. Read the thread's open links fresh once that command
703 + // sends no merge notification. Read the thread's unmerged links fresh once that command
704 // ends, while the agent usually still replies, so settlement can land before the run
705 // ends. Read again when the run ends, since `merge --auto` returns before it merges.
706 case "turn-item.updated": {
@@ -710,11 +712,11 @@ export const make = Effect.gen(function* () {
712 closeCommands.set(event.threadId, ended);
713 if (isOrchestrationV2WorkActive(item.status) || ended.has(item.id)) return linked;
714 ended.add(item.id);
713 - return Effect.andThen(linked, refreshOpenLinks(event.threadId));
715 + return Effect.andThen(linked, refreshUnmergedLinks(event.threadId));
716 }
717 case "run.updated":
718 return isTerminalRunStatus(event.payload.status) && closeCommands.delete(event.threadId)
717 - ? refreshOpenLinks(event.threadId)
719 + ? refreshUnmergedLinks(event.threadId)
720 : Effect.void;
721 default:
722 return Effect.void;