Public: anyone can read this; the crew can change it.

Automate a review finding as a lint or test: Noise: the comment rewraps in route_marks.ts and forge-looks.test.ts are unrelat #13914

Merged opened this

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

Merged

torque/forge:torque/13913-automate-a-review-finding-as-a-lint-or-t → main 1 commit fork

  • Merged. torque/13913-automate-a-review-finding-as-a-lint-or-t was merged into main as afec33b.

Review

Sign in to review this change.
3 files changed +191 −6
scripts/gate.ts +50 −6
@@ -27,7 +27,8 @@
2727 * test that lists the gate's files through it says `@tree-scan` itself.
2828 */
2929
30 import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
30 import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
31 import { tmpdir } from "node:os";
3132 import { join } from "node:path";
3233
3334 export type Exclusion = {
@@ -579,16 +580,58 @@ export function wrapNote(root = ".", all = false): string[] {
579580 */
580581 export function wrapFix(root = ".", all = false): string[] {
581582 const { rewrap } = require("../src/kernel/comment_wrap.ts") as typeof import("../src/kernel/comment_wrap.ts");
583 const { parseAdded } = require("../src/kernel/review_rules.ts") as typeof import("../src/kernel/review_rules.ts");
584 const { parseHunks, keepHunks } = require("../src/kernel/rewrap_noise.ts") as typeof import("../src/kernel/rewrap_noise.ts");
585 const base = all ? "" : process.env.FORGE_CHECK_BASE || git(["merge-base", "origin/main", "HEAD"], root).out || git(["merge-base", "main", "HEAD"], root).out;
586 // The lines the pull wrote, by file (#13913): only comments touching one are rewrapped.
587 const written = new Map<string, Set<number>>();
588 if (!all) {
589 const diff = git(["diff", "-U0", "--no-color", "--no-ext-diff", base, "--", "src", "test", "scripts"], root);
590 for (const a of diff.ok ? parseAdded(diff.out) : []) (written.get(a.path) ?? written.set(a.path, new Set()).get(a.path)!).add(a.line);
591 }
592 const scratch = mkdtempSync(join(tmpdir(), "forge-wrap-"));
582593 const changed: string[] = [];
583 for (const { path, text } of wrapSources(root, all, true)) {
584 const next = rewrap(text);
585 if (next === text) continue;
586 writeFileSync(join(root, path), next);
587 changed.push(path);
594 try {
595 for (const { path, text } of wrapSources(root, all, true)) {
596 const full = rewrap(text);
597 if (full === text) continue;
598 let next = full;
599 if (!all) {
600 // A file git does not track yet is new: all of it is the pull's.
601 const mine = written.get(path);
602 writeFileSync(join(scratch, "a.ts"), text);
603 writeFileSync(join(scratch, "b.ts"), full);
604 // `--no-index` exits 1 when the files differ, so `git` (which reads that as failure) is not
605 // used.
606 const d = Bun.spawnSync(["git", "diff", "--no-index", "-U0", "--no-color", "a.ts", "b.ts"], { cwd: scratch }).stdout.toString();
607 const tracked = git(["ls-files", "--error-unmatch", path], root).ok;
608 next = keepHunks(text, parseHunks(d), tracked ? mine ?? new Set() : undefined);
609 }
610 if (next === text) continue;
611 writeFileSync(join(root, path), next);
612 changed.push(path);
613 }
614 } finally {
615 rmSync(scratch, { recursive: true, force: true });
588616 }
589617 return changed;
590618 }
591619
620 /**
621 * A pull's diff holds no hunk that only rewraps a comment (#13913): reviewers asked for it four
622 * times. Same base and pass-when-unknown as `addedRefusal`.
623 */
624 export async function rewrapNoiseRefusal(cwd: string = process.cwd()): Promise<string[]> {
625 const { parseHunks, rewrapNoiseProblems } = await import("../src/kernel/rewrap_noise.ts");
626 const base =
627 process.env.FORGE_CHECK_BASE ||
628 git(["merge-base", "origin/main", "HEAD"], cwd).out ||
629 git(["merge-base", "main", "HEAD"], cwd).out;
630 if (!base) return [];
631 const diff = git(["diff", "-U0", "--no-color", "--no-ext-diff", `${base}..${judgedHead(cwd)}`, "--", "src", "test", "scripts"], cwd);
632 return diff.ok ? rewrapNoiseProblems(parseHunks(diff.out)) : [];
633 }
634
592635 /**
593636 * What the pull adds, judged by `addedProblems` (#11884). Passes when no base resolves, like the
594637 * rest.
@@ -767,6 +810,7 @@ export async function gateVerdict(cwd: string = process.cwd()): Promise<{ notes:
767810 ...strayTestRefusal(cwd),
768811 ...reviewRefusal(cwd),
769812 ...(await addedRefusal(cwd)),
813 ...(await rewrapNoiseRefusal(cwd)),
770814 ...(await dependencyRefusal(cwd)),
771815 ...(await readmeCapRefusal(cwd)),
772816 ...billingDeletesRefusal(cwd),
src/kernel/rewrap_noise.ts added +78
@@ -0,0 +1,78 @@
1 /**
2 * The rewrap rule (#13913): a pull's diff is its change. A hunk that only rewraps comments, the
3 * same words on other lines, says nothing about the pull and buries what it did, so it is refused
4 * and the file's rewrapped comments are left as they were. `bun scripts/gate.ts --fix` rewraps
5 * only the comments the pull wrote (`pullRanges`, `keepHunks`).
6 */
7
8 export type Hunk = { path: string; oldStart: number; oldCount: number; removed: string[]; added: string[] };
9
10 /** The hunks of a `git diff -U0`, per file. */
11 export function parseHunks(diff: string): Hunk[] {
12 const out: Hunk[] = [];
13 let path = "";
14 let inHunk = false;
15 let cur: Hunk | undefined;
16 for (const row of diff.split("\n")) {
17 if (row.startsWith("diff --git ")) {
18 inHunk = false;
19 path = "";
20 } else if (!inHunk && row.startsWith("+++ ")) {
21 path = row.startsWith("+++ b/") ? row.slice(6) : "";
22 } else if (row.startsWith("@@")) {
23 inHunk = true;
24 const m = row.match(/^@@ -(\d+)(?:,(\d+))? /);
25 cur = { path, oldStart: m ? Number(m[1]) : 0, oldCount: m ? (m[2] === undefined ? 1 : Number(m[2])) : 0, removed: [], added: [] };
26 out.push(cur);
27 } else if (inHunk && cur) {
28 if (row.startsWith("-")) cur.removed.push(row.slice(1));
29 else if (row.startsWith("+")) cur.added.push(row.slice(1));
30 }
31 }
32 return out;
33 }
34
35 const COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*)/;
36 /** A comment line's words: its markers and spacing gone, so a rewrap compares equal. */
37 const words = (lines: readonly string[]): string =>
38 lines.map((l) => l.replace(/^\s*(?:\/\/+|\/\*+|\*+\/?)/, "").replace(/\*\/\s*$/, "")).join(" ").split(/\s+/).filter(Boolean).join(" ");
39
40 /** True when a hunk only moves a comment's words onto other lines. */
41 export function isRewrap(h: Hunk): boolean {
42 if (!h.removed.length || !h.added.length) return false;
43 if (![...h.removed, ...h.added].every((l) => COMMENT_LINE.test(l))) return false;
44 return words(h.removed) === words(h.added);
45 }
46
47 const CODE_FILE = /^(?:src|test|scripts)\/(?!(?:.*\/)?vendor\/).+\.ts$/;
48 export const REWRAP_RULE = "rule \"the diff is the change\"";
49
50 /** One line per hunk that only rewraps a comment, naming the rule and the fix. */
51 export function rewrapNoiseProblems(hunks: readonly Hunk[]): string[] {
52 return hunks
53 .filter((h) => CODE_FILE.test(h.path) && isRewrap(h))
54 .map((h) => `${REWRAP_RULE}: ${h.path}:${h.oldStart} only rewraps a comment this pull did not write; restore the lines, and keep the diff to the change (\`bun scripts/gate.ts --fix\` rewraps only your own comments)`);
55 }
56
57 /**
58 * Of the hunks that turn `before` into its rewrap, the ones that touch a line the pull wrote
59 * (`written`, 1-based lines of `before`); the rest are left out. `undefined` written means the
60 * whole file is the pull's.
61 */
62 export function keepHunks(before: string, hunks: readonly Hunk[], written: ReadonlySet<number> | undefined): string {
63 const lines = before.split("\n");
64 const out: string[] = [];
65 let at = 1;
66 for (const h of hunks) {
67 const first = h.oldCount === 0 ? h.oldStart + 1 : h.oldStart;
68 const upto = h.oldCount === 0 ? first : h.oldStart + h.oldCount;
69 out.push(...lines.slice(at - 1, first - 1));
70 let mine = written === undefined;
71 for (let n = first; !mine && n < Math.max(upto, first + 1); n++) if (written!.has(n)) mine = true;
72 if (mine) out.push(...h.added);
73 else out.push(...lines.slice(first - 1, upto - 1));
74 at = upto;
75 }
76 out.push(...lines.slice(at - 1));
77 return out.join("\n");
78 }
test/gate-rewrap-noise.test.ts added +63
@@ -0,0 +1,63 @@
1 import { afterEach, expect, test } from "bun:test";
2 import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { rewrapNoiseRefusal, wrapFix } from "../scripts/gate.ts";
6
7 const dirs: string[] = [];
8 const saved = process.env.FORGE_CHECK_BASE;
9 afterEach(() => {
10 for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
11 if (saved === undefined) delete process.env.FORGE_CHECK_BASE;
12 else process.env.FORGE_CHECK_BASE = saved;
13 });
14
15 function sh(cwd: string, ...args: string[]): string {
16 const r = Bun.spawnSync(["git", "-c", "user.email=t@t", "-c", "user.name=t", "-c", "commit.gpgsign=false", ...args], { cwd });
17 if (r.exitCode !== 0) throw new Error(r.stderr.toString());
18 return r.stdout.toString().trim();
19 }
20
21 const LONG = "// " + "word ".repeat(30).trim();
22 /** A repo whose base holds two over-long comments, far apart. */
23 function repo(): { dir: string; base: string; file: string } {
24 const dir = mkdtempSync(join(tmpdir(), "forge-noise-"));
25 dirs.push(dir);
26 sh(dir, "init", "-q", "-b", "trunk");
27 mkdirSync(join(dir, "src"));
28 const file = join(dir, "src/a.ts");
29 const filler = Array.from({ length: 6 }, (_, i) => `const f${i} = ${i};`).join("\n");
30 writeFileSync(file, `${LONG}\nconst a = 1;\n${filler}\n${LONG}\nconst b = 2;\n`);
31 sh(dir, "add", "-A");
32 sh(dir, "commit", "-qm", "base");
33 const base = sh(dir, "rev-parse", "HEAD");
34 process.env.FORGE_CHECK_BASE = base;
35 return { dir, base, file };
36 }
37
38 test("a pull that only rewraps a comment is refused, naming the rule", async () => {
39 const { dir, file } = repo();
40 const half = "word ".repeat(15).trim();
41 writeFileSync(file, readFileSync(file, "utf8").replace(LONG, `// ${half}\n// ${half}`));
42 sh(dir, "commit", "-qam", "rewrap");
43 const out = await rewrapNoiseRefusal(dir);
44 expect(out).toHaveLength(1);
45 expect(out[0]).toContain('rule "the diff is the change"');
46 expect(out[0]).toContain("src/a.ts:1");
47 });
48
49 test("a changed comment or code is not noise", async () => {
50 const { dir, file } = repo();
51 writeFileSync(file, readFileSync(file, "utf8").replace("const b = 2;", "const b = 3; // edited"));
52 sh(dir, "commit", "-qam", "edit");
53 expect(await rewrapNoiseRefusal(dir)).toEqual([]);
54 });
55
56 test("--fix rewraps the comment the pull wrote and leaves the one it did not", () => {
57 const { dir, file } = repo();
58 writeFileSync(file, readFileSync(file, "utf8").replace("const b = 2;", `const b = 2;\n${LONG}`));
59 expect(wrapFix(dir)).toEqual(["src/a.ts"]);
60 const text = readFileSync(file, "utf8");
61 expect(text.startsWith(`${LONG}\n`)).toBe(true); // the first, old comment is untouched
62 expect(text).not.toContain(`const b = 2;\n${LONG}\n`); // the new one is folded
63 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs