Skip to content

Commit b41599d

Browse files
fix(server): clear missing PR backoff when reading project changes
1 parent b8f7c4e commit b41599d

2 files changed

Lines changed: 71 additions & 13 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,43 @@ describe("PullRequestSyncReactor", () => {
10721072
),
10731073
);
10741074

1075+
it.effect("retries a missing link when the remaining project uses different credentials", () =>
1076+
Effect.scoped(
1077+
Effect.gen(function* () {
1078+
yield* TestClock.setTime(Date.parse(NOW));
1079+
const projectA = ProjectId.make("project-a");
1080+
const projectB = ProjectId.make("project-b");
1081+
const first = makeThread("first", { projectId: projectA, pullRequests: [makeLink(9)] });
1082+
const second = makeThread("second", { projectId: projectB, pullRequests: [makeLink(9)] });
1083+
const fixture = yield* makeHarness({
1084+
snapshot: makeSnapshot([first, second]),
1085+
summary: (input) =>
1086+
input.projectId === projectA ? missingSummary : Effect.succeed(makeSummary(input)),
1087+
});
1088+
yield* Effect.gen(function* () {
1089+
const reactor = yield* startAndSweep(fixture);
1090+
assert.deepStrictEqual(
1091+
(yield* Ref.get(fixture.summaryCalls)).map((call) => call.projectId),
1092+
[projectA],
1093+
);
1094+
assert.deepStrictEqual(yield* Ref.get(fixture.syncCommands), []);
1095+
1096+
yield* Ref.set(fixture.snapshots, makeSnapshot([{ ...first, pullRequests: [] }, second]));
1097+
yield* sweepAgain(fixture, reactor);
1098+
1099+
assert.deepStrictEqual(
1100+
(yield* Ref.get(fixture.summaryCalls)).map((call) => call.projectId),
1101+
[projectA, projectB],
1102+
);
1103+
assert.deepStrictEqual(
1104+
(yield* Ref.get(fixture.syncCommands)).map((command) => command.threadId),
1105+
[second.id],
1106+
);
1107+
}).pipe(Effect.provide(fixture.layer));
1108+
}),
1109+
),
1110+
);
1111+
10751112
it.effect(
10761113
"holds missing links through sibling and link updates but honors a reader refresh",
10771114
() =>

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { siblingPullRequestUrl } from "@t3tools/shared/changeRequestUrl";
22
import {
33
CommandId,
4+
type ProjectId,
45
type PullRequestSummary,
56
type ThreadId,
67
type ThreadPullRequestKey,
@@ -158,7 +159,7 @@ export const make = Effect.gen(function* () {
158159
// linking dozens of pull requests) is read together and shares the summary batches.
159160
let requestedSweepQueued = false;
160161
const retryStacks = new Set<string>();
161-
const missing = new Set<string>();
162+
const missing = new Map<string, ProjectId>();
162163
// Rate limit pauses by project and host, since each project reads with its own credential.
163164
// A paused host refuses every read without asking it, so the sweep leaves its pull requests
164165
// due until the pause ends rather than failing each of them every minute.
@@ -208,7 +209,12 @@ export const make = Effect.gen(function* () {
208209

209210
for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key);
210211
for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key);
211-
for (const key of missing) if (!groups.has(key)) missing.delete(key);
212+
for (const [key, projectId] of missing) {
213+
if (groups.get(key)?.[0]?.thread.projectId !== projectId) {
214+
missing.delete(key);
215+
lastSyncedAt.delete(key);
216+
}
217+
}
212218
for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key);
213219

214220
const linkedThisSweep = new Set<string>();
@@ -296,7 +302,7 @@ export const make = Effect.gen(function* () {
296302
Effect.tapError((error) =>
297303
error.reason === "not-found"
298304
? Effect.sync(() => {
299-
missing.add(key);
305+
missing.set(key, ref.projectId);
300306
// Not-found is a host answer: consume only this refresh and start its backoff.
301307
lastSyncedAt.set(key, nowMs);
302308
if (requested.get(key) === generation) requested.delete(key);
@@ -432,6 +438,28 @@ export const make = Effect.gen(function* () {
432438
Effect.catchCause(logSkipped("pull request refresh after run skipped", { threadId })),
433439
);
434440

441+
const requestUnsyncedLinks = Effect.fnUntraced(
442+
function* (links: ReadonlyArray<ThreadPullRequestLink>) {
443+
const unsynced = links.filter((link) => link.snapshot === null);
444+
const threads = unsynced.some((link) => missing.has(threadPullRequestKeyOf(link)))
445+
? yield* projections.getThreadsWithPullRequests()
446+
: [];
447+
for (const link of unsynced) {
448+
const projectId = missing.get(threadPullRequestKeyOf(link));
449+
if (projectId !== undefined) {
450+
const first = threads.find((thread) =>
451+
visibleThreadPullRequests(thread.pullRequests ?? []).some((candidate) =>
452+
threadPullRequestKeysEqual(candidate, link),
453+
),
454+
);
455+
if (first === undefined || first.projectId === projectId) continue;
456+
}
457+
yield* requestSync(link);
458+
}
459+
},
460+
Effect.catchCause(logSkipped("pull request refresh after link update skipped", {})),
461+
);
462+
435463
const start: PullRequestSyncReactor["Service"]["start"] = Effect.fn(
436464
"PullRequestSyncReactor.start",
437465
)(function* () {
@@ -449,17 +477,10 @@ export const make = Effect.gen(function* () {
449477
Stream.runForEach(events, (event) => {
450478
switch (event.type) {
451479
case "thread.pull-request-link-updated":
452-
return event.payload.snapshot === null &&
453-
!missing.has(threadPullRequestKeyOf(event.payload))
454-
? requestSync(event.payload)
455-
: Effect.void;
480+
return requestUnsyncedLinks([event.payload]);
456481
case "thread.pull-request-synced":
457-
return Effect.forEach(
458-
visibleThreadPullRequests(event.payload.pullRequests ?? []).filter(
459-
(link) => link.snapshot === null && !missing.has(threadPullRequestKeyOf(link)),
460-
),
461-
requestSync,
462-
{ discard: true },
482+
return requestUnsyncedLinks(
483+
visibleThreadPullRequests(event.payload.pullRequests ?? []),
463484
);
464485
// An agent can merge or close its pull request from a shell (`gh pr merge`), which
465486
// sends no merge notification. When a run that ran such a command ends, read the

0 commit comments

Comments
 (0)