Skip to content

Commit 58fd275

Browse files
abiassiclaude
andcommitted
fix(server): PR sync stops reading missing pull requests every minute
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f3821b7 commit 58fd275

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,27 @@ export const make = Effect.gen(function* () {
166166
// linking dozens of pull requests) is read together and shares the summary batches.
167167
let requestedSweepQueued = false;
168168
const retryStacks = new Set<string>();
169+
// Pull requests the host last answered it cannot find, such as one in a deleted repository.
170+
const missing = new Set<string>();
169171
// Rate limit pauses by project and host, since each project reads with its own credential.
170172
// A paused host refuses every read without asking it, so the sweep leaves its pull requests
171173
// due until the pause ends rather than failing each of them every minute.
172174
const pausedUntil = new Map<string, number>();
173175

174176
const isDue = (key: string, entries: ReadonlyArray<LinkEntry>, nowMs: number): boolean => {
175-
if (requested.has(key) || retryStacks.has(key)) return true;
176-
if (entries.some((entry) => entry.link.snapshot === null)) return true;
177+
if (requested.has(key) || (retryStacks.has(key) && !missing.has(key))) return true;
177178
// Settled threads stop watching their pull requests. Unsettling one makes its links due on
178179
// the next sweep, since the cadence clock below kept running while it was settled.
179180
const active = entries.filter((entry) => isUnsettled(entry.thread));
181+
if (active.length === 0) return false;
182+
// Closed requests can reopen on the host, and a missing one can come back.
183+
const last = lastSyncedAt.get(key);
184+
const slowDue = last === undefined || nowMs - last >= SLOW_SYNC_INTERVAL_MS;
185+
if (missing.has(key)) return slowDue;
186+
if (active.some((entry) => entry.link.snapshot === null)) return true;
180187
if (active.every((entry) => entry.link.snapshot?.state === "merged")) return false;
181188
if (active.some((entry) => entry.link.snapshot?.state === "open")) return true;
182-
// Closed requests can reopen on the host.
183-
const last = lastSyncedAt.get(key);
184-
return last === undefined || nowMs - last >= SLOW_SYNC_INTERVAL_MS;
189+
return slowDue;
185190
};
186191

187192
const logSkipped =
@@ -208,6 +213,7 @@ export const make = Effect.gen(function* () {
208213

209214
for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key);
210215
for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key);
216+
for (const key of missing) if (!groups.has(key)) missing.delete(key);
211217
for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key);
212218

213219
// Layers auto-linked this sweep, so two links of one thread that share a
@@ -286,7 +292,19 @@ export const make = Effect.gen(function* () {
286292
};
287293
const generation = requested.get(key);
288294
if (generation !== undefined) yield* pullRequests.invalidate({ reference: ref });
289-
const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false });
295+
const summary = yield* pullRequests.summary(ref, { recoverTransientFailure: false }).pipe(
296+
Effect.tapError((error) =>
297+
error._tag === "PullRequestOperationError" && error.reason === "not-found"
298+
? // The host answered, so the request is done and the cadence clock ticks.
299+
Effect.sync(() => {
300+
missing.add(key);
301+
lastSyncedAt.set(key, nowMs);
302+
if (requested.get(key) === generation) requested.delete(key);
303+
})
304+
: Effect.void,
305+
),
306+
);
307+
missing.delete(key);
290308
const fields = snapshotFieldsOf(summary);
291309
const needsStack =
292310
generation !== undefined ||
@@ -440,10 +458,11 @@ export const make = Effect.gen(function* () {
440458
yield* forkParked(
441459
Stream.runForEach(events, (event) => {
442460
switch (event.type) {
461+
// Another link's sync is no reason to ask again for one the host cannot find.
443462
case "thread.pull-request-synced":
444463
return Effect.forEach(
445464
visibleThreadPullRequests(event.payload.pullRequests ?? []).filter(
446-
(link) => link.snapshot === null,
465+
(link) => link.snapshot === null && !missing.has(threadPullRequestKeyOf(link)),
447466
),
448467
requestSync,
449468
{ discard: true },

0 commit comments

Comments
 (0)