Skip to content

Commit 18db1dd

Browse files
committed
feat(github): unvouched PRs get an open PR limit, a conflict window, and a 72 hour close notice
Authors who are not vouched can hold 8 open PRs; a further one closes on open. A PR of theirs that conflicts with main gets a rebase request and closes after 7 days without a push. Triage closures become a notice: the triage:closing label closes the PR after 72 hours unless its author replies or pushes.
1 parent 11f61a5 commit 18db1dd

5 files changed

Lines changed: 446 additions & 4 deletions

File tree

‎.agents/skills/contribution-triage/SKILL.md‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ comment text but do not write to GitHub. Keep the eligibility finding separate f
126126
This is a routing exception, not a claim that the PR passed eligibility or correctness review.
127127
- **Closure warranted.** The assessment is complete and establishes a specific policy violation.
128128
Prepare a clear explanation under [closure and reconsideration](../../../CONTRIBUTING.md#closure-and-reconsideration),
129-
post it, and close the PR. Verify that the explanation is present and the PR is closed. The comment
129+
post it, and apply `triage:closing`. Verify that the explanation and the label are present. Do not
130+
close the PR yourself: [pr-limits.yml](../../../.github/workflows/pr-limits.yml) closes it 72 hours
131+
after the label. A reply or a push from the author removes the label first; treat that as new
132+
evidence, reassess, and apply the label again only if the finding still holds. The comment
130133
must name the violated rule, cite supporting submission evidence, link the maintained guide section,
131134
and give a concrete remedy. Missing contributor evidence can warrant closure; explain what must be
132135
established. Missing product approval calls for maintainer discussion, not an agent's product decision.
@@ -173,9 +176,10 @@ description, evidence, approvals, existing triage comments, PR open/closed state
173176
label when relevant. Reassess changes that could invalidate the finding. Reuse an existing explanation
174177
only if it still matches the current finding; avoid duplicate comments, closures, or label applications.
175178

176-
For closure, establish that the required explanation is posted before closing. If posting fails or its
177-
result is ambiguous, read back the comments before retrying or proceeding. If the comment succeeds but
178-
closure fails, preserve the comment and retry only the unfinished closure after checking current state.
179+
For closure, establish that the required explanation is posted before applying `triage:closing`. If
180+
posting fails or its result is ambiguous, read back the comments before retrying or proceeding. If the
181+
comment succeeds but labeling fails, preserve the comment and retry only the label after checking
182+
current state.
179183
Handle a failed or ambiguous label application the same way: inspect labels before any retry. If access
180184
or state still cannot be verified, report an incomplete action and the step needed to resume. Do not
181185
retry blindly or claim success for an unverified action.

‎.github/scripts/pr-limits.cjs‎

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
const fs = require("node:fs");
2+
3+
const OPEN_LIMIT = 8;
4+
const CONFLICT_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
5+
const CLOSING_WINDOW_MS = 72 * 60 * 60 * 1000;
6+
// A first run meets the whole backlog; cap it so one run cannot post hundreds of comments.
7+
const MAX_WARNINGS_PER_RUN = 100;
8+
const MAX_CLOSES_PER_RUN = 50;
9+
// The Actions token gets 1,000 GraphQL points an hour; each lookup costs one.
10+
const MAX_LOOKUPS_PER_RUN = 300;
11+
const MERGEABLE_ATTEMPTS = 3;
12+
const MERGEABLE_RETRY_MS = 30_000;
13+
14+
const REBASE_LABEL = "needs-rebase";
15+
const CLOSING_LABEL = "triage:closing";
16+
const KEEP_OPEN_LABEL = "triage:keep-open";
17+
const UNVOUCHED_LABEL = "vouch:unvouched";
18+
const TRUSTED_VOUCH_STATUSES = new Set(["bot", "collaborator", "vouched"]);
19+
const EXEMPTIONS_PATH = ".github/TRIAGE_EXEMPTIONS.td";
20+
const LOGIN_PATTERN = /^github:([A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38})$/;
21+
22+
const OPEN_PRS_QUERY = `
23+
query ($owner: String!, $repo: String!, $cursor: String) {
24+
repository(owner: $owner, name: $repo) {
25+
pullRequests(states: OPEN, first: 100, after: $cursor, orderBy: { field: CREATED_AT, direction: ASC }) {
26+
pageInfo { hasNextPage endCursor }
27+
nodes {
28+
number
29+
mergeable
30+
labels(first: 50) { nodes { name } }
31+
timelineItems(itemTypes: [LABELED_EVENT], last: 30) {
32+
nodes { ... on LabeledEvent { createdAt label { name } } }
33+
}
34+
}
35+
}
36+
}
37+
}`;
38+
39+
const PR_DETAIL_QUERY = `
40+
query ($owner: String!, $repo: String!, $number: Int!) {
41+
repository(owner: $owner, name: $repo) {
42+
pullRequest(number: $number) {
43+
state
44+
mergeable
45+
labels(first: 50) { nodes { name } }
46+
timelineItems(itemTypes: [LABELED_EVENT], last: 100) {
47+
nodes { ... on LabeledEvent { createdAt label { name } } }
48+
}
49+
comments(first: 100) { totalCount nodes { author { login } } }
50+
reviews(first: 100) { totalCount nodes { author { login } } }
51+
}
52+
}
53+
}`;
54+
55+
const AUTHOR_OPEN_PRS_QUERY = `
56+
query ($q: String!) {
57+
search(query: $q, type: ISSUE, first: 50) {
58+
nodes { ... on PullRequest { number } }
59+
}
60+
}`;
61+
62+
// Fails closed: a missing, empty, or malformed list must never read as "nobody is exempt".
63+
function parseExemptions(text) {
64+
const logins = new Set();
65+
for (const raw of text.split("\n")) {
66+
const line = raw.trim();
67+
if (line === "" || line.startsWith("#")) continue;
68+
const match = LOGIN_PATTERN.exec(line);
69+
if (!match) throw new Error(`${EXEMPTIONS_PATH}: malformed entry "${line}"`);
70+
const login = match[1].toLowerCase();
71+
if (logins.has(login)) throw new Error(`${EXEMPTIONS_PATH}: duplicate entry "${line}"`);
72+
logins.add(login);
73+
}
74+
if (logins.size === 0) throw new Error(`${EXEMPTIONS_PATH}: no entries`);
75+
return logins;
76+
}
77+
78+
function readsMergeable(pr) {
79+
return pr.labels.nodes.some(
80+
(label) => label.name === UNVOUCHED_LABEL || label.name === REBASE_LABEL,
81+
);
82+
}
83+
84+
function sweepStep(pr) {
85+
const labels = new Set(pr.labels.nodes.map((label) => label.name));
86+
const keepOpen = labels.has(KEEP_OPEN_LABEL);
87+
if (labels.has(CLOSING_LABEL)) return keepOpen ? "skip" : "closing-window";
88+
if (labels.has(REBASE_LABEL)) {
89+
if (keepOpen || !labels.has(UNVOUCHED_LABEL) || pr.mergeable === "MERGEABLE") {
90+
return "clear-rebase";
91+
}
92+
return pr.mergeable === "CONFLICTING" ? "conflict-window" : "skip";
93+
}
94+
if (!keepOpen && labels.has(UNVOUCHED_LABEL) && pr.mergeable === "CONFLICTING") {
95+
return "conflict-warning";
96+
}
97+
return "skip";
98+
}
99+
100+
// CONTRIBUTING.md leaves a PR to maintainers once an exempt login has commented or reviewed.
101+
function maintainerOn(detail, exemptions) {
102+
if (detail.comments.totalCount > 100 || detail.reviews.totalCount > 100) {
103+
return "more activity than one page";
104+
}
105+
for (const node of [...detail.comments.nodes, ...detail.reviews.nodes]) {
106+
const login = node.author?.login;
107+
if (login && exemptions.has(login.toLowerCase())) return login;
108+
}
109+
return undefined;
110+
}
111+
112+
function labeledAt(detail, name) {
113+
const events = detail.timelineItems.nodes.filter((event) => event.label?.name === name);
114+
const last = events.at(-1);
115+
return last ? Date.parse(last.createdAt) : undefined;
116+
}
117+
118+
function guideLink(context, anchor) {
119+
return `https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/CONTRIBUTING.md#${anchor}`;
120+
}
121+
122+
async function removeLabel(github, context, number, name) {
123+
try {
124+
await github.rest.issues.removeLabel({ ...context.repo, issue_number: number, name });
125+
} catch (error) {
126+
if (error.status !== 404) throw error;
127+
}
128+
}
129+
130+
async function closeWithComment(github, context, number, body) {
131+
await github.rest.issues.createComment({ ...context.repo, issue_number: number, body });
132+
await github.rest.pulls.update({ ...context.repo, pull_number: number, state: "closed" });
133+
}
134+
135+
async function sweep({ github, context, core, dryRun }) {
136+
const exemptions = parseExemptions(fs.readFileSync(EXEMPTIONS_PATH, "utf8"));
137+
const repo = { owner: context.repo.owner, repo: context.repo.repo };
138+
139+
// GitHub computes mergeability lazily and forgets it whenever main moves. Asking starts the
140+
// computation, so list again until the PRs this sweep reads it for have an answer.
141+
let openPrs = [];
142+
for (let attempt = 0; attempt < MERGEABLE_ATTEMPTS; attempt += 1) {
143+
if (attempt > 0) await new Promise((resolve) => setTimeout(resolve, MERGEABLE_RETRY_MS));
144+
openPrs = [];
145+
for (let cursor = null; ;) {
146+
const page = (await github.graphql(OPEN_PRS_QUERY, { ...repo, cursor })).repository
147+
.pullRequests;
148+
openPrs.push(...page.nodes);
149+
if (!page.pageInfo.hasNextPage) break;
150+
cursor = page.pageInfo.endCursor;
151+
}
152+
if (!openPrs.some((pr) => pr.mergeable === "UNKNOWN" && readsMergeable(pr))) break;
153+
}
154+
const candidates = openPrs.filter((pr) => sweepStep(pr) !== "skip");
155+
156+
const plan = [];
157+
let warnings = 0;
158+
let closes = 0;
159+
let lookups = 0;
160+
const now = Date.now();
161+
const windowOf = (step) => (step === "closing-window" ? CLOSING_WINDOW_MS : CONFLICT_WINDOW_MS);
162+
const labelOf = (step) => (step === "closing-window" ? CLOSING_LABEL : REBASE_LABEL);
163+
for (const candidate of candidates) {
164+
const { number } = candidate;
165+
const listed = sweepStep(candidate);
166+
// Skip the lookup when the list already shows there is nothing to do this run.
167+
if (listed === "conflict-warning" && warnings >= MAX_WARNINGS_PER_RUN) continue;
168+
if (listed === "conflict-window" || listed === "closing-window") {
169+
const since = labeledAt(candidate, labelOf(listed));
170+
if (closes >= MAX_CLOSES_PER_RUN) continue;
171+
if (since !== undefined && now - since < windowOf(listed)) continue;
172+
}
173+
if (lookups >= MAX_LOOKUPS_PER_RUN) {
174+
core.warning(`Stopped at ${MAX_LOOKUPS_PER_RUN} lookups; the next run continues.`);
175+
break;
176+
}
177+
lookups += 1;
178+
// The list can be minutes old by the time we act, so decide again on fresh state. A merge to
179+
// main in between resets mergeability; it cannot resolve a conflict, so keep the listed answer.
180+
const detail = (await github.graphql(PR_DETAIL_QUERY, { ...repo, number })).repository
181+
.pullRequest;
182+
if (detail.mergeable === "UNKNOWN") detail.mergeable = candidate.mergeable;
183+
const step = detail.state === "OPEN" ? sweepStep(detail) : "skip";
184+
if (step === "skip") continue;
185+
186+
if (step === "clear-rebase") {
187+
plan.push({ number, action: "clear-rebase" });
188+
if (!dryRun) await removeLabel(github, context, number, REBASE_LABEL);
189+
continue;
190+
}
191+
192+
const maintainer = maintainerOn(detail, exemptions);
193+
if (maintainer) {
194+
const reason = `maintainer involved (${maintainer})`;
195+
if (step === "conflict-window") {
196+
plan.push({ number, action: "clear-rebase", reason });
197+
if (!dryRun) await removeLabel(github, context, number, REBASE_LABEL);
198+
} else {
199+
plan.push({ number, action: "leave-open", reason });
200+
}
201+
continue;
202+
}
203+
204+
if (step === "conflict-warning") {
205+
if (warnings >= MAX_WARNINGS_PER_RUN) continue;
206+
warnings += 1;
207+
plan.push({ number, action: "warn-conflict" });
208+
if (dryRun) continue;
209+
await github.rest.issues.addLabels({
210+
...context.repo,
211+
issue_number: number,
212+
labels: [REBASE_LABEL],
213+
});
214+
await github.rest.issues.createComment({
215+
...context.repo,
216+
issue_number: number,
217+
body: [
218+
"This PR conflicts with `main`. Rebase it or merge `main` into it and push within 7 days, or it will be closed automatically.",
219+
"",
220+
`Pushing removes the \`${REBASE_LABEL}\` label and restarts the check. See the [conflict window](${guideLink(context, "conflict-window")}).`,
221+
].join("\n"),
222+
});
223+
continue;
224+
}
225+
226+
const since = labeledAt(detail, labelOf(step));
227+
if (since === undefined || now - since < windowOf(step)) continue;
228+
if (closes >= MAX_CLOSES_PER_RUN) continue;
229+
closes += 1;
230+
plan.push({ number, action: step === "closing-window" ? "close-triage" : "close-conflict" });
231+
if (dryRun) continue;
232+
await closeWithComment(
233+
github,
234+
context,
235+
number,
236+
step === "closing-window"
237+
? [
238+
"Closing: 72 hours have passed since the triage notice above with no reply or push from the author.",
239+
"",
240+
`To ask for reconsideration, address the notice and comment here, or open a focused replacement PR that links this one. See [closure and reconsideration](${guideLink(context, "closure-and-reconsideration")}).`,
241+
].join("\n")
242+
: [
243+
"Closing: this PR still conflicts with `main` 7 days after the rebase request, with no push since.",
244+
"",
245+
`To continue, rebase the branch and open a new PR that links this one. See the [conflict window](${guideLink(context, "conflict-window")}).`,
246+
].join("\n"),
247+
);
248+
}
249+
250+
const count = (action) => plan.filter((entry) => entry.action === action).length;
251+
core.info(
252+
`${dryRun ? "Dry run. " : ""}${candidates.length} candidates: ` +
253+
`${count("warn-conflict")} conflict warnings, ${count("close-conflict")} conflict closes, ` +
254+
`${count("close-triage")} triage closes, ${count("clear-rebase")} labels cleared, ` +
255+
`${count("leave-open")} left to maintainers.`,
256+
);
257+
for (const entry of plan) {
258+
core.info(`#${entry.number} ${entry.action}${entry.reason ? ` (${entry.reason})` : ""}`);
259+
}
260+
return plan;
261+
}
262+
263+
async function enforceOpenLimit({ github, context, core }) {
264+
const pr = context.payload.pull_request;
265+
const author = pr.user.login;
266+
const status = process.env.VOUCH_STATUS;
267+
if (!status) {
268+
core.warning(`PR #${pr.number}: no vouch status, leaving it open.`);
269+
return;
270+
}
271+
if (TRUSTED_VOUCH_STATUSES.has(status)) return;
272+
if (pr.labels.some((label) => label.name === KEEP_OPEN_LABEL)) return;
273+
// Someone else reopening a PR is a maintainer overriding the limit.
274+
if (context.payload.action === "reopened" && context.payload.sender.login !== author) return;
275+
276+
const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open author:${author}`;
277+
const { search } = await github.graphql(AUTHOR_OPEN_PRS_QUERY, { q });
278+
const others = search.nodes.filter((node) => node.number && node.number !== pr.number).length;
279+
if (others < OPEN_LIMIT) {
280+
core.info(`PR #${pr.number}: ${author} has ${others} other open PRs.`);
281+
return;
282+
}
283+
284+
await closeWithComment(
285+
github,
286+
context,
287+
pr.number,
288+
[
289+
`Closing: ${author} already has ${OPEN_LIMIT} or more open PRs here, which is the limit for contributors who are not vouched.`,
290+
"",
291+
`Once one of them is merged or closed, open this again. See the [open PR limit](${guideLink(context, "open-pr-limit")}).`,
292+
].join("\n"),
293+
);
294+
core.info(`PR #${pr.number}: closed, ${author} has ${others} other open PRs.`);
295+
}
296+
297+
module.exports = { enforceOpenLimit, maintainerOn, parseExemptions, sweep, sweepStep };

0 commit comments

Comments
 (0)