From 2b3b552db43b3aab2b86cd9c2bc622e885ab17af Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:47:33 -0800 Subject: [PATCH] Keep retimed PR retry gates on their current request --- ...pr-refresh-coordinator-restoration.test.ts | 75 +++++++++++++++++++ .../refresh/pr-refresh-immediate-read.ts | 2 +- .../refresh/pr-refresh-queue-drainer.ts | 2 +- .../refresh/pr-refresh-queue-scheduler.ts | 10 ++- .../refresh/pr-refresh-retry-state.ts | 13 ++-- 5 files changed, 94 insertions(+), 8 deletions(-) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-restoration.test.ts b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-restoration.test.ts index c425a23c4c3..91ed58e2c90 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-coordinator-restoration.test.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-coordinator-restoration.test.ts @@ -535,3 +535,78 @@ it('a watch that waited for quota while Off slowed it to Minimal reads at the Mi await vi.advanceTimersByTimeAsync(1) expect(readsOf('watched')).toBe(2) }) + +it('retiming an old HEAD failure cannot delay the newer HEAD return under Off', async () => { + const { coordinator, switchTo } = await load() + const target = makeCandidate({ branch: 'target', linkedPRNumber: 7, currentHeadOid: 'head-a' }) + const older = Promise.withResolvers() + const newer = Promise.withResolvers() + const observed: PRRefreshOutcome[] = [] + coordinator.addPRRefreshOutcomeObserver((_, outcome) => observed.push(outcome)) + provider.resolve.mockImplementationOnce(() => older.promise) + coordinator.reportVisiblePRRefreshCandidates([target], 1, 1) + await vi.advanceTimersByTimeAsync(0) + + await vi.advanceTimersByTimeAsync(1_000) + const current = { ...target, currentHeadOid: 'head-b' } + coordinator.reportVisiblePRRefreshCandidates([current], 2, 1) + provider.resolve.mockImplementationOnce(() => newer.promise) + const manual = coordinator.refreshPRNow(current, 'manual') + await vi.advanceTimersByTimeAsync(0) + await vi.advanceTimersByTimeAsync(1_000) + older.resolve(failed()) + await vi.advanceTimersByTimeAsync(0) + const errorsBeforeRetime = observed.filter((outcome) => outcome.kind === 'upstream-error').length + expect(errorsBeforeRetime).toBe(1) + await switchTo('off', 3_500) + await vi.advanceTimersByTimeAsync(500) + newer.resolve({ + kind: 'found', + fetchedAt: 4_000, + pr: makePR({ + number: 7, + title: 'newer', + headSha: 'head-b', + state: 'merged', + checksStatus: 'success' + }) + }) + expect(await manual).toMatchObject({ fetchedAt: 4_000, pr: { title: 'newer' } }) + expect(deliveredTo('target')).toBe('newer') + coordinator.clearVisiblePRRefreshWindow(1) + await vi.advanceTimersByTimeAsync(65_000 - Date.now()) + provider.resolve.mockImplementationOnce(async () => ({ + kind: 'found', + fetchedAt: Date.now(), + pr: makePR({ + number: 7, + title: 'returned', + headSha: 'head-b', + state: 'merged', + checksStatus: 'success' + }) + })) + coordinator.reportVisiblePRRefreshCandidates( + [ + { + ...current, + cachedFetchedAt: 4_000, + cachedHeadOid: 'head-b', + cachedHasPR: true, + cachedPRState: 'merged', + cachedChecksStatus: 'success' + } + ], + 3, + 1 + ) + await vi.advanceTimersByTimeAsync(0) + expect(deliveredTo('target')).toBe('returned') + const delivered = coordinatorMocks.sendToTrustedUIRendererMock.mock.calls + .map(([, event]) => event) + .findLast((event) => event.outcome?.pr?.title === 'returned') + expect(delivered?.outcome.fetchedAt).toBe(65_000) + expect(observed.filter((outcome) => outcome.kind === 'upstream-error')).toHaveLength( + errorsBeforeRetime + ) +}) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts b/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts index 609725d29fd..589bc4d3f37 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-immediate-read.ts @@ -124,7 +124,7 @@ export async function runImmediatePRRefresh( queue, producer, visibility.has(key), - queue.ownsRequest(key, requestSequence) + () => queue.ownsRequest(key, requestSequence) ) events.observe(candidate, settled, aliases) return settled diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts index 817e5aee747..94d79cd70fd 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-drainer.ts @@ -224,7 +224,7 @@ export class PRRefreshQueueDrainer { this.queue, next.hosted ? { retained: producer.retained } : producer, Boolean(next.hosted) || this.visibility.has(next.key), - this.queue.ownsRequest(next.key, requestSequence) + () => this.queue.ownsRequest(next.key, requestSequence) ) this.events.observe(next.candidate, settled, aliases) // A host job's read also plans the visible row of the same request, under its own claim. diff --git a/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts b/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts index 0f78e0678b2..462f9fccc86 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-queue-scheduler.ts @@ -189,7 +189,15 @@ export class PRRefreshQueueScheduler { candidate, prepared, this.queue, - producer + producer, + true, + () => { + const current = this.visibility.shown(key) + return ( + current !== undefined && + prBranchReadKeys(...candidateReadRequest(current, 'visible'))[1] === prepared.key + ) + } ) // A rate limit the read reported holds the row's retries, as a local read's would. this.retry.noteManualGate(key, settled) diff --git a/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts b/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts index a356c433a99..6780faef5a4 100644 --- a/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts +++ b/src/main/github/pull-requests/refresh/pr-refresh-retry-state.ts @@ -27,7 +27,7 @@ type ErrorBackoff = { * and delivery all read this; `retime` re-plans it and returns the key's gate, or null once a * newer read replaced it. */ settled?: PRRefreshOutcome - retime?: () => { gate: number; timed: boolean } | null + retime?: () => { gate: number; timed: boolean; ownsRequest: boolean } | null /** Whether the settled failure still owns its request: no newer provider read started, its scope * is unchanged and the exact cache still holds the error it scheduled. */ current?: () => boolean @@ -180,7 +180,9 @@ export class PRRefreshRetryState { revoke(state) } else if (plan !== undefined) { const gate = Math.max(plan.gate, this.manualGateUntil(key)) - queue.protectBackgroundUntil(key, gate) + if (plan.ownsRequest) { + queue.protectBackgroundUntil(key, gate) + } if (plan.timed) { retries.set(key, gate) } @@ -244,7 +246,7 @@ export class PRRefreshRetryState { queue: PRRefreshQueue, producer: Parameters[1] = { retained: true }, demand = true, - ownsRequest = true + ownsRequest: () => boolean = () => true ): PRRefreshOutcome { if (outcome.kind !== 'upstream-error' || !demand || (!producer.retained && !producer.hold)) { return outcome @@ -298,10 +300,11 @@ export class PRRefreshRetryState { state.settled = next return { gate: Math.max(this.errorGateAt(key, candidate, planned), rateLimitedUntil), - timed: Number.isFinite(planned) + timed: Number.isFinite(planned), + ownsRequest: ownsRequest() } } - if (ownsRequest) { + if (ownsRequest()) { queue.protectBackgroundUntil( key, Math.max(this.errorGateAt(key, candidate, retryAt), rateLimitedUntil)