#!/usr/bin/env node // `node .agents/scripts/pr-body-check.mjs [--draft-ok]`: the mechanical half of delivery // rules 12, 14 and 15 for a PR body, so no prose has to repeat them. It fails (exit 1, one // `pr-body: ...` line per defect) when: // // - the last line is not `*Written by an agent.*` (rule 12); // - there is no `## Review` section, or its first line is not the counts line // `Spec review: blocking, fixed, notes. Code review: ... Claims review: ...` // (`skipped` in place of the counts for a review the diff's kind left out) (rule 14); // - a line under `Rejected:` is not tagged `spec:`, `code:` or `claims:` (rule 14); // - the body says "spec needs" anywhere outside a code block (rule 15); // - on a / branch, `### Status` or `## Assumptions` is missing (coding rule 2); // - on a / branch, the Review counts line names a review that ran and the PR has no // comment headed `## Review ledger` (REVIEW.md, "Measurement"); checked only with a PR number. // The ledger's json block has no finding whose `outcome` is `open`, and the `## Review` // section has the line `Assessment: complete` (rule 15). The ledger's `Round : ` // lines are 40-hex SHAs with at most one round 1 and one round 2, round 1 first, each an // ancestor of the head (a warning when this clone lacks a SHA), a fix-check's line ending // with its trigger or nothing; at most one comment is headed `## Review notes` (REVIEW.md, // the cap). More than three Round lines is a warning that counts them by round and // trigger, and so is an `extension:` line over 1; // - `## Assumptions` holds lines other than `none` or `- Assumption: — ` (coding // rule 2); // - the prose runs over 200 words: everything outside code blocks, checklist lines and the // Status, Assumptions and Review sections (rule 12); // - with a PR number, on a / branch: a `## Done when` bullet of issue matches no // `- [ ]` or `- [x]` line of the body, or matches one that is not ticked. A line matches when // it holds at least half the bullet's content words (lowercased, camelCase split, punctuation // dropped, a trailing `s` dropped, stop words left out; words in code spans and parentheses // count only when nothing else is left), so it may reword the bullet or add `— `; and when every // other open sub-issue of the issue's parent is closed by a PR below this one in its stack // (or there is none), the same for the parent's `## Completion conditions` (coding rule 5); // - the diff's kind (`diff-kind.mjs` from the merge-base to the head) and the counts line // disagree: Code review skipped on a `full`, `large` or `tests-only` diff, or Spec review // skipped on a / branch but `refactor/` (REVIEW.md, "Routing"); a Claims review that ran on an // `adr-only` diff is a warning; // - on a `refactor/` branch (delivery rule 18): Spec review ran, or the body lacks the line // `Tests: before, after.` with m at least n, or a `Mutation:` line ending // `0 alive.`; // - a routine's branch changed what its routine may not: `chore/garden-comments-*` is // `comments-only`, `chore/audit-tests-*` and `chore/harden-tests-*` are `tests-only`; // - on a `bug/` branch with a PR number, the last triage report (a comment with `## Root // cause`) of bug or of another bug the body closes says `Seen on: device` or `Seen on: // tool output` (or `--seen-on ` says so), and the diff adds or changes no file under // `e2e/slow-*` or a `fixtures/` or `recordings/` folder of `src/`, and the body has no // `No hardware test: ` line (testing rule 9). // // The diff is read from this clone: a PR's base and head SHAs, or `--base --head `; // `--kind ` gives the kind instead. A PR whose SHAs this clone lacks gets a warning in // place of the diff checks. It warns (exit 0) when the Review section has no `Rounds: . // Tokens: M.` line. `--draft-ok` turns every failure into a warning, for the check that runs // while the PR is still a draft. `--body-file [--branch ]` reads a body from a file // instead of GitHub. The repo is GH_REPO, else the origin remote's owner/name. import { execFileSync } from "node:child_process"; import { readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { option, repository } from "./cli-args.mjs"; const BUDGET = 200; const HERE = dirname(fileURLToPath(import.meta.url)); // why: the check catches a line the PR never walked, not paraphrase; half the words is enough. const MATCH = 0.5; const STOP_WORDS = new Set( "a an the and or but nor of to in on at by for with from into onto as is are was were be been it its this that these those than then there their they them which who whom what when where how all any each every both either no not only so such too very can could may might must shall should will would do doe did has have had here our we you your one".split( " ", ), ); const REVIEW_COUNTS = /^Spec review: (?:\d+ blocking, \d+ fixed, \d+ notes?|skipped)\. Code review: (?:\d+ blocking, \d+ fixed, \d+ notes?|skipped)\. Claims review: (?:\d+ blocking, \d+ fixed, \d+ notes?|skipped)\.(?: Comment review: (?:\d+ blocking, \d+ fixed, \d+ notes?|skipped)\.)?\s*$/; const ROUNDS = /^Rounds: \d+\. Tokens: (?:[\d.]+M|n\/a)\.\s*$/m; const DELIVERY_BRANCH = /^(bug|feature|refactor|task)\/(\d+)$/; const SEEN_ON = /^Seen on: (device|tool output|fakes)\b/m; const HARDWARE_TEST = /^e2e\/slow-|^src\/(?:.+\/)?(?:fixtures|recordings)\//; const REFACTOR_BRANCH = /^refactor\/\d+$/; const ROUTINES = [ [/^chore\/garden-comments-/, "comments-only"], [/^chore\/(?:audit|harden)-tests-/, "tests-only"], ]; const args = process.argv.slice(2); const draftOk = args.includes("--draft-ok"); let body; let branch = ""; let prNumber = null; let pull = null; let diffBase = option("--base", args); let diffHead = option("--head", args); let mergeBaseRead; const fileAt = args.indexOf("--body-file"); if (fileAt !== -1) { body = readFileSync(args[fileAt + 1], "utf8"); const branchAt = args.indexOf("--branch"); if (branchAt !== -1) branch = args[branchAt + 1]; } else { const pr = args.find((a) => /^\d+$/.test(a)); if (pr === undefined) { console.error("usage: pr-body-check.mjs [--draft-ok] | --body-file [--branch ]"); process.exit(64); } // why: a cloud session's GitHub proxy refuses GraphQL, which `gh pr view` uses; repo-scoped // REST works. pull = api(`pulls/${pr}`); body = pull.body ?? ""; branch = pull.head.ref; prNumber = pr; diffHead = pull.head.sha ?? null; diffBase = pull.base?.sha ?? null; } body = body.replace(/\r/g, ""); const failures = []; const warnings = []; const prose = withoutCodeBlocks(body); const lines = prose.split("\n"); const last = body .split("\n") .map((l) => l.trim()) .filter((l) => l !== "") .at(-1) ?? ""; if (!/^[*_]Written by an agent\.[*_]$/.test(last)) { failures.push('the last line is not "*Written by an agent.*" (delivery rule 12)'); } const review = section(lines, /^## Review\s*$/); if (review === null) { failures.push("no `## Review` section (delivery rule 14)"); } else { const first = review.find((l) => l.trim() !== "") ?? ""; if (!REVIEW_COUNTS.test(first)) { failures.push( "the `## Review` section does not start with the counts line " + '"Spec review: blocking, fixed, notes. Code review: ... Claims review: ..." (delivery rule 14)', ); } if (!ROUNDS.test(review.join("\n"))) { warnings.push('the `## Review` section has no "Rounds: . Tokens: M." line'); } if (/^Assessment: incomplete\b/im.test(review.join("\n"))) { failures.push("review assessment is incomplete; resolve missing coverage before ready"); } else if (DELIVERY_BRANCH.test(branch) && !/^Assessment: complete\b/m.test(review.join("\n"))) { failures.push("the `## Review` section has no `Assessment: complete` line (delivery rule 15)"); } const at = review.findIndex((l) => /^Rejected:\s*$/.test(l)); if (at !== -1) { for (const l of review.slice(at + 1)) { if (!l.startsWith("- ")) continue; if (!/^- (spec|code|claims|comments): /.test(l)) { failures.push( `a Rejected line is not tagged spec:, code: or claims:: "${l.slice(0, 60)}" (delivery rule 14)`, ); } } } } if (/spec needs/i.test(prose)) { failures.push( 'the body says "spec needs": the spec must change before this PR merges (delivery rule 15)', ); } if (DELIVERY_BRANCH.test(branch)) { if (section(lines, /^### Status\s*$/) === null) failures.push("no `### Status` section"); if (section(lines, /^## Assumptions\s*$/) === null) { failures.push("no `## Assumptions` section (coding rule 2)"); } } for (const l of section(lines, /^## Assumptions\s*$/) ?? []) { if (l.trim() === "" || /^none\.?$/i.test(l.trim()) || /^- Assumption: .+ — .+/.test(l)) continue; failures.push( `an Assumptions line is not "none" or "- Assumption: — ": "${l.slice(0, 60)}" (coding rule 2)`, ); } // why: only a delivery PR (/) has a ledger; a routine's or a person's chore PR gets // the reviewer's findings comment instead. if ( prNumber !== null && DELIVERY_BRANCH.test(branch) && lines.some( (l) => REVIEW_COUNTS.test(l) && !/^(Spec|Code|Claims) review: skipped\. (Code|Claims) review: skipped\. Claims review: skipped\.$/.test( l, ), ) ) { const comments = api(`issues/${prNumber}/comments`, "-f", "per_page=100"); const ledger = comments.find((c) => /^## Review ledger\s*$/m.test(c.body ?? "")); if (ledger === undefined) { failures.push("no comment headed `## Review ledger` (REVIEW.md, Measurement)"); } else { checkLedger(ledger.body.replace(/\r/g, "")); } if (comments.filter((c) => /^## Review notes\s*$/m.test(c.body ?? "")).length > 1) { failures.push( "more than one comment is headed `## Review notes`; notes go out once (REVIEW.md)", ); } } if (prNumber !== null && DELIVERY_BRANCH.test(branch)) checkDoneWhen(); checkDiff(); const words = budgetedWords(lines); if (words > BUDGET) { failures.push( `${words} words outside the checklist, Status, Assumptions and Review; the budget is ${BUDGET}: cut it (delivery rule 12)`, ); } const gh = process.env.GITHUB_ACTIONS === "true"; for (const w of warnings) console.log(`${gh ? "::warning::" : "pr-body: warning: "}${w}`); for (const f of failures) { console.log(`${gh ? (draftOk ? "::warning::" : "::error::") : "pr-body: "}${f}`); } process.exit(failures.length > 0 && !draftOk ? 1 : 0); function checkLedger(text) { for (const f of ledgerFindings(text).filter((f) => f.outcome === "open")) { failures.push(`finding ${f.id} is open in the Review ledger (delivery rule 15)`); } const rounds = ledgerRounds(text); checkRoundOrder(rounds.map((r) => r.round).filter((r) => r !== "fix-check")); if (diffHead !== null) for (const r of rounds) checkAncestry(r); if (rounds.length > 3) { const names = rounds.map((r) => (r.round === "fix-check" ? (r.trigger ?? r.round) : r.round)); const counted = [...new Set(names)].map((n) => `${n} ${names.filter((x) => x === n).length}`); warnings.push(`the ledger has ${rounds.length} checks: ${counted.join(", ")}`); } const extension = Number(/^extension: (\d+)\s*$/m.exec(text)?.[1] ?? 0); if (extension > 1) { warnings.push( `the ledger has ${extension} review fix-checks past the cap; one is automatic, a maintainer allows the rest (REVIEW.md, the cap)`, ); } } function ledgerFindings(text) { const json = /```json\n([\s\S]*?)\n```/.exec(text); try { return JSON.parse(json?.[1] ?? "").findings ?? []; } catch { failures.push( "the `## Review ledger` comment has no json block that parses (REVIEW.md, Measurement)", ); return []; } } function ledgerRounds(text) { const rounds = []; for (const l of text.split("\n").filter((l) => l.startsWith("Round "))) { const round = /^Round (?:(1|2): ([0-9a-f]{40})|(fix-check): ([0-9a-f]{40})(?: (ci|merge|review|hardware))?)\s*$/.exec( l, ); if (round === null) { failures.push( `a ledger line is not "Round <1|2|fix-check>: ", a fix-check's with its trigger: "${l.slice(0, 60)}"`, ); } else { rounds.push({ round: round[1] ?? round[3], sha: round[2] ?? round[4], trigger: round[5] }); } } return rounds; } function checkRoundOrder(order) { const twice = (r) => order.filter((x) => x === r).length > 1; if (twice("1") || twice("2")) { failures.push("the ledger has more than one round 1 or round 2 (REVIEW.md, the cap)"); } else if (order.length > 0 && order[0] !== "1") { failures.push("the ledger's first round is not round 1 (REVIEW.md, the cap)"); } } function checkAncestry({ round, sha }) { if (!hasCommit(sha) || !hasCommit(diffHead)) { warnings.push( `round ${round}'s ${sha.slice(0, 9)} or the head is not in this clone; its ancestry is unchecked`, ); } else if (!git(["merge-base", "--is-ancestor", sha, diffHead], true)) { failures.push(`round ${round}'s ${sha.slice(0, 9)} is not an ancestor of the head`); } } function checkDoneWhen() { const number = Number(DELIVERY_BRANCH.exec(branch)[2]); const items = checklistItems(); walkLines(items, api(`issues/${number}`).body ?? "", "Done when", `#${number}`); const parent = apiOrNull(`issues/${number}/parent`); if (parent !== null && closesLastSubIssue(parent.number, number)) { walkLines(items, parent.body ?? "", "Completion conditions", `#${parent.number}`); } } // why: a checklist line may wrap onto indented lines; a blank or unindented line ends it. function checklistItems() { const checklist = []; for (const l of lines) { const item = /^\s*- \[([ xX])\] (.*)$/.exec(l); if (item !== null) checklist.push({ ticked: item[1] !== " ", text: item[2] }); else if (/^\s+\S/.test(l) && checklist.at(-1) != null) checklist.at(-1).text += ` ${l.trim()}`; else if (l.trim() === "" || !/^\s/.test(l)) checklist.push(null); } return checklist .filter((c) => c !== null) .map((c) => ({ ...c, words: new Set(contentWords(c.text)) })); } function walkLines(items, text, heading, from) { for (const bullet of bullets(text, heading)) { const line = matchingLine(items, bullet); const quoted = `"${bullet.slice(0, 50)}"`; if (line === undefined) { failures.push( `${from}'s ${heading} line ${quoted} is not a checklist line of the body (coding rule 5)`, ); } else if (!line.ticked) { failures.push(`${from}'s ${heading} line ${quoted} is not ticked (coding rule 5)`); } } } function matchingLine(items, bullet) { const prose = bullet.replace(/`[^`]*`/g, " ").replace(/\([^)]*\)/g, " "); const wanted = [...new Set(contentWords(contentWords(prose).length > 0 ? prose : bullet))]; const share = (c) => wanted.filter((w) => c.words.has(w)).length / Math.max(1, wanted.length); const best = items.reduce((a, c) => (a === undefined || share(c) > share(a) ? c : a), undefined); return best !== undefined && share(best) >= MATCH ? best : undefined; } // why: in a stack every other open sub-issue must be closed by a PR below this one. function closesLastSubIssue(parent, number) { const open = api(`issues/${parent}/sub_issues`, "-f", "per_page=100") .filter((i) => i.state === "open" && i.number !== number) .map((i) => i.number); if (open.length === 0) return true; const stack = apiOrNull(`stacks?pull_request=${prNumber}`) ?? []; const members = (stack.find((x) => x.open)?.pull_requests ?? []).map((p) => p.number); const below = members.slice(0, Math.max(0, members.indexOf(Number(prNumber)))); const closed = new Set(below.flatMap((m) => closes(api(`pulls/${m}`).body ?? ""))); return open.every((n) => closed.has(n)); } function checkDiff() { const counts = lines.find((l) => REVIEW_COUNTS.test(l)) ?? ""; const refactor = REFACTOR_BRANCH.test(branch); if (DELIVERY_BRANCH.test(branch) && !refactor && counts.startsWith("Spec review: skipped.")) { failures.push( "Spec review is skipped on a branch whose issue is its spec (REVIEW.md, Routing)", ); } if (refactor) checkRefactor(counts); const kind = option("--kind", args) ?? kindFromGit(); if (kind !== null) checkKind(kind, counts); checkSeenOn(); } function checkSeenOn() { const flag = option("--seen-on", args); const seen = (flag !== null ? [{ bug: "the bug", seen: flag }] : seenOnFromTriage()).filter( (s) => s.seen === "device" || s.seen === "tool output", ); if (seen.length === 0 || /^No hardware test: \S/m.test(prose)) return; const said = seen.map((s) => `${s.bug} was seen on ${s.seen}`).join(", "); const files = changedFiles(); if (files === null) { warnings.push(`${said}; the diff is unread, so its hardware test is unchecked`); } else if (!files.some((f) => HARDWARE_TEST.test(f))) { failures.push( `${said}: add a test under e2e/slow-*, a recorded fixture under a fixtures/ or recordings/ ` + 'folder of src/, or a line "No hardware test: " (testing rule 9)', ); } } // why: a bundled PR (coding rule 3) closes other bugs too; each bug's last report wins, as a // later one supersedes the report above it. function seenOnFromTriage() { const bug = /^bug\/(\d+)$/.exec(branch); if (prNumber === null || bug === null) return []; const bugs = [...new Set([Number(bug[1]), ...closes(prose)])]; return bugs.map((n) => { const comments = apiOrNull(`issues/${n}/comments`, "-f", "per_page=100") ?? []; const report = comments.filter((c) => /^## Root cause\s*$/m.test(c.body ?? "")).at(-1); return { bug: `#${n}`, seen: SEEN_ON.exec((report?.body ?? "").replace(/\r/g, ""))?.[1] }; }); } // why: a deleted fixture proves nothing, so deletions are left out. function changedFiles() { const base = mergeBase(); if (base === null) return null; return git(["diff", "--name-only", "--diff-filter=d", base, diffHead]) .split("\n") .filter((f) => f !== ""); } function checkRefactor(counts) { if (counts !== "" && !counts.startsWith("Spec review: skipped.")) { failures.push("a refactor gets no Spec review (delivery rule 18)"); } const tests = lines.map((l) => /^Tests: (\d+) before, (\d+) after\.$/.exec(l)).find(Boolean); if (tests === undefined) { failures.push("no `Tests: before, after.` line on a refactor (delivery rule 18)"); } else if (Number(tests[2]) < Number(tests[1])) { failures.push( `the refactor lost tests: ${tests[1]} before, ${tests[2]} after (delivery rule 18)`, ); } if (!lines.some((l) => /^Mutation: .*\b0 alive\.$/.test(l))) { failures.push("no `Mutation: ... 0 alive.` line on a refactor (delivery rule 18)"); } } // why: null when the diff cannot be read: no SHAs given, or this clone lacks them. function kindFromGit() { const base = mergeBase(); if (base === null) return null; return execFileSync(process.execPath, [join(HERE, "diff-kind.mjs"), base, diffHead], { encoding: "utf8", }).trim(); } // why: read once, so a clone that lacks the SHAs warns once for every check of the diff. function mergeBase() { if (mergeBaseRead !== undefined) return mergeBaseRead; mergeBaseRead = null; if (diffBase === null || diffHead === null) return null; if (!hasCommit(diffBase) || !hasCommit(diffHead)) { warnings.push( "the PR's base or head is not in this clone (git fetch origin); its diff is unchecked", ); return null; } mergeBaseRead = git(["merge-base", diffBase, diffHead]).trim(); return mergeBaseRead; } function checkKind(kind, counts) { const routine = ROUTINES.find(([re]) => re.test(branch)); if (routine !== undefined && kind !== routine[1]) { failures.push(`this routine's PR must be ${routine[1]}, and its diff is ${kind}`); } if (skipsNeededCodeReview(kind, counts, routine)) { failures.push(`Code review is skipped on a ${kind} diff (REVIEW.md, Routing)`); } if (kind === "adr-only" && counts !== "" && !/\bClaims review: skipped\./.test(counts)) { warnings.push("a Claims review ran on an adr-only diff, which needs the spec review alone"); } } // why: the review protocol routes a delivery PR's and a routine's lenses; a person's chore PR // has no review round to route, and merges on the maintainer's say. function skipsNeededCodeReview(kind, counts, routine) { const reviewed = DELIVERY_BRANCH.test(branch) || routine !== undefined; const needsCode = ["full", "large", "tests-only"].includes(kind); return reviewed && needsCode && /\bCode review: skipped\./.test(counts); } function bullets(text, heading) { const all = withoutCodeBlocks(text.replace(/\r/g, "")).split("\n"); const out = []; for (const l of section(all, new RegExp(`^## ${heading}\\s*$`)) ?? []) { if (/^\s*[-*] /.test(l)) out.push(l.replace(/^\s*[-*] /, "")); else if (/^\s+\S/.test(l) && out.length > 0) out[out.length - 1] += ` ${l.trim()}`; } return out; } function contentWords(text) { return text .replace(/`/g, "") .replace(/([a-z\d])([A-Z])/g, "$1 $2") .toLowerCase() .split(/[^a-z\d]+/) .map((w) => (w.length > 3 && w.endsWith("s") ? w.slice(0, -1) : w)) .filter((w) => w !== "" && !STOP_WORDS.has(w)); } function closes(text) { return [ ...text.matchAll( /\b(?:close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)\s+#(\d+)/gi, ), ].map((m) => Number(m[1])); } function api(at, ...extra) { return JSON.parse( execFileSync("gh", ["api", "-X", "GET", `repos/${repository()}/${at}`, ...extra], { encoding: "utf8", maxBuffer: 64 * 1024 * 1024, stdio: ["ignore", "pipe", "pipe"], }), ); } // why: GitHub answers 404 for an issue with no parent and a PR in no stack. function apiOrNull(at, ...extra) { try { return api(at, ...extra); } catch { return null; } } function git(argv, status = false) { try { const out = execFileSync("git", argv, { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], }); return status ? true : out; } catch (error) { if (status) return false; throw error; } } function hasCommit(sha) { return git(["cat-file", "-e", `${sha}^{commit}`], true); } function withoutCodeBlocks(text) { let inside = false; return text .split("\n") .map((l) => { if (/^\s*(```|~~~)/.test(l)) { inside = !inside; return ""; } return inside ? "" : l; }) .join("\n"); } function section(all, heading) { const start = all.findIndex((l) => heading.test(l)); if (start === -1) return null; const level = /^(#+)/.exec(all[start])[1].length; const out = []; for (const l of all.slice(start + 1)) { const h = /^(#+)\s/.exec(l); if (h !== null && h[1].length <= level) break; out.push(l); } return out; } function budgetedWords(all) { let skipping = 0; let count = 0; for (const l of all) { const h = /^(#+)\s+(.*)$/.exec(l); if (h !== null) { const level = h[1].length; if (skipping > 0 && level <= skipping) skipping = 0; if ( skipping === 0 && /^(Status|Assumptions|Review|Mutants?\b.*|Alive mutants|Review notes)/i.test(h[2]) ) { skipping = level; } continue; } if (skipping > 0 || /^\s*- \[[ x]\]/i.test(l)) continue; count += l.split(/\s+/).filter((w) => /\w/.test(w)).length; } return count; }