The best way to control Claude Code, Codex and more from any computer or phone
fix(github): the PR limits sweep retries a failed read (#34671)
maria committed
Nov 12, 2026 at 01:20 UTC
af2421f23a81c4bc7e197bfe3ecf6e34036b58a0
1 file changed
+31
-6
.github/scripts/pr-limits.cjs
+31
-6
@@ -8,9 +8,13 @@ const NOTICE_MS = 72 * HOUR_MS;
8
// limit on how fast a token may comment.
9
const MAX_REQUESTS_PER_RUN = 100;
10
const MAX_CLOSES_PER_RUN = 50;
11
+// A last look that ends in "not today" closes nothing, so the looks need a cap of their own.
12
+const MAX_RECHECKS_PER_RUN = 150;
13
const WRITE_PAUSE_MS = 1_000;
14
// Measured here: about two and a half minutes until every open PR has an answer.
15
const MERGEABLE_WAIT_MS = 150_000;
16
+const READ_ATTEMPTS = 4;
17
+const READ_RETRY_MS = 5_000;
18
// GitHub stamps a label event and the PR's updatedAt a second or two apart.
19
const SAME_MOMENT_MS = 15_000;
20
@@ -27,7 +31,7 @@ const LOGIN_PATTERN = /^github:([A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38
31
const SWEEP_QUERY = `
32
query ($owner: String!, $repo: String!, $labels: [String!], $cursor: String) {
33
repository(owner: $owner, name: $repo) {
30
- pullRequests(states: OPEN, labels: $labels, first: 100, after: $cursor) {
34
+ pullRequests(states: OPEN, labels: $labels, first: 50, after: $cursor) {
35
pageInfo { hasNextPage endCursor }
36
nodes {
37
number
@@ -63,6 +67,22 @@ const AUTHOR_OPEN_PRS_QUERY = `
67
}
68
}`;
69
70
+const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
71
+
72
+// GitHub answers a heavy list request with a 502 now and then, and one of those must not end the
73
+// run. Only reads go through here: repeating a read is safe, repeating a comment is not.
74
+async function read(github, query, variables) {
75
+ for (let attempt = 1; ; attempt += 1) {
76
+ try {
77
+ return await github.graphql(query, variables);
78
+ } catch (error) {
79
+ const transient = typeof error.status === "number" && error.status >= 500;
80
+ if (attempt === READ_ATTEMPTS || !transient) throw error;
81
+ await pause(attempt * READ_RETRY_MS);
82
+ }
83
+ }
84
+}
85
+
86
// Fails closed: a missing, empty, or malformed list must never read as "nobody is exempt".
87
function parseExemptions(text) {
88
const logins = new Set();
@@ -141,13 +161,12 @@ async function closeWithComment(github, context, number, body) {
161
async function sweep({ github, context, core, dryRun }) {
162
const exemptions = parseExemptions(fs.readFileSync(EXEMPTIONS_PATH, "utf8"));
163
const repo = { owner: context.repo.owner, repo: context.repo.repo };
144
- const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
164
165
const list = async () => {
166
const prs = [];
167
for (let cursor = null; ;) {
168
const page = (
150
- await github.graphql(SWEEP_QUERY, {
169
+ await read(github, SWEEP_QUERY, {
170
...repo,
171
cursor,
172
labels: [UNVOUCHED_LABEL, REBASE_LABEL, CLOSING_LABEL],
@@ -167,7 +186,12 @@ async function sweep({ github, context, core, dryRun }) {
186
}
187
188
const now = Date.now();
189
+ // Start further down the list each day. The step is no larger than a run's cap, so a cap that
190
+ // is reached cannot leave the same PRs waiting behind the same others every day.
191
+ const start = (Math.floor(now / (24 * HOUR_MS)) * MAX_RECHECKS_PER_RUN) % (prs.length || 1);
192
+ prs = [...prs.slice(start), ...prs.slice(0, start)];
193
const count = {};
194
+ let rechecks = 0;
195
for (const pr of prs) {
196
const { number } = pr;
197
let action = decide(pr, exemptions, now);
@@ -181,9 +205,10 @@ async function sweep({ github, context, core, dryRun }) {
205
if ((count["close-conflict"] ?? 0) + (count["close-notice"] ?? 0) >= MAX_CLOSES_PER_RUN) {
206
continue;
207
}
208
+ if (rechecks >= MAX_RECHECKS_PER_RUN) continue;
209
+ rechecks += 1;
210
// The list is minutes old by now. Closing is the one step worth a second look.
185
- const fresh = (await github.graphql(RECHECK_QUERY, { ...repo, number })).repository
186
- .pullRequest;
211
+ const fresh = (await read(github, RECHECK_QUERY, { ...repo, number })).repository.pullRequest;
212
if (fresh.state !== "OPEN") continue;
213
// A merge to the base can resolve a conflict without touching the PR, so this is re-read
214
// too. An answer that is not ready yet reads as "not conflicting" and waits for tomorrow.
@@ -265,7 +290,7 @@ async function enforceOpenLimit({ github, context, core, dryRun }) {
290
if (reopened && context.payload.sender.login.toLowerCase() !== author.toLowerCase()) return;
291
292
const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open author:${author} sort:created-asc`;
268
- const { search } = await github.graphql(AUTHOR_OPEN_PRS_QUERY, { q });
293
+ const { search } = await read(github, AUTHOR_OPEN_PRS_QUERY, { q });
294
// A new PR counts only the older ones, so two opened together cannot close each other.
295
const others = search.nodes.filter(
296
(node) => node.number && (reopened ? node.number !== pr.number : node.number < pr.number),