Skip to content

Commit 18d9026

Browse files
committed
fix(server): links on hosts the server cannot browse no longer re-read every minute
1 parent ac5e48c commit 18d9026

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ProjectId,
99
ProviderInstanceId,
1010
PullRequestOperationError,
11+
PullRequestUnavailableError,
1112
RunId,
1213
ThreadId,
1314
TurnItemId,
@@ -166,7 +167,7 @@ interface HarnessOptions {
166167
readonly snapshot: TestShellSnapshot;
167168
readonly summary?: (
168169
input: PullRequestRef,
169-
) => Effect.Effect<PullRequestSummary, PullRequestOperationError>;
170+
) => Effect.Effect<PullRequestSummary, PullRequestService.PullRequestError>;
170171
readonly stack?: (
171172
input: PullRequestRef,
172173
) => Effect.Effect<PullRequestStack | null, PullRequestOperationError>;
@@ -1519,6 +1520,60 @@ describe("PullRequestSyncReactor", () => {
15191520
),
15201521
);
15211522

1523+
it.effect("reads a link on a host it cannot browse on the slow cadence", () =>
1524+
Effect.scoped(
1525+
Effect.gen(function* () {
1526+
yield* TestClock.setTime(Date.parse(NOW));
1527+
const fixture = yield* makeHarness({
1528+
snapshot: makeSnapshot([makeThread("unsupported", { pullRequests: [makeLink(7)] })]),
1529+
summary: (input) =>
1530+
input.number === 7
1531+
? Effect.fail(new PullRequestUnavailableError({ reason: "provider-unsupported" }))
1532+
: Effect.succeed(makeSummary(input)),
1533+
});
1534+
const reads = (number: number) =>
1535+
Ref.get(fixture.summaryCalls).pipe(
1536+
Effect.map((calls) => calls.filter((call) => call.number === number).length),
1537+
);
1538+
1539+
yield* Effect.gen(function* () {
1540+
const reactor = yield* startAndSweep(fixture);
1541+
yield* sweepAgain(fixture, reactor);
1542+
assert.strictEqual(yield* reads(7), 1);
1543+
1544+
// Only the failing link waits; another link on the same host still reads.
1545+
yield* Ref.set(
1546+
fixture.snapshots,
1547+
makeSnapshot([makeThread("unsupported", { pullRequests: [makeLink(7), makeLink(8)] })]),
1548+
);
1549+
yield* sweepAgain(fixture, reactor);
1550+
assert.strictEqual(yield* reads(8), 1);
1551+
assert.strictEqual(yield* reads(7), 1);
1552+
1553+
yield* TestClock.adjust("13 minutes");
1554+
yield* Queue.take(fixture.snapshotReads);
1555+
yield* reactor.drain;
1556+
assert.strictEqual(yield* reads(7), 2);
1557+
1558+
// A refresh the user asks for reads through the pause, for example after a sign-in.
1559+
yield* reactor.requestSync({
1560+
host: "github.com",
1561+
repository: "owner/repository",
1562+
number: 7,
1563+
});
1564+
yield* Queue.take(fixture.snapshotReads);
1565+
yield* reactor.drain;
1566+
assert.strictEqual(yield* reads(7), 3);
1567+
1568+
// A failed refresh does not keep bypassing the pause.
1569+
yield* sweepAgain(fixture, reactor);
1570+
yield* sweepAgain(fixture, reactor);
1571+
assert.strictEqual(yield* reads(7), 3);
1572+
}).pipe(Effect.provide(fixture.layer));
1573+
}),
1574+
),
1575+
);
1576+
15221577
it.effect.each([
15231578
"gh pr merge 7 --squash 2>&1 | tail -3",
15241579
"cafe pr merge 7 --squash",

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
isOrchestrationV2WorkActive,
44
type OrchestrationV2TurnItem,
55
type PullRequestSummary,
6+
PullRequestUnavailableError,
67
type ThreadId,
78
type ThreadPullRequestKey,
89
type ThreadPullRequestLink,
@@ -175,6 +176,7 @@ export function changeRequestsCreatedBy(input: string, output: string) {
175176
}
176177

177178
const isPullRequestProviderError = Schema.is(PullRequestProviderError);
179+
const isPullRequestUnavailableError = Schema.is(PullRequestUnavailableError);
178180

179181
type SnapshotFields = Omit<ThreadPullRequestSnapshot, "syncedAt">;
180182

@@ -316,6 +318,11 @@ function rateLimitRetryAt(cause: Cause.Cause<unknown>): number | undefined {
316318
return undefined;
317319
}
318320

321+
function isProviderUnsupported(cause: Cause.Cause<unknown>): boolean {
322+
const error = Cause.squash(cause);
323+
return isPullRequestUnavailableError(error) && error.reason === "provider-unsupported";
324+
}
325+
319326
function isUnsettled(thread: ProjectionStore.ProjectionThreadPullRequests): boolean {
320327
return thread.settledOverride !== "settled" && thread.settledAt === null;
321328
}
@@ -348,6 +355,8 @@ export const make = Effect.gen(function* () {
348355
const retryStacks = new Set<string>();
349356
// Pause per project and host because credentials are project-specific; leave paused PRs due until reset.
350357
const pausedUntil = new Map<string, number>();
358+
// Links whose host this server cannot browse; only sweeps wait, a requested refresh reads through.
359+
const unsupportedUntil = new Map<string, number>();
351360

352361
const isDue = (key: string, entries: ReadonlyArray<LinkEntry>, nowMs: number): boolean => {
353362
if (requested.has(key) || retryStacks.has(key)) return true;
@@ -404,6 +413,7 @@ export const make = Effect.gen(function* () {
404413
for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key);
405414
for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key);
406415
for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key);
416+
for (const key of unsupportedUntil.keys()) if (!groups.has(key)) unsupportedUntil.delete(key);
407417

408418
const linkedThisSweep = new Set<string>();
409419
// Fresh stack reads by group, so a member flagged by a sibling is compared against what the
@@ -582,12 +592,18 @@ export const make = Effect.gen(function* () {
582592
const skips = new Map<string, { count: number; readonly key: string }>();
583593
const readGroup = (key: string, entries: ReadonlyArray<LinkEntry>, pauseKey: string) =>
584594
syncGroup(key, entries).pipe(
595+
Effect.tap(() => Effect.sync(() => unsupportedUntil.delete(key))),
585596
Effect.catchCause((cause) => {
586597
if (Cause.hasInterruptsOnly(cause)) return Effect.failCause(cause);
587598
const retryAt = rateLimitRetryAt(cause);
588599
if (retryAt !== undefined) {
589600
pausedUntil.set(pauseKey, Math.max(retryAt, pausedUntil.get(pauseKey) ?? 0));
590601
}
602+
if (isProviderUnsupported(cause)) {
603+
unsupportedUntil.set(key, nowMs + SLOW_SYNC_INTERVAL_MS);
604+
// The refresh was answered; keeping it would bypass the pause on every sweep.
605+
requested.delete(key);
606+
}
591607
const reason = skipReason(cause);
592608
const skip = skips.get(reason);
593609
if (skip === undefined) skips.set(reason, { count: 1, key });
@@ -606,7 +622,8 @@ export const make = Effect.gen(function* () {
606622
// Check pauses at each read so changes during the sweep apply immediately.
607623
return Clock.currentTimeMillis.pipe(
608624
Effect.flatMap((startedAtMs) =>
609-
(pausedUntil.get(pauseKey) ?? 0) > startedAtMs
625+
(pausedUntil.get(pauseKey) ?? 0) > startedAtMs ||
626+
(!requested.has(key) && (unsupportedUntil.get(key) ?? 0) > startedAtMs)
610627
? Effect.void
611628
: readGroup(key, entries, pauseKey),
612629
),

0 commit comments

Comments
 (0)