Public: anyone can read this; the crew can change it.
The release tick's gate passes on main's tip: test/attention.test.ts "each page stays inside its first-view budget" fails there and passes alone #15510
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15505-the-release-tick-s-gate-passes-on-main-s → main 0 commits fork
3 files
changed +297
−137
test/attention-stamped.test.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * The first-view budget holds in a stamped process (#15505). The release tick | |
| 3 | * gates main's tip with the version stamp a release carries (FORGE_VERSION and | |
| 4 | * FORGE_COMMIT, as the Dockerfile sets them); there /changelog's "Running …" | |
| 5 | * line named "0.7.629 · commit abc1234" where an unstamped run reads "a | |
| 6 | * development build", one word more, and the tick held on | |
| 7 | * test/attention.test.ts while the same file passed alone. A file before it | |
| 8 | * in the same process that leaves the stamp set does the same. Here the stamp | |
| 9 | * is set first, and the pages must still count as the budget file says. | |
| 10 | */ | |
| 11 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 12 | import { readFileSync } from "node:fs"; | |
| 13 | import { quietLogs } from "./helpers/globals.ts"; | |
| 14 | import { freshStallState } from "./helpers/stall-state.ts"; | |
| 15 | import { firstViewPages, measure, overBudget, STAMP, type Budget } from "./helpers/first-views.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | freshStallState(); | |
| 19 | ||
| 20 | const budget = JSON.parse(readFileSync("docs/design/attention-budget.json", "utf8")) as Budget; | |
| 21 | ||
| 22 | describe("the first-view budget in a process stamped as a release", () => { | |
| 23 | const stamp: Record<string, string> = { | |
| 24 | FORGE_VERSION: "0.7.629", | |
| 25 | FORGE_COMMIT: "2aec48903cd3bfca486aba28a9af9e2e6cef5097", | |
| 26 | FORGE_APP_COMMIT: "2aec48903cd3bfca486aba28a9af9e2e6cef5097", | |
| 27 | FORGE_APP_RELEASE: "0.7.629", | |
| 28 | }; | |
| 29 | const before = Object.fromEntries(STAMP.map((k) => [k, process.env[k]])); | |
| 30 | let pages: Record<string, string> = {}; | |
| 31 | beforeAll(async () => { | |
| 32 | Object.assign(process.env, stamp); | |
| 33 | pages = await firstViewPages(); | |
| 34 | }); | |
| 35 | afterAll(() => { | |
| 36 | for (const k of STAMP) { | |
| 37 | if (before[k] === undefined) delete process.env[k]; | |
| 38 | else process.env[k] = before[k]; | |
| 39 | } | |
| 40 | }); | |
| 41 | ||
| 42 | test("every page stays inside its budget, /changelog included", () => { | |
| 43 | expect(overBudget(measure(pages), budget)).toEqual([]); | |
| 44 | expect(pages.changelog).toContain("a development build"); | |
| 45 | }); | |
| 46 | ||
| 47 | test("the stamp the process had is put back once the pages are drawn", () => { | |
| 48 | for (const k of STAMP) expect(process.env[k]).toBe(stamp[k]!); | |
| 49 | }); | |
| 50 | }); | |
| 51 | ||
| 52 | test("a page over its budget is named with its width and the key that went over", () => { | |
| 53 | const at = { words: 19, blocks: 4, colours: 0 }; | |
| 54 | const seen: Budget = { changelog: { "390": at, "1280": { ...at, words: 18 } } }; | |
| 55 | const max: Budget = { changelog: { "390": { words: 18, blocks: 5, colours: 0 }, "1280": { words: 18, blocks: 5, colours: 0 } } }; | |
| 56 | expect(overBudget(seen, max)).toEqual(["changelog @390: words 19 > budget 18"]); | |
| 57 | expect(overBudget({ nowhere: seen.changelog! }, max)).toEqual(["nowhere @390: no budget", "nowhere @1280: no budget"]); | |
| 58 | }); |
test/attention.test.ts +18 −137
| @@ -4,152 +4,37 @@ | ||
| 4 | 4 | * docs/design/attention-budget.json. A page over its budget fails. A pass may |
| 5 | 5 | * lower a budget (run with FORGE_ATTENTION_WRITE=1 to rewrite the file); |
| 6 | 6 | * nothing may raise one without a note in the pull. |
| 7 | * | |
| 8 | * The pages come from a forge `test/helpers/first-views.ts` builds, with the | |
| 9 | * version stamp unset while they are drawn, so the counts are the same alone, | |
| 10 | * after any other file, and in a stamped release gate (#15505). | |
| 7 | 11 | */ |
| 8 | 12 | import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 9 | import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { enableApp } from "../src/actions/apps.ts"; | |
| 13 | import { linkIssue } from "../src/actions/dependencies.ts"; | |
| 14 | import { openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 15 | import { openPull } from "../src/actions/pulls.ts"; | |
| 16 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 17 | import { createApp, type App } from "../src/app.ts"; | |
| 18 | import { createUser } from "../src/core/users.ts"; | |
| 19 | import { createRun } from "../src/core/runs.ts"; | |
| 20 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 13 | import { readFileSync, writeFileSync } from "node:fs"; | |
| 21 | 14 | import { firstView, WIDTHS, type Attention } from "../src/measure/attention.ts"; |
| 22 | import { proveAll } from "./helpers/verified.ts"; | |
| 23 | import { useLive, type Live } from "../src/system/live.ts"; | |
| 24 | import type { SystemView } from "../src/system/view.ts"; | |
| 25 | 15 | import { quietLogs } from "./helpers/globals.ts"; |
| 26 | 16 | import { freshStallState } from "./helpers/stall-state.ts"; |
| 17 | import { measure, openFirstViews, overBudget, pinnedSystem, type Budget, type FirstViews } from "./helpers/first-views.ts"; | |
| 27 | 18 | |
| 28 | 19 | quietLogs(); |
| 29 | 20 | // /system draws the process's stall watch and telemetry readers; start from a clean one. |
| 30 | 21 | freshStallState(); |
| 31 | 22 | |
| 32 | 23 | const BUDGET = "docs/design/attention-budget.json"; |
| 33 | type Budget = Record<string, Record<string, Attention>>; | |
| 34 | 24 | // An entry may carry a "note" string beside its widths; it is read by people only. |
| 35 | 25 | const widthsOf = (entry: object): string[] => Object.keys(entry).filter((k) => k !== "note"); |
| 36 | 26 | |
| 37 | let root = ""; | |
| 38 | let app: App; | |
| 39 | let servers: Servers; | |
| 40 | let cookie = ""; | |
| 27 | let views: FirstViews; | |
| 41 | 28 | let pages: Record<string, string> = {}; |
| 42 | const seen: Budget = {}; | |
| 43 | ||
| 44 | // /system draws the machine it runs on (cores, disks, processes, whether the | |
| 45 | // kernel caps cells), so the same code counts differently on every host. The | |
| 46 | // test hands it an empty reading and one fixed sentence for each cap line. | |
| 47 | const emptyView: SystemView = { | |
| 48 | at: "", seconds: 0, cpu: { busyPct: null, cores: [], history: [] }, temps: [], hottestC: null, load: null, | |
| 49 | memory: null, disks: [], net: [], io: [], processes: 0, groups: [], duplicates: [], | |
| 50 | }; | |
| 51 | const fixedLive: Live = { | |
| 52 | read: () => emptyView, subscribe: () => () => {}, running: () => false, publish: () => {}, | |
| 53 | }; | |
| 54 | const fixedCaps = (html: string): string => | |
| 55 | html.replace(/<p class="small" id="(cell-caps|cell-pids)"[^>]*>[\s\S]*?<\/p>/g, '<p class="small" id="$1">Each cell is capped.</p>'); | |
| 56 | ||
| 57 | // The queue's "oldest has waited Ns" is the clock's, not the page's: a loaded machine reads 1s | |
| 58 | // where a quiet one reads 0s, and the count of blocks moves with it. | |
| 59 | const fixedWait = (html: string): string => | |
| 60 | html.replace(/has waited \d+s/g, "has waited 0s").replace(/(data-label="Oldest" >)\d+s/g, "$10s"); | |
| 61 | ||
| 62 | // /system holds three readings that finish in the background (the largest directories, the | |
| 63 | // database's tables, the node's cluster read). The first request waits up to 0.8 s for the first | |
| 64 | // two, so a quiet machine shows them measured and a loaded one, or a later request, does not: +3 | |
| 65 | // words and +2 blocks on a page held at its budget exactly (#15504). The budget was set on the | |
| 66 | // unmeasured form, so the test pins that form whatever the machine managed to read. | |
| 67 | const UNREAD_CLUSTER = "the cluster is being read; reload in a moment"; | |
| 68 | const fixedReads = (html: string): string => | |
| 69 | html | |
| 70 | .replace( | |
| 71 | /<section class="settings-block" id="sys-largest">[\s\S]*?<\/section>/, | |
| 72 | `<section class="settings-block" id="sys-largest"><h2>Largest under the forge's data</h2><p class="muted small">Being measured; reload in a moment.</p></section>`, | |
| 73 | ) | |
| 74 | .replace(/<div class="status-row"><dt>Free inside<\/dt>[\s\S]*?<\/div>/, "") | |
| 75 | .replace( | |
| 76 | /<div class="table-scroll"><table class="table" data-dense aria-label="The \d+ largest tables[\s\S]*?<\/table><\/div>\s*<p class="muted small">Measured[\s\S]*?at most once an hour\.<\/p>/, | |
| 77 | `<p class="muted small">Its tables are being measured; reload in a moment.</p>`, | |
| 78 | ) | |
| 79 | .replace(/not read: not running in a cluster/g, UNREAD_CLUSTER); | |
| 80 | 29 | |
| 81 | 30 | beforeAll(async () => { |
| 82 | root = mkdtempSync(join(tmpdir(), "forge-attention-")); | |
| 83 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 84 | const u = await createUser(app.db, { | |
| 85 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 86 | password: "a-good-passphrase", adminIfFirst: true, | |
| 87 | }); | |
| 88 | if (!u.ok) throw new Error(u.error); | |
| 89 | proveAll(app); | |
| 90 | const actor = { id: u.value.id, username: "alice" } as never; | |
| 91 | const made = await createRepo(app, actor, { name: "vault" }); | |
| 92 | if (!made.ok) throw new Error(String(made.error)); | |
| 93 | const repo = made.value; | |
| 94 | for (const [branch, path] of [["main", "README.md"], ["feature/x", "x.ts"]] as const) { | |
| 95 | const c = await commitToBranch(app, u.value, repo, { | |
| 96 | message: "Start", branch, files: [{ path, content: "v\n" }], | |
| 97 | } as never); | |
| 98 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 99 | } | |
| 100 | const enabled = await enableApp(app, u.value, repo, { slug: "vault" }); | |
| 101 | if (!enabled.ok) throw new Error(String(enabled.error)); | |
| 102 | const issue = openIssue(app, u.value, repo, { title: "Make the vault faster", body: "It is slow." }); | |
| 103 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 104 | const pull = await openPull(app, u.value, repo, { title: "Add x", baseBranch: "main", headBranch: "feature/x" }); | |
| 105 | if (!pull.ok) throw new Error(String(pull.error)); | |
| 106 | const run = createRun(app.db, { repoId: repo.id, kind: "check", env: "main", commitOid: "0".repeat(40), reason: "push" }); | |
| 107 | useLive(fixedLive); | |
| 108 | servers = await serve(app); | |
| 109 | const signin = await fetch(`${servers.url}/signin`, { | |
| 110 | method: "POST", headers: { origin: servers.url }, | |
| 111 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), redirect: "manual", | |
| 112 | }); | |
| 113 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 114 | const r = "/alice/vault"; | |
| 115 | const paths: Record<string, string> = { | |
| 116 | home: "/", repository: r, app: `${r}/app`, pull: `${r}/pulls/${pull.value.number}`, | |
| 117 | issue: `${r}/issues/${issue.value.number}`, "issues list": `${r}/issues`, "pulls list": `${r}/pulls`, | |
| 118 | runs: `${r}/runs`, run: `${r}/runs/${run.id}`, settings: "/settings", | |
| 119 | search: "/search", "search results": "/search?q=forge", map: "/map", flow: "/flow", system: "/system", | |
| 120 | explore: "/explore", changelog: "/changelog", boards: "/boards", forges: "/forges", crew: `${r}/settings/crew`, | |
| 121 | }; | |
| 122 | for (const [name, path] of Object.entries(paths)) { | |
| 123 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie }, redirect: "follow" }); | |
| 124 | if (res.status !== 200) throw new Error(`${name} ${path} answered ${res.status}`); | |
| 125 | const text = await res.text(); | |
| 126 | pages[name] = name === "system" ? fixedReads(fixedCaps(text)) : name === "boards" ? fixedWait(text) : text; | |
| 127 | } | |
| 128 | // A milestone (#14795), made after the pages above so their counts stay put: | |
| 129 | // one open step and one done, which folds behind its count. | |
| 130 | const steps = ["Seal the vault", "Count the keys"].map((title) => { | |
| 131 | const made = openIssue(app, u.value, repo, { title, body: "" }); | |
| 132 | if (!made.ok) throw new Error(String(made.error)); | |
| 133 | const linked = linkIssue(app, u.value, repo, issue.value, made.value); | |
| 134 | if (!linked.ok) throw new Error(String(linked.error)); | |
| 135 | return made.value; | |
| 136 | }); | |
| 137 | if (!setIssueState(app, u.value, repo, steps[1]!, "closed").ok) throw new Error("close"); | |
| 138 | const res = await fetch(`${servers.url}${r}/milestones/${issue.value.number}`, { headers: { cookie } }); | |
| 139 | if (res.status !== 200) throw new Error(`milestone answered ${res.status}`); | |
| 140 | pages.milestone = await res.text(); | |
| 141 | }); | |
| 142 | afterAll(() => { | |
| 143 | servers.stop(); | |
| 144 | app.close(); | |
| 145 | rmSync(root, { recursive: true, force: true }); | |
| 31 | views = await openFirstViews(); | |
| 32 | pages = views.pages; | |
| 146 | 33 | }); |
| 34 | afterAll(() => views.close()); | |
| 147 | 35 | |
| 148 | 36 | test("each page stays inside its first-view budget", () => { |
| 149 | for (const [name, html] of Object.entries(pages)) { | |
| 150 | seen[name] = {}; | |
| 151 | for (const w of WIDTHS) seen[name]![String(w)] = firstView(html, w); | |
| 152 | } | |
| 37 | const seen = measure(pages); | |
| 153 | 38 | if (process.env.FORGE_ATTENTION_WRITE === "1") { |
| 154 | 39 | const old = JSON.parse(readFileSync(BUDGET, "utf8")) as Record<string, { note?: string }>; |
| 155 | 40 | const out: Record<string, unknown> = {}; |
| @@ -157,31 +42,27 @@ test("each page stays inside its first-view budget", () => { | ||
| 157 | 42 | writeFileSync(BUDGET, `${JSON.stringify(out, null, 2)}\n`); |
| 158 | 43 | } |
| 159 | 44 | const budget = JSON.parse(readFileSync(BUDGET, "utf8")) as Budget; |
| 160 | const rows: string[] = ["page".padEnd(13) + "width words blocks colours"]; | |
| 161 | const over: string[] = []; | |
| 45 | const rows: string[] = ["page".padEnd(15) + "width words blocks colours"]; | |
| 162 | 46 | for (const name of Object.keys(pages)) |
| 163 | 47 | for (const w of WIDTHS) { |
| 164 | 48 | const got = seen[name]![String(w)]!; |
| 165 | 49 | const max = budget[name]?.[String(w)]; |
| 166 | 50 | const cell = (k: keyof Attention): string => `${got[k]}/${max?.[k] ?? "-"}`.padEnd(8); |
| 167 | rows.push(`${name.padEnd(13)}${String(w).padEnd(7)}${cell("words")}${cell("blocks")}${cell("colours")}`); | |
| 168 | if (max === undefined) over.push(`${name} @${w}: no budget`); | |
| 169 | else for (const k of ["words", "blocks", "colours"] as const) | |
| 170 | if (got[k] > max[k]) over.push(`${name} @${w}: ${k} ${got[k]} > budget ${max[k]}`); | |
| 51 | rows.push(`${name.padEnd(15)}${String(w).padEnd(7)}${cell("words")}${cell("blocks")}${cell("colours")}`); | |
| 171 | 52 | } |
| 172 | 53 | console.log(`${rows.join("\n")}\n(seen/budget)`); |
| 173 | expect(over).toEqual([]); | |
| 54 | // Each line names the page, the width and the key that went over: "changelog @390: words 19 > | |
| 55 | // budget 18". | |
| 56 | expect(overBudget(seen, budget)).toEqual([]); | |
| 174 | 57 | }); |
| 175 | 58 | |
| 176 | 59 | test("a /system read once its background measures finish counts as the unmeasured page", async () => { |
| 177 | 60 | // The unmeasured page is what the budget holds; the measured one is the machine's luck. Each |
| 178 | 61 | // read waits up to 0.8 s for the measures, so asking again is the wait. |
| 179 | 62 | let measured = ""; |
| 180 | for (let i = 0; i < 20 && !measured.includes("at most once an hour"); i++) { | |
| 181 | measured = await (await fetch(`${servers.url}/system`, { headers: { cookie } })).text(); | |
| 182 | } | |
| 63 | for (let i = 0; i < 20 && !measured.includes("at most once an hour"); i++) measured = await views.get("/system"); | |
| 183 | 64 | expect(measured).toContain("at most once an hour"); |
| 184 | const pinned = fixedReads(fixedCaps(measured)); | |
| 65 | const pinned = pinnedSystem(measured); | |
| 185 | 66 | expect(pinned).not.toContain("at most once an hour"); |
| 186 | 67 | expect(pinned).not.toContain("not running in a cluster"); |
| 187 | 68 | for (const w of WIDTHS) { |
test/helpers/first-views.ts added +221
| @@ -0,0 +1,221 @@ | ||
| 1 | /** | |
| 2 | * The pages the first-view budget measures (#10297), drawn from a forge this | |
| 3 | * helper builds itself, so the counts depend on nothing outside it (#15505). | |
| 4 | * | |
| 5 | * The process's version stamp is part of that fixture. `running()` | |
| 6 | * (`src/kernel/version.ts`) reads FORGE_VERSION, FORGE_COMMIT, | |
| 7 | * FORGE_APP_COMMIT and FORGE_APP_RELEASE from `process.env`, and since #15361 | |
| 8 | * /changelog's first line names them: "Running a development build" unstamped, | |
| 9 | * "Running 0.7.629 · commit abc1234" stamped, one word more. The release | |
| 10 | * tick gates a stamped tree, so the same test counted 19 words there and 18 | |
| 11 | * alone. While the pages are drawn the stamp is unset, as a development build | |
| 12 | * has it and as the budget file was measured, and then put back as it was. | |
| 13 | */ | |
| 14 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 15 | import { tmpdir } from "node:os"; | |
| 16 | import { join } from "node:path"; | |
| 17 | import { enableApp } from "../../src/actions/apps.ts"; | |
| 18 | import { linkIssue } from "../../src/actions/dependencies.ts"; | |
| 19 | import { openIssue, setIssueState } from "../../src/actions/issues.ts"; | |
| 20 | import { openPull } from "../../src/actions/pulls.ts"; | |
| 21 | import { commitToBranch, createRepo } from "../../src/actions/repos.ts"; | |
| 22 | import { createApp } from "../../src/app.ts"; | |
| 23 | import { createUser } from "../../src/core/users.ts"; | |
| 24 | import { createRun } from "../../src/core/runs.ts"; | |
| 25 | import { serve } from "../../src/http/server.ts"; | |
| 26 | import { firstView, WIDTHS, type Attention } from "../../src/measure/attention.ts"; | |
| 27 | import { useLive, type Live } from "../../src/system/live.ts"; | |
| 28 | import type { SystemView } from "../../src/system/view.ts"; | |
| 29 | import { proveAll } from "./verified.ts"; | |
| 30 | ||
| 31 | /** page → width → what its first view holds. */ | |
| 32 | export type Budget = Record<string, Record<string, Attention>>; | |
| 33 | ||
| 34 | /** The variables `running()` reads: the stamp a release build carries. */ | |
| 35 | export const STAMP = ["FORGE_VERSION", "FORGE_COMMIT", "FORGE_APP_COMMIT", "FORGE_APP_RELEASE"] as const; | |
| 36 | ||
| 37 | /** Runs `body` with each named variable set as given (undefined unsets it), then puts each back. */ | |
| 38 | export async function withEnv<T>(vars: Readonly<Record<string, string | undefined>>, body: () => Promise<T>): Promise<T> { | |
| 39 | const before = Object.fromEntries(Object.keys(vars).map((k) => [k, process.env[k]])); | |
| 40 | const put = (from: Readonly<Record<string, string | undefined>>): void => { | |
| 41 | for (const [k, v] of Object.entries(from)) { | |
| 42 | if (v === undefined) delete process.env[k]; | |
| 43 | else process.env[k] = v; | |
| 44 | } | |
| 45 | }; | |
| 46 | put(vars); | |
| 47 | try { | |
| 48 | return await body(); | |
| 49 | } finally { | |
| 50 | put(before); | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | // /system draws the machine it runs on (cores, disks, processes, whether the | |
| 55 | // kernel caps cells), so the same code counts differently on every host. The | |
| 56 | // pages get an empty reading and one fixed sentence for each cap line. | |
| 57 | const emptyView: SystemView = { | |
| 58 | at: "", seconds: 0, cpu: { busyPct: null, cores: [], history: [] }, temps: [], hottestC: null, load: null, | |
| 59 | memory: null, disks: [], net: [], io: [], processes: 0, groups: [], duplicates: [], | |
| 60 | }; | |
| 61 | const fixedLive: Live = { | |
| 62 | read: () => emptyView, subscribe: () => () => {}, running: () => false, publish: () => {}, | |
| 63 | }; | |
| 64 | const fixedCaps = (html: string): string => | |
| 65 | html.replace(/<p class="small" id="(cell-caps|cell-pids)"[^>]*>[\s\S]*?<\/p>/g, '<p class="small" id="$1">Each cell is capped.</p>'); | |
| 66 | ||
| 67 | // The queue's "oldest has waited Ns" is the clock's, not the page's: a loaded machine reads 1s | |
| 68 | // where a quiet one reads 0s, and the count of blocks moves with it. | |
| 69 | const fixedWait = (html: string): string => | |
| 70 | html.replace(/has waited \d+s/g, "has waited 0s").replace(/(data-label="Oldest" >)\d+s/g, "$10s"); | |
| 71 | ||
| 72 | // /system holds three readings that finish in the background (the largest directories, the | |
| 73 | // database's tables, the node's cluster read). The first request waits up to 0.8 s for the first | |
| 74 | // two, so a quiet machine shows them measured and a loaded one, or a later request, does not: +3 | |
| 75 | // words and +2 blocks on a page held at its budget exactly (#15504). The budget was set on the | |
| 76 | // unmeasured form, so the pages pin that form whatever the machine managed to read. | |
| 77 | const UNREAD_CLUSTER = "the cluster is being read; reload in a moment"; | |
| 78 | const fixedReads = (html: string): string => | |
| 79 | html | |
| 80 | .replace( | |
| 81 | /<section class="settings-block" id="sys-largest">[\s\S]*?<\/section>/, | |
| 82 | `<section class="settings-block" id="sys-largest"><h2>Largest under the forge's data</h2><p class="muted small">Being measured; reload in a moment.</p></section>`, | |
| 83 | ) | |
| 84 | .replace(/<div class="status-row"><dt>Free inside<\/dt>[\s\S]*?<\/div>/, "") | |
| 85 | .replace( | |
| 86 | /<div class="table-scroll"><table class="table" data-dense aria-label="The \d+ largest tables[\s\S]*?<\/table><\/div>\s*<p class="muted small">Measured[\s\S]*?at most once an hour\.<\/p>/, | |
| 87 | `<p class="muted small">Its tables are being measured; reload in a moment.</p>`, | |
| 88 | ) | |
| 89 | .replace(/not read: not running in a cluster/g, UNREAD_CLUSTER); | |
| 90 | ||
| 91 | /** /system as the budget counts it: the cap lines fixed and the background readings unmeasured. */ | |
| 92 | export const pinnedSystem = (html: string): string => fixedReads(fixedCaps(html)); | |
| 93 | ||
| 94 | /** The measured pages, and the forge they came from, still serving until `close`. */ | |
| 95 | export interface FirstViews { | |
| 96 | readonly pages: Record<string, string>; | |
| 97 | /** | |
| 98 | * A page of the same forge as the signed-in owner, drawn with the stamp unset as the pages were. | |
| 99 | */ | |
| 100 | readonly get: (path: string) => Promise<string>; | |
| 101 | readonly close: () => void; | |
| 102 | } | |
| 103 | ||
| 104 | const UNSTAMPED_ENV = (): Record<string, undefined> => Object.fromEntries(STAMP.map((k) => [k, undefined])); | |
| 105 | ||
| 106 | /** | |
| 107 | * Every measured page's HTML, by its name in the budget file, from a fresh forge; the forge is gone | |
| 108 | * after. | |
| 109 | */ | |
| 110 | export async function firstViewPages(): Promise<Record<string, string>> { | |
| 111 | const views = await openFirstViews(); | |
| 112 | views.close(); | |
| 113 | return views.pages; | |
| 114 | } | |
| 115 | ||
| 116 | /** The measured pages from a fresh forge that keeps serving; the caller closes it. */ | |
| 117 | export function openFirstViews(): Promise<FirstViews> { | |
| 118 | return withEnv(UNSTAMPED_ENV(), drawPages); | |
| 119 | } | |
| 120 | ||
| 121 | async function drawPages(): Promise<FirstViews> { | |
| 122 | const root = mkdtempSync(join(tmpdir(), "forge-attention-")); | |
| 123 | const app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 124 | let stop = (): void => {}; | |
| 125 | const close = (): void => { | |
| 126 | stop(); | |
| 127 | app.close(); | |
| 128 | rmSync(root, { recursive: true, force: true }); | |
| 129 | }; | |
| 130 | try { | |
| 131 | const u = await createUser(app.db, { | |
| 132 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 133 | password: "a-good-passphrase", adminIfFirst: true, | |
| 134 | }); | |
| 135 | if (!u.ok) throw new Error(u.error); | |
| 136 | proveAll(app); | |
| 137 | const actor = { id: u.value.id, username: "alice" } as never; | |
| 138 | const made = await createRepo(app, actor, { name: "vault" }); | |
| 139 | if (!made.ok) throw new Error(String(made.error)); | |
| 140 | const repo = made.value; | |
| 141 | for (const [branch, path] of [["main", "README.md"], ["feature/x", "x.ts"]] as const) { | |
| 142 | const c = await commitToBranch(app, u.value, repo, { | |
| 143 | message: "Start", branch, files: [{ path, content: "v\n" }], | |
| 144 | } as never); | |
| 145 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 146 | } | |
| 147 | const enabled = await enableApp(app, u.value, repo, { slug: "vault" }); | |
| 148 | if (!enabled.ok) throw new Error(String(enabled.error)); | |
| 149 | const issue = openIssue(app, u.value, repo, { title: "Make the vault faster", body: "It is slow." }); | |
| 150 | if (!issue.ok) throw new Error(String(issue.error)); | |
| 151 | const pull = await openPull(app, u.value, repo, { title: "Add x", baseBranch: "main", headBranch: "feature/x" }); | |
| 152 | if (!pull.ok) throw new Error(String(pull.error)); | |
| 153 | const run = createRun(app.db, { repoId: repo.id, kind: "check", env: "main", commitOid: "0".repeat(40), reason: "push" }); | |
| 154 | useLive(fixedLive); | |
| 155 | const servers = await serve(app); | |
| 156 | stop = () => servers.stop(); | |
| 157 | const signin = await fetch(`${servers.url}/signin`, { | |
| 158 | method: "POST", headers: { origin: servers.url }, | |
| 159 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), redirect: "manual", | |
| 160 | }); | |
| 161 | const cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 162 | const r = "/alice/vault"; | |
| 163 | const paths: Record<string, string> = { | |
| 164 | home: "/", repository: r, app: `${r}/app`, pull: `${r}/pulls/${pull.value.number}`, | |
| 165 | issue: `${r}/issues/${issue.value.number}`, "issues list": `${r}/issues`, "pulls list": `${r}/pulls`, | |
| 166 | runs: `${r}/runs`, run: `${r}/runs/${run.id}`, settings: "/settings", | |
| 167 | search: "/search", "search results": "/search?q=forge", map: "/map", flow: "/flow", system: "/system", | |
| 168 | explore: "/explore", changelog: "/changelog", boards: "/boards", forges: "/forges", crew: `${r}/settings/crew`, | |
| 169 | }; | |
| 170 | const pages: Record<string, string> = {}; | |
| 171 | for (const [name, path] of Object.entries(paths)) { | |
| 172 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie }, redirect: "follow" }); | |
| 173 | if (res.status !== 200) throw new Error(`${name} ${path} answered ${res.status}`); | |
| 174 | const text = await res.text(); | |
| 175 | pages[name] = name === "system" ? pinnedSystem(text) : name === "boards" ? fixedWait(text) : text; | |
| 176 | } | |
| 177 | // A milestone (#14795), made after the pages above so their counts stay put: | |
| 178 | // one open step and one done, which folds behind its count. | |
| 179 | const steps = ["Seal the vault", "Count the keys"].map((title) => { | |
| 180 | const step = openIssue(app, u.value, repo, { title, body: "" }); | |
| 181 | if (!step.ok) throw new Error(String(step.error)); | |
| 182 | const linked = linkIssue(app, u.value, repo, issue.value, step.value); | |
| 183 | if (!linked.ok) throw new Error(String(linked.error)); | |
| 184 | return step.value; | |
| 185 | }); | |
| 186 | if (!setIssueState(app, u.value, repo, steps[1]!, "closed").ok) throw new Error("close"); | |
| 187 | const res = await fetch(`${servers.url}${r}/milestones/${issue.value.number}`, { headers: { cookie } }); | |
| 188 | if (res.status !== 200) throw new Error(`milestone answered ${res.status}`); | |
| 189 | pages.milestone = await res.text(); | |
| 190 | const get = (path: string): Promise<string> => | |
| 191 | withEnv(UNSTAMPED_ENV(), async () => (await fetch(`${servers.url}${path}`, { headers: { cookie } })).text()); | |
| 192 | return { pages, get, close }; | |
| 193 | } catch (e) { | |
| 194 | close(); | |
| 195 | throw e; | |
| 196 | } | |
| 197 | } | |
| 198 | ||
| 199 | /** What each page's first view holds at each width. */ | |
| 200 | export function measure(pages: Readonly<Record<string, string>>): Budget { | |
| 201 | const seen: Budget = {}; | |
| 202 | for (const [name, html] of Object.entries(pages)) { | |
| 203 | seen[name] = {}; | |
| 204 | for (const w of WIDTHS) seen[name]![String(w)] = firstView(html, w); | |
| 205 | } | |
| 206 | return seen; | |
| 207 | } | |
| 208 | ||
| 209 | /** One line per page, width and key over its budget: "changelog @390: words 19 > budget 18". */ | |
| 210 | export function overBudget(seen: Budget, budget: Budget): string[] { | |
| 211 | const over: string[] = []; | |
| 212 | for (const name of Object.keys(seen)) | |
| 213 | for (const w of WIDTHS) { | |
| 214 | const got = seen[name]![String(w)]!; | |
| 215 | const max = budget[name]?.[String(w)]; | |
| 216 | if (max === undefined) over.push(`${name} @${w}: no budget`); | |
| 217 | else for (const k of ["words", "blocks", "colours"] as const) | |
| 218 | if (got[k] > max[k]) over.push(`${name} @${w}: ${k} ${got[k]} > budget ${max[k]}`); | |
| 219 | } | |
| 220 | return over; | |
| 221 | } |
new above
offline