fix: clear visible pr refresh state on window destroy (#4148)

This commit is contained in:
Neil
2026-05-31 06:04:54 -07:00
committed by GitHub
parent 3162d0fad0
commit 9d66aceaa6
3 changed files with 66 additions and 13 deletions
@@ -402,6 +402,31 @@ describe('pr-refresh-coordinator', () => {
expect(getPRForBranchOutcomeMock).toHaveBeenCalledTimes(2)
})
it('clears visible follow-ups when the owning window is destroyed', async () => {
const {
_getVisiblePRRefreshWindowCountForTests,
clearVisiblePRRefreshWindow,
reportVisiblePRRefreshCandidates
} = await import('./pr-refresh-coordinator')
getPRForBranchOutcomeMock.mockResolvedValue({
kind: 'found',
pr: makePR({ checksStatus: 'pending', mergeable: 'MERGEABLE' }),
fetchedAt: Date.now()
})
reportVisiblePRRefreshCandidates([makeCandidate()], 1, 1)
await vi.runOnlyPendingTimersAsync()
expect(_getVisiblePRRefreshWindowCountForTests()).toBe(1)
expect(getPRForBranchOutcomeMock).toHaveBeenCalledTimes(1)
clearVisiblePRRefreshWindow(1)
await vi.advanceTimersByTimeAsync(90_000)
expect(_getVisiblePRRefreshWindowCountForTests()).toBe(0)
expect(getPRForBranchOutcomeMock).toHaveBeenCalledTimes(1)
})
it('retries visible PRs with unknown mergeability before the success-check interval', async () => {
const { reportVisiblePRRefreshCandidates } = await import('./pr-refresh-coordinator')
getPRForBranchOutcomeMock
+29 -12
View File
@@ -51,6 +51,30 @@ export function setPRRefreshOutcomeObserver(observer: PRRefreshOutcomeObserver |
outcomeObserver = observer
}
function removeInvisibleVisibleRefreshes(): void {
for (const [key, entry] of queue) {
if (entry.reason === 'visible' && !isVisibleKey(key)) {
queue.delete(key)
errorBackoff.delete(key)
broadcast({
aliases: Array.from(entry.aliases.values()),
reason: 'visible',
status: 'skipped',
skippedReason: 'fresh'
})
}
}
}
export function clearVisiblePRRefreshWindow(windowId: number): void {
if (!visibleByWindow.delete(windowId)) {
return
}
// Why: visible follow-ups are owned by the renderer that reported them.
// If that WebContents is destroyed, no later visibility report may arrive.
removeInvisibleVisibleRefreshes()
}
function nextSequence(): number {
sequence += 1
return sequence
@@ -548,23 +572,16 @@ export function reportVisiblePRRefreshCandidates(
return
}
visibleByWindow.set(windowId, { generation, keys: new Set(candidates.map(refreshKey)) })
for (const [key, entry] of queue) {
if (entry.reason === 'visible' && !isVisibleKey(key)) {
queue.delete(key)
errorBackoff.delete(key)
broadcast({
aliases: Array.from(entry.aliases.values()),
reason: 'visible',
status: 'skipped',
skippedReason: 'fresh'
})
}
}
removeInvisibleVisibleRefreshes()
for (const candidate of candidates) {
enqueuePRRefresh(candidate, 'visible', 40, windowId)
}
}
export function _getVisiblePRRefreshWindowCountForTests(): number {
return visibleByWindow.size
}
export async function refreshPRNow(candidate: GitHubPRRefreshCandidate): Promise<PRRefreshOutcome> {
const alias = aliasFromCandidate(candidate)
const key = refreshKey(candidate)
+12 -1
View File
@@ -49,6 +49,7 @@ import {
starOrca
} from '../github/client'
import {
clearVisiblePRRefreshWindow,
enqueuePRRefresh,
refreshPRNow,
reportVisiblePRRefreshCandidates,
@@ -98,6 +99,8 @@ import { appStarSourceSchema } from '../../shared/gh-star-source'
import { track } from '../telemetry/client'
import { getCohortAtEmit } from '../telemetry/cohort-classifier'
const prRefreshVisibilityCleanupRegistered = new Set<number>()
// Why: notify every renderer (each window has its own SWR cache instance)
// that a work item was mutated locally so they can drop their cached entry
// and refetch on the next open. Only emitted after a successful mutation.
@@ -248,6 +251,14 @@ export function registerGitHubHandlers(store: Store, stats: StatsCollector): voi
ipcMain.handle(
'gh:reportVisiblePRRefreshCandidates',
(event, args: { candidates: GitHubPRRefreshCandidate[]; generation: number }) => {
const senderId = event.sender.id
if (!prRefreshVisibilityCleanupRegistered.has(senderId)) {
prRefreshVisibilityCleanupRegistered.add(senderId)
event.sender.once('destroyed', () => {
prRefreshVisibilityCleanupRegistered.delete(senderId)
clearVisiblePRRefreshWindow(senderId)
})
}
const candidates = args.candidates.map((candidate) => {
const repo = assertRegisteredRepo(candidate.repoPath, store)
return {
@@ -258,7 +269,7 @@ export function registerGitHubHandlers(store: Store, stats: StatsCollector): voi
connectionState: repo.connectionId ? 'connected' : candidate.connectionState
}
})
reportVisiblePRRefreshCandidates(candidates, args.generation, event.sender.id)
reportVisiblePRRefreshCandidates(candidates, args.generation, senderId)
return true
}
)