diff --git a/.github/scripts/maintenance-audit-lane-runs/collect.mjs b/.github/scripts/maintenance-audit-lane-runs/collect.mjs new file mode 100644 index 00000000000..4abb361627b --- /dev/null +++ b/.github/scripts/maintenance-audit-lane-runs/collect.mjs @@ -0,0 +1,271 @@ +// The lane sweep's collect job: reads the last day's runs and the open PRs +// through the REST API with the job's read-only token, and writes one data +// file per unit of model work, plus the open items the judge checks for +// coverage. No model. Stops gathering at its call budget and says so. +import { randomUUID } from "node:crypto"; +import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import process from "node:process"; +import { pathToFileURL } from "node:url"; + +export const MAX_UNITS = 8; +const A_WORKFLOWS = 4; +const D_WORKFLOWS = 2; +const PRS_PER_UNIT = 10; +const RUNS_PER_UNIT = 5; +const FAILED_JOBS_PER_RUN = 3; +const LOG_LINES = 60; +const ANSI = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*[A-Za-z]`, "g"); +const LANE_WORKFLOW = /^\.github\/workflows\/(pr|work|intake)-[a-z0-9-]+\.ya?ml$/; + +export function createBudget(limit) { + return { + used: 0, + limit, + spend() { + if (this.used >= limit) throw new Error(`call budget of ${limit} reached`); + this.used += 1; + }, + }; +} + +const ranked = (runs, keep) => { + const groups = new Map(); + for (const r of runs) groups.set(r.path, [...(groups.get(r.path) ?? []), r.id]); + return [...groups.entries()] + .sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0])) + .slice(0, keep) + .map(([workflow, runIds]) => ({ workflow, runIds })); +}; + +// Units in order A, D, C, at most MAX_UNITS; ids are slice letter plus position. +export function selectUnits({ repo, runs, pulls, trusted }) { + const own = runs.filter((r) => r.head_repository?.full_name === repo); + const units = [ + ...ranked( + own.filter((r) => r.conclusion === "failure"), + A_WORKFLOWS, + ).map((u) => ({ slice: "A", ...u })), + ...ranked( + own.filter((r) => LANE_WORKFLOW.test(r.path)), + D_WORKFLOWS, + ).map((u) => ({ slice: "D", ...u })), + ]; + const listed = pulls.filter((p) => trusted.has(String(p.user?.id))); + for (let i = 0; i < listed.length; i += PRS_PER_UNIT) { + units.push({ + slice: "C", + pulls: listed.slice(i, i + PRS_PER_UNIT).map((p) => p.number), + }); + } + return units.slice(0, MAX_UNITS).map((u, i) => ({ id: `${u.slice.toLowerCase()}${i + 1}`, ...u })); +} + +// The last `count` lines, with ANSI codes and the runner's timestamp prefix removed. +export function tailLog(text, count = LOG_LINES) { + return String(text) + .split("\n") + .map((line) => + line + .replace(ANSI, "") + .replace(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z ?/, "") + .trimEnd(), + ) + .filter((line, i, all) => line !== "" || i < all.length - 1) + .slice(-count) + .join("\n"); +} + +function client({ api, token, budget }) { + const call = async (path, { text = false } = {}) => { + budget.spend(); + const response = await fetch(`${api}${path}`, { + headers: { + accept: "application/vnd.github+json", + authorization: `Bearer ${token}`, + "x-github-api-version": "2022-11-28", + }, + }); + if (!response.ok) throw new Error(`GET ${path}: HTTP ${response.status}`); + return text ? response.text() : response.json(); + }; + return { call }; +} + +const seconds = (r) => Math.round((new Date(r.updated_at) - new Date(r.run_started_at ?? r.created_at)) / 1000); + +async function detailA(gh, repo, unit) { + const runs = []; + for (const id of unit.runIds.slice(0, RUNS_PER_UNIT)) { + const { jobs } = await gh.call(`/repos/${repo}/actions/runs/${id}/jobs?filter=latest&per_page=100`); + const failed = []; + for (const job of jobs.filter((j) => j.conclusion === "failure").slice(0, FAILED_JOBS_PER_RUN)) { + const checkRunId = /\/check-runs\/(\d+)$/.exec(job.check_run_url ?? "")?.[1]; + const annotations = checkRunId + ? (await gh.call(`/repos/${repo}/check-runs/${checkRunId}/annotations?per_page=50`)).map((a) => ({ + level: a.annotation_level, + title: a.title, + message: String(a.message).slice(0, 500), + path: a.path, + line: a.start_line, + })) + : []; + let log; + try { + log = tailLog( + await gh.call(`/repos/${repo}/actions/jobs/${job.id}/logs`, { + text: true, + }), + ); + } catch (error) { + if (/call budget/.test(error.message)) throw error; + log = `log unavailable (${error.message})`; + } + failed.push({ + id: job.id, + name: job.name, + failed_steps: (job.steps ?? []).filter((s) => s.conclusion === "failure").map((s) => s.name), + annotations, + log_tail: log, + }); + } + runs.push({ + id, + url: `https://github.com/${repo}/actions/runs/${id}`, + failed_jobs: failed, + }); + } + return { runs }; +} + +async function detailD(gh, repo, unit, allRuns) { + const mine = allRuns.filter((r) => unit.runIds.includes(r.id)); + const conclusions = {}; + for (const r of mine) conclusions[r.conclusion ?? r.status] = (conclusions[r.conclusion ?? r.status] ?? 0) + 1; + const durations = mine.map(seconds).sort((a, b) => a - b); + const sampled = []; + for (const r of mine.filter((x) => x.conclusion === "success").slice(0, 3)) { + const { jobs } = await gh.call(`/repos/${repo}/actions/runs/${r.id}/jobs?filter=latest&per_page=100`); + sampled.push({ + id: r.id, + url: `https://github.com/${repo}/actions/runs/${r.id}`, + jobs: jobs.map((j) => ({ + name: j.name, + conclusion: j.conclusion, + seconds: Math.round((new Date(j.completed_at) - new Date(j.started_at)) / 1000), + failed_steps_in_green_job: (j.steps ?? []).filter((s) => s.conclusion === "failure").map((s) => s.name), + })), + }); + } + return { + runs: mine.length, + conclusions, + reruns: mine.filter((r) => r.run_attempt > 1).length, + seconds_median: durations[Math.floor(durations.length / 2)] ?? null, + seconds_max: durations.at(-1) ?? null, + events: [...new Set(mine.map((r) => r.event))], + sampled_success_runs: sampled, + }; +} + +async function detailC(gh, repo, unit, pullsByNumber) { + const pulls = []; + for (const number of unit.pulls) { + const p = pullsByNumber.get(number); + const { check_runs: checks } = await gh.call(`/repos/${repo}/commits/${p.head.sha}/check-runs?per_page=100`); + const counts = {}; + for (const c of checks) counts[c.conclusion ?? c.status] = (counts[c.conclusion ?? c.status] ?? 0) + 1; + pulls.push({ + number, + title: p.title, + draft: p.draft, + labels: p.labels.map((l) => l.name), + created_at: p.created_at, + updated_at: p.updated_at, + head_sha: p.head.sha, + checks: counts, + failing_checks: checks.filter((c) => c.conclusion === "failure").map((c) => c.name), + }); + } + return { pulls }; +} + +function writeOutput(name, value) { + const delimiter = `ghadelimiter_${randomUUID()}`; + appendFileSync(process.env.GITHUB_OUTPUT, `${name}<<${delimiter}\n${value}\n${delimiter}\n`); +} + +async function main() { + const env = process.env; + const repo = env.REPOSITORY; + const out = env.OUT_DIR; + const budget = createBudget(Number(env.CALL_BUDGET ?? 350)); + const gh = client({ + api: env.GITHUB_API_URL, + token: env.GITHUB_TOKEN, + budget, + }); + const list = JSON.parse(readFileSync(env.TRUSTED_ACTORS_PATH, "utf8")); + const trusted = new Set(list.actors.map((a) => String(a.id))); + if (/^[1-9][0-9]*$/.test(env.LANES_APP_ID ?? "")) trusted.add(env.LANES_APP_ID); + const since = new Date(Date.now() - Number(env.WINDOW_HOURS ?? 24) * 3_600_000).toISOString(); + + const runs = []; + for (let page = 1; page <= 20; page += 1) { + const batch = await gh.call(`/repos/${repo}/actions/runs?created=%3E%3D${since}&per_page=100&page=${page}`); + runs.push(...batch.workflow_runs.filter((r) => r.path !== ".github/workflows/maintenance-audit-lane-runs.yml")); + if (batch.workflow_runs.length < 100) break; + } + const pulls = await gh.call(`/repos/${repo}/pulls?state=open&per_page=100`); + const pullsByNumber = new Map(pulls.map((p) => [p.number, p])); + const open = []; + for (let page = 1; page <= 3; page += 1) { + const batch = await gh.call(`/repos/${repo}/issues?state=open&per_page=100&page=${page}`); + open.push(...batch.filter((i) => trusted.has(String(i.user?.id)))); + if (batch.length < 100) break; + } + + const units = selectUnits({ repo, runs, pulls, trusted }); + mkdirSync(join(out, "units"), { recursive: true }); + const written = []; + let truncated = null; + for (const unit of units) { + let detail; + try { + if (unit.slice === "A") detail = await detailA(gh, repo, unit); + else if (unit.slice === "D") detail = await detailD(gh, repo, unit, runs); + else detail = await detailC(gh, repo, unit, pullsByNumber); + } catch (error) { + if (!/call budget/.test(error.message)) throw error; + truncated = `stopped at unit ${unit.id}: ${error.message}`; + break; + } + writeFileSync( + join(out, "units", `${unit.id}.json`), + `${JSON.stringify({ ...unit, window_start: since, ...detail }, null, 2)}\n`, + ); + written.push(unit.id); + } + writeFileSync( + join(out, "open-items.json"), + `${JSON.stringify( + open.map((i) => ({ + number: i.number, + kind: i.pull_request ? "pr" : "issue", + title: i.title, + })), + null, + 2, + )}\n`, + ); + console.log( + `collect: ${runs.length} runs, ${pulls.length} open PRs, units ${written.join(",") || "none"}, ${budget.used} calls`, + ); + if (truncated) console.log(`::warning::collect ${truncated}`); + writeOutput("units", JSON.stringify(written)); + writeOutput("calls", String(budget.used)); +} + +if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { + await main(); +} diff --git a/.github/scripts/maintenance-audit-lane-runs/collect.test.mjs b/.github/scripts/maintenance-audit-lane-runs/collect.test.mjs new file mode 100644 index 00000000000..cb41fc86929 --- /dev/null +++ b/.github/scripts/maintenance-audit-lane-runs/collect.test.mjs @@ -0,0 +1,114 @@ +// The collector's unit rules from docs/conventions/pr-pipeline/README.md, +// "The lane sweep": slice A takes the four workflows with the most failed +// runs, slice D the two lane workflows with the most runs, slice C open PRs by +// listed authors in tens, fork runs never count, at most 8 units, and the +// collector stops at its call budget. +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { createBudget, selectUnits, tailLog } from "./collect.mjs"; + +const run = (id, path, conclusion, over = {}) => ({ + id, + path, + conclusion, + head_repository: { full_name: "o/r" }, + ...over, +}); + +test("slice A takes the four workflows with the most failed same-repository runs", () => { + const runs = [ + ...[1, 2, 3].map((id) => run(id, ".github/workflows/a.yml", "failure")), + ...[4, 5].map((id) => run(id, ".github/workflows/b.yml", "failure")), + run(6, ".github/workflows/c.yml", "failure"), + run(7, ".github/workflows/d.yml", "failure"), + run(8, ".github/workflows/e.yml", "success"), + ...[9, 10, 11, 12].map((id) => + run(id, ".github/workflows/fork.yml", "failure", { + head_repository: { full_name: "x/r" }, + }), + ), + ]; + const units = selectUnits({ + repo: "o/r", + runs, + pulls: [], + trusted: new Set(), + }); + assert.deepEqual( + units.filter((u) => u.slice === "A").map((u) => [u.workflow, u.runIds]), + [ + [".github/workflows/a.yml", [1, 2, 3]], + [".github/workflows/b.yml", [4, 5]], + [".github/workflows/c.yml", [6]], + [".github/workflows/d.yml", [7]], + ], + ); +}); + +test("slice D takes the two lane workflows with the most runs, successes included", () => { + const runs = [ + ...[1, 2, 3].map((id) => run(id, ".github/workflows/pr-merge.yml", "success")), + ...[4, 5].map((id) => run(id, ".github/workflows/work-unblock.yml", "skipped")), + run(6, ".github/workflows/pr-refine.yml", "failure"), + ...[7, 8, 9, 10].map((id) => run(id, ".github/workflows/release-publish-plugins.yml", "success")), + ]; + const units = selectUnits({ + repo: "o/r", + runs, + pulls: [], + trusted: new Set(), + }); + assert.deepEqual( + units.filter((u) => u.slice === "D").map((u) => [u.workflow, u.runIds.length]), + [ + [".github/workflows/pr-merge.yml", 3], + [".github/workflows/work-unblock.yml", 2], + ], + ); +}); + +test("slice C takes listed authors' open PRs in tens, and units stop at 8", () => { + const pulls = Array.from({ length: 25 }, (_, i) => ({ + number: i + 1, + user: { id: i % 5 === 0 ? 99 : 1 }, + })); + const runs = Array.from({ length: 6 }, (_, i) => run(i + 1, `.github/workflows/w${i}.yml`, "failure")); + const units = selectUnits({ + repo: "o/r", + runs, + pulls, + trusted: new Set(["1"]), + }); + assert.deepEqual( + units.filter((u) => u.slice === "C").map((u) => u.pulls.length), + [10, 10], + ); + assert.equal(units.length, 6); + assert.ok(units.every((u, i) => u.id === `${u.slice.toLowerCase()}${i + 1}`)); + const many = selectUnits({ + repo: "o/r", + runs: [...runs, ...[1, 2, 3].map((id) => run(100 + id, `.github/workflows/pr-x${id}.yml`, "success"))], + pulls: Array.from({ length: 40 }, (_, i) => ({ + number: i + 1, + user: { id: 1 }, + })), + trusted: new Set(["1"]), + }); + assert.equal(many.length, 8); +}); + +test("the budget refuses the call past its limit", () => { + const budget = createBudget(2); + budget.spend(); + budget.spend(); + assert.throws(() => budget.spend(), /call budget of 2/); + assert.equal(budget.used, 2); +}); + +test("a log tail keeps the last lines without ANSI codes or timestamps", () => { + const log = ["2026-10-17T21:13:05.1234567Z one", "2026-10-17T21:13:06.0000000Z \u001b[31mtwo\u001b[0m", "three"].join( + "\n", + ); + assert.equal(tailLog(log, 2), "two\nthree"); +}); diff --git a/.github/scripts/maintenance-audit-lane-runs/file-findings.mjs b/.github/scripts/maintenance-audit-lane-runs/file-findings.mjs new file mode 100644 index 00000000000..0ada2cff2b6 --- /dev/null +++ b/.github/scripts/maintenance-audit-lane-runs/file-findings.mjs @@ -0,0 +1,170 @@ +// The lane sweep's file job: checks every claim against main, fingerprints +// each finding, reads the issues earlier sweeps filed, and files, comments or +// skips per planFiling. No model. Writes with the job's own token, one call at +// a time, 1 s apart, within its call budget. +import { appendFileSync, readdirSync, readFileSync } from "node:fs"; +import { join } from "node:path"; +import process from "node:process"; +import { setTimeout as delay } from "node:timers/promises"; +import { pathToFileURL } from "node:url"; + +import { createBudget } from "./collect.mjs"; +import { + checkClaim, + fingerprint, + inertTitle, + planFiling, + readFingerprint, + renderBody, + validateFinding, +} from "./findings.mjs"; + +export const FILER_LOGIN = "github-actions[bot]"; +const LABELS = ["needs-triage", "source: lane-sweep"]; + +export function prepareItems({ findingsDir, baseDir }) { + const items = []; + const dropped = []; + for (const entry of readdirSync(findingsDir).sort()) { + let data; + try { + data = JSON.parse(readFileSync(join(findingsDir, entry, "findings.json"), "utf8")); + } catch { + dropped.push({ unit: entry, title: null, reason: "unreadable" }); + continue; + } + for (const finding of Array.isArray(data.findings) ? data.findings : []) { + const bad = validateFinding(finding); + if (bad.length > 0) { + dropped.push({ + unit: data.unit, + title: String(finding?.title ?? ""), + reason: bad.join(","), + }); + continue; + } + const failed = finding.evidence + .map((claim) => [claim, checkClaim(baseDir, claim)]) + .find(([, result]) => !result.ok); + if (failed) { + dropped.push({ + unit: data.unit, + title: finding.title, + reason: `${failed[0].path}:${failed[0].line} ${failed[1].reason}`, + }); + continue; + } + items.push({ fingerprint: fingerprint(finding), finding }); + } + } + return { items, dropped }; +} + +// Model text in the step summary: one line, no code spans, links, mentions +// or HTML. +const inertLine = (text) => inertTitle(String(text ?? "").replace(/[\r\n]+/g, " ").replace(/`/g, "")); + +export const droppedLine = (d) => `- dropped (${d.unit}) ${inertLine(d.title)}: ${inertLine(d.reason)}`; + +// Each write is its own attempt: a failed one is recorded and the rest still +// run, so one rejected title cannot stop the findings after it. +export async function writeAll({ plan, call, repo, labels, runUrl, wait = () => delay(1000) }) { + const filed = []; + const commented = []; + const failed = []; + for (const { fingerprint: fp, finding, regressionOf } of plan.file) { + await wait(); + try { + const issue = await call("POST", `/repos/${repo}/issues`, { + title: inertTitle(finding.title), + body: renderBody(finding, fp, runUrl, regressionOf), + labels, + }); + filed.push(issue.number); + } catch (error) { + failed.push({ target: fp, error: error.message }); + } + } + for (const { number } of plan.comment) { + await wait(); + try { + await call("POST", `/repos/${repo}/issues/${number}/comments`, { + body: `Seen again by the lane sweep in [this run](${runUrl}).`, + }); + commented.push(number); + } catch (error) { + failed.push({ target: `#${number}`, error: error.message }); + } + } + return { filed, commented, failed }; +} + +async function main() { + const env = process.env; + const repo = env.REPOSITORY; + const budget = createBudget(Number(env.CALL_BUDGET ?? 50)); + const call = async (method, path, body) => { + budget.spend(); + const response = await fetch(`${env.GITHUB_API_URL}${path}`, { + method, + headers: { + accept: "application/vnd.github+json", + authorization: `Bearer ${env.GITHUB_TOKEN}`, + "x-github-api-version": "2022-11-28", + ...(body ? { "content-type": "application/json" } : {}), + }, + body: body ? JSON.stringify(body) : undefined, + }); + if (response.status === 404 && method === "GET") return null; + if (!response.ok) throw new Error(`${method} ${path}: HTTP ${response.status}`); + return response.json(); + }; + + const { items, dropped } = prepareItems({ + findingsDir: env.FINDINGS_DIR, + baseDir: env.BASE_DIR, + }); + const existing = []; + for (let page = 1; page <= 10; page += 1) { + const batch = await call( + "GET", + `/repos/${repo}/issues?creator=${encodeURIComponent(FILER_LOGIN)}&state=all&per_page=100&page=${page}`, + ); + existing.push(...batch.filter((i) => !i.pull_request && readFingerprint(i.body))); + if (batch.length < 100) break; + } + const openItems = JSON.parse(readFileSync(env.OPEN_ITEMS_PATH, "utf8")); + const plan = planFiling({ + items, + existing, + openNumbers: new Set(openItems.map((i) => i.number)), + now: new Date(), + }); + const labels = []; + for (const name of LABELS) { + if (await call("GET", `/repos/${repo}/labels/${encodeURIComponent(name)}`)) labels.push(name); + } + const runUrl = `${env.GITHUB_SERVER_URL}/${repo}/actions/runs/${env.GITHUB_RUN_ID}`; + const { filed, commented, failed } = await writeAll({ plan, call, repo, labels, runUrl }); + const lines = [ + "### Lane sweep filing", + "", + `Filed: ${filed.map((n) => `#${n}`).join(", ") || "none"}. Commented: ${commented.map((n) => `#${n}`).join(", ") || "none"}. Labels applied: ${labels.join(", ") || "none"}. Calls: ${budget.used}.`, + "", + ...failed.map((f) => `- failed ${f.target}: ${f.error}`), + ...plan.skip.map((s) => `- skipped ${s.fingerprint}: ${s.reason}`), + ...dropped.map(droppedLine), + ]; + appendFileSync(env.GITHUB_STEP_SUMMARY, `${lines.join("\n")}\n`); + console.log( + `file-findings: filed ${filed.length}, commented ${commented.length}, failed ${failed.length}, skipped ${plan.skip.length}, dropped ${dropped.length}`, + ); + if (failed.length > 0) { + console.log(`::error::${failed.length} lane sweep write(s) failed; see the step summary`); + process.exitCode = 1; + } +} + +if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { + await main(); +} diff --git a/.github/scripts/maintenance-audit-lane-runs/file-findings.test.mjs b/.github/scripts/maintenance-audit-lane-runs/file-findings.test.mjs new file mode 100644 index 00000000000..f9412c95a28 --- /dev/null +++ b/.github/scripts/maintenance-audit-lane-runs/file-findings.test.mjs @@ -0,0 +1,107 @@ +// The judge's output is untrusted: write-findings keeps at most three +// well-formed findings and names what it dropped; the filer drops any finding +// with a failed claim before planning, and stamps each kept one with its +// fingerprint. +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test } from "node:test"; + +import { droppedLine, prepareItems, writeAll } from "./file-findings.mjs"; +import { parseJudgeOutput } from "./write-findings.mjs"; + +const finding = (over = {}) => ({ + slice: "D", + title: "fix(ci): skip the dispatch runner for a skipped review", + problem: "p", + root_cause: "r", + proposed_fix: "f", + workflow: ".github/workflows/pr-feedback-dispatch.yml", + job: "dispatch", + signature: "skipped upstream run started a runner", + evidence: [{ path: "a.yml", line: 1, quote: "if: true" }], + covered_by: null, + ...over, +}); + +test("the judge's output keeps at most three well-formed findings", () => { + const text = JSON.stringify({ + findings: [finding(), finding({ slice: "Z" }), finding(), finding(), finding()], + }); + const { findings, dropped } = parseJudgeOutput(text); + assert.equal(findings.length, 3); + assert.deepEqual(dropped, [ + { index: 1, reason: "slice" }, + { index: 4, reason: "over-limit" }, + ]); + assert.deepEqual(parseJudgeOutput("not json"), { + findings: [], + dropped: [{ index: null, reason: "unparsable" }], + }); + assert.deepEqual(parseJudgeOutput('{"findings": 3}').dropped, [{ index: null, reason: "unparsable" }]); +}); + +test("a finding with any failed claim is dropped before planning", (t) => { + const base = mkdtempSync(join(tmpdir(), "lane-sweep-file-")); + writeFileSync(join(base, "a.yml"), "if: true\n"); + const dir = mkdtempSync(join(tmpdir(), "lane-sweep-findings-")); + t.after(() => { + rmSync(base, { recursive: true, force: true }); + rmSync(dir, { recursive: true, force: true }); + }); + mkdirSync(join(dir, "findings-d1")); + writeFileSync( + join(dir, "findings-d1", "findings.json"), + JSON.stringify({ + unit: "d1", + findings: [ + finding(), + finding({ + evidence: [ + { path: "a.yml", line: 1, quote: "if: true" }, + { path: "a.yml", line: 1, quote: "if: false" }, + ], + }), + ], + }), + ); + const { items, dropped } = prepareItems({ findingsDir: dir, baseDir: base }); + assert.equal(items.length, 1); + assert.match(items[0].fingerprint, /^[0-9a-f]{16}$/); + assert.deepEqual(dropped, [{ unit: "d1", title: finding().title, reason: "a.yml:1 quote-not-on-line" }]); +}); + +test("a dropped line renders the title and reason inert on one line", () => { + const line = droppedLine({ + unit: "a1", + title: "fix: `x` @octocat", + reason: "[approve here](https://evil.example)\n## Action required `y` @octocat:1 bad-path", + }); + assert.equal(line.split("\n").length, 1); + assert.doesNotMatch(line, /[<`]|(?@​octocat<\/b>: /); +}); + +test("one failed write is recorded and the rest still file", async () => { + const calls = []; + const call = async (method, path, body) => { + calls.push(path); + if (body?.title === "fix: rejected") throw new Error(`${method} ${path}: HTTP 422`); + return { number: 100 + calls.length }; + }; + const plan = { + file: [ + { fingerprint: "aaaa", finding: finding({ title: "fix: rejected" }), regressionOf: null }, + { fingerprint: "bbbb", finding: finding(), regressionOf: null }, + ], + comment: [{ number: 7 }], + }; + const result = await writeAll({ plan, call, repo: "o/r", labels: [], runUrl: "u", wait: async () => {} }); + assert.equal(calls.length, 3); + assert.deepEqual(result, { + filed: [102], + commented: [7], + failed: [{ target: "aaaa", error: "POST /repos/o/r/issues: HTTP 422" }], + }); +}); diff --git a/.github/scripts/maintenance-audit-lane-runs/findings.mjs b/.github/scripts/maintenance-audit-lane-runs/findings.mjs new file mode 100644 index 00000000000..53ffc2292ca --- /dev/null +++ b/.github/scripts/maintenance-audit-lane-runs/findings.mjs @@ -0,0 +1,175 @@ +// The lane sweep's finding rules, shared by the judge's writer and the filer: +// shape checks, the claim check against main, the dedup fingerprint, the +// issue body, and the filing plan. No model, no network. +import { createHash } from "node:crypto"; +import { lstatSync, readFileSync } from "node:fs"; +import { join, normalize } from "node:path"; + +export const SLICES = new Set(["A", "C", "D"]); +export const MAX_NEW_ISSUES = 5; +const REGRESSION_DAYS = 14; +const MARKER = /^$/; +const TEXT_FIELDS = ["title", "problem", "root_cause", "proposed_fix", "workflow", "job", "signature"]; + +const squash = (text) => String(text).replace(/\s+/g, " ").trim(); + +// Field names that fail, empty when the finding is well formed. +export function validateFinding(finding) { + const bad = []; + if (!SLICES.has(finding?.slice)) bad.push("slice"); + for (const field of TEXT_FIELDS) { + if (typeof finding?.[field] !== "string" || squash(finding[field]) === "") bad.push(field); + } + if (typeof finding?.title === "string" && finding.title.length > 120) bad.push("title"); + const evidence = finding?.evidence; + const claimOk = (c) => typeof c?.path === "string" && Number.isInteger(c?.line) && typeof c?.quote === "string"; + if (!Array.isArray(evidence) || evidence.length < 1 || evidence.length > 5 || !evidence.every(claimOk)) { + bad.push("evidence"); + } + if (!(finding?.covered_by === null || (Number.isInteger(finding?.covered_by) && finding.covered_by > 0))) { + bad.push("covered_by"); + } + return [...new Set(bad)]; +} + +// A claim holds when its path is a regular file under base (no symlink, no +// escape) and its quote, whitespace squashed, is on that 1-based line. +export function checkClaim(base, { path, line, quote }) { + if ( + !/^[A-Za-z0-9._@+-][A-Za-z0-9._@+/-]*$/.test(path) || + normalize(path) !== path || + path.split("/").includes("..") + ) { + return { ok: false, reason: "bad-path" }; + } + if (squash(quote) === "") return { ok: false, reason: "empty-quote" }; + const full = join(base, path); + let stat; + try { + stat = lstatSync(full); + } catch { + return { ok: false, reason: "not-a-file" }; + } + if (!stat.isFile()) return { ok: false, reason: "not-a-file" }; + const lines = readFileSync(full, "utf8").split("\n"); + if (lines.at(-1) === "") lines.pop(); + if (line < 1 || line > lines.length) return { ok: false, reason: "line-out-of-range" }; + return squash(lines[line - 1]).includes(squash(quote)) ? { ok: true } : { ok: false, reason: "quote-not-on-line" }; +} + +export function normalizeSignature(text) { + return squash(text) + .replace(/\b\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z\b/g, "