fix(native-chat): reload a GitHub link preview when its account binding comes back (#43483)

* fix(native-chat): retry a GitHub link preview when its account binding comes back

A preview lookup that started under account A is retired when the repo's
binding changes, so its late answer can't paint. If the binding returns to
A before the card re-renders, the card keeps its key and never asks again,
leaving it on "Preview unavailable". Ask again when the retired binding is
current once more, and cover both orderings with a card-level test.

* fix(native-chat): restart a preview as soon as its account binding returns

Start the fresh lookup when the retired binding is current again instead
of waiting for the retired request to settle, so a slow, hanging or
failing retired request cannot leave the card failed. Errors under an
unchanged binding still reach the card.
This commit is contained in:
Jinjing
2026-11-16 23:12:19 -08:00
committed by GitHub
parent 01b8705f7e
commit bf33c20951
2 changed files with 199 additions and 8 deletions
@@ -17,6 +17,7 @@ import {
} from './native-chat-github-route-repo'
const previews = createHoverPreviewCache<GitHubWorkItem>({ freshMs: 60_000, maxEntries: 200 })
const RESTARTED = Symbol('restarted')
/** Repo ids repeat across hosts, and each host answers with its own GitHub account. */
export function githubLinkPreviewKey(repo: Repo, link: GitHubIssueOrPRLink): string {
@@ -152,6 +153,17 @@ async function loadPreviewFromCatalog(
capturedIdentities ??
new Set(githubPreviewRouteCandidates(repos, routeAuthority).map(githubPreviewRepoIdentity))
const retired = new Set<string>()
// The selected binding, once known; a request under it that was retired and is current again
// starts over right away instead of waiting on, or failing with, the retired request.
let selected: string | null = null
let restart = (): void => {}
const restarted = new Promise<typeof RESTARTED>((resolve) => {
restart = () => resolve(RESTARTED)
})
const restored = (): boolean => {
const current = selected && retired.has(selected) && read(useAppStore.getState().repos)
return !!current && githubPreviewRepoIdentity(current) === selected
}
const stop = useAppStore.subscribe((state, previous) => {
if (state.repos === previous.repos) {
return
@@ -165,6 +177,9 @@ async function loadPreviewFromCatalog(
retired.add(identity)
}
}
if (restored()) {
restart()
}
})
try {
const repo = await select(repos)
@@ -172,6 +187,7 @@ async function loadPreviewFromCatalog(
return null
}
const identity = githubPreviewRepoIdentity(repo)
selected = identity
const accepts = () => {
const currentRepos = useAppStore.getState().repos
const currentRepo = read(currentRepos)
@@ -182,16 +198,26 @@ async function loadPreviewFromCatalog(
githubPreviewRepoIdentity(currentRepo) === identity
)
}
if (!accepts()) {
return null
if (accepts()) {
const item = await Promise.race([
previews.load(
githubLinkPreviewKey(repo, link),
() => fetchPreview(repo, link, accepts),
accepts
),
restarted
])
if (item !== RESTARTED && accepts()) {
return item
}
}
} catch (error) {
if (!restored()) {
throw error
}
const item = await previews.load(
githubLinkPreviewKey(repo, link),
() => fetchPreview(repo, link, accepts),
accepts
)
return accepts() ? item : null
} finally {
stop()
}
// The card keeps its key across a binding that changed and came back, so nothing else asks again.
return restored() ? loadPreviewFromCatalog(routeAuthority, link, select, read) : null
}
@@ -0,0 +1,165 @@
// @vitest-environment happy-dom
import { act, cleanup, render, screen } from '@testing-library/react'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import type { Repo } from '../../../../../../shared/repository/repo-types'
import { parseGitHubIssueOrPRLink } from '../../../../../../shared/github/repositories/links'
import { clearRepoSlugCacheValues } from '@/lib/workspace/catalog/repo-slug-cache'
const mocks = vi.hoisted(() => ({ endpoint: vi.fn() }))
vi.mock('@/store/index', async () => {
const { create } = await import('zustand')
return { useAppStore: create<{ repos: Repo[] }>(() => ({ repos: [] })) }
})
vi.mock('@/runtime/rpc/calls/runtime-rpc-call', () => ({ callRuntimeRpc: vi.fn() }))
vi.mock('@/i18n/use-translate', () => ({
useTranslate: () => (_key: string, fallback: string) => fallback,
useIntlLocale: () => 'en-US'
}))
vi.mock('@/lib/workspace/catalog/repo-slug-index', async () => {
const { rememberRepoSlug, repoSlugCacheKey } =
await import('@/lib/workspace/catalog/repo-slug-cache')
return {
resolveRepoSlug: async (repo: Repo) => {
const key = repoSlugCacheKey(repo)
if (key) {
rememberRepoSlug(key, 'acme/router')
}
return 'acme/router'
}
}
})
const { useAppStore } = await import('@/store/index')
const { NativeChatGitHubPeekBody } = await import('./NativeChatGitHubLinkPeek')
const { loadGitHubLinkPreview, loadRoutedGitHubLinkPreview } =
await import('./native-chat-github-link-preview')
const alice: Repo = {
id: 'router',
path: '/router',
displayName: 'Router',
badgeColor: '',
addedAt: 0,
ghAccount: { host: 'github.com', user: 'alice' }
}
const bob: Repo = { ...alice, ghAccount: { host: 'github.com', user: 'bob' } }
type Request = {
user: string | undefined
answer: (title: string) => void
fail: (error: Error) => void
}
let requests: Request[] = []
beforeEach(() => {
clearRepoSlugCacheValues()
requests = []
useAppStore.setState({ repos: [alice] })
mocks.endpoint.mockReset()
mocks.endpoint.mockImplementation(
({ number }: { number: number }) =>
new Promise((resolve, reject) => {
requests.push({
user: useAppStore.getState().repos[0]?.ghAccount?.user,
fail: reject,
answer: (title) =>
resolve({
type: 'pr',
number,
title,
state: 'open',
url: `https://github.com/acme/private/pull/${number}`
})
})
})
)
vi.stubGlobal('api', { gh: { workItemByOwnerRepo: mocks.endpoint } })
})
afterEach(() => {
cleanup()
vi.unstubAllGlobals()
})
const settle = () =>
act(async () => {
for (let i = 0; i < 10; i += 1) {
await Promise.resolve()
}
})
it.each([
{ number: 901, source: 'routed', commits: 'separate', retired: 'answers' },
{ number: 902, source: 'routed', commits: 'one', retired: 'answers' },
{ number: 903, source: 'repo', commits: 'separate', retired: 'answers' },
{ number: 904, source: 'repo', commits: 'one', retired: 'answers' },
{ number: 905, source: 'routed', commits: 'one', retired: 'rejects' },
{ number: 906, source: 'repo', commits: 'one', retired: 'rejects' },
{ number: 907, source: 'routed', commits: 'one', retired: 'hangs' },
{ number: 908, source: 'repo', commits: 'one', retired: 'hangs' }
] as const)(
'$source card shows the current account after A -> B -> A in $commits commit(s) when the retired A request $retired',
async ({ number, source, commits, retired }) => {
const link = parseGitHubIssueOrPRLink(`https://github.com/acme/private/pull/${number}`)!
function Card() {
const repo = useAppStore((state) => state.repos[0])
return (
<NativeChatGitHubPeekBody
source={
source === 'routed' ? { kind: 'routed', authorityKey: 'local' } : { kind: 'repo', repo }
}
link={link}
/>
)
}
render(<Card />)
await settle()
if (commits === 'separate') {
act(() => useAppStore.setState({ repos: [bob] }))
await settle()
act(() => useAppStore.setState({ repos: [alice] }))
} else {
act(() => {
useAppStore.setState({ repos: [bob] })
useAppStore.setState({ repos: [alice] })
})
}
await settle()
// Recovery asks again without waiting on the retired first request.
expect(requests.map((request) => request.user)).toEqual(
commits === 'separate' ? ['alice', 'bob', 'alice'] : ['alice', 'alice']
)
for (const request of requests.slice(1)) {
await act(async () => request.answer(`${request.user} answer`))
}
await settle()
// Its late outcome must not paint.
if (retired === 'answers') {
await act(async () => requests[0].answer('retired answer'))
} else if (retired === 'rejects') {
await act(async () => requests[0].fail(new Error('retired failure')))
}
await settle()
expect(screen.getByText('alice answer')).toBeTruthy()
expect(screen.queryByText('retired answer')).toBeNull()
expect(screen.queryByRole('button')).toBeNull()
}
)
it.each(['routed', 'repo'] as const)(
'%s lookup still fails with its error when the binding never changed',
async (source) => {
const link = parseGitHubIssueOrPRLink(
`https://github.com/acme/private/pull/${source === 'routed' ? 911 : 912}`
)!
const pending =
source === 'routed'
? loadRoutedGitHubLinkPreview('local', link)
: loadGitHubLinkPreview(alice, link)
await settle()
requests[0].fail(new Error('gh failed'))
await expect(pending).rejects.toThrow('gh failed')
expect(requests).toHaveLength(1)
}
)