Repository navigation
Reconcile open bot PRs on demand with backoff and batched GraphQL reads - #1397
Conversation
…ed GraphQL reads The fallback reconcile read the 25 newest open bot PRs of every repository over REST every minute, whether or not anything changed. On a busy installation that alone used most of the hourly core quota, and older PRs were never read. Each open bot PR now has its own schedule in bot_pr_states: it is read 5 minutes after a change, then twice as long each time nothing changed, up to 4 hours, and never more often than every 5 minutes. A change it finds, a webhook about the PR, its head commit or its base branch, or a change to the project's local settings makes it due again. Every open bot PR is covered, and a tick with no PR due makes no GitHub request. The due PRs of a repository are read with one GraphQL query per 50 PRs (state, draft, mergeability, head, base and base tip, updated_at, head check suites, including startup_failure suites without check runs). Only PRs whose snapshot changed, or whose patrol is due, are read over REST as before; check runs and suites are read only when a completed suite that did not pass appears. When GraphQL fails for another reason than its rate limit, the due PRs are read over REST. GraphQL queries now count against their own window: a used-up core window no longer stops queries, a used-up GraphQL window no longer stops REST requests, and a RATE_LIMITED answer stops as github_rate_limited. Fixes agent-CI-Infra#1396
|
Reviewed head Checked the per-PR reconcile schedule (#1396): 5 min floor, 5→10→20→40→80→160→240 min backoff, 4 h cap, reset on change/webhook/local-env change, all open bot PRs covered, no connect while nothing is due; the batched GraphQL read (50 aliases per query), REST details only for changed PRs (and check runs/suites only for a changed completed non-passing suite), the REST fallback when GraphQL fails for other reasons, and the separate No blocking issues. Enqueued for merge with |
| } catch { | ||
| // Isolate per-PR patrol errors so one malformed row or transient failure never stalls scanning other PRs. | ||
| } |
There was a problem hiding this comment.
F1(suggestion)逐 PR try 里的异常不会被记为「未读到」,会把该 PR 的下一次读取推迟到翻倍后的间隔(最长 4 小时)
同一段代码已在别处定下「读不到就下轮重读」的约定:第 2992 行 REST 详情读取失败时 seen = null,第 3316 行落库为 'unreadable',saveBotPrReconcile 判定摘要变化后把间隔复位到 5 分钟。但这里的 catch 没有做同样的事——此时 seen 仍是前面 GraphQL 快照算出的摘要,finally 落库后判定为「没变化」,间隔按 prior * 2 翻倍(封顶 4 小时)。
叠加 botPrReconcileDue(第 2847 行 if (patrolDueAt <= nowMs && last < patrolDueAt) return true;)会因为本次已写入 reconcile_at = now 而不再命中巡检兜底分支,结果是:一次瞬时失败(例如 SQLITE_BUSY、recordEvent 事务异常、巡检路径中除 guardRequest 之外的意外抛错)会让该 PR 的巡检、冲突与失败检查在最长 4 小时内不再重试。改动前这类异常由每分钟一轮的扫描自然重试(last_patrol_at 未推进,下一 tick 仍判定巡检到期)。在当前改动下只有该 PR 恰好又收到 Webhook 时才会在 5 分钟后重读,而没有 Webhook 的安静 PR 正是巡检要覆盖的场景。
建议在 catch 中把本次读取标记为未读到,与上面两处语义对齐(一行改动,不影响其它 PR):
} catch {
// Isolate per-PR patrol errors ...; the read is recorded as unread so it is retried in 5 minutes.
seen = null;
}
检视结论(head
|
| 编号 | 严重度 | 结论 | 位置 |
|---|---|---|---|
| F1 | suggestion | open | src/review/bot-pr-wake.ts:3311 |
F1 不阻断合入(不影响正确性主线,只在瞬时异常时把重试推迟),其余未发现逻辑/并发/资源/安全/API 破坏类缺陷。第 3316 行 finally 用的 store.db.isOpen 是正确的(文中其它 store.db.open 判断属于改动前既有写法,本 PR 未新增)。
未独立复核:PR 描述中「50 个别名 PR 连同 check suites 实测 1 点 GraphQL 额度」来自作者在真实 GitHub 的实测;仓库内假 GitHub 不模拟 rateLimit.cost,我无法在本地复现该数值,仅确认窗口分离逻辑本身正确。
Reviewed commit: 98fb257
Coverage: PR #1397; 12 changed file(s)
Fixes #1396
Problem
BotPrWakeScannerranreconcileOpenBotPullRequestsForRepoevery 60 s for every repository with open bot PRs and read the 25 newest of them over REST each time (GET /pulls/<n>each,GET refper base branch, check runs and suites whenever a 10-minute in-memory cache expired), whether or not anything had changed. On the openJiuwen-ai installation (sciencediscovery 41 open bot PRs, DigitalMate 17) that loop alone was about 3,300 core requests an hour against a 5,000 quota, and the 16 older sciencediscovery PRs were never reconciled.Change
Per-PR schedule (
bot_pr_states.reconcile_at,reconcile_due_at,reconcile_interval_ms,reconcile_fingerprint,reconcile_env):BOT_PR_RECONCILE_MIN_MS/BOT_PR_RECONCILE_MAX_MS).updated_at, or the head commit's check suites.markBotPrsForReconcile, called frombotPrWakeEvents): PR events, reviews and comments;check_run/check_suite/workflow_run/statuson its head commit or branch; a push to its head or base branch; another PR merged into its base. Such a PR becomes due atmax(now, reconcile_at + 5 min).LIMIT 25is gone). The scanner still ticks every minute, but a repository with no due PR does not connect and sends nothing.Batched reads: all due PRs of a repository go into one GraphQL query per 50 PRs (
pN: pullRequest(number: N)aliases), with these fields:state isDraft mergeable updatedAt headRefName headRefOid baseRefName baseRefOid baseRef{target{oid}} commits(last:1){…checkSuites(first:100){totalCount nodes{databaseId status conclusion}}}.GET /pulls/<n>, conflict handling, mergeability recheck, patrol). The base tip comes from the query, so there is noGET ref.startup_failuresuites without check runs, so those are still detected.NOT_FOUNDyields a partial answer and is backed off like an unchanged PR.GraphQL rate-limit window:
blockedUntil/refusednow take the resource.request()usesgraphqlforPOST /graphqlandcoreotherwise, and thex-ratelimit-resource: graphqlheaders are recorded ingithub_rate_limitsas before.graphql()maps aRATE_LIMITEDanswer (HTTP 200) togithub_rate_limited/retry_waitinstead ofgithub_graphql_failed.Webhook-driven rechecks (
schedulePendingSuiteRecheck, which passesonlyPrNumbers) are not scheduled; they still read their PRs over REST right away.Why GraphQL rather than REST conditional requests
I checked the query against real GitHub (agent-test-zone/bot-demo). 50 aliased PRs with their check suites cost 1 point. A missing number answers
nullplus aNOT_FOUNDerror for that alias only.CheckConclusionStateincludesSTARTUP_FAILURE, andbaseRef.target.oidgives the base tip in the same query.With
If-None-Match, a 304 does not count, but each poll would still be one request per PR, plus separate check-runs and check-suites requests (each needing its own ETag) and a ref read. GraphQL covers all of that in one request and moves the polling load to the separategraphqlwindow.Requests per hour, before and after (fake GitHub)
Scenario: 41 open bot PRs (sciencediscovery's count) on one App installation, nothing changes, 6-hour patrol on, scanner ticking every minute for 8 simulated hours. The counts are installation-token REST requests, not the App's own JWT requests (installation lookup, token minting). The same scenario was run against
origin/main(65724a6) and this branch.GET /pulls/<n>)GET /pulls/<n>The "after" column is printed by the last test in
test/st/src/review/bot-pr-reconcile-schedule.test.mjs, which also bounds it. Once PRs change, each changed PR costs oneGET /pulls/<n>, plus check runs and suites when a suite that did not pass appears, plus comments for an unrecorded conflict, at most once per 5 minutes.Behaviour changes worth noting
bot-pr-patrol.test.mjs, four 2-minute clock steps became 5-minute steps for this reason.settledReconcileCheckscache is gone; the persisted snapshot digest replaces it.Tests
test/st/src/review/bot-pr-reconcile-schedule.test.mjs(weight added totest-set-weights.json), on a fake App installation:graphqlwindow is recorded.startup_failuresuite that still wakes the Manager) resets only that PR. Reads are triggered by a comment webhook, by acheck_suiteon the head (held back by the 5-minute floor), by another PR merged intomain(all PRs), and by a local policy change.github-rate-limits.test.mjs: aRATE_LIMITEDGraphQL answer stops asgithub_rate_limited, the next query is not sent, and REST still is.bot-pr-wakeandbot-pr-patrolfixtures answer the batched query through the newtest/fixtures/bot-pr-graphql.mjs, so their scanner scenarios (startup_failure detection, conflicts, patrol, suppression) now run through the GraphQL path.Verification
Head
98fb2576, onorigin/main65724a65, Node 22.npm ci→ exit 0;(cd runner && npm ci)→ exit 0.npm run check→ exit 0 (tsc, runtime syntax checks of 853 files).npm test→ exit 1. 350 of 352 sets passed; the two failures are unrelated to this change and pass on their own:runner-transport:listen EADDRINUSEon a 119-byte socket path under this deep worktree's.tmp/tests-*/…(over the 108-bytesun_pathlimit).TMPDIR=$PWD/.tmp/s node --test test/ut/manager/runner-transport.test.mjs→ exit 0, 4/4.codex-harness: a timing assertion ("stopped promptly after interrupt_requested") under the parallel load.node test/framework/test-sets.mjs test/st/src/harness/codex-harness.test.mjs→ exit 0, 68/68.bot-pr-reconcile-schedule,bot-pr-wake,bot-pr-patrol,continuous-pr-task,github-rate-limits) → exit 0, 28/28.cost: 1,x-ratelimit-resource: graphql, missing numbers answernullandNOT_FOUNDfor that alias only.