Reduce GitHub GraphQL rate-limit spend (#1974)
This commit is contained in:
@@ -12,6 +12,8 @@ const {
|
||||
getIssueOwnerRepoMock,
|
||||
getOwnerRepoForRemoteMock,
|
||||
resolveIssueSourceMock,
|
||||
rateLimitGuardMock,
|
||||
noteRateLimitSpendMock,
|
||||
acquireMock,
|
||||
releaseMock
|
||||
} = vi.hoisted(() => ({
|
||||
@@ -21,6 +23,8 @@ const {
|
||||
getIssueOwnerRepoMock: vi.fn(),
|
||||
getOwnerRepoForRemoteMock: vi.fn(),
|
||||
resolveIssueSourceMock: vi.fn(),
|
||||
rateLimitGuardMock: vi.fn(() => ({ blocked: false })),
|
||||
noteRateLimitSpendMock: vi.fn(),
|
||||
acquireMock: vi.fn(),
|
||||
releaseMock: vi.fn()
|
||||
}))
|
||||
@@ -41,6 +45,11 @@ vi.mock('./gh-utils', async () => {
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('./rate-limit', () => ({
|
||||
rateLimitGuard: rateLimitGuardMock,
|
||||
noteRateLimitSpend: noteRateLimitSpendMock
|
||||
}))
|
||||
|
||||
import { countWorkItems, getWorkItem, listWorkItems, _resetOwnerRepoCache } from './client'
|
||||
|
||||
describe('GitHub issue source split', () => {
|
||||
@@ -51,6 +60,9 @@ describe('GitHub issue source split', () => {
|
||||
getIssueOwnerRepoMock.mockReset()
|
||||
getOwnerRepoForRemoteMock.mockReset()
|
||||
resolveIssueSourceMock.mockReset()
|
||||
rateLimitGuardMock.mockReset()
|
||||
rateLimitGuardMock.mockReturnValue({ blocked: false })
|
||||
noteRateLimitSpendMock.mockReset()
|
||||
acquireMock.mockReset()
|
||||
releaseMock.mockReset()
|
||||
acquireMock.mockResolvedValue(undefined)
|
||||
|
||||
@@ -6,6 +6,8 @@ const {
|
||||
getOwnerRepoMock,
|
||||
getIssueOwnerRepoMock,
|
||||
gitExecFileAsyncMock,
|
||||
rateLimitGuardMock,
|
||||
noteRateLimitSpendMock,
|
||||
acquireMock,
|
||||
releaseMock
|
||||
} = vi.hoisted(() => ({
|
||||
@@ -14,6 +16,8 @@ const {
|
||||
getOwnerRepoMock: vi.fn(),
|
||||
getIssueOwnerRepoMock: vi.fn(),
|
||||
gitExecFileAsyncMock: vi.fn(),
|
||||
rateLimitGuardMock: vi.fn(() => ({ blocked: false })),
|
||||
noteRateLimitSpendMock: vi.fn(),
|
||||
acquireMock: vi.fn(),
|
||||
releaseMock: vi.fn()
|
||||
}))
|
||||
@@ -37,6 +41,11 @@ vi.mock('../git/runner', () => ({
|
||||
gitExecFileAsync: gitExecFileAsyncMock
|
||||
}))
|
||||
|
||||
vi.mock('./rate-limit', () => ({
|
||||
rateLimitGuard: rateLimitGuardMock,
|
||||
noteRateLimitSpend: noteRateLimitSpendMock
|
||||
}))
|
||||
|
||||
import { getPRChecks, _resetOwnerRepoCache } from './client'
|
||||
|
||||
describe('getPRChecks', () => {
|
||||
@@ -46,6 +55,9 @@ describe('getPRChecks', () => {
|
||||
getOwnerRepoMock.mockReset()
|
||||
getIssueOwnerRepoMock.mockReset()
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
rateLimitGuardMock.mockReset()
|
||||
rateLimitGuardMock.mockReturnValue({ blocked: false })
|
||||
noteRateLimitSpendMock.mockReset()
|
||||
acquireMock.mockReset()
|
||||
releaseMock.mockReset()
|
||||
acquireMock.mockResolvedValue(undefined)
|
||||
|
||||
@@ -11,6 +11,8 @@ const {
|
||||
getOwnerRepoForRemoteMock,
|
||||
resolveIssueSourceMock,
|
||||
gitExecFileAsyncMock,
|
||||
rateLimitGuardMock,
|
||||
noteRateLimitSpendMock,
|
||||
acquireMock,
|
||||
releaseMock
|
||||
} = vi.hoisted(() => ({
|
||||
@@ -21,6 +23,8 @@ const {
|
||||
getOwnerRepoForRemoteMock: vi.fn(),
|
||||
resolveIssueSourceMock: vi.fn(),
|
||||
gitExecFileAsyncMock: vi.fn(),
|
||||
rateLimitGuardMock: vi.fn(() => ({ blocked: false })),
|
||||
noteRateLimitSpendMock: vi.fn(),
|
||||
acquireMock: vi.fn(),
|
||||
releaseMock: vi.fn()
|
||||
}))
|
||||
@@ -48,6 +52,11 @@ vi.mock('../git/runner', () => ({
|
||||
gitExecFileAsync: gitExecFileAsyncMock
|
||||
}))
|
||||
|
||||
vi.mock('./rate-limit', () => ({
|
||||
rateLimitGuard: rateLimitGuardMock,
|
||||
noteRateLimitSpend: noteRateLimitSpendMock
|
||||
}))
|
||||
|
||||
import { listWorkItems, _resetOwnerRepoCache } from './client'
|
||||
|
||||
describe('listWorkItems', () => {
|
||||
@@ -59,6 +68,9 @@ describe('listWorkItems', () => {
|
||||
getOwnerRepoForRemoteMock.mockReset()
|
||||
resolveIssueSourceMock.mockReset()
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
rateLimitGuardMock.mockReset()
|
||||
rateLimitGuardMock.mockReturnValue({ blocked: false })
|
||||
noteRateLimitSpendMock.mockReset()
|
||||
acquireMock.mockReset()
|
||||
releaseMock.mockReset()
|
||||
acquireMock.mockResolvedValue(undefined)
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
/* oxlint-disable max-lines -- Why: GitHub client fixtures cover local and SSH repo identity paths in one suite so mocked CLI behavior stays consistent. */
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
type RateLimitGuardResult =
|
||||
| { blocked: false }
|
||||
| { blocked: true; remaining: number; limit: number; resetAt: number }
|
||||
|
||||
const {
|
||||
execFileAsyncMock,
|
||||
ghExecFileAsyncMock,
|
||||
@@ -9,6 +13,8 @@ const {
|
||||
getOwnerRepoForRemoteMock,
|
||||
getRemoteUrlForRepoMock,
|
||||
gitExecFileAsyncMock,
|
||||
rateLimitGuardMock,
|
||||
noteRateLimitSpendMock,
|
||||
ghRepoExecOptionsMock,
|
||||
githubRepoContextMock,
|
||||
acquireMock,
|
||||
@@ -21,6 +27,8 @@ const {
|
||||
getOwnerRepoForRemoteMock: vi.fn(),
|
||||
getRemoteUrlForRepoMock: vi.fn(),
|
||||
gitExecFileAsyncMock: vi.fn(),
|
||||
rateLimitGuardMock: vi.fn<() => RateLimitGuardResult>(() => ({ blocked: false })),
|
||||
noteRateLimitSpendMock: vi.fn(),
|
||||
ghRepoExecOptionsMock: vi.fn((context) =>
|
||||
context.connectionId ? {} : { cwd: context.repoPath }
|
||||
),
|
||||
@@ -59,7 +67,18 @@ vi.mock('../git/runner', () => ({
|
||||
gitExecFileAsync: gitExecFileAsyncMock
|
||||
}))
|
||||
|
||||
import { getPRForBranch, getPullRequestPushTarget, _resetOwnerRepoCache } from './client'
|
||||
vi.mock('./rate-limit', () => ({
|
||||
rateLimitGuard: rateLimitGuardMock,
|
||||
noteRateLimitSpend: noteRateLimitSpendMock
|
||||
}))
|
||||
|
||||
import {
|
||||
getPRComments,
|
||||
getPRForBranch,
|
||||
getPullRequestPushTarget,
|
||||
resolveReviewThread,
|
||||
_resetOwnerRepoCache
|
||||
} from './client'
|
||||
|
||||
describe('getPRForBranch', () => {
|
||||
beforeEach(() => {
|
||||
@@ -70,6 +89,9 @@ describe('getPRForBranch', () => {
|
||||
getOwnerRepoForRemoteMock.mockReset()
|
||||
getRemoteUrlForRepoMock.mockReset()
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
rateLimitGuardMock.mockReset()
|
||||
rateLimitGuardMock.mockReturnValue({ blocked: false })
|
||||
noteRateLimitSpendMock.mockReset()
|
||||
ghRepoExecOptionsMock.mockClear()
|
||||
githubRepoContextMock.mockClear()
|
||||
acquireMock.mockReset()
|
||||
@@ -422,3 +444,67 @@ describe('getPRForBranch', () => {
|
||||
expect(gitExecFileAsyncMock).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('GitHub GraphQL rate-limit guard', () => {
|
||||
beforeEach(() => {
|
||||
execFileAsyncMock.mockReset()
|
||||
ghExecFileAsyncMock.mockReset()
|
||||
getOwnerRepoMock.mockReset()
|
||||
getIssueOwnerRepoMock.mockReset()
|
||||
getOwnerRepoForRemoteMock.mockReset()
|
||||
getRemoteUrlForRepoMock.mockReset()
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
rateLimitGuardMock.mockReset()
|
||||
rateLimitGuardMock.mockReturnValue({ blocked: false })
|
||||
noteRateLimitSpendMock.mockReset()
|
||||
acquireMock.mockReset()
|
||||
releaseMock.mockReset()
|
||||
acquireMock.mockResolvedValue(undefined)
|
||||
_resetOwnerRepoCache()
|
||||
})
|
||||
|
||||
it('skips PR review-thread GraphQL fetch while preserving REST comments', async () => {
|
||||
rateLimitGuardMock.mockReturnValue({
|
||||
blocked: true,
|
||||
remaining: 4,
|
||||
limit: 5000,
|
||||
resetAt: 1_800_000_000
|
||||
})
|
||||
getOwnerRepoMock.mockResolvedValueOnce({ owner: 'acme', repo: 'widgets' })
|
||||
ghExecFileAsyncMock
|
||||
.mockResolvedValueOnce({
|
||||
stdout: JSON.stringify([
|
||||
{
|
||||
id: 10,
|
||||
user: { login: 'octo', avatar_url: 'https://avatar', type: 'User' },
|
||||
body: 'top-level',
|
||||
created_at: '2026-04-01T00:00:00Z',
|
||||
html_url: 'https://github.com/acme/widgets/pull/7#issuecomment-10'
|
||||
}
|
||||
])
|
||||
})
|
||||
.mockResolvedValueOnce({ stdout: '[]' })
|
||||
|
||||
const comments = await getPRComments('/repo-root', 7)
|
||||
|
||||
expect(comments).toHaveLength(1)
|
||||
expect(comments[0].body).toBe('top-level')
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(2)
|
||||
expect(ghExecFileAsyncMock.mock.calls.some((call) => call[0][1] === 'graphql')).toBe(false)
|
||||
expect(noteRateLimitSpendMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('blocks review-thread resolve mutations before spawning gh when GraphQL is low', async () => {
|
||||
rateLimitGuardMock.mockReturnValue({
|
||||
blocked: true,
|
||||
remaining: 4,
|
||||
limit: 5000,
|
||||
resetAt: 1_800_000_000
|
||||
})
|
||||
|
||||
await expect(resolveReviewThread('/repo-root', 'thread-1', true)).resolves.toBe(false)
|
||||
|
||||
expect(ghExecFileAsyncMock).not.toHaveBeenCalled()
|
||||
expect(noteRateLimitSpendMock).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -53,6 +53,7 @@ import {
|
||||
deriveCheckStatus
|
||||
} from './mappers'
|
||||
import { mapGraphQLReactionGroups, type GitHubGraphQLReactionGroup } from './comment-reactions'
|
||||
import { noteRateLimitSpend, rateLimitGuard } from './rate-limit'
|
||||
|
||||
const ORCA_REPO = 'stablyai/orca'
|
||||
|
||||
@@ -1358,12 +1359,13 @@ export async function getPRComments(
|
||||
// permissions, transient network error) doesn't blank out all comments.
|
||||
// Each source is parsed independently; failed sources contribute zero
|
||||
// comments instead of aborting the entire fetch.
|
||||
const [issueResult, threadsResult, reviewsResult] = await Promise.allSettled([
|
||||
ghExecFileAsync(
|
||||
['api', ...cacheArgs, `${base}/issues/${prNumber}/comments?per_page=100`],
|
||||
ghOptions
|
||||
),
|
||||
ghExecFileAsync(
|
||||
const reviewThreadsGuard = rateLimitGuard('graphql')
|
||||
let reviewThreadsFetch: Promise<{ stdout: string; stderr: string } | null>
|
||||
if (reviewThreadsGuard.blocked) {
|
||||
reviewThreadsFetch = Promise.resolve(null)
|
||||
} else {
|
||||
noteRateLimitSpend('graphql')
|
||||
reviewThreadsFetch = ghExecFileAsync(
|
||||
[
|
||||
'api',
|
||||
'graphql',
|
||||
@@ -1377,7 +1379,14 @@ export async function getPRComments(
|
||||
`pr=${prNumber}`
|
||||
],
|
||||
ghOptions
|
||||
)
|
||||
}
|
||||
const [issueResult, threadsResult, reviewsResult] = await Promise.allSettled([
|
||||
ghExecFileAsync(
|
||||
['api', ...cacheArgs, `${base}/issues/${prNumber}/comments?per_page=100`],
|
||||
ghOptions
|
||||
),
|
||||
reviewThreadsFetch,
|
||||
// Why: review summaries (approve, request changes, general comments) live
|
||||
// under pulls/{n}/reviews, not under issue comments or review threads.
|
||||
// Without this, a reviewer who submits "LGTM" without inline threads
|
||||
@@ -1442,7 +1451,7 @@ export async function getPRComments(
|
||||
reactionGroups?: GitHubGraphQLReactionGroup[] | null
|
||||
}
|
||||
const reviewComments: PRComment[] = []
|
||||
if (threadsResult.status === 'fulfilled') {
|
||||
if (threadsResult.status === 'fulfilled' && threadsResult.value) {
|
||||
const threadsData = JSON.parse(threadsResult.value.stdout) as {
|
||||
data: {
|
||||
repository: {
|
||||
@@ -1494,7 +1503,9 @@ export async function getPRComments(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.warn('Failed to fetch review threads:', threadsResult.reason)
|
||||
if (threadsResult.status === 'rejected') {
|
||||
console.warn('Failed to fetch review threads:', threadsResult.reason)
|
||||
}
|
||||
}
|
||||
|
||||
// Parse review summaries (REST) — only include reviews with a body,
|
||||
@@ -1572,8 +1583,16 @@ export async function resolveReviewThread(
|
||||
const mutation = resolve ? 'resolveReviewThread' : 'unresolveReviewThread'
|
||||
const query = `mutation($threadId: ID!) { ${mutation}(input: { threadId: $threadId }) { thread { isResolved } } }`
|
||||
const ghOptions = ghRepoExecOptions(githubRepoContext(repoPath, connectionId))
|
||||
const guard = rateLimitGuard('graphql')
|
||||
if (guard.blocked) {
|
||||
console.warn(
|
||||
`${mutation} skipped: GitHub GraphQL rate limit nearly exhausted (${guard.remaining}/${guard.limit})`
|
||||
)
|
||||
return false
|
||||
}
|
||||
await acquire()
|
||||
try {
|
||||
noteRateLimitSpend('graphql')
|
||||
await ghExecFileAsync(
|
||||
['api', 'graphql', '-f', `query=${query}`, '-f', `threadId=${threadId}`],
|
||||
ghOptions
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
type RateLimitGuardResult =
|
||||
| { blocked: false }
|
||||
| { blocked: true; remaining: number; limit: number; resetAt: number }
|
||||
|
||||
const {
|
||||
ghExecFileAsyncMock,
|
||||
getOwnerRepoMock,
|
||||
@@ -7,6 +11,8 @@ const {
|
||||
getWorkItemMock,
|
||||
getPRChecksMock,
|
||||
getPRCommentsMock,
|
||||
rateLimitGuardMock,
|
||||
noteRateLimitSpendMock,
|
||||
ghRepoExecOptionsMock,
|
||||
githubRepoContextMock,
|
||||
acquireMock,
|
||||
@@ -18,6 +24,8 @@ const {
|
||||
getWorkItemMock: vi.fn(),
|
||||
getPRChecksMock: vi.fn(),
|
||||
getPRCommentsMock: vi.fn(),
|
||||
rateLimitGuardMock: vi.fn<() => RateLimitGuardResult>(() => ({ blocked: false })),
|
||||
noteRateLimitSpendMock: vi.fn(),
|
||||
ghRepoExecOptionsMock: vi.fn((context) =>
|
||||
context.connectionId ? {} : { cwd: context.repoPath }
|
||||
),
|
||||
@@ -45,6 +53,11 @@ vi.mock('./client', () => ({
|
||||
getPRComments: getPRCommentsMock
|
||||
}))
|
||||
|
||||
vi.mock('./rate-limit', () => ({
|
||||
rateLimitGuard: rateLimitGuardMock,
|
||||
noteRateLimitSpend: noteRateLimitSpendMock
|
||||
}))
|
||||
|
||||
import { getWorkItemDetails } from './work-item-details'
|
||||
|
||||
describe('getWorkItemDetails', () => {
|
||||
@@ -55,6 +68,9 @@ describe('getWorkItemDetails', () => {
|
||||
getWorkItemMock.mockReset()
|
||||
getPRChecksMock.mockReset()
|
||||
getPRCommentsMock.mockReset()
|
||||
rateLimitGuardMock.mockReset()
|
||||
rateLimitGuardMock.mockReturnValue({ blocked: false })
|
||||
noteRateLimitSpendMock.mockReset()
|
||||
ghRepoExecOptionsMock.mockClear()
|
||||
githubRepoContextMock.mockClear()
|
||||
acquireMock.mockReset()
|
||||
@@ -158,6 +174,38 @@ describe('getWorkItemDetails', () => {
|
||||
expect(details?.body).toBe('Issue body')
|
||||
})
|
||||
|
||||
it('skips optional GraphQL issue detail calls when the cached GraphQL budget is low', async () => {
|
||||
rateLimitGuardMock.mockReturnValue({
|
||||
blocked: true,
|
||||
remaining: 3,
|
||||
limit: 5000,
|
||||
resetAt: 1_800_000_000
|
||||
})
|
||||
getWorkItemMock.mockResolvedValueOnce({
|
||||
id: 'issue:923',
|
||||
type: 'issue',
|
||||
number: 923,
|
||||
title: 'Use upstream issues',
|
||||
state: 'open',
|
||||
url: 'https://github.com/stablyai/orca/issues/923',
|
||||
labels: [],
|
||||
updatedAt: '2026-04-01T00:00:00Z',
|
||||
author: 'octocat'
|
||||
})
|
||||
getIssueOwnerRepoMock.mockResolvedValue({ owner: 'stablyai', repo: 'orca' })
|
||||
ghExecFileAsyncMock
|
||||
.mockResolvedValueOnce({ stdout: JSON.stringify({ body: 'Issue body', assignees: [] }) })
|
||||
.mockResolvedValueOnce({ stdout: '[]' })
|
||||
|
||||
const details = await getWorkItemDetails('/repo-root', 923, 'issue')
|
||||
|
||||
expect(ghExecFileAsyncMock).toHaveBeenCalledTimes(2)
|
||||
expect(ghExecFileAsyncMock.mock.calls.some((call) => call[0][1] === 'graphql')).toBe(false)
|
||||
expect(noteRateLimitSpendMock).not.toHaveBeenCalled()
|
||||
expect(details?.body).toBe('Issue body')
|
||||
expect(details?.participants).toEqual([])
|
||||
})
|
||||
|
||||
it('uses SSH connection context for issue details without local cwd', async () => {
|
||||
getWorkItemMock.mockResolvedValueOnce({
|
||||
id: 'issue:923',
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
githubRepoContext
|
||||
} from './gh-utils'
|
||||
import { getWorkItem, getPRChecks, getPRComments } from './client'
|
||||
import { noteRateLimitSpend, rateLimitGuard } from './rate-limit'
|
||||
|
||||
// Why: a PR "changed file" listing returned by the REST endpoint is paginated
|
||||
// at 100 per page; we cap at a reasonable total so a massive PR cannot starve
|
||||
@@ -112,7 +113,11 @@ async function getIssueDetailsViaGraphQL(
|
||||
if (!ownerRepo) {
|
||||
return null
|
||||
}
|
||||
if (rateLimitGuard('graphql').blocked) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
noteRateLimitSpend('graphql')
|
||||
const { stdout } = await ghExecFileAsync(
|
||||
[
|
||||
'api',
|
||||
@@ -441,7 +446,11 @@ async function getWorkItemParticipants(
|
||||
if (!ownerRepo) {
|
||||
return []
|
||||
}
|
||||
if (rateLimitGuard('graphql').blocked) {
|
||||
return []
|
||||
}
|
||||
try {
|
||||
noteRateLimitSpend('graphql')
|
||||
const { stdout } = await ghExecFileAsync(
|
||||
[
|
||||
'api',
|
||||
@@ -498,6 +507,9 @@ async function getGitHubUsersByLogin(
|
||||
if (uniqueLogins.length === 0) {
|
||||
return []
|
||||
}
|
||||
if (rateLimitGuard('graphql').blocked) {
|
||||
return []
|
||||
}
|
||||
const fields = uniqueLogins
|
||||
.map(
|
||||
(login, index) =>
|
||||
@@ -505,6 +517,7 @@ async function getGitHubUsersByLogin(
|
||||
)
|
||||
.join('\n')
|
||||
try {
|
||||
noteRateLimitSpend('graphql')
|
||||
const { stdout } = await ghExecFileAsync(
|
||||
['api', 'graphql', '-f', `query=query { ${fields} }`],
|
||||
ghRepoExecOptions(githubRepoContext(repoPath, connectionId))
|
||||
|
||||
Reference in New Issue
Block a user