fix: preserve fallback PR cache on refresh miss (#2566)

This commit is contained in:
Jinjing
2026-05-21 16:06:41 -07:00
committed by GitHub
parent 99cd83ed35
commit c949af129a
4 changed files with 189 additions and 23 deletions
+18 -20
View File
@@ -156,6 +156,19 @@ function freshRetryAt(candidate: GitHubPRRefreshCandidate): number | null {
: candidate.cachedFetchedAt + refreshIntervalForCandidate(candidate)
}
function aliasFromCandidate(candidate: GitHubPRRefreshCandidate): GitHubPRRefreshAlias {
return {
cacheKey: candidate.cacheKey,
repoId: candidate.repoId,
repoPath: candidate.repoPath,
branch: candidate.branch,
worktreeId: candidate.worktreeId,
connectionId: candidate.connectionId ?? null,
linkedPRNumber: candidate.linkedPRNumber ?? null,
fallbackPRNumber: candidate.linkedPRNumber == null ? (candidate.fallbackPRNumber ?? null) : null
}
}
function visibleCandidateAfterOutcome(
candidate: GitHubPRRefreshCandidate,
outcome: PRRefreshOutcome
@@ -457,14 +470,7 @@ export function enqueuePRRefresh(
priority = 0,
windowId?: number
): void {
const alias: GitHubPRRefreshAlias = {
cacheKey: candidate.cacheKey,
repoId: candidate.repoId,
repoPath: candidate.repoPath,
branch: candidate.branch,
worktreeId: candidate.worktreeId,
connectionId: candidate.connectionId ?? null
}
const alias = aliasFromCandidate(candidate)
const key = refreshKey(candidate)
const skippedReason = validateCandidate(candidate)
if (skippedReason) {
@@ -541,20 +547,12 @@ export function reportVisiblePRRefreshCandidates(
}
export async function refreshPRNow(candidate: GitHubPRRefreshCandidate): Promise<PRRefreshOutcome> {
const alias: GitHubPRRefreshAlias = {
cacheKey: candidate.cacheKey,
repoId: candidate.repoId,
repoPath: candidate.repoPath,
branch: candidate.branch,
worktreeId: candidate.worktreeId,
connectionId: candidate.connectionId ?? null
}
const alias = aliasFromCandidate(candidate)
const key = refreshKey(candidate)
const existing = queue.get(key)
const aliases = existing ? Array.from(existing.aliases.values()) : [alias]
if (!aliases.some((entry) => entry.cacheKey === alias.cacheKey)) {
aliases.push(alias)
}
const aliasMap = new Map(existing ? existing.aliases : [])
aliasMap.set(alias.cacheKey, alias)
const aliases = Array.from(aliasMap.values())
const skippedReason = validateCandidate(candidate)
if (skippedReason) {
removeQueuedAliasForInvalidCandidate(key, alias)
@@ -1050,6 +1050,59 @@ describe('createGitHubSlice.fetchPRForBranch', () => {
expect(store.getState().prCache[`repo-1::${branch}`]?.data).toEqual(cachedPR)
})
it('preserves visible cached PR data when a fallback refresh misses', async () => {
const store = createTestStore()
const repoPath = '/repo'
const repoId = 'repo-1'
const branch = 'feature/fallback-miss'
const cachedPR = makePR({ number: 12, title: 'Visible cached PR' })
const hostedReviewCacheKey = getHostedReviewCacheKey(repoPath, branch, null, repoId)
store.setState({
repos: [{ id: repoId, path: repoPath, name: 'repo', kind: 'git' }],
prCache: {
[`${repoId}::${branch}`]: {
data: cachedPR,
fetchedAt: 1
}
},
hostedReviewCache: {
[hostedReviewCacheKey]: {
data: {
provider: 'github',
number: 12,
title: 'Visible cached PR',
state: 'open',
url: 'https://github.com/acme/orca/pull/12',
status: 'pending',
updatedAt: '2026-03-28T00:00:00Z',
mergeable: 'UNKNOWN'
},
fetchedAt: 1,
linkedReviewHintKey: 'github:12'
}
}
} as unknown as Partial<AppState>)
mockApi.gh.refreshPRNow.mockResolvedValueOnce({ kind: 'no-pr', fetchedAt: 2 })
await expect(
store.getState().fetchPRForBranch(repoPath, branch, {
force: true,
repoId,
fallbackPRNumber: 12
})
).resolves.toEqual(cachedPR)
expect(store.getState().prCache[`${repoId}::${branch}`]).toEqual({
data: cachedPR,
fetchedAt: 1
})
expect(store.getState().hostedReviewCache[hostedReviewCacheKey]).toMatchObject({
data: expect.objectContaining({ provider: 'github', number: 12 }),
fetchedAt: 1,
linkedReviewHintKey: 'github:12'
})
})
it('records PR refresh errors without clearing cached PR data', () => {
const store = createTestStore()
const repoPath = '/repo'
@@ -1086,6 +1139,55 @@ describe('createGitHubSlice.fetchPRForBranch', () => {
})
})
it('preserves visible cached PR data when a fallback refresh event misses', () => {
const store = createTestStore()
const repoPath = '/repo'
const repoId = 'repo-1'
const branch = 'feature/event-fallback-miss'
const cacheKey = `${repoId}::${branch}`
const cachedPR = makePR({ number: 12, title: 'Visible event PR' })
const hostedReviewCacheKey = getHostedReviewCacheKey(repoPath, branch, null, repoId)
store.setState({
prCache: {
[cacheKey]: {
data: cachedPR,
fetchedAt: 1
}
},
hostedReviewCache: {
[hostedReviewCacheKey]: {
data: {
provider: 'github',
number: 12,
title: 'Visible event PR',
state: 'open',
url: 'https://github.com/acme/orca/pull/12',
status: 'pending',
updatedAt: '2026-03-28T00:00:00Z',
mergeable: 'UNKNOWN'
},
fetchedAt: 1,
linkedReviewHintKey: 'github:12'
}
}
} as unknown as Partial<AppState>)
store.getState().applyGitHubPRRefreshEvent({
sequence: 1,
aliases: [{ cacheKey, repoId, repoPath, branch, fallbackPRNumber: 12 }],
reason: 'visible',
outcome: { kind: 'no-pr', fetchedAt: 2 }
})
expect(store.getState().prCache[cacheKey]).toEqual({ data: cachedPR, fetchedAt: 1 })
expect(store.getState().hostedReviewCache[hostedReviewCacheKey]).toMatchObject({
data: expect.objectContaining({ provider: 'github', number: 12 }),
fetchedAt: 1,
linkedReviewHintKey: 'github:12'
})
})
it('updates hosted review cache from GitHub PR refresh events', () => {
const store = createTestStore()
const repoPath = '/repo'
+67 -1
View File
@@ -608,6 +608,8 @@ function syncHostedReviewCacheFromGitHubPRResult(args: {
connectionId?: string | null
pr: PRInfo | null
fetchedAt: number
linkedPRNumber?: number | null
fallbackPRNumber?: number | null
requestStartedAt?: number
requestStartedEntry?: AppState['hostedReviewCache'][string]
}): { cache: AppState['hostedReviewCache']; accepted: boolean } {
@@ -640,6 +642,15 @@ function syncHostedReviewCacheFromGitHubPRResult(args: {
if (args.pr && hostedReviewEntry?.data && hostedReviewEntry.data.provider !== 'github') {
return { cache: args.cache, accepted: false }
}
if (
!args.pr &&
args.linkedPRNumber == null &&
args.fallbackPRNumber != null &&
hostedReviewEntry?.data?.provider === 'github' &&
hostedReviewEntry.data.number === args.fallbackPRNumber
) {
return { cache: args.cache, accepted: false }
}
if (!args.pr && !shouldClearHostedReviewForNoGitHubPR(hostedReviewEntry)) {
return { cache: args.cache, accepted: hostedReviewEntry?.data == null }
}
@@ -666,13 +677,34 @@ function shouldWritePRCacheForHostedReviewSync(args: {
return args.hostedReviewSyncAccepted
}
function shouldPreserveExistingPRForFallbackMiss(args: {
currentPR: PRInfo | null | undefined
nextPR: PRInfo | null
linkedPRNumber?: number | null
fallbackPRNumber?: number | null
}): boolean {
// Why: fallback PR numbers come from already-visible cache, not durable
// worktree metadata. A branch/fallback miss is weaker than the current exact
// PR context, so do not make the Checks panel blink to "No pull request".
return (
args.nextPR === null &&
args.linkedPRNumber == null &&
args.fallbackPRNumber != null &&
args.currentPR?.number === args.fallbackPRNumber
)
}
function applyPRCacheResult(
cache: AppState['prCache'],
cacheKey: string,
pr: PRInfo | null,
fetchedAt: number,
accepted: boolean
accepted: boolean,
preserveExisting: boolean
): AppState['prCache'] {
if (preserveExisting) {
return cache
}
if (accepted) {
return { ...cache, [cacheKey]: { data: pr, fetchedAt } }
}
@@ -699,6 +731,8 @@ function setGitHubPRResultCaches(
connectionId?: string | null
pr: PRInfo | null
fetchedAt: number
linkedPRNumber?: number | null
fallbackPRNumber?: number | null
requestStartedAt?: number
requestStartedEntry?: AppState['hostedReviewCache'][string]
}
@@ -712,6 +746,8 @@ function setGitHubPRResultCaches(
connectionId: args.connectionId,
pr: args.pr,
fetchedAt: args.fetchedAt,
linkedPRNumber: args.linkedPRNumber,
fallbackPRNumber: args.fallbackPRNumber,
requestStartedAt: args.requestStartedAt,
requestStartedEntry: args.requestStartedEntry
})
@@ -723,6 +759,12 @@ function setGitHubPRResultCaches(
args.fetchedAt,
shouldWritePRCacheForHostedReviewSync({
hostedReviewSyncAccepted: hostedReviewSync.accepted
}),
shouldPreserveExistingPRForFallbackMiss({
currentPR: state.prCache[args.prCacheKey]?.data,
nextPR: args.pr,
linkedPRNumber: args.linkedPRNumber,
fallbackPRNumber: args.fallbackPRNumber
})
),
...(hostedReviewSync.cache === state.hostedReviewCache
@@ -742,6 +784,8 @@ function applyGitHubPRResultToCaches(args: {
connectionId?: string | null
pr: PRInfo | null
fetchedAt: number
linkedPRNumber?: number | null
fallbackPRNumber?: number | null
requestStartedAt?: number
requestStartedEntry?: AppState['hostedReviewCache'][string]
}): {
@@ -757,6 +801,8 @@ function applyGitHubPRResultToCaches(args: {
connectionId: args.connectionId,
pr: args.pr,
fetchedAt: args.fetchedAt,
linkedPRNumber: args.linkedPRNumber,
fallbackPRNumber: args.fallbackPRNumber,
requestStartedAt: args.requestStartedAt,
requestStartedEntry: args.requestStartedEntry
})
@@ -768,6 +814,12 @@ function applyGitHubPRResultToCaches(args: {
args.fetchedAt,
shouldWritePRCacheForHostedReviewSync({
hostedReviewSyncAccepted: hostedReviewSync.accepted
}),
shouldPreserveExistingPRForFallbackMiss({
currentPR: args.prCache[args.prCacheKey]?.data,
nextPR: args.pr,
linkedPRNumber: args.linkedPRNumber,
fallbackPRNumber: args.fallbackPRNumber
})
),
hostedReviewCache: hostedReviewSync.cache
@@ -1801,12 +1853,24 @@ export const createGitHubSlice: StateCreator<AppState, [], [], GitHubSlice> = (s
connectionId: repo?.connectionId,
pr,
fetchedAt: outcome.fetchedAt,
linkedPRNumber,
fallbackPRNumber,
requestStartedAt,
requestStartedEntry: requestStartedHostedReviewEntry
})
)
debouncedSaveCache(get())
}
if (
shouldPreserveExistingPRForFallbackMiss({
currentPR: get().prCache[cacheKey]?.data,
nextPR: pr,
linkedPRNumber,
fallbackPRNumber
})
) {
return get().prCache[cacheKey]?.data ?? null
}
return pr ?? null
} catch (err) {
console.error('Failed to fetch PR:', err)
@@ -2260,6 +2324,8 @@ export const createGitHubSlice: StateCreator<AppState, [], [], GitHubSlice> = (s
connectionId: alias.connectionId,
pr: data,
fetchedAt: event.outcome.fetchedAt,
linkedPRNumber: alias.linkedPRNumber,
fallbackPRNumber: alias.fallbackPRNumber,
requestStartedAt: event.requestStartedAt,
requestStartedEntry
})
+2 -2
View File
@@ -644,11 +644,11 @@ export type GitHubPRRefreshAlias = {
branch: string
worktreeId?: string
connectionId?: string | null
linkedPRNumber?: number | null
fallbackPRNumber?: number | null
}
export type GitHubPRRefreshCandidate = GitHubPRRefreshAlias & {
linkedPRNumber?: number | null
fallbackPRNumber?: number | null
repoKind: RepoKind
repoId: string
isBare?: boolean