fix(github): keep work-item searches repo-scoped (#9668)
* fix(github): prevent unscoped work item queries * fix(review): translate non-English rationale comment to English (client.ts:1151) Restores the English '// Why:' convention and re-states the allSettled partial-results rationale that was dropped when the comment was replaced. Flagged by Ce-code-review (maintainability/previous-comments) and a prior PR reviewer. No behavior change. Co-authored-by: Orca <help@stably.ai> * fix(tasks): surface repos with no resolvable GitHub source Since #9660 an unresolvable GitHub source returns an empty null-source envelope instead of an unscoped search. That empty was indistinguishable from a genuine zero-result query — no log, no telemetry, no UI signal. Surface it in the Tasks list: a per-repo info row with Retry for each fetched repo that resolved neither an issue nor a PR source (told apart from genuine-zero and not-yet-fetched purely via the cached `sources`). Renderer-only — the signal was already threaded through the envelope. - selectTaskPageUnresolvedSourceRepos selector (+ unit tests) - per-repo row reusing the existing banner region + handleRetryIssuesFetch - suppress the generic empty state while those rows show - assert the null-source envelope contract in client-work-items test Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -117,7 +117,6 @@ import {
|
||||
_resetMergeQueueCacheForTests,
|
||||
_resetOwnerRepoCache
|
||||
} from './client'
|
||||
import { _resetGhCwdRepoNegativeCache } from './gh-cwd-repo-negative-cache'
|
||||
import { GITHUB_WORK_ITEMS_QUERY_MAX_BYTES } from '../../shared/github-work-items-query-bounds'
|
||||
|
||||
import { _resetOriginGitHubApiRepositoryCache } from './github-api-repository'
|
||||
@@ -158,7 +157,6 @@ describe('listWorkItems query paging', () => {
|
||||
getOwnerRepoForRemoteMock.mockResolvedValue(null)
|
||||
_resetOwnerRepoCache()
|
||||
_resetMergeQueueCacheForTests()
|
||||
_resetGhCwdRepoNegativeCache()
|
||||
})
|
||||
|
||||
it('returns zero for oversized count queries before resolving repo sources', async () => {
|
||||
|
||||
@@ -111,7 +111,6 @@ import {
|
||||
_resetMergeQueueCacheForTests,
|
||||
_resetOwnerRepoCache
|
||||
} from './client'
|
||||
import { _resetGhCwdRepoNegativeCache } from './gh-cwd-repo-negative-cache'
|
||||
import { GITHUB_WORK_ITEMS_QUERY_MAX_BYTES } from '../../shared/github-work-items-query-bounds'
|
||||
|
||||
import { _resetOriginGitHubApiRepositoryCache } from './github-api-repository'
|
||||
@@ -154,7 +153,6 @@ describe('listWorkItems', () => {
|
||||
)
|
||||
_resetOwnerRepoCache()
|
||||
_resetMergeQueueCacheForTests()
|
||||
_resetGhCwdRepoNegativeCache()
|
||||
})
|
||||
|
||||
it('routes GHES work-item listing through the Enterprise host', async () => {
|
||||
@@ -172,22 +170,36 @@ describe('listWorkItems', () => {
|
||||
expect(calls.every(([, options]) => options?.host === 'github.acme-corp.com')).toBe(true)
|
||||
})
|
||||
|
||||
it('stops re-spawning gh for a repo whose cwd resolution already failed with no remotes', async () => {
|
||||
it('does not run unscoped work-item queries when a local repository has no GitHub source', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValue(null)
|
||||
getOwnerRepoMock.mockResolvedValue(null)
|
||||
ghExecFileAsyncMock.mockRejectedValue(
|
||||
Object.assign(new Error('Command failed: gh pr list\nno git remotes found'), {
|
||||
stderr: 'no git remotes found'
|
||||
})
|
||||
|
||||
// Why: unresolved sources must return empty null-source envelopes (not throw, not query) so the renderer can
|
||||
// distinguish "no GitHub source detected" from genuine zero via `sources` (#9660 follow-up).
|
||||
await expect(listWorkItems('/private-repo', 36)).resolves.toMatchObject({
|
||||
items: [],
|
||||
sources: { issues: null, prs: null }
|
||||
})
|
||||
await expect(listWorkItems('/private-repo', 36, 'is:open')).resolves.toMatchObject({
|
||||
items: [],
|
||||
sources: { issues: null, prs: null }
|
||||
})
|
||||
|
||||
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('queries only the resolved PR source when the issue source is unavailable', async () => {
|
||||
getIssueOwnerRepoMock.mockResolvedValue(null)
|
||||
getOwnerRepoMock.mockResolvedValue({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock.mockResolvedValue({ stdout: '[]' })
|
||||
|
||||
await expect(listWorkItems('/private-repo', 36)).resolves.toMatchObject({ items: [] })
|
||||
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(1)
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledWith(
|
||||
expect.arrayContaining(['pr', 'list', '--repo', 'acme/widgets']),
|
||||
{ cwd: '/private-repo' }
|
||||
)
|
||||
|
||||
await expect(listWorkItems('/no-remote-repo', 36)).rejects.toThrow('no git remotes found')
|
||||
// The first refresh pays the two cwd-fallback spawns (issue + pr list).
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(2)
|
||||
|
||||
await expect(listWorkItems('/no-remote-repo', 36)).rejects.toThrow('no git remotes found')
|
||||
// The second refresh is served from the negative cache — zero new spawns.
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('runs both issue and PR GitHub searches for a mixed query and merges the results by recency', async () => {
|
||||
|
||||
+87
-142
@@ -74,12 +74,6 @@ import {
|
||||
} from '../source-control/hosted-review-git-options'
|
||||
import { shouldHideNonOpenReviewOnDefaultBranch } from '../source-control/repo-default-branch'
|
||||
import { readLocalGitConfigSignature } from './local-git-config-signature'
|
||||
import {
|
||||
getRememberedGhCwdResolutionFailure,
|
||||
isGhCwdRepoResolutionFailure,
|
||||
rememberGhCwdResolutionFailure
|
||||
} from './gh-cwd-repo-negative-cache'
|
||||
import type { GitHubRepoContext } from './github-repository-identity'
|
||||
import {
|
||||
getGitHubApiRepositoryForRemote,
|
||||
getIssueGitHubApiRepository,
|
||||
@@ -1001,7 +995,7 @@ function normalizeWorkItemPage(page: number | undefined): number {
|
||||
|
||||
function buildWorkItemListRequest(args: {
|
||||
kind: 'issue' | 'pr'
|
||||
ownerRepo: OwnerRepo | null
|
||||
ownerRepo: OwnerRepo
|
||||
limit: number
|
||||
query: ParsedTaskQuery
|
||||
page: number
|
||||
@@ -1009,7 +1003,7 @@ function buildWorkItemListRequest(args: {
|
||||
const { kind, ownerRepo, limit, query, page } = args
|
||||
const searchParts: string[] = []
|
||||
|
||||
if (kind === 'issue' && ownerRepo) {
|
||||
if (kind === 'issue') {
|
||||
searchParts.push(`repo:${ownerRepo.owner}/${ownerRepo.repo}`)
|
||||
}
|
||||
searchParts.push(kind === 'issue' ? 'is:issue' : 'is:pr')
|
||||
@@ -1076,9 +1070,7 @@ function buildWorkItemListRequest(args: {
|
||||
'--json',
|
||||
WORK_ITEM_PR_LIST_JSON_FIELDS
|
||||
]
|
||||
if (ownerRepo) {
|
||||
out.push('--repo', `${ownerRepo.owner}/${ownerRepo.repo}`)
|
||||
}
|
||||
out.push('--repo', `${ownerRepo.owner}/${ownerRepo.repo}`)
|
||||
out.push('--search', searchParts.join(' '))
|
||||
return { args: out, offset: (page - 1) * limit }
|
||||
}
|
||||
@@ -1122,30 +1114,6 @@ async function resolvePrWorkItemSource(
|
||||
return { source, originCandidate, upstreamCandidate }
|
||||
}
|
||||
|
||||
/**
|
||||
* gh exec relying on gh's own cwd→repo resolution. Serves a remembered resolution failure
|
||||
* without spawning, so a remote-less repo costs one gh spawn per config change/TTL, not two per refresh.
|
||||
*/
|
||||
async function ghCwdResolvedExec(
|
||||
context: GitHubRepoContext,
|
||||
args: string[],
|
||||
ghOptions: GhExecOptions
|
||||
): Promise<{ stdout: string; stderr: string }> {
|
||||
const remembered = await getRememberedGhCwdResolutionFailure(context)
|
||||
if (remembered !== null) {
|
||||
throw new Error(remembered)
|
||||
}
|
||||
try {
|
||||
return await ghExecFileAsync(args, ghOptions)
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
if (isGhCwdRepoResolutionFailure(message)) {
|
||||
await rememberGhCwdResolutionFailure(context, message)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async function listRecentWorkItems(
|
||||
repoPath: string,
|
||||
issueOwnerRepo: OwnerRepo | null,
|
||||
@@ -1156,104 +1124,87 @@ async function listRecentWorkItems(
|
||||
noCache?: boolean,
|
||||
localGitOptions: LocalGitExecOptions = {}
|
||||
): Promise<PartialWorkItemsResult> {
|
||||
const repoContext = githubRepoContext(repoPath, connectionId, localGitOptions)
|
||||
const ghOptions = ghRepoExecOptions(repoContext)
|
||||
const requiresExplicitRepo = Boolean(connectionId)
|
||||
const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions))
|
||||
assertSshRepoHasResolvedGitHubSource({ connectionId, issueOwnerRepo, prOwnerRepo })
|
||||
const recentQuery = parseTaskQuery('is:open')
|
||||
const issueRequest = buildWorkItemListRequest({
|
||||
kind: 'issue',
|
||||
ownerRepo: issueOwnerRepo,
|
||||
limit,
|
||||
query: recentQuery,
|
||||
page
|
||||
})
|
||||
const prRequest = buildWorkItemListRequest({
|
||||
kind: 'pr',
|
||||
ownerRepo: prOwnerRepo,
|
||||
limit,
|
||||
query: recentQuery,
|
||||
page
|
||||
})
|
||||
if (noCache) {
|
||||
const issueRequest = issueOwnerRepo
|
||||
? buildWorkItemListRequest({
|
||||
kind: 'issue',
|
||||
ownerRepo: issueOwnerRepo,
|
||||
limit,
|
||||
query: recentQuery,
|
||||
page
|
||||
})
|
||||
: null
|
||||
const prRequest = prOwnerRepo
|
||||
? buildWorkItemListRequest({
|
||||
kind: 'pr',
|
||||
ownerRepo: prOwnerRepo,
|
||||
limit,
|
||||
query: recentQuery,
|
||||
page
|
||||
})
|
||||
: null
|
||||
if (noCache && issueRequest) {
|
||||
issueRequest.args.splice(1, 2)
|
||||
}
|
||||
if (issueOwnerRepo || prOwnerRepo || requiresExplicitRepo) {
|
||||
// Why: allSettled so a 403 on the issue side doesn't zero the PR half — partial results + banner (parent doc §2).
|
||||
const [issuesSettled, prsSettled] = await Promise.allSettled([
|
||||
issueOwnerRepo
|
||||
? ghExecFileAsync(issueRequest.args, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(issueOwnerRepo)
|
||||
})
|
||||
: requiresExplicitRepo
|
||||
? Promise.resolve({ stdout: '[]' })
|
||||
: ghCwdResolvedExec(repoContext, issueRequest.args, ghOptions),
|
||||
prOwnerRepo
|
||||
? ghExecFileAsync(prRequest.args, { ...ghOptions, ...githubHostExecOptions(prOwnerRepo) })
|
||||
: requiresExplicitRepo
|
||||
? Promise.resolve({ stdout: '[]' })
|
||||
: ghCwdResolvedExec(repoContext, prRequest.args, ghOptions)
|
||||
])
|
||||
|
||||
let issues: MainWorkItem[] = []
|
||||
let issuesError: ClassifiedError | undefined
|
||||
if (issuesSettled.status === 'fulfilled') {
|
||||
issues = (JSON.parse(issuesSettled.value.stdout) as Record<string, unknown>[])
|
||||
// Why: search/issues can still return PRs (pull_request marker) even with is:issue; filter them out.
|
||||
.filter((item) => !('pull_request' in item))
|
||||
.map(mapIssueWorkItem)
|
||||
} else {
|
||||
const stderr =
|
||||
issuesSettled.reason instanceof Error
|
||||
? issuesSettled.reason.message
|
||||
: String(issuesSettled.reason)
|
||||
issuesError = classifyListIssuesError(stderr)
|
||||
}
|
||||
|
||||
let prs: MainWorkItem[] = []
|
||||
if (prsSettled.status === 'fulfilled') {
|
||||
prs = (JSON.parse(prsSettled.value.stdout) as Record<string, unknown>[])
|
||||
.slice(prRequest.offset, prRequest.offset + limit)
|
||||
.map((item) => mapPullRequestWorkItem(item, prOwnerRepo))
|
||||
prs = await hydrateWorkItemRepositoryMergeMetadata(prs, prOwnerRepo, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(prOwnerRepo)
|
||||
})
|
||||
} else {
|
||||
// Why: re-throw PR errors so the cross-repo aggregator counts the repo failed; this feature only fixes issue-side swallowing (#1076).
|
||||
// Why: log issuesError first so a both-sides-failed case isn't blind to the classification we're about to drop.
|
||||
if (issuesError) {
|
||||
console.warn(
|
||||
'listRecentWorkItems: both issue and PR sides failed; issuesError was classified:',
|
||||
issuesError.type,
|
||||
issuesError.message
|
||||
)
|
||||
}
|
||||
throw prsSettled.reason
|
||||
}
|
||||
|
||||
return {
|
||||
items: sortWorkItemsByNumber([...issues, ...prs]).slice(0, limit),
|
||||
issuesError
|
||||
}
|
||||
}
|
||||
|
||||
// Why: non-GitHub remotes have no sources for a partial-failure banner, so keep Promise.all (reject-all) instead of allSettled.
|
||||
const [issuesResult, prsResult] = await Promise.all([
|
||||
ghCwdResolvedExec(repoContext, issueRequest.args, ghOptions),
|
||||
ghCwdResolvedExec(repoContext, prRequest.args, ghOptions)
|
||||
// Why: unresolved sources must stay empty — an unscoped Search API would return other public repos' issues (#9660).
|
||||
// Why: allSettled so a 403 on the issue side doesn't zero the PR half (partial results + banner).
|
||||
const [issuesSettled, prsSettled] = await Promise.allSettled([
|
||||
issueRequest && issueOwnerRepo
|
||||
? ghExecFileAsync(issueRequest.args, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(issueOwnerRepo)
|
||||
})
|
||||
: Promise.resolve({ stdout: '[]' }),
|
||||
prRequest && prOwnerRepo
|
||||
? ghExecFileAsync(prRequest.args, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(prOwnerRepo)
|
||||
})
|
||||
: Promise.resolve({ stdout: '[]' })
|
||||
])
|
||||
|
||||
const issues = (JSON.parse(issuesResult.stdout) as Record<string, unknown>[])
|
||||
.filter((item) => !('pull_request' in item))
|
||||
.map(mapIssueWorkItem)
|
||||
const prs = (JSON.parse(prsResult.stdout) as Record<string, unknown>[])
|
||||
.slice(prRequest.offset, prRequest.offset + limit)
|
||||
.map((item) => mapPullRequestWorkItem(item, null))
|
||||
let issues: MainWorkItem[] = []
|
||||
let issuesError: ClassifiedError | undefined
|
||||
if (issuesSettled.status === 'fulfilled') {
|
||||
issues = (JSON.parse(issuesSettled.value.stdout) as Record<string, unknown>[])
|
||||
// Why: search/issues can still return PRs (pull_request marker) even with is:issue; filter them out.
|
||||
.filter((item) => !('pull_request' in item))
|
||||
.map(mapIssueWorkItem)
|
||||
} else {
|
||||
const stderr =
|
||||
issuesSettled.reason instanceof Error
|
||||
? issuesSettled.reason.message
|
||||
: String(issuesSettled.reason)
|
||||
issuesError = classifyListIssuesError(stderr)
|
||||
}
|
||||
|
||||
let prs: MainWorkItem[] = []
|
||||
if (prsSettled.status === 'fulfilled') {
|
||||
prs = (JSON.parse(prsSettled.value.stdout) as Record<string, unknown>[])
|
||||
.slice(prRequest?.offset ?? 0, (prRequest?.offset ?? 0) + limit)
|
||||
.map((item) => mapPullRequestWorkItem(item, prOwnerRepo))
|
||||
prs = await hydrateWorkItemRepositoryMergeMetadata(prs, prOwnerRepo, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(prOwnerRepo)
|
||||
})
|
||||
} else {
|
||||
// Why: re-throw PR errors so the cross-repo aggregator counts the repo failed; this feature only fixes issue-side swallowing (#1076).
|
||||
// Why: log issuesError first so a both-sides-failed case isn't blind to the classification we're about to drop.
|
||||
if (issuesError) {
|
||||
console.warn(
|
||||
'listRecentWorkItems: both issue and PR sides failed; issuesError was classified:',
|
||||
issuesError.type,
|
||||
issuesError.message
|
||||
)
|
||||
}
|
||||
throw prsSettled.reason
|
||||
}
|
||||
|
||||
return {
|
||||
items: sortWorkItemsByNumber([...issues, ...prs]).slice(0, limit)
|
||||
items: sortWorkItemsByNumber([...issues, ...prs]).slice(0, limit),
|
||||
issuesError
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1267,9 +1218,7 @@ async function listQueriedWorkItems(
|
||||
connectionId?: string | null,
|
||||
localGitOptions: LocalGitExecOptions = {}
|
||||
): Promise<PartialWorkItemsResult> {
|
||||
const repoContext = githubRepoContext(repoPath, connectionId, localGitOptions)
|
||||
const ghOptions = ghRepoExecOptions(repoContext)
|
||||
const requiresExplicitRepo = Boolean(connectionId)
|
||||
const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId, localGitOptions))
|
||||
assertSshRepoHasResolvedGitHubSource({ connectionId, issueOwnerRepo, prOwnerRepo })
|
||||
const hasPrOnlyFilter =
|
||||
query.state === 'merged' ||
|
||||
@@ -1287,7 +1236,7 @@ async function listQueriedWorkItems(
|
||||
if (!issueScope) {
|
||||
return { items: [] }
|
||||
}
|
||||
if (requiresExplicitRepo && !issueOwnerRepo) {
|
||||
if (!issueOwnerRepo) {
|
||||
return { items: [] }
|
||||
}
|
||||
const request = buildWorkItemListRequest({
|
||||
@@ -1298,12 +1247,10 @@ async function listQueriedWorkItems(
|
||||
page: page ?? 1
|
||||
})
|
||||
try {
|
||||
const { stdout } = issueOwnerRepo
|
||||
? await ghExecFileAsync(request.args, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(issueOwnerRepo)
|
||||
})
|
||||
: await ghCwdResolvedExec(repoContext, request.args, ghOptions)
|
||||
const { stdout } = await ghExecFileAsync(request.args, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(issueOwnerRepo)
|
||||
})
|
||||
const items = (JSON.parse(stdout) as Record<string, unknown>[])
|
||||
.filter((item) => !('pull_request' in item))
|
||||
.map(mapIssueWorkItem)
|
||||
@@ -1324,7 +1271,7 @@ async function listQueriedWorkItems(
|
||||
if (!prScope) {
|
||||
return []
|
||||
}
|
||||
if (requiresExplicitRepo && !prOwnerRepo) {
|
||||
if (!prOwnerRepo) {
|
||||
return []
|
||||
}
|
||||
const request = buildWorkItemListRequest({
|
||||
@@ -1335,12 +1282,10 @@ async function listQueriedWorkItems(
|
||||
page: page ?? 1
|
||||
})
|
||||
try {
|
||||
const { stdout } = prOwnerRepo
|
||||
? await ghExecFileAsync(request.args, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(prOwnerRepo)
|
||||
})
|
||||
: await ghCwdResolvedExec(repoContext, request.args, ghOptions)
|
||||
const { stdout } = await ghExecFileAsync(request.args, {
|
||||
...ghOptions,
|
||||
...githubHostExecOptions(prOwnerRepo)
|
||||
})
|
||||
const mapped = (JSON.parse(stdout) as Record<string, unknown>[])
|
||||
.slice(request.offset, request.offset + limit)
|
||||
.map((item) => mapPullRequestWorkItem(item, prOwnerRepo))
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { readLocalGitConfigSignatureMock } = vi.hoisted(() => ({
|
||||
readLocalGitConfigSignatureMock: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('./local-git-config-signature', () => ({
|
||||
readLocalGitConfigSignature: readLocalGitConfigSignatureMock
|
||||
}))
|
||||
|
||||
import {
|
||||
_resetGhCwdRepoNegativeCache,
|
||||
getRememberedGhCwdResolutionFailure,
|
||||
isGhCwdRepoResolutionFailure,
|
||||
rememberGhCwdResolutionFailure
|
||||
} from './gh-cwd-repo-negative-cache'
|
||||
|
||||
const context = { repoPath: '/repo', connectionId: null }
|
||||
|
||||
afterEach(() => {
|
||||
_resetGhCwdRepoNegativeCache()
|
||||
readLocalGitConfigSignatureMock.mockReset()
|
||||
})
|
||||
|
||||
describe('isGhCwdRepoResolutionFailure', () => {
|
||||
it('matches gh cwd-resolution failures only', () => {
|
||||
expect(isGhCwdRepoResolutionFailure('Command failed: gh\nno git remotes found')).toBe(true)
|
||||
expect(
|
||||
isGhCwdRepoResolutionFailure(
|
||||
'none of the git remotes configured for this repository point to a known GitHub host'
|
||||
)
|
||||
).toBe(true)
|
||||
expect(isGhCwdRepoResolutionFailure('gh: API rate limit exceeded (HTTP 403)')).toBe(false)
|
||||
expect(isGhCwdRepoResolutionFailure('gh: Not Found (HTTP 404)')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('remembered failures', () => {
|
||||
it('serves the remembered failure while the config signature is unchanged', async () => {
|
||||
readLocalGitConfigSignatureMock.mockResolvedValue('sig-1')
|
||||
await rememberGhCwdResolutionFailure(context, 'no git remotes found')
|
||||
await expect(getRememberedGhCwdResolutionFailure(context)).resolves.toBe('no git remotes found')
|
||||
})
|
||||
|
||||
it('invalidates when the git config signature changes (remote added)', async () => {
|
||||
readLocalGitConfigSignatureMock.mockResolvedValueOnce('sig-1')
|
||||
await rememberGhCwdResolutionFailure(context, 'no git remotes found')
|
||||
readLocalGitConfigSignatureMock.mockResolvedValue('sig-2')
|
||||
await expect(getRememberedGhCwdResolutionFailure(context)).resolves.toBeNull()
|
||||
// The stale entry is gone, not just skipped.
|
||||
readLocalGitConfigSignatureMock.mockResolvedValue('sig-1')
|
||||
await expect(getRememberedGhCwdResolutionFailure(context)).resolves.toBeNull()
|
||||
})
|
||||
|
||||
it('separates repos and runtimes by cache key', async () => {
|
||||
readLocalGitConfigSignatureMock.mockResolvedValue('sig')
|
||||
await rememberGhCwdResolutionFailure(context, 'no git remotes found')
|
||||
await expect(
|
||||
getRememberedGhCwdResolutionFailure({ repoPath: '/other-repo', connectionId: null })
|
||||
).resolves.toBeNull()
|
||||
await expect(
|
||||
getRememberedGhCwdResolutionFailure({ repoPath: '/repo', connectionId: 'ssh-1' })
|
||||
).resolves.toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -1,83 +0,0 @@
|
||||
import { readLocalGitConfigSignature } from './local-git-config-signature'
|
||||
import type { GitHubRepoContext } from './github-repository-identity'
|
||||
|
||||
/**
|
||||
* Negative cache for repos where gh's cwd-based repo resolution fails
|
||||
* ("no git remotes found" etc). Orca only falls back to cwd resolution when
|
||||
* it could not resolve an owner/repo itself, so for a remote-less repo the
|
||||
* failure is deterministic — yet the Tasks page re-spawned gh for every such
|
||||
* repo on every refresh (×90 repos in the reported storm). Remember the
|
||||
* failure and re-throw it without a subprocess until the repo's git config
|
||||
* changes (signature check) or the TTL lapses.
|
||||
*/
|
||||
const NEGATIVE_CACHE_TTL_MS = 5 * 60_000
|
||||
const MAX_ENTRIES = 512
|
||||
|
||||
type NegativeCacheEntry = {
|
||||
message: string
|
||||
configSignature: string | undefined
|
||||
expiresAt: number
|
||||
}
|
||||
|
||||
const entries = new Map<string, NegativeCacheEntry>()
|
||||
|
||||
function negativeCacheKey(context: GitHubRepoContext): string {
|
||||
const runtimeKey = context.connectionId ?? `local:${context.wslDistro ?? 'host'}`
|
||||
return `${runtimeKey}\0${context.repoPath}`
|
||||
}
|
||||
|
||||
export function isGhCwdRepoResolutionFailure(text: string): boolean {
|
||||
const s = text.toLowerCase()
|
||||
return (
|
||||
s.includes('no git remotes found') ||
|
||||
s.includes('not a git repository') ||
|
||||
s.includes('unable to determine base repository') ||
|
||||
s.includes('none of the git remotes configured')
|
||||
)
|
||||
}
|
||||
|
||||
export async function getRememberedGhCwdResolutionFailure(
|
||||
context: GitHubRepoContext
|
||||
): Promise<string | null> {
|
||||
const key = negativeCacheKey(context)
|
||||
const entry = entries.get(key)
|
||||
if (!entry) {
|
||||
return null
|
||||
}
|
||||
if (entry.expiresAt <= Date.now()) {
|
||||
entries.delete(key)
|
||||
return null
|
||||
}
|
||||
// Why: adding a remote is the way out of this failure; the config signature
|
||||
// (mtime/size of the repo's git config chain) changes when that happens.
|
||||
const signature = await readLocalGitConfigSignature(context)
|
||||
if (signature !== entry.configSignature) {
|
||||
entries.delete(key)
|
||||
return null
|
||||
}
|
||||
return entry.message
|
||||
}
|
||||
|
||||
export async function rememberGhCwdResolutionFailure(
|
||||
context: GitHubRepoContext,
|
||||
message: string
|
||||
): Promise<void> {
|
||||
const configSignature = await readLocalGitConfigSignature(context)
|
||||
entries.set(negativeCacheKey(context), {
|
||||
message,
|
||||
configSignature,
|
||||
expiresAt: Date.now() + NEGATIVE_CACHE_TTL_MS
|
||||
})
|
||||
while (entries.size > MAX_ENTRIES) {
|
||||
const oldest = entries.keys().next()
|
||||
if (oldest.done) {
|
||||
break
|
||||
}
|
||||
entries.delete(oldest.value)
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal — test-only */
|
||||
export function _resetGhCwdRepoNegativeCache(): void {
|
||||
entries.clear()
|
||||
}
|
||||
Reference in New Issue
Block a user