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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13375-automate-a-review-finding-as-a-lint-or-t → main 4 commits fork 3 threads · 3 unresolved

  • Merged. scout/13375-automate-a-review-finding-as-a-lint-or-t was merged into main as dd117b8.

Review

Sign in to review this change.
4 files changed +132 −11
scripts/gate.ts +34 −7
@@ -625,19 +625,46 @@ export async function lookRefusal(cwd: string = process.cwd()): Promise<string[]
625625 return lookProblems(parseNameStatus(diff.out), log.out.split("\0").map((m) => m.trim()).filter(Boolean), looks);
626626 }
627627
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] ?? "";
outdated

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 directionRefusal returns a line naming rule "pipeline first". A broken numstat parse currently leaves every test green. Also, the numstat call result .ok is not checked, so a failure silently disables the new rule.

641 if (path === "") {
642 path = f[i + 2] ?? "";
643 i += 2;
outdated

With -M, git diff --numstat prints renames as src/web/{old => new}/x.ts or old => new, not the new path that --name-status gives. path.at(-1) then never matches c.path, so a renamed-and-rewritten page reads 0 lines and escapes the rule. Fix: use git diff --numstat -z -M and parse the NUL-separated rename records, or drop -M from the numstat call and key on both paths. Also treat binary - rows explicitly rather than via Number(add)||0.

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[]> {
630655 const { parseNameStatus } = await import("../src/kernel/pin_edits.ts");
631656 const { directionProblems } = await import("../src/kernel/direction.ts");
632657 const base =
633658 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;
636661 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 git diff --numstat fail while --name-status still succeeds, and expect one line containing rule "pipeline first" and "line counts". Alternatively, extract the failure message into a small exported function and assert on it. Without a test the guard can be deleted unnoticed.

639664 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));
641668 }
642669
643670 /**
src/kernel/direction.ts +19 −3
@@ -21,9 +21,25 @@ export const MAX_FEATURES = 2;
2121 export const MAX_FILES = 20;
2222 const WAIVER = /^Direction:[ \t]*\S[^\n]{9,}/im;
2323
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);
2743 if (features.length === 0) return [];
2844 if (changes.some((c) => PIPELINE.test(c.path))) return [];
2945 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";
22 import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
33 import { join } from "node:path";
44 import { tmpdir } from "node:os";
5 import { directionRefusal } from "../scripts/gate.ts";
5 import { directionRefusal, numstatLines } from "../scripts/gate.ts";
66
77 const root = mkdtempSync(join(tmpdir(), "direction-"));
88 afterAll(() => rmSync(root, { recursive: true, force: true }));
@@ -69,3 +69,71 @@ test("a range that adds the page with a pipeline change passes", async () => {
6969 run(dir, "commit", "-qm", "Add machines");
7070 expect(await withBase(base, () => directionRefusal(dir))).toEqual([]);
7171 });
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", () => {
2323 expect(directionProblems([ch("A", "docs/features/x.md")], [])).toHaveLength(1);
2424 });
2525
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
2636 test("the reason must sit on the Direction line itself, not a later paragraph", () => {
2737 expect(directionProblems([page], ["Direction:\n\nsome later paragraph of text"])).toHaveLength(1);
2838 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs