Public: anyone can read this; the crew can change it.
Automate a review finding as a lint or test: Direction: the owner says pipeline first (#6534, #6611, #5276, #3316, #6217, #64 #13377
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13375-automate-a-review-finding-as-a-lint-or-t → main 4 commits fork 3 threads · 3 unresolved
scripts/gate.ts +34 −7
| @@ -625,19 +625,46 @@ export async function lookRefusal(cwd: string = process.cwd()): Promise<string[] | ||
| 625 | 625 | return lookProblems(parseNameStatus(diff.out), log.out.split("\0").map((m) => m.trim()).filter(Boolean), looks); |
| 626 | 626 | } |
| 627 | 627 | |
| 628 | /** A pull that adds a feature without touching the pipeline says why (#11036). */ | |
| 629 | export async function directionRefusal(cwd: string = process.cwd()): Promise<string[]> { | |
| 628 | /** | |
| 629 | * Changed lines per path from `git diff --numstat -z -M`: a record is | |
| 630 | * `add\tdel\tpath`, or `add\tdel\t` then the old and new path as two more | |
| 631 | * NUL fields for a rename, keyed by the new path as `--name-status` gives. | |
| 632 | * A binary row (`-\t-`) counts 0: it is not a page rewrite. | |
| 633 | */ | |
| 634 | export function numstatLines(raw: string): Map<string, number> { | |
| 635 | const out = new Map<string, number>(); | |
| 636 | const f = raw.split("\0"); | |
| 637 | for (let i = 0; i < f.length; i++) { | |
| 638 | const m = /^(\d+|-)\t(\d+|-)\t(.*)$/s.exec(f[i] ?? ""); | |
| 639 | if (!m) continue; | |
| 640 | let path = m[3] ?? ""; | |
| 641 | if (path === "") { | |
| 642 | path = f[i + 2] ?? ""; | |
| 643 | i += 2; | |
outdated
With |
||
| 644 | } | |
| 645 | out.set(path, m[1] === "-" || m[2] === "-" ? 0 : Number(m[1]) + Number(m[2])); | |
| 646 | } | |
| 647 | return out; | |
| 648 | } | |
| 649 | ||
| 650 | /** | |
| 651 | * A pull that adds a feature without touching the pipeline says why (#11036). | |
| 652 | * `run` is the git runner; a test hands one in that fails `--numstat` alone. | |
| 653 | */ | |
| 654 | export async function directionRefusal(cwd: string = process.cwd(), run: typeof git = git): Promise<string[]> { | |
| 630 | 655 | const { parseNameStatus } = await import("../src/kernel/pin_edits.ts"); |
| 631 | 656 | const { directionProblems } = await import("../src/kernel/direction.ts"); |
| 632 | 657 | const base = |
| 633 | 658 | process.env.FORGE_CHECK_BASE || |
| 634 | git(["merge-base", "origin/main", "HEAD"], cwd).out || | |
| 635 | git(["merge-base", "main", "HEAD"], cwd).out; | |
| 659 | run(["merge-base", "origin/main", "HEAD"], cwd).out || | |
| 660 | run(["merge-base", "main", "HEAD"], cwd).out; | |
| 636 | 661 | if (!base) return []; |
| 637 | const diff = git(["diff", "--name-status", "-M", `${base}..HEAD`], cwd); | |
| 638 | const log = git(["log", "--format=%B%x00", `${base}..HEAD`], cwd); | |
| 662 | const diff = run(["diff", "--name-status", "-M", `${base}..HEAD`], cwd); | |
| 663 | const log = run(["log", "--format=%B%x00", `${base}..HEAD`], cwd); | |
outdated
This branch is untested. Add a test that makes |
||
| 639 | 664 | if (!diff.ok || !log.ok) return []; |
| 640 | return directionProblems(parseNameStatus(diff.out), log.out.split("\0").map((m) => m.trim()).filter(Boolean)); | |
| 665 | const stat = run(["diff", "--numstat", "-z", "-M", `${base}..HEAD`], cwd); | |
| 666 | if (!stat.ok) return [`rule "pipeline first": could not read the pull's line counts (git diff --numstat failed), so redesigns cannot be checked`]; | |
| 667 | return directionProblems(parseNameStatus(diff.out), log.out.split("\0").map((m) => m.trim()).filter(Boolean), numstatLines(stat.out)); | |
| 641 | 668 | } |
| 642 | 669 | |
| 643 | 670 | /** |
src/kernel/direction.ts +19 −3
| @@ -21,9 +21,25 @@ export const MAX_FEATURES = 2; | ||
| 21 | 21 | export const MAX_FILES = 20; |
| 22 | 22 | const WAIVER = /^Direction:[ \t]*\S[^\n]{9,}/im; |
| 23 | 23 | |
| 24 | /** One line per problem: none unless a feature is added with no pipeline change and no Direction line. */ | |
| 25 | export function directionProblems(changes: readonly Change[], messages: readonly string[]): string[] { | |
| 26 | const features = changes.filter((c) => c.status.startsWith("A") && FEATURE.test(c.path)).map((c) => c.path); | |
| 24 | /** | |
| 25 | * A redesign edits a page rather than adding one (#10380): an existing | |
| 26 | * feature file with this many lines added and removed counts as a feature. | |
| 27 | */ | |
| 28 | export const REDESIGN_LINES = 150; | |
| 29 | ||
| 30 | /** | |
| 31 | * One line per problem: none unless a feature is added (or a page redesigned, | |
| 32 | * given `lines`: changed lines per path) with no pipeline change and no | |
| 33 | * Direction line. | |
| 34 | */ | |
| 35 | export function directionProblems( | |
| 36 | changes: readonly Change[], | |
| 37 | messages: readonly string[], | |
| 38 | lines: ReadonlyMap<string, number> = new Map(), | |
| 39 | ): string[] { | |
| 40 | const features = changes | |
| 41 | .filter((c) => FEATURE.test(c.path) && (c.status.startsWith("A") || (lines.get(c.path) ?? 0) >= REDESIGN_LINES)) | |
| 42 | .map((c) => c.path); | |
| 27 | 43 | if (features.length === 0) return []; |
| 28 | 44 | if (changes.some((c) => PIPELINE.test(c.path))) return []; |
| 29 | 45 | if (!messages.some((m) => WAIVER.test(m))) return [problem(features)]; |
test/direction-refusal.test.ts +69 −1
| @@ -2,7 +2,7 @@ import { afterAll, expect, test } from "bun:test"; | ||
| 2 | 2 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | 3 | import { join } from "node:path"; |
| 4 | 4 | import { tmpdir } from "node:os"; |
| 5 | import { directionRefusal } from "../scripts/gate.ts"; | |
| 5 | import { directionRefusal, numstatLines } from "../scripts/gate.ts"; | |
| 6 | 6 | |
| 7 | 7 | const root = mkdtempSync(join(tmpdir(), "direction-")); |
| 8 | 8 | afterAll(() => rmSync(root, { recursive: true, force: true })); |
| @@ -69,3 +69,71 @@ test("a range that adds the page with a pipeline change passes", async () => { | ||
| 69 | 69 | run(dir, "commit", "-qm", "Add machines"); |
| 70 | 70 | expect(await withBase(base, () => directionRefusal(dir))).toEqual([]); |
| 71 | 71 | }); |
| 72 | ||
| 73 | const bigPage = (tag: string) => Array.from({ length: 160 }, (_, i) => `line ${tag} ${i}`).join("\n"); | |
| 74 | ||
| 75 | test("rewriting an existing page by 150+ lines is refused; a small edit passes", async () => { | |
| 76 | const { dir } = repo("redesign"); | |
| 77 | put(dir, "src/web/pages/home.ts", bigPage("a")); | |
| 78 | run(dir, "add", "-A"); | |
| 79 | run(dir, "commit", "-qm", "page"); | |
| 80 | const start = run(dir, "rev-parse", "HEAD"); | |
| 81 | put(dir, "src/web/pages/home.ts", bigPage("b")); | |
| 82 | run(dir, "add", "-A"); | |
| 83 | run(dir, "commit", "-qm", "Redesign"); | |
| 84 | const out = await withBase(start, () => directionRefusal(dir)); | |
| 85 | expect(out).toHaveLength(1); | |
| 86 | expect(out[0]).toContain('rule "pipeline first"'); | |
| 87 | expect(out[0]).toContain("src/web/pages/home.ts"); | |
| 88 | expect(gateMain(dir, start)).toContain('rule "pipeline first"'); | |
| 89 | ||
| 90 | const small = repo("small-edit"); | |
| 91 | put(small.dir, "src/web/pages/home.ts", bigPage("a")); | |
| 92 | run(small.dir, "add", "-A"); | |
| 93 | run(small.dir, "commit", "-qm", "page"); | |
| 94 | const s2 = run(small.dir, "rev-parse", "HEAD"); | |
| 95 | put(small.dir, "src/web/pages/home.ts", bigPage("a").replace("line a 3", "changed")); | |
| 96 | run(small.dir, "add", "-A"); | |
| 97 | run(small.dir, "commit", "-qm", "Fix"); | |
| 98 | expect(await withBase(s2, () => directionRefusal(small.dir))).toEqual([]); | |
| 99 | }); | |
| 100 | ||
| 101 | test("a renamed and rewritten page is still a redesign", async () => { | |
| 102 | const { dir } = repo("renamed"); | |
| 103 | put(dir, "src/web/pages/old.ts", bigPage("a")); | |
| 104 | run(dir, "add", "-A"); | |
| 105 | run(dir, "commit", "-qm", "page"); | |
| 106 | const start = run(dir, "rev-parse", "HEAD"); | |
| 107 | run(dir, "mv", "src/web/pages/old.ts", "src/web/pages/new.ts"); | |
| 108 | put(dir, "src/web/pages/new.ts", bigPage("a").split("\n").slice(0, 5).join("\n") + "\n" + bigPage("z")); | |
| 109 | run(dir, "add", "-A"); | |
| 110 | run(dir, "commit", "-qm", "Rename and rewrite"); | |
| 111 | const out = await withBase(start, () => directionRefusal(dir)); | |
| 112 | expect(out).toHaveLength(1); | |
| 113 | expect(out[0]).toContain("src/web/pages/new.ts"); | |
| 114 | }); | |
| 115 | ||
| 116 | test("numstatLines keys renames by the new path and counts binary rows as 0", () => { | |
| 117 | const m = numstatLines("3\t4\ta.ts\x00-\t-\tb.png\x00150\t2\t\x00old.ts\x00new.ts\x00"); | |
| 118 | expect(m.get("a.ts")).toBe(7); | |
| 119 | expect(m.get("b.png")).toBe(0); | |
| 120 | expect(m.get("new.ts")).toBe(152); | |
| 121 | expect(m.has("old.ts")).toBe(false); | |
| 122 | }); | |
| 123 | ||
| 124 | test("a failing git diff --numstat refuses the pull rather than skipping the redesign check", async () => { | |
| 125 | const { dir, base } = repo("numstat-fails"); | |
| 126 | put(dir, "src/web/pages/home.ts", "x"); | |
| 127 | put(dir, "scripts/lint.ts", "x"); | |
| 128 | run(dir, "add", "-A"); | |
| 129 | run(dir, "commit", "-qm", "Add home"); | |
| 130 | const git = (args: string[], cwd: string = dir) => { | |
| 131 | if (args.includes("--numstat")) return { ok: false, out: "" }; | |
| 132 | const r = Bun.spawnSync(["git", ...args], { cwd }); | |
| 133 | return { ok: r.exitCode === 0, out: r.stdout.toString().trim() }; | |
| 134 | }; | |
| 135 | const out = await withBase(base, () => directionRefusal(dir, git)); | |
| 136 | expect(out).toHaveLength(1); | |
| 137 | expect(out[0]).toContain('rule "pipeline first"'); | |
| 138 | expect(out[0]).toContain("line counts"); | |
| 139 | }); |
test/direction.test.ts +10
| @@ -23,6 +23,16 @@ test("edits and non-feature files are never asked", () => { | ||
| 23 | 23 | expect(directionProblems([ch("A", "docs/features/x.md")], [])).toHaveLength(1); |
| 24 | 24 | }); |
| 25 | 25 | |
| 26 | test("a redesign — an existing page with 150+ changed lines — is a feature; a small edit is not", () => { | |
| 27 | const edit = ch("M", "src/web/pages/machines.ts"); | |
| 28 | const out = directionProblems([edit], ["Redesign"], new Map([[edit.path, 150]])); | |
| 29 | expect(out).toHaveLength(1); | |
| 30 | expect(out[0]).toContain('rule "pipeline first"'); | |
| 31 | expect(directionProblems([edit], [], new Map([[edit.path, 149]]))).toEqual([]); | |
| 32 | expect(directionProblems([edit], ["Direction: owner asked for the redesign on #10299"], new Map([[edit.path, 400]]))).toEqual([]); | |
| 33 | expect(directionProblems([edit, ch("M", "scripts/gate.ts")], [], new Map([[edit.path, 400]]))).toEqual([]); | |
| 34 | }); | |
| 35 | ||
| 26 | 36 | test("the reason must sit on the Direction line itself, not a later paragraph", () => { |
| 27 | 37 | expect(directionProblems([page], ["Direction:\n\nsome later paragraph of text"])).toHaveLength(1); |
| 28 | 38 | }); |
This parsing is the only path that feeds real line counts, and nothing tests it. test/direction.test.ts passes a hand-built Map. Extend test/direction-refusal.test.ts: build a temp git repo, rewrite a src/web/pages file by 150+ lines (and one rename+rewrite), and assert
directionRefusalreturns a line namingrule "pipeline first". A broken numstat parse currently leaves every test green. Also, the numstat call result.okis not checked, so a failure silently disables the new rule.