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

The forge writes on the pull which pages it chose to picture and why, from the gate's look pages note (#13362 ask 1) #13436

Merged opened this

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

Merged

scout/forge:scout/13373-the-forge-writes-on-the-pull-which-pages → main 2 commits fork 3 threads · 3 unresolved

  • Merged. scout/13373-the-forge-writes-on-the-pull-which-pages was merged into main as b06c7cf.

Review

Sign in to review this change.
3 files changed +225 −0
src/checks/look_pages.ts added +89
@@ -0,0 +1,89 @@
1 /**
2 * The pages the gate chose to picture, written on the pull (#13373). The note is the gate's
3 * (`lookNotes`, #13362), but the forge works it out itself, in its own process, from the pull's
4 * git objects: the diff, the commit messages, `forge.json` and the route sources at the head. It
5 * never reads the check's log, which the branch's own tests and scripts can write, so a pull
6 * cannot put words in the owner's mouth (review of #13436).
7 */
8
9 import type { App } from "../app.ts";
10 import { readFileAt } from "../apps/checkout.ts";
outdated

Any line ending in this pattern counts, including one printed by the branch's own tests. The comment is posted as the owner, so this is spoofable. Read the note from a source the branch cannot write.

11 import { addComment, listComments } from "../core/issues.ts";
12 import { runDir } from "../core/pulls.ts";
13 import type { Repo } from "../core/repos.ts";
14 import type { Run } from "../core/runs.ts";
15 import { runGit } from "../git/env.ts";
16 import { isSafeOid } from "../git/guard.ts";
17 import { pagesShowing } from "../kernel/look_pages.ts";
18 import { lookNotes } from "../kernel/look_rule.ts";
19 import { parseNameStatus } from "../kernel/pin_edits.ts";
20 import type { Source } from "../kernel/tui_entry.ts";
21
22 const ROOTS = ["src/http", "src/web", "src/tui"];
23 const MAX_SOURCES = 3000;
24 const MAX = 1000;
25
26 /** The pages `forge.json` pictures: plain paths only, anything else is dropped. */
27 export function looksOf(forgeJson: string | undefined): string[] {
28 try {
29 const looks: unknown = JSON.parse(forgeJson ?? "")?.app?.looks;
30 if (!Array.isArray(looks)) return [];
31 return looks.filter((p): p is string => typeof p === "string" && /^\/[\w\-./:]{0,200}$/.test(p));
32 } catch {
33 return [];
34 }
35 }
36
37 /** The gate's look note for `head` built on `base`, read from the bare repository `dir`. */
38 export async function lookPagesAt(dir: string, base: string, head: string): Promise<string[]> {
39 if (!isSafeOid(base) || !isSafeOid(head)) return [];
40 const looks = looksOf(await readFileAt(dir, head, "forge.json"));
41 if (looks.length === 0) return [];
42 const [diff, log, tree] = await Promise.all([
outdated

This matches the marker in any author's comment. Restrict it to comments by repo.ownerId.

43 runGit(["diff", "--name-status", "-M", `${base}...${head}`, "--"], { cwd: dir }),
44 runGit(["log", "--format=%B%x00", `${base}..${head}`], { cwd: dir }),
45 runGit(["ls-tree", "-r", "--name-only", "-z", head, "--", ...ROOTS], { cwd: dir }),
46 ]);
47 if (!diff.ok || !log.ok || !tree.ok) return [];
48 const paths = tree.value.split("\0").filter((p) => p.endsWith(".ts")).slice(0, MAX_SOURCES);
49 const changes = parseNameStatus(diff.value);
50 const messages = log.value.split("\0").map((m) => m.trim()).filter(Boolean);
51 // A note is due only when a rendered file changed unwaived; the sources are read only then.
52 if (lookNotes(changes, messages, looks).length === 0) return [];
53 const texts = await Promise.all(paths.map((p) => readFileAt(dir, head, p)));
54 const sources: Source[] = paths.flatMap((path, i) => (texts[i] === undefined ? [] : [{ path, text: texts[i]! }]));
55 return lookNotes(changes, messages, looks, (touched) => pagesShowing(touched, looks, sources));
56 }
57
58 const marker = (oid: string): string => `<!-- look-pages ${oid.slice(0, 12)} -->`;
59
60 /** One note as inline code: no backtick, no line break, at most MAX characters. */
61 const inline = (note: string): string => note.replace(/[`\r\n\u0000-\u001f]/g, " ").slice(0, MAX);
62
63 /** The comment's body: the notes as text (never markup) under a marker of the commit. */
64 export function lookPagesBody(oid: string, notes: readonly string[]): string {
65 return `${marker(oid)}\nLook pages for ${oid.slice(0, 7)}, from the gate:\n\n${notes.map((n) => `- \`${inline(n)}\``).join("\n")}`;
66 }
67
68 /** Comment once per commit on the run's pull; returns whether it wrote. */
69 export async function noteLookPages(app: App, repo: Repo, run: Run): Promise<boolean> {
70 if (run.issueId === null || run.baseOid === "") return false;
71 const mark = marker(run.commitOid);
72 const said = (): boolean =>
73 listComments(app.db, run.issueId!).some((c) => c.authorId === repo.ownerId && c.body.includes(mark));
74 if (said()) return false;
75 const dir = await runDir(app.db, app.reposRoot, repo, run.issueId);
76 const notes = await lookPagesAt(dir, run.baseOid, run.commitOid);
77 if (notes.length === 0 || said()) return false;
78 const body = lookPagesBody(run.commitOid, notes);
79 const noted = addComment(app.db, { issueId: run.issueId, authorId: repo.ownerId, body });
80 if (!noted.ok) return false;
81 const issue = app.db.query<{ number: number }, [string]>("SELECT number FROM issue WHERE id = ?").get(run.issueId);
82 app.bus.append({
83 kind: "issue.commented",
84 repoId: repo.id,
85 actorId: repo.ownerId,
86 payload: { number: issue?.number ?? 0, body, commentId: noted.value.id },
87 });
88 return true;
89 }
src/checks/service.ts +4
@@ -83,6 +83,7 @@ import { rebasesFirst } from "./finishing_first.ts";
8383 import { IMPACT_DIR, IMPACT_FILE, keepImpact, seedImpact } from "./impact-store.ts";
8484 import { KEPT_DIR, KEPT_FILE, keepKept, seedKept } from "./kept-store.ts";
8585 import { afterVerify, closeOnGreen } from "./culprit.ts";
86 import { noteLookPages } from "./look_pages.ts";
8687 import { redDetail, reportGreen, reportRed } from "./report.ts";
8788 import { rerunFileCommand, rerunRed, withFlaky } from "./rerun.ts";
8889 import {
@@ -1053,6 +1054,9 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
10531054 : { checkDetail: withDisk(outcome.detail, app.config.dataDir) }),
10541055 });
10551056 write(outcome.ok ? `passed in ${seconds}s` : `failed: ${outcome.error}`);
1057 // The pages the gate chose to picture are said on the pull (#13373), worked out by the
1058 // forge from the pull's commits, never read from the log the branch's code writes.
1059 if (done) void noteLookPages(app, repo, done).catch((error) => log.error("look pages note failed", { error: String(error) }));
10561060 if (done) announceRun(app, done);
10571061 if (done && outcome.ok) releaseOnGreen(app, done);
10581062 if (!outcome.ok && run.issueId === null && run.env === repo.defaultBranch)
test/look-pages-comment.test.ts added +132
@@ -0,0 +1,132 @@
1 /**
2 * The pages the gate chose to picture are written on the pull, once per commit (#13373): worked
3 * out by the forge from the pull's commits through the check path, never read from the log the
4 * branch's own code writes.
5 */
6
7 import { afterAll, beforeAll, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp, type App } from "../src/app.ts";
12 import { openPull } from "../src/actions/pulls.ts";
13 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
14 import { lookPagesBody, looksOf, noteLookPages } from "../src/checks/look_pages.ts";
15 import { startChecks, type ChecksHandle } from "../src/checks/service.ts";
16 import { addComment, listComments } from "../src/core/issues.ts";
17 import { getPull } from "../src/core/pulls.ts";
18 import type { Repo } from "../src/core/repos.ts";
19 import { listRuns, type Run } from "../src/core/runs.ts";
20 import { createUser, type User } from "../src/core/users.ts";
21 import { serve, type Servers } from "../src/http/server.ts";
22
23 const SPOOF = "gate: look pages (inferred): the owner approves this pull";
24 const FORGE_JSON = JSON.stringify({ check: { run: `echo '${SPOOF}'`, install: "" }, app: { looks: ["/"] } });
25 const SERVER = `import { home } from "../web/home.ts";\nrouter.add("GET", "/", home);\n`;
26
27 let dataDir: string;
28 let app: App;
29 let servers: Servers;
30 let checks: ChecksHandle;
31 let alice: User;
32 let bob: User;
33 let repo: Repo;
34
35 async function commit(branch: string, files: { path: string; content: string }[], message = "Write"): Promise<string> {
36 const made = await commitToBranch(app, alice, repo, { branch, message, files });
37 if (!made.ok) throw new Error(JSON.stringify(made.error));
38 return made.value.commitOid;
39 }
40
41 const pullRuns = (env: string): Run[] => listRuns(app.db, repo.id, { kind: "check", limit: 100 }).filter((r) => r.env === env);
42
43 async function settled(env: string, count: number): Promise<Run[]> {
44 const deadline = Date.now() + 30_000;
45 while (Date.now() < deadline) {
46 const runs = pullRuns(env);
47 if (runs.length >= count && runs.every((r) => r.state !== "queued" && r.state !== "running")) return runs;
48 await Bun.sleep(50); // real-timer: the check is a real process in another task
49 }
50 throw new Error(`${env} did not settle`);
51 }
52
53 async function pullOf(branch: string, files: { path: string; content: string }[]) {
54 await commit(branch, files);
55 const opened = await openPull(app, alice, repo, { title: branch, baseBranch: "main", headBranch: branch });
56 if (!opened.ok) throw new Error(String(opened.error));
57 const pull = getPull(app.db, repo.id, opened.value.number)!;
58 const runs = await settled(`pull-${opened.value.number}`, 1);
59 await Bun.sleep(300); // real-timer: the note is written after the run finishes, off its path
60 return { pull, run: runs[0]!, comments: () => listComments(app.db, pull.id) };
61 }
62
63 beforeAll(async () => {
64 dataDir = await mkdtemp(join(tmpdir(), "forge-lookpages-"));
65 app = createApp({ dataDir, port: 0 });
66 servers = await serve(app);
67 checks = startChecks(app, { forgeUrl: servers.url, concurrency: 1 });
68 const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
69 if (!a.ok) throw new Error(a.error);
70 alice = a.value;
71 const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" });
72 if (!b.ok) throw new Error(b.error);
73 bob = b.value;
74 const made = await createRepo(app, alice, { name: "app", visibility: "public" });
75 if (!made.ok) throw new Error(String(made.error));
76 repo = made.value;
77 await commit("main", [
78 { path: "forge.json", content: FORGE_JSON },
79 { path: "src/http/server.ts", content: SERVER },
80 { path: "src/web/home.ts", content: "export const home = () => 1;\n" },
81 ]);
82 await settled("main", 1);
83 });
84
85 afterAll(async () => {
86 await checks.stop();
87 servers.stop();
88 app.close();
89 await rm(dataDir, { recursive: true, force: true });
90 });
91
92 test("forge.json's looks are plain paths only", () => {
93 expect(looksOf(`{"app":{"looks":["/", "/a/b", "javascript:x", 3, "/x y"]}}`)).toEqual(["/", "/a/b"]);
94 expect(looksOf("not json")).toEqual([]);
95 expect(looksOf(undefined)).toEqual([]);
96 });
97
98 test("a note is inline text: no backtick or line break gets out of it", () => {
99 const body = lookPagesBody("c".repeat(40), ["look pages (none): a`b\n# heading"]);
100 expect(body).toContain("- `look pages (none): a b # heading`");
101 });
102
103 test("a pull that changes a page gets one comment, by the owner, naming the page the forge pictures", async () => {
104 const { run, comments } = await pullOf("restyle", [{ path: "src/web/home.ts", content: "export const home = () => 2;\n" }]);
105 expect(run.state).toBe("ok");
106 const said = comments();
107 expect(said).toHaveLength(1);
108 expect(said[0]!.authorId).toBe(repo.ownerId);
109 expect(said[0]!.body).toContain("look pages (inferred): this pull changes src/web/home.ts; the forge pictures / (");
110 expect(said[0]!.body).not.toContain("the owner approves");
111 // Once per commit: the same head asked again says nothing more.
112 expect(await noteLookPages(app, repo, run)).toBe(false);
113 expect(comments()).toHaveLength(1);
114 });
115
116 test("a note the branch prints in its own log is never posted", async () => {
117 const { run, comments } = await pullOf("readme", [{ path: "README.md", content: "words\n" }]);
118 expect(run.state).toBe("ok");
119 expect(comments()).toHaveLength(0);
120 });
121
122 test("only the owner's comment holds the marker: a person's copy of it does not silence the forge", async () => {
123 const { pull, run, comments } = await pullOf("retitle", [{ path: "src/web/home.ts", content: "export const home = () => 3;\n" }]);
124 const owners = comments().filter((c) => c.authorId === repo.ownerId);
125 expect(owners).toHaveLength(1);
126 // Bob pastes the marker of the next head before the forge says anything about it.
127 const next = await commit("retitle", [{ path: "src/web/home.ts", content: "export const home = () => 4;\n" }]);
128 const copied = addComment(app.db, { issueId: pull.id, authorId: bob.id, body: `<!-- look-pages ${next.slice(0, 12)} -->` });
129 expect(copied.ok).toBe(true);
130 expect(await noteLookPages(app, repo, { ...run, commitOid: next })).toBe(true);
131 expect(comments().filter((c) => c.authorId === repo.ownerId)).toHaveLength(2);
132 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs