Public: anyone can read this; the crew can change it.
The daily count: one line a day in the check log of how many pulls had their pages inferred, named by the builder, or left without (#13362 ask 4) #13433
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13372-the-daily-count-one-line-a-day-in-the-ch → main 4 commits fork 4 threads · 4 unresolved
7 files
changed +219
−8
docs/features/README.md +1
| @@ -171,6 +171,7 @@ Read with the guide page [Ship and undo](../guide-ship-and-undo.md). | ||
| 171 | 171 | - [](merge-crash-recovery.md) |
| 172 | 172 | - [](done-on-main.md) |
| 173 | 173 | - [](preview-owner-looks.md) |
| 174 | - [](look-count.md) | |
| 174 | 175 | - [](pull-verdict.md) |
| 175 | 176 | - [](pull-looks.md) |
| 176 | 177 | - [](approval-base.md) |
docs/features/look-count.md added +7
| @@ -0,0 +1,7 @@ | ||
| 1 | # The daily count of look pages | |
| 2 | ||
| 3 | The gate prints one `look pages (inferred|named|none)` note per pull that changes what `src/web/` renders (#13362): the forge inferred the pages to picture, a commit's `Looks:` line named one, or none could be found. The gate keeps no state, so the forge counts: `src/checks/look_count.ts` reads the note from each gate's lines (`landing.ts`), adds it to today's tally in `checks/look_pages.json` under the data directory (30 days kept), and when the next day's first gate arrives writes the finished day as one log line: | |
| 4 | ||
| 5 | `[checks] look pages 2026-11-14: 3 inferred, 1 named, 2 none` | |
| 6 | ||
| 7 | A pull with a `No look:` waiver or no rendered change prints no note and is not counted. Terminal door: none, the line is a log line for the operator (`No door:` in the commit). |
src/checks/landing.ts +3 −1
| @@ -31,6 +31,7 @@ import { keepForgeGate } from "./gate_last.ts"; | ||
| 31 | 31 | import { gateReason } from "./gate_reason.ts"; |
| 32 | 32 | import { gateTests } from "./gate_tests.ts"; |
| 33 | 33 | import { keepGateLog } from "./gate_log.ts"; |
| 34 | import { noteLookPages } from "./look_count.ts"; | |
| 34 | 35 | import { GATE_DEADLINE_MS, type GateRecord } from "./queue.ts"; |
| 35 | 36 | import { gateRecorded, gateRunning, recordedVerdict } from "./queue_store.ts"; |
| 36 | 37 | import { checksFor, type GateDone } from "./service.ts"; |
| @@ -103,12 +104,13 @@ export async function adoptGate( | ||
| 103 | 104 | } |
| 104 | 105 | |
| 105 | 106 | /** What the lane's answer on `commitOid` decides: its log kept, its red named. */ |
| 106 | function gateVerdict(app: App, repo: Repo, commitOid: string, done: GateDone, last = true): LandVerdict { | |
| 107 | export function gateVerdict(app: App, repo: Repo, commitOid: string, done: GateDone, last = true): LandVerdict { | |
| 107 | 108 | // Cut off, not judged: no verdict, and not the gate log a red reads (#3155). |
| 108 | 109 | if (done.interrupted === true) return { lands: false, interrupted: true }; |
| 109 | 110 | if (last) keepForgeGate(app.config.dataDir, app.config.forgeRepo, `${repo.ownerName}/${repo.name}`, done.lines); |
| 110 | 111 | const slug = `${repo.ownerName}/${repo.name}`; |
| 111 | 112 | const kept = keepGateLog(app.config.dataDir, slug, commitOid, done.ok, done.lines, Date.now()); |
| 113 | noteLookPages(app.config.dataDir, `${slug}@${commitOid}`, done.lines, Date.now()); | |
| 112 | 114 | const failed = failuresOf(done.lines); |
| 113 | 115 | const link = kept === undefined ? undefined : `/api/v1/repos/${slug}/gates/${kept}/log`; |
| 114 | 116 | // Why it went red, said in the queue's log and read by its search (#5276). |
src/checks/look_count.ts added +100
| @@ -0,0 +1,100 @@ | ||
| 1 | /** | |
| 2 | * The daily count (#13372): the gate runs in the check sandbox and keeps no state, but it says | |
| 3 | * one `look pages (inferred|named|none)` note per gate (#13362). The forge reads those notes | |
| 4 | * from each gate's lines, adds them to today's tally, once per pull and head, in one small file | |
| 5 | * under the data directory, and when a new day's first gate arrives writes the finished day as one | |
| 6 | * line of the log: `[checks] look pages 2026-11-14: 3 inferred, 1 named, 2 none`. At most 30 days | |
| 7 | * are kept. | |
| 8 | */ | |
| 9 | ||
| 10 | import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 11 | import { dirname, join } from "node:path"; | |
| 12 | ||
| 13 | export const KEEP_DAYS = 30; | |
| 14 | export type Tally = { inferred: number; named: number; none: number }; | |
| 15 | type Day = Tally & { said?: true; seen?: string[] }; | |
| 16 | type Days = Record<string, Day>; | |
| 17 | ||
| 18 | const NOTE = /^(?:gate: )?look pages \((inferred|named|none)\)/; | |
| 19 | const DAY = /^\d{4}-\d{2}-\d{2}$/; | |
| 20 | ||
| 21 | /** The kind of look note a gate's lines hold, if any; the first one counts. */ | |
| 22 | export function lookKind(lines: readonly string[]): keyof Tally | undefined { | |
| 23 | for (const l of lines) { | |
| 24 | const m = NOTE.exec(l.trim()); | |
| 25 | if (m) return m[1] as keyof Tally; | |
| 26 | } | |
| 27 | return undefined; | |
| 28 | } | |
| 29 | ||
| 30 | export const dayOf = (now: number): string => new Date(now).toISOString().slice(0, 10); | |
| 31 | const fileOf = (dataDir: string): string => join(dataDir, "checks", "look_pages.json"); | |
| 32 | ||
| 33 | function read(dataDir: string): Days { | |
| 34 | try { | |
| 35 | const raw = JSON.parse(readFileSync(fileOf(dataDir), "utf8")) as Record<string, Partial<Day>>; | |
| 36 | const out: Days = {}; | |
| 37 | for (const [d, t] of Object.entries(raw)) { | |
| 38 | const seen = Array.isArray(t.seen) ? t.seen.filter((k) => typeof k === "string") : []; | |
| 39 | out[d] = { | |
| 40 | inferred: Number(t.inferred) || 0, | |
| 41 | named: Number(t.named) || 0, | |
| 42 | none: Number(t.none) || 0, | |
| 43 | ...(t.said === true ? { said: true as const } : {}), | |
| 44 | ...(seen.length > 0 ? { seen } : {}), | |
| 45 | }; | |
| 46 | } | |
| 47 | return Object.fromEntries(Object.entries(out).filter(([d]) => DAY.test(d))); | |
| 48 | } catch { | |
| 49 | return {}; | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | export const countLine = (day: string, t: Tally): string => | |
| 54 | `[checks] look pages ${day}: ${t.inferred} inferred, ${t.named} named, ${t.none} none`; | |
| 55 | ||
| 56 | /** | |
| 57 | * Count one gate's look note into today, once per `key` (repo and head commit: a pull pushed | |
| 58 | * three times is three heads, a re-check of one head is not counted again), and say each | |
| 59 | * earlier day not yet said (`say`) once. Returns the kind counted, or undefined when the gate | |
| 60 | * had no look note or the head was already counted. Throws on disk errors: see `noteLookPages`. | |
| 61 | */ | |
| 62 | export function countLookPages( | |
outdated
|
||
| 63 | dataDir: string, | |
| 64 | key: string, | |
| 65 | lines: readonly string[], | |
| 66 | now: number, | |
| 67 | say: (line: string) => void = (l) => console.log(l), | |
| 68 | ): keyof Tally | undefined { | |
| 69 | const days = read(dataDir); | |
| 70 | const today = dayOf(now); | |
| 71 | const counted = Object.values(days).some((d) => d.seen?.includes(key)); | |
| 72 | const kind = counted ? undefined : lookKind(lines); | |
| 73 | const prior = Object.keys(days).filter((d) => d < today && days[d]!.said !== true).sort(); | |
| 74 | if (kind === undefined && prior.length === 0) return undefined; | |
| 75 | for (const d of prior) { | |
outdated
Unguarded mkdir/write/rename: throwing here escapes into landing. Catch and log once. |
||
| 76 | say(countLine(d, days[d]!)); | |
| 77 | days[d]!.said = true; | |
| 78 | } | |
| 79 | if (kind !== undefined) { | |
| 80 | const t = (days[today] ??= { inferred: 0, named: 0, none: 0 }); | |
| 81 | t[kind]++; | |
| 82 | t.seen = [...(t.seen ?? []), key]; | |
| 83 | } | |
| 84 | const out: Record<string, unknown> = {}; | |
| 85 | for (const d of Object.keys(days).sort().slice(-KEEP_DAYS)) out[d] = days[d]; | |
| 86 | const file = fileOf(dataDir); | |
| 87 | mkdirSync(dirname(file), { recursive: true }); | |
| 88 | writeFileSync(`${file}.tmp`, JSON.stringify(out)); | |
| 89 | renameSync(`${file}.tmp`, file); | |
| 90 | return kind; | |
| 91 | } | |
| 92 | ||
| 93 | /** The same, for the landing path: a counting failure is one log line and never a verdict's. */ | |
| 94 | export function noteLookPages(dataDir: string, key: string, lines: readonly string[], now: number): void { | |
| 95 | try { | |
| 96 | countLookPages(dataDir, key, lines, now); | |
| 97 | } catch (e) { | |
| 98 | console.log(`[checks] look pages not counted: ${e instanceof Error ? e.message : String(e)}`); | |
| 99 | } | |
| 100 | } |
src/kernel/look_rule.ts +7 −5
| @@ -23,10 +23,11 @@ export function renderChanged(changes: readonly Change[]): string[] { | ||
| 23 | 23 | export type Inferred = { found: { page: string; via: string }[]; untraced: string[] }; |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | * One note, never a failure (#13362): nothing when nothing rendered changes, or a commit names a | |
| 27 | * pictured page or waives it. Otherwise the pages the forge chose (`infer`: the pages of | |
| 28 | * `looks` whose routes reach the changed files) and why, or that none was found. Each note | |
| 29 | * starts `look pages (inferred|named|none)` so a log can be counted. | |
| 26 | * One note, never a failure (#13362): nothing when nothing rendered changes, or a commit waives it. | |
| 27 | * A commit that names a pictured page is one `named` line, so the daily count (#13372) can see it. | |
| 28 | * Otherwise the pages the forge chose (`infer`: the pages of `looks` whose routes reach the changed | |
| 29 | * files) and why, or that none was found. Each note starts `look pages (inferred|named|none)` so a | |
| 30 | * log can be counted. | |
| 30 | 31 | */ |
| 31 | 32 | export function lookNotes( |
| 32 | 33 | changes: readonly Change[], |
| @@ -38,7 +39,8 @@ export function lookNotes( | ||
| 38 | 39 | if (touched.length === 0) return []; |
| 39 | 40 | if (messages.some((m) => WAIVER.test(m))) return []; |
| 40 | 41 | const named = messages.flatMap((m) => [...m.matchAll(new RegExp(LOOKS.source, "gim"))].map((x) => x[1]!)); |
| 41 | if (named.some((p) => looks.includes(p))) return []; | |
| 42 | const chosen = named.find((p) => looks.includes(p)); | |
| 43 | if (chosen !== undefined) return [`look pages (named): this pull changes ${touched.slice(0, 3).join(", ")}; a commit names ${chosen}`]; | |
| 42 | 44 | const shown = touched.slice(0, 3).join(", ") + (touched.length > 3 ? ` and ${touched.length - 3} more` : ""); |
| 43 | 45 | const stray = named.length ? `"Looks: ${named[0]}" is not a page of app.looks in forge.json; ` : ""; |
| 44 | 46 | const { found, untraced } = infer(touched); |
test/look-count.test.ts added +99
| @@ -0,0 +1,99 @@ | ||
| 1 | import { afterEach, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp } from "../src/app.ts"; | |
| 6 | import { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { createUser } from "../src/core/users.ts"; | |
| 8 | import type { GateDone } from "../src/checks/service.ts"; | |
| 9 | import { gateVerdict } from "../src/checks/landing.ts"; | |
| 10 | import { countLookPages, lookKind, noteLookPages } from "../src/checks/look_count.ts"; | |
| 11 | ||
| 12 | const dirs: string[] = []; | |
| 13 | const dir = () => { | |
| 14 | const d = mkdtempSync(join(tmpdir(), "forge-count-")); | |
| 15 | dirs.push(d); | |
| 16 | return d; | |
| 17 | }; | |
| 18 | afterEach(() => { for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true }); }); | |
| 19 | const day = (d: string) => Date.parse(`${d}T12:00:00Z`); | |
| 20 | const note = (k: string) => [`gate: look pages (${k}): x`]; | |
| 21 | ||
| 22 | test("the kind is read from a gate's lines; none when there is no note", () => { | |
| 23 | expect(lookKind(["ok", ...note("none")])).toBe("none"); | |
| 24 | expect(lookKind(["ok", "look pages (named): y"])).toBe("named"); | |
| 25 | expect(lookKind(["ok"])).toBeUndefined(); | |
| 26 | }); | |
| 27 | ||
| 28 | test("a day's counts are said once, as one line, when the next day's first gate arrives", () => { | |
| 29 | const d = dir(); | |
| 30 | const said: string[] = []; | |
| 31 | const say = (l: string) => said.push(l); | |
| 32 | countLookPages(d, "r@a", note("inferred"), day("2026-11-14"), say); | |
| 33 | countLookPages(d, "r@b", note("inferred"), day("2026-11-14"), say); | |
| 34 | countLookPages(d, "r@c", note("named"), day("2026-11-14"), say); | |
| 35 | countLookPages(d, "r@d", note("none"), day("2026-11-14"), say); | |
| 36 | expect(said).toEqual([]); | |
| 37 | countLookPages(d, "r@e", note("none"), day("2026-11-15"), say); | |
| 38 | countLookPages(d, "r@a", [], day("2026-11-15"), say); | |
| 39 | expect(said).toEqual(["[checks] look pages 2026-11-14: 2 inferred, 1 named, 1 none"]); | |
| 40 | expect(JSON.parse(readFileSync(join(d, "checks", "look_pages.json"), "utf8"))["2026-11-15"].none).toBe(1); | |
| 41 | }); | |
| 42 | ||
| 43 | test("a gate with no note on a quiet new day still says the day before, and a corrupt file starts over", () => { | |
| 44 | const d = dir(); | |
outdated
Bun.write is not awaited, so the corrupt file may not exist when read, and the test would pass without the corrupt-file handling. Use writeFileSync, and assert the tally restarts. |
||
| 45 | const said: string[] = []; | |
| 46 | countLookPages(d, "r@a", note("none"), day("2026-11-10"), () => {}); | |
| 47 | countLookPages(d, "r@a", [], day("2026-11-12"), (l) => said.push(l)); | |
| 48 | expect(said).toEqual(["[checks] look pages 2026-11-10: 0 inferred, 0 named, 1 none"]); | |
| 49 | Bun.write(join(d, "checks", "look_pages.json"), "{not json"); | |
| 50 | expect(countLookPages(d, "r@c", note("named"), day("2026-11-13"), () => {})).toBe("named"); | |
| 51 | }); | |
| 52 | ||
| 53 | test("a pull's head is counted once however many gates run on it; a new head counts again", () => { | |
| 54 | const d = dir(); | |
| 55 | const at = day("2026-11-14"); | |
| 56 | expect(countLookPages(d, "r@one", note("none"), at, () => {})).toBe("none"); | |
| 57 | expect(countLookPages(d, "r@one", note("none"), at, () => {})).toBeUndefined(); | |
| 58 | expect(countLookPages(d, "r@one", note("named"), day("2026-11-15"), () => {})).toBeUndefined(); | |
| 59 | expect(countLookPages(d, "r@two", note("inferred"), at, () => {})).toBe("inferred"); | |
| 60 | const t = JSON.parse(readFileSync(join(d, "checks", "look_pages.json"), "utf8"))["2026-11-14"]; | |
| 61 | expect([t.inferred, t.named, t.none]).toEqual([1, 0, 1]); | |
| 62 | }); | |
| 63 | ||
| 64 | test("a counting failure is one line and never throws", () => { | |
| 65 | const d = dir(); | |
| 66 | writeFileSync(join(d, "checks"), "a file where the directory should be"); | |
| 67 | expect(() => noteLookPages(d, "r@a", note("none"), day("2026-11-14"))).not.toThrow(); | |
| 68 | }); | |
| 69 | ||
| 70 | test("the gate's verdict counts its look note, once per head, and survives a failed count", async () => { | |
| 71 | const d = dir(); | |
| 72 | const app = createApp({ dataDir: d, port: 0 }); | |
| 73 | const bad = dir(); | |
| 74 | const broken = createApp({ dataDir: bad, port: 0 }); | |
| 75 | try { | |
| 76 | const made = await createUser(app.db, { | |
| 77 | origin: "seed", | |
| 78 | trusted: true, | |
| 79 | username: "alice", | |
| 80 | email: "[email protected]", | |
| 81 | password: "a-good-passphrase", | |
| 82 | adminIfFirst: true, | |
| 83 | }); | |
| 84 | if (!made.ok) throw new Error(made.error); | |
| 85 | const r = await createRepo(app, made.value, { name: "n", visibility: "public" }); | |
| 86 | if (!r.ok) throw new Error(String(r.error)); | |
| 87 | const done: GateDone = { ok: true, error: "", lines: note("inferred") }; | |
| 88 | expect(gateVerdict(app, r.value, "abc", done, false).lands).toBe(true); | |
| 89 | gateVerdict(app, r.value, "abc", done, false); | |
| 90 | const f = JSON.parse(readFileSync(join(d, "checks", "look_pages.json"), "utf8")); | |
| 91 | expect(Object.values(f as Record<string, { inferred: number }>)[0]!.inferred).toBe(1); | |
| 92 | rmSync(join(bad, "checks"), { recursive: true, force: true }); | |
| 93 | writeFileSync(join(bad, "checks"), "x"); | |
| 94 | expect(() => gateVerdict(broken, r.value, "abc", done, false)).not.toThrow(); | |
| 95 | } finally { | |
| 96 | app.close(); | |
| 97 | broken.close(); | |
| 98 | } | |
| 99 | }); |
test/look-rule.test.ts +2 −2
| @@ -20,8 +20,8 @@ test("no page found says so and asks for a line", () => { | ||
| 20 | 20 | }); |
| 21 | 21 | |
| 22 | 22 | test("a Looks line naming a page of app.looks is the override; a stray one is noted", () => { |
| 23 | expect(lookNotes([edit], ["x\n\nLooks: /flow"], looks)).toEqual([]); | |
| 24 | expect(lookNotes([edit], ["Looks: /settings/crew?help open prompt"], looks)).toEqual([]); | |
| 23 | expect(lookNotes([edit], ["x\n\nLooks: /flow"], looks)[0]).toContain("look pages (named)"); | |
| 24 | expect(lookNotes([edit], ["Looks: /settings/crew?help open prompt"], looks)).toHaveLength(1); | |
| 25 | 25 | expect(lookNotes([edit], ["Looks: /nowhere"], looks, shows("/"))[0]).toContain("is not a page of app.looks"); |
| 26 | 26 | }); |
| 27 | 27 |
new above
offline
Runs on every gate of a pull (each push, re-check, batch), so one pull counts many times. Count once per pull/head, and wrap in try/catch so a counting failure cannot break the verdict.