perf(source-control): stop gh rate-limit storms and idle git-status spawn churn (#7595)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo Hong
2026-07-06 15:12:56 -07:00
committed by GitHub
co-authored by Orca
parent 377bde351c
commit 417723411e
23 changed files with 1671 additions and 34 deletions
+38 -1
View File
@@ -63,7 +63,8 @@ vi.mock('../git/runner', () => ({
vi.mock('./rate-limit', () => ({
rateLimitGuard: rateLimitGuardMock,
noteRateLimitSpend: noteRateLimitSpendMock
noteRateLimitSpend: noteRateLimitSpendMock,
getRateLimit: vi.fn(async () => ({ ok: false, error: 'not probed in tests' }))
}))
import {
@@ -72,6 +73,7 @@ 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'
describe('listWorkItems', () => {
@@ -99,6 +101,25 @@ describe('listWorkItems', () => {
getOwnerRepoForRemoteMock.mockResolvedValue(null)
_resetOwnerRepoCache()
_resetMergeQueueCacheForTests()
_resetGhCwdRepoNegativeCache()
})
it('stops re-spawning gh for a repo whose cwd resolution already failed with no remotes', 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'
})
)
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 () => {
@@ -537,6 +558,22 @@ describe('listWorkItems', () => {
expect(apiPath).not.toContain('-is:merged')
})
it('returns zero without spawning gh when the search bucket is rate-limit blocked', async () => {
getIssueOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
rateLimitGuardMock.mockReturnValue({
blocked: true,
remaining: 0,
limit: 30,
resetAt: Math.floor(Date.now() / 1000) + 60
} as unknown as { blocked: boolean })
await expect(countWorkItems('/repo-root', 'is:issue is:open')).resolves.toBe(0)
expect(rateLimitGuardMock).toHaveBeenCalledWith('search')
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
})
it('returns zero for oversized count queries before resolving repo sources', async () => {
const secret = 'main-github-work-items-secret'
const oversizedQuery = secret + 'x'.repeat(GITHUB_WORK_ITEMS_QUERY_MAX_BYTES)