#!/usr/bin/env node // The bug fast track (delivery rule 17): the two checks that take a bug from its triage report // to `main` without the maintainer's `bug:ready`. Both read facts, never an agent's confidence. // // node .agents/scripts/fast-track.mjs entry check, after the triage report // node .agents/scripts/fast-track.mjs --dry-run the entry decision; changes nothing // node .agents/scripts/fast-track.mjs --pr exit check, before the merge // // Entry passes when issue is open, carries `bug:new`, has no assignee, has a // `bug/-repro` branch, its newest triage report says `yes` on all five Fast track lines // (`FAST_TRACK` in body-rules.mjs) and was posted from an account that can write to the repo // (an agent posts under a maintainer's), and every comment after that report is an agent's // from such an account. It then // adds `bug:ready` and `fast-track`, removes `bug:new`, prints `fast-track: # is bug:ready` // and exits 0. Otherwise it changes nothing, prints `fast-track: # held: ` and // exits 1. With `--dry-run` a pass prints `fast-track: # would be bug:ready` instead. An // issue whose author cannot write to the repo also gets `fast-track-review`, and the line ends // ` and fast-track-review (reported by , ...)`: an agent fixes it, the maintainer merges. // // Exit holds PR , whatever the labels say, when issue 's author cannot write to the repo // (read from GitHub at that moment): the maintainer merges it by hand. Otherwise it has nothing to // check, and exits 0, unless the issue carries `fast-track` or `fast-track-review`. // `fast-track-review` always holds. With `fast-track`, PR // passes when it changes at most MAX_LINES lines in at most MAX_FILES files, touches no path in // surface-paths.mjs (a surface file, the protocol version, an ADR), has no `## Decision taken` // comment, and fewer than MAX_A_DAY merged PRs fixed a `fast-track` bug in the 24 hours before // (its `bug/` branch or a `Closes #` in its body); // otherwise the same `held:` line and exit // 1. `merge-pr.sh` runs it for every bug a PR fixes, before CI and again right before the merge. The maintainer lets a held PR // through by removing the label the line names, or merges it by hand. // // Both stop with the same `held:` line while agents are paused (paused.mjs). // // It calls repo-scoped REST only, as merge-pr.sh does. The repo is GH_REPO, else the origin // remote's owner/name. import { execFileSync } from "node:child_process"; import { FAST_TRACK, fastTrackFacts, writtenByAgent } from "./body-rules.mjs"; import { option, repository } from "./cli-args.mjs"; import { stopWhenPaused } from "./paused.mjs"; import { surfaceHits } from "./surface-paths.mjs"; const MAX_LINES = 250; const MAX_FILES = 8; const MAX_A_DAY = 8; const CLOSES = /\b(?:close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)\s+#(\d+)/gi; const DAY_MS = 24 * 60 * 60 * 1000; // why: a comment counts as an agent's, and a report as real, only from an account that can // write to the repo; anyone can type the footer or a report's headings into a comment, and // `author_association` tells an organization's member from an outsider, not what either may // do in this repo. const WRITE = new Set(["admin", "write"]); const permissions = new Map(); const [number] = process.argv.slice(2); const pr = option("--pr"); const dryRun = process.argv.includes("--dry-run"); if (!/^\d+$/.test(number ?? "") || (process.argv.includes("--pr") && !/^\d+$/.test(pr ?? ""))) { console.error("usage: fast-track.mjs [--dry-run | --pr ]"); process.exit(64); } const nwo = repository(); stopWhenPaused(nwo, `fast-track: #${number} held:`); const issue = api("GET", `issues/${number}`); const labels = issue.labels.map((l) => (typeof l === "string" ? l : l.name)); if (pr === null) entryCheck(); else exitCheck(); function entryCheck() { const holds = [...stateHolds(), ...reportHolds()]; if (!exists(`branches/bug/${number}-repro`)) holds.push(`bug/${number}-repro does not exist`); if (holds.length > 0) held(holds); // why: an outsider's report is text nobody with write access wrote, and the fix follows it; // the fix is built and reviewed as any other, and a person reads it before it reaches main. const review = canWrite(issue) ? "" : ` and fast-track-review (reported by ${issue.user?.login ?? "an unknown account"}, who cannot write to the repo: the maintainer merges the fix)`; if (dryRun) { console.log(`fast-track: #${number} would be bug:ready${review}`); return; } const added = ["bug:ready", "fast-track", ...(review === "" ? [] : ["fast-track-review"])]; api("POST", `issues/${number}/labels`, ...added.flatMap((l) => ["-f", `labels[]=${l}`])); // why: the issue-state workflow removes the previous state label too; whoever is second // gets a 404, which is the state both want. exists(`issues/${number}/labels/bug:new`, "DELETE"); console.log(`fast-track: #${number} is bug:ready${review}`); } function stateHolds() { const holds = []; if (issue.state !== "open") holds.push("the issue is closed"); if (!labels.includes("bug:new")) { holds.push( `it carries ${labels.filter((l) => /^\w+:/.test(l)).join(", ") || "no state label"}, not bug:new`, ); } if (issue.assignees.length > 0) { holds.push(`it is assigned to ${issue.assignees.map((a) => a.login).join(", ")}`); } return holds; } function reportHolds() { const comments = paged(`issues/${number}/comments`); const at = comments.findLastIndex((c) => fastTrackFacts(c.body) !== null); if (at === -1) return ["it has no triage report"]; if (!canWrite(comments[at])) { return ["its newest triage report is not from an account that can write to the repo"]; } const holds = factHolds(fastTrackFacts(comments[at].body)); const agents = (c) => canWrite(c) && writtenByAgent(c.body); if (!comments.slice(at + 1).every(agents)) holds.push("a person commented after the report"); return holds; } function canWrite(written) { const login = written.user?.login; if (login === undefined) return false; if (!permissions.has(login)) { const at = `collaborators/${encodeURIComponent(login)}/permission`; permissions.set(login, exists(at) ? api("GET", at).permission : "none"); } return WRITE.has(permissions.get(login)); } function factHolds(facts) { if (facts.map((f) => f.name).join("|") !== FAST_TRACK.join("|")) { return ["its report has no Fast track section with the five lines"]; } return facts.filter((f) => !f.yes).map((f) => `${f.name}: no — ${f.why}`); } function exitCheck() { // why: a label can be taken off in more ways than any guard sees; who wrote the issue cannot. if (!canWrite(issue)) { held( [ `its reporter ${issue.user?.login ?? "(unknown)"} cannot write to the repo, so the maintainer merges PR #${pr}`, ], " The maintainer merges it by hand.", ); } const kept = ["fast-track-review", "fast-track"].filter((l) => labels.includes(l)); if (kept.length === 0) { console.log(`fast-track: #${number} was not fast-tracked; nothing to check`); return; } const holds = []; if (kept.includes("fast-track-review")) { holds.push(`it carries fast-track-review, so the maintainer merges PR #${pr}`); } if (kept.includes("fast-track")) holds.push(...fixHolds(), ...dayHolds()); if (holds.length > 0) { held( holds, ` Remove the ${kept.join(" and ")} label${kept.length > 1 ? "s" : ""} from #${number} to let it merge.`, ); } console.log(`fast-track: #${number} PR #${pr} passes the exit check`); } function fixHolds() { const pull = api("GET", `pulls/${pr}`); const holds = []; const lines = pull.additions + pull.deletions; if (lines > MAX_LINES) holds.push(`PR #${pr} changes ${lines} lines, over ${MAX_LINES}`); if (pull.changed_files > MAX_FILES) { holds.push(`PR #${pr} changes ${pull.changed_files} files, over ${MAX_FILES}`); } const files = paged(`pulls/${pr}/files`).map((f) => f.filename); // why: a fix that touches one may add to or break what a user meets, so it is the // maintainer's to read before it merges. for (const { what, path } of surfaceHits(files)) holds.push(`PR #${pr} touches ${what} (${path})`); if (paged(`issues/${pr}/comments`).some((c) => /^## Decision taken\s*$/m.test(c.body ?? ""))) { holds.push(`PR #${pr} has a "Decision taken" comment`); } return holds; } // why: a wrong report shape or a bad day could send many fixes to main before anyone looks; // the busiest 24 hours of November 2026 had 5. function dayHolds() { const since = Date.now() - DAY_MS; const today = []; for (const bugs of mergedSince(since)) { if (today.length === MAX_A_DAY) break; const tracked = bugs.find((bug) => api("GET", `issues/${bug}`).labels.some( (l) => (typeof l === "string" ? l : l.name) === "fast-track", ), ); if (tracked !== undefined) today.push(`#${tracked}`); } if (today.length < MAX_A_DAY) return []; return [ `${MAX_A_DAY} fast-track fixes merged in the last 24 hours (${today.join(", ")}), the most a day takes`, ]; } // why: a PR fixes the bug its `bug/` branch names and every issue its body closes (coding // rule 3 lets one bug PR close several), so one merge counts once for any of them. function mergedSince(since) { const merged = []; for (let page = 1; ; page++) { const batch = api( "GET", "pulls", ...["state=closed", "sort=updated", "direction=desc", "per_page=100", `page=${page}`].flatMap( (f) => ["-f", f], ), ); for (const p of batch) { if (!(Date.parse(p.merged_at ?? "") >= since)) continue; const bugs = new Set([ ...(/^bug\/(\d+)$/.exec(p.head.ref)?.slice(1) ?? []), ...[...(p.body ?? "").matchAll(CLOSES)].map((m) => m[1]), ]); if (bugs.size > 0) merged.push([...bugs]); } if (batch.length < 100 || Date.parse(batch.at(-1).updated_at) < since) return merged; } } function held(holds, how = "") { console.error(`fast-track: #${number} held: ${holds.join("; ")}.${how}`); process.exit(1); } function paged(at) { const all = []; for (let page = 1; ; page++) { const batch = api("GET", at, "-f", "per_page=100", "-f", `page=${page}`); all.push(...batch); if (batch.length < 100) return all; } } // why: a missing branch, label or account answers 404, which is an answer here, not a failure. function exists(at, method = "GET") { try { execFileSync("gh", ["api", "-X", method, `repos/${nwo}/${at}`], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], }); return true; } catch (error) { if (/404|Not Found/.test(String(error.stderr))) return false; throw error; } } function api(method, at, ...extra) { return JSON.parse( execFileSync("gh", ["api", "-X", method, `repos/${nwo}/${at}`, ...extra], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024, stdio: ["ignore", "pipe", "pipe"], }), ); }