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

Fast everywhere: displays first, then a time budget every page is tested against #10448

Parked opened this in answer to
Closed without merging

torque/forge:torque/10445-fast-everywhere-displays-first-then-a-ti → main 9 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
27 files changed +1105 −96
bench/display-phases.ts added +77
@@ -0,0 +1,77 @@
1 /**
2 * A display's page and `/displays`, warm: Server-Timing phases p50 and p95
3 * in ms, and git work per request (#10445). Six source repositories of
4 * twelve showcased builds each, read by the display's writer:
5 * `bun run bench/display-phases.ts [n]`.
6 */
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
11 import { createApp } from "../src/app.ts";
12 import { getRepo } from "../src/core/repos.ts";
13 import { createSession } from "../src/core/sessions.ts";
14 import { createUser } from "../src/core/users.ts";
15 import { pinBuild } from "../src/git/builds.ts";
16 import { repoDir } from "../src/git/repos.ts";
17 import { serve } from "../src/http/server.ts";
18 import { gitWork, gitWorkSince } from "../src/perf/git_work.ts";
19 import { proveAll } from "../test/helpers/verified.ts";
20
21 const n = Number(process.argv[2]) > 0 ? Number(process.argv[2]) : 30;
22 const SOURCES = 6;
23 const BUILDS = 12;
24
25 const dataDir = await mkdtemp(join(tmpdir(), "forge-bench-display-"));
26 const app = createApp({ dataDir, port: 0 });
27 const servers = await serve(app);
28 try {
29 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
30 if (!made.ok) throw new Error(made.error);
31 const alice = made.value;
32 proveAll(app);
33 const cookie = `forge_session=${createSession(app.db, alice.id).token}`;
34 const commit = async (name: string, path: string, content: string) => {
35 const done = await commitToBranch(app, alice, getRepo(app.db, "alice", name)!, { message: path, files: [{ path, content }] });
36 if (!done.ok) throw new Error(JSON.stringify(done.error));
37 return done.value.commitOid;
38 };
39 const names = Array.from({ length: SOURCES }, (_, i) => `games-${String(i)}`);
40 let at = 1_800_000_000;
41 for (const name of [...names, "shelf"]) await createRepo(app, alice, { name, visibility: "public" });
42 for (const name of names)
43 for (let b = 0; b < BUILDS; b++) {
44 const oid = await commit(name, "forge.json", JSON.stringify({ app: { run: "bun run main.ts", pages: ["/", `/p${String(b)}`] } }));
45 await pinBuild(repoDir(app.reposRoot, "alice", name), {
46 label: `v${String(b)}`, oid, note: "", source: "commit", tagger: { name: "alice", email: "[email protected]" }, at: at++,
47 demo: { name, version: `v${String(b)}`, page: "/", showcase: true },
48 });
49 }
50 await commit("shelf", "display.json", JSON.stringify({ title: "Games shelf", layout: "shelf", sources: names.map((s) => `alice/${s}`) }));
51
52 for (const path of ["/alice/shelf/display", "/displays"]) {
53 const seen = new Map<string, number[]>();
54 let work = 0;
55 for (let i = 0; i < n + 1; i++) {
56 const before = gitWork();
57 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
58 await res.arrayBuffer();
59 if (i === 0) continue;
60 const w = gitWorkSince(before);
61 work = w.spawned + w.lookups;
62 for (const m of (res.headers.get("server-timing") ?? "").matchAll(/([a-z-]+);dur=([\d.]+)/g))
63 if (m[1] !== "lag") (seen.get(m[1]!) ?? seen.set(m[1]!, []).get(m[1]!)!).push(Number(m[2]));
64 }
65 console.log(`\n\`GET ${path}\`, ${String(SOURCES * BUILDS)} builds in ${String(SOURCES)} sources, ${String(n)} warm hits, git work ${String(work)} per request\n`);
66 console.log("| phase | p50 ms | p95 ms |\n| ----- | -----: | -----: |");
67 for (const [name, xs] of seen) {
68 xs.sort((a, b) => a - b);
69 const q = (p: number) => xs[Math.min(xs.length - 1, Math.floor((xs.length * p) / 100))]!;
70 console.log(`| \`${name}\` | ${q(50).toFixed(1)} | ${q(95).toFixed(1)} |`);
71 }
72 }
73 } finally {
74 servers.stop();
75 app.close();
76 await rm(dataDir, { recursive: true, force: true });
77 }
docs/features/page-budgets.md added +41
@@ -0,0 +1,41 @@
1 # Page budgets
2
3 Every page the forge serves has one budget (#10445), in `src/perf/budgets.ts`:
4
5 - **Server:** p95 under 150 ms (`PAGE_BUDGET`, `pageBudgetFor`), for every route
6 pattern that is a page. The API, assets, streams and live fragments are not
7 pages (`isPagePattern`).
8 - **First paint:** p75 under 1 s on a real browser (`PAINT_BUDGET`): the Largest
9 Contentful Paint the page-view beacon reports (`src/web/beacon.ts`). p75 is
10 the share web-vitals judges by. Safari reports no LCP and is not counted.
11
12 ## Where it is held
13
14 - **In the check, by work.** `test/page-budget-walk.test.ts` walks every GET
15 page the router registers, on a forge with fixture data (showcased builds and
16 a display over them). It holds each warm request to at most 6 units of git work
17 (processes spawned plus objects looked up, counted by `src/perf/git_work.ts`)
18 and 100 SQL statements. A page over that fails by name, with its counts and
19 its slowest Server-Timing phase. Work and not the clock, as
20 `docs/PRINCIPLES.md` asks: an N+1 shows in every run, on any machine.
21 - **In production, by the clock.** The performance watcher (`src/perf/watch.ts`)
22 files a finding at its own looser budgets (`BUDGETS`: 800 ms for home and the
23 repository page, 1 s for a repository's sub-pages), after 15 minutes over
24 with at least 20 requests. That way it opens an issue for a page that is badly
25 slow, and not one for every page on the day the page budget arrived.
26 - **On the ops view.** `/system`, for the owner, opens with **Pages over
27 budget** only when a page misses: one line per page, giving the server p95 and
28 its slowest phase, or how many of today's and yesterday's views painted after
29 1 s (at least 20 views). When every page is fast, the section is not drawn
30 (`src/perf/page_budgets.ts`).
31 - **In the terminal.** The same lines, for the same people (a forge admin who
32 is a person): ssh to the forge, open the command palette and choose **Pages
33 over budget** (`src/tui/screens/overbudget.ts`), listed after Main-thread
34 stalls. It reads once when opened, and says so when every page is within
35 its budget.
36
37 ## Measuring one page
38
39 `bun run bench/display-phases.ts [n]` prints a display page's and `/displays`'
40 Server-Timing phases, p50 and p95, and their git work per request, over 72
41 builds in 6 source repositories.
scripts/unreached-allow.json −1
@@ -15,7 +15,6 @@
1515 "src/flow/review_rounds.ts",
1616 "src/flow/shared_red.ts",
1717 "src/flow/stale_fold.ts",
18 "src/kernel/why_now.ts",
1918 "src/spawn/pod.ts",
2019 "src/tui/gallery.ts",
2120 "src/tui/ui/qr.ts",
src/actions/displays.ts +12 −4
@@ -12,6 +12,7 @@ import { canRead, getRepo, type Repo } from "../core/repos.ts";
1212 import type { User } from "../core/users.ts";
1313 import { DISPLAY_FILE, parseBuilds, parseDisplay } from "../display/config.ts";
1414 import { sourceRepos } from "../display/items.ts";
15 import { DisplayReads } from "../display/reads.ts";
1516 import { writesDisplay } from "../display/selections.ts";
1617 import { exactRefOid } from "../git/branches.ts";
1718 import { listBuildPins } from "../git/builds.ts";
@@ -86,11 +87,18 @@ export async function chooseBuilds(app: App, user: User | undefined, repo: Repo,
8687 }
8788
8889 /** Every build the person could add: the pins of the display's sources they read, plus those already chosen. */
89 export async function choosableBuilds(app: App, sources: Parameters<typeof sourceRepos>[1], chosen: readonly string[], user: User | undefined): Promise<string[]> {
90 export async function choosableBuilds(
91 app: App,
92 sources: Parameters<typeof sourceRepos>[1],
93 chosen: readonly string[],
94 user: User | undefined,
95 reads: DisplayReads = new DisplayReads(app),
96 ): Promise<string[]> {
9097 const out = [...chosen];
91 for (const r of sourceRepos(app, sources)) {
92 if (!canRead(app.db, r, user)) continue;
93 for (const pin of await listBuildPins(repoDir(app.reposRoot, r.ownerName, r.name))) {
98 const readable = sourceRepos(app, sources).filter((r) => canRead(app.db, r, user));
99 const pinsOf = await Promise.all(readable.map((r) => reads.pins(r)));
100 for (const [i, r] of readable.entries()) {
101 for (const pin of pinsOf[i]!) {
94102 const key = `${r.ownerName}/${r.name}:${pin.label}`;
95103 if (!out.includes(key)) out.push(key);
96104 }
src/actions/pin_demos.ts +54 −22
@@ -20,36 +20,68 @@ export interface DemoFacts {
2020 readonly launch?: Launch | undefined;
2121 }
2222
23 /**
24 * What one commit's own `forge.json` says, and whether the object is a
25 * commit: a function of the commit's id, so kept by it (objects never
26 * change) — once the commit has been read; an absent object is asked again. Bounded: the oldest entry goes first past `FACTS_KEPT`.
27 */
28 const FACTS_KEPT = 4096;
29 const factsKept = new Map<string, Promise<{ readonly facts: DemoFacts; readonly commit: boolean }>>();
30
31 function commitFacts(dir: string, oid: string): Promise<{ readonly facts: DemoFacts; readonly commit: boolean }> {
32 const key = `${dir}\0${oid}`;
33 const seen = factsKept.get(key);
34 if (seen !== undefined) return seen;
35 const read = (async () => {
36 const [forgeJson, packageJson] = await Promise.all([
37 readFileAt(dir, oid, "forge.json"),
38 readFileAt(dir, oid, "package.json"),
39 ]);
40 const manifest = parseManifest(forgeJson, packageJson);
41 const facts: DemoFacts = !manifest.ok
42 ? { pages: [] }
43 : { pages: manifest.value.pages ?? [], ...(manifest.value.launch === undefined ? {} : { launch: manifest.value.launch }) };
44 // Only a commit with no pages of its own asks what it is (see `demoFactsAt`).
45 if (facts.pages.length > 0) return { facts, commit: true };
46 const stat = await readerFor(dir).stat(oid);
47 return { facts, commit: stat.ok && stat.value.type === "commit" };

This keeps {pages:[], commit:false} for an oid that was missing or had no manifest at first read. Don't cache a result from an object that was not found, or the page is stale until eviction.

48 })();
49 factsKept.set(key, read);
50 // Only what a commit said is kept. An object not (yet) in the repository —
51 // a fork's pin, a push still landing — and a failed read are not: the next
52 // asks again, so a pin made before its objects arrive recovers.
53 const forget = () => {
54 if (factsKept.get(key) === read) factsKept.delete(key);
55 };
56 read.then((r) => (r.commit ? undefined : forget()), forget);
57 if (factsKept.size > FACTS_KEPT) factsKept.delete(factsKept.keys().next().value!);
58 return read;
59 }
60
2361 /**
2462 * The pages and the launch a commit's `forge.json` names — or, for a
2563 * commit pinned before the repository named any pages, what its default
2664 * branch names now. A page the old build does not serve is simply not
27 * found when played.
65 * found when played. `tipOf` reads the default branch's tip; a page that
66 * asks for many pins passes one that reads it once (`display/reads.ts`).
2867 */
29 export async function demoFactsAt(app: App, repo: Repo, oid: string): Promise<DemoFacts> {
68 export async function demoFactsAt(
69 app: App,
70 repo: Repo,
71 oid: string,
72 tipOf: () => Promise<string | undefined> = () => exactRefOid(repoDir(app.reposRoot, repo.ownerName, repo.name), `refs/heads/${repo.defaultBranch}`),
73 ): Promise<DemoFacts> {
3074 if (!isSafeOid(oid)) return { pages: [] };
3175 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
32 const read = async (at: string): Promise<DemoFacts> => {
33 const [forgeJson, packageJson] = await Promise.all([
34 readFileAt(dir, at, "forge.json"),
35 readFileAt(dir, at, "package.json"),
36 ]);
37 const manifest = parseManifest(forgeJson, packageJson);
38 if (!manifest.ok) return { pages: [] };
39 return {
40 pages: manifest.value.pages ?? [],
41 ...(manifest.value.launch === undefined ? {} : { launch: manifest.value.launch }),
42 };
43 };
44 const own = await read(oid);
45 if (own.pages.length > 0) return own;
76 const own = await commitFacts(dir, oid);
77 if (own.facts.pages.length > 0) return own.facts;
4678 // Only a commit this repository holds borrows anything.
47 const stat = await readerFor(dir).stat(oid);
48 if (!stat.ok || stat.value.type !== "commit") return own;
49 const tip = await exactRefOid(dir, `refs/heads/${repo.defaultBranch}`);
50 if (tip === undefined || tip === oid) return own;
51 const now = await read(tip);
52 return { pages: now.pages, ...((own.launch ?? now.launch) === undefined ? {} : { launch: own.launch ?? now.launch }) };
79 if (!own.commit) return own.facts;
80 const tip = await tipOf();
81 if (tip === undefined || tip === oid || !isSafeOid(tip)) return own.facts;
82 const now = (await commitFacts(dir, tip)).facts;
83 const launch = own.facts.launch ?? now.launch;
84 return { pages: now.pages, ...(launch === undefined ? {} : { launch }) };
5385 }
5486
5587 /** The facts a pin's demo starts from, before anyone edits them. */
src/core/migration-pins/20261109200000-page-vital-route-paint.pin added +1
@@ -0,0 +1,1 @@
1 0bcd33c478f3478ed045a883c8d9ba54c5703ff0e78003dfd2ae09128bc31ad1
src/core/migrations/20261109200000-page-vital-route-paint.ts added +7
@@ -0,0 +1,7 @@
1 // The ops view's pages over budget (#10445) count, per page, today's views
2 // and those whose first paint (LCP) passed the budget. Read by day, grouped
3 // by route: this index answers it covered, in route order, with no scan and
4 // no sort. Additive.
5 export default `
6 CREATE INDEX page_vital_day_route_lcp ON page_vital(day, route, lcp);
7 `;
src/display/items.ts +60 −25
@@ -21,10 +21,10 @@ import { demoDefaults, demoFactsAt } from "../actions/pin_demos.ts";
2121 import { capturePath } from "../arcade/carts.ts";
2222 import { canRead, getRepo, listAllReposByOwner, ownerInStanding, type Repo } from "../core/repos.ts";
2323 import type { User } from "../core/users.ts";
24 import { isCopiedPin, launchOf, listBuildPins, type BuildPin, type Demo, type Launch } from "../git/builds.ts";
25 import { repoDir } from "../git/repos.ts";
24 import { isCopiedPin, launchOf, type BuildPin, type Demo, type Launch } from "../git/builds.ts";
2625 import { loadKey } from "../kernel/secret.ts";
2726 import { demoKey, type DisplayConfig } from "./config.ts";
27 import { DisplayReads } from "./reads.ts";
2828
2929 export interface DisplayItem {
3030 /** `owner/name:label`. Never shown to a viewer for whom the item is not `open`. */
@@ -87,23 +87,30 @@ export function sourceRepos(app: App, config: DisplayConfig): Repo[] {
8787 * With `builds`, the items are those builds in that order, showcased or
8888 * not: a showcased one is published to every viewer, as below; one not
8989 * showcased is seen only by a viewer who reads its repository. Without
90 * `builds`, every showcased pin in `sources`, in `order`.
90 * `builds`, every showcased pin in `sources`, in `order`. `reads` is the
91 * page's (`reads.ts`): pass the same one to `selectionKeys` and the rest, and
92 * each source's pins are read once.
9193 */
92 export async function displayItems(app: App, display: Repo, config: DisplayConfig, viewer: User | undefined): Promise<DisplayItem[]> {
93 if (config.builds !== undefined) return chosenItems(app, display, config, config.builds, viewer);
94 const found: DisplayItem[] = [];
95 for (const { repo, pin } of await showcasedPins(app, config))
96 found.push(await itemOf(app, display, config, repo, pin, pin.demo!, canRead(app.db, repo, viewer)));
94 export async function displayItems(
95 app: App,
96 display: Repo,
97 config: DisplayConfig,
98 viewer: User | undefined,
99 reads: DisplayReads = new DisplayReads(app),
100 ): Promise<DisplayItem[]> {
101 if (config.builds !== undefined) return chosenItems(app, display, config, config.builds, viewer, reads);
102 const shown = await showcasedPins(app, config, reads);
103 const found = await Promise.all(
104 shown.map(({ repo, pin }) => itemOf(app, display, config, repo, pin, pin.demo!, canRead(app.db, repo, viewer), reads)),
105 );
97106 return orderItems(found, config.order);
98107 }
99108
100109 /** Every showcased pin of the display's sources, each once: today's rule, before `builds`. */
101 async function showcasedPins(app: App, config: DisplayConfig): Promise<{ repo: Repo; pin: BuildPin }[]> {
110 async function showcasedPins(app: App, config: DisplayConfig, reads: DisplayReads): Promise<{ repo: Repo; pin: BuildPin }[]> {
102111 const out: { repo: Repo; pin: BuildPin }[] = [];
103112 const repos = sourceRepos(app, config);
104 const pinsOf = new Map(
105 await Promise.all(repos.map(async (r) => [r.id, await listBuildPins(repoDir(app.reposRoot, r.ownerName, r.name))] as const)),
106 );
113 const pinsOf = new Map(await Promise.all(repos.map(async (r) => [r.id, await reads.pins(r)] as const)));
107114 for (const repo of repos) {
108115 // A fork carries its parent's pins: one both sources hold shows once, from the parent.
109116 const parentPins = repo.forkParentId === null ? undefined : pinsOf.get(repo.forkParentId);
@@ -123,9 +130,14 @@ async function showcasedPins(app: App, config: DisplayConfig): Promise<{ repo: R
123130 * view, never shown). Reads no launch facts, so a page that lists
124131 * many displays' selections stays cheap.
125132 */
126 export async function selectionKeys(app: App, config: DisplayConfig, viewer: User | undefined | "everyone"): Promise<string[]> {
133 export async function selectionKeys(
134 app: App,
135 config: DisplayConfig,
136 viewer: User | undefined | "everyone",
137 reads: DisplayReads = new DisplayReads(app),
138 ): Promise<string[]> {
127139 if (config.builds !== undefined) return [...config.builds];
128 const found = (await showcasedPins(app, config))
140 const found = (await showcasedPins(app, config, reads))
129141 .filter(({ repo }) => viewer === "everyone" || canRead(app.db, repo, viewer))
130142 .map(({ repo, pin }) => ({ key: demoKey(repo.ownerName, repo.name, pin.label), pin }));
131143 return orderItems(found, config.order).map((i) => i.key);
@@ -135,13 +147,26 @@ export async function selectionKeys(app: App, config: DisplayConfig, viewer: Use
135147 * Items for the given keys, in that order, as `viewer` may see them: what a
136148 * display's page draws for builds that are not on it yet (its sidebar).
137149 */
138 export const itemsOfKeys = (app: App, display: Repo, config: DisplayConfig, keys: readonly string[], viewer: User | undefined): Promise<DisplayItem[]> =>
139 chosenItems(app, display, config, keys, viewer);
150 export const itemsOfKeys = (
151 app: App,
152 display: Repo,
153 config: DisplayConfig,
154 keys: readonly string[],
155 viewer: User | undefined,
156 reads: DisplayReads = new DisplayReads(app),
157 ): Promise<DisplayItem[]> => chosenItems(app, display, config, keys, viewer, reads);
140158
141159 /** The chosen builds a viewer may see on the display, in the chosen order. */
142 async function chosenItems(app: App, display: Repo, config: DisplayConfig, builds: readonly string[], viewer: User | undefined): Promise<DisplayItem[]> {
160 async function chosenItems(
161 app: App,
162 display: Repo,
163 config: DisplayConfig,
164 builds: readonly string[],
165 viewer: User | undefined,
166 reads: DisplayReads,
167 ): Promise<DisplayItem[]> {
143168 const byRepo = new Map<string, { repo: Repo | undefined; pins: Map<string, BuildPin> }>();
144 const out: DisplayItem[] = [];
169 const out: Promise<DisplayItem>[] = [];
145170 for (const key of builds) {
146171 const at = key.lastIndexOf(":");
147172 const [owner = "", name = ""] = key.slice(0, at).split("/");
@@ -151,7 +176,7 @@ async function chosenItems(app: App, display: Repo, config: DisplayConfig, build
151176 if (held === undefined) {
152177 const found = getRepo(app.db, owner, name);
153178 const repo = found !== undefined && ownerInStanding(app.db, found) ? found : undefined;
154 const pins = repo === undefined ? [] : await listBuildPins(repoDir(app.reposRoot, repo.ownerName, repo.name));
179 const pins = repo === undefined ? [] : await reads.pins(repo);
155180 held = { repo, pins: new Map(pins.map((p) => [p.label, p])) };
156181 byRepo.set(repoKey, held);
157182 }
@@ -161,23 +186,33 @@ async function chosenItems(app: App, display: Repo, config: DisplayConfig, build
161186 const showcased = pin.demo?.showcase === true;
162187 // Showcase publishes; anything else on a display is for its repository's readers alone.
163188 if (!showcased && !open) continue;
164 out.push(await itemOf(app, display, config, held.repo, pin, pin.demo ?? (await plainDemo(app, held.repo, pin)), open));
189 const repo = held.repo;
190 out.push(itemOf(app, display, config, repo, pin, pin.demo ?? (await plainDemo(app, repo, pin, reads)), open, reads));
165191 }
166 return out;
192 return Promise.all(out);
167193 }
168194
169195 /** What a display shows for a pin that names no demo: its repository and label, its default page, not showcased. */
170 async function plainDemo(app: App, repo: Repo, pin: BuildPin): Promise<Demo> {
171 const facts = await demoFactsAt(app, repo, pin.oid);
196 async function plainDemo(app: App, repo: Repo, pin: BuildPin, reads: DisplayReads): Promise<Demo> {
197 const facts = await demoFactsAt(app, repo, pin.oid, () => reads.tip(repo));
172198 return { ...demoDefaults(repo, pin.label, facts.pages), version: pin.label, showcase: false };
173199 }
174200
175 async function itemOf(app: App, display: Repo, config: DisplayConfig, repo: Repo, pin: BuildPin, demo: Demo, open: boolean): Promise<DisplayItem> {
201 async function itemOf(
202 app: App,
203 display: Repo,
204 config: DisplayConfig,
205 repo: Repo,
206 pin: BuildPin,
207 demo: Demo,
208 open: boolean,
209 reads: DisplayReads,
210 ): Promise<DisplayItem> {
176211 const key = demoKey(repo.ownerName, repo.name, pin.label);
177212 const override = config.demos.get(key)?.launch;
178213 const launch =
179214 override ?? demo.launch ?? (await (async () => {
180 const facts = await demoFactsAt(app, repo, pin.oid);
215 const facts = await demoFactsAt(app, repo, pin.oid, () => reads.tip(repo));
181216 return launchOf(demo, facts.launch, facts.pages);
182217 })());
183218 const base = capturePath(app.config.dataDir, repo, pin.oid, demo.page);
src/display/reads.ts added +41
@@ -0,0 +1,41 @@
1 /**
2 * One page's git reads of a display's sources (#10445): each repository's
3 * pins listed once and its default branch's tip read once, however many
4 * parts of the page ask — the items, the writer's selection and the
5 * choices all share one `DisplayReads`. Made per request and dropped with
6 * it, so nothing here can go stale: a pin pinned after the page began is
7 * on the next page.
8 */
9
10 import type { App } from "../app.ts";
11 import type { Repo } from "../core/repos.ts";
12 import { exactRefOid } from "../git/branches.ts";
13 import { listBuildPins, type BuildPin } from "../git/builds.ts";
14 import { repoDir } from "../git/repos.ts";
15
16 export class DisplayReads {
17 private readonly pinsBy = new Map<string, Promise<BuildPin[]>>();
18 private readonly tipsBy = new Map<string, Promise<string | undefined>>();
19
20 constructor(private readonly app: App) {}
21
22 /** `repo`'s pins, newest first, read once. */
23 pins(repo: Repo): Promise<BuildPin[]> {
24 let p = this.pinsBy.get(repo.id);
25 if (p === undefined) {
26 p = listBuildPins(repoDir(this.app.reposRoot, repo.ownerName, repo.name));
27 this.pinsBy.set(repo.id, p);
28 }
29 return p;
30 }
31
32 /** `repo`'s default branch tip, read once. */
33 tip(repo: Repo): Promise<string | undefined> {
34 let p = this.tipsBy.get(repo.id);
35 if (p === undefined) {
36 p = exactRefOid(repoDir(this.app.reposRoot, repo.ownerName, repo.name), `refs/heads/${repo.defaultBranch}`);
37 this.tipsBy.set(repo.id, p);
38 }
39 return p;
40 }
41 }
src/git/read.ts +3
@@ -9,6 +9,7 @@ import { defect } from "../kernel/error.ts";
99 import { err, ok, type Result } from "../kernel/result.ts";
1010 import { gitEnv, mediaGate, runGit, runGitRaw, type GitFailure } from "./env.ts";
1111 import { isSafeOid, isSafeRef } from "./guard.ts";
12 import { noteGitLookup, noteGitSpawn } from "../perf/git_work.ts";
1213
1314 export type ObjectType = "blob" | "tree" | "commit" | "tag";
1415
@@ -197,6 +198,7 @@ class BatchProc {
197198 rev: string,
198199 withBody: boolean,
199200 ): Promise<Result<GitObject | ObjectStat, "missing">> {
201 noteGitLookup();
200202 const run = this.queue.then(() => this.exchange(rev, withBody));
201203 // The queue must survive a failed exchange; errors belong to the caller.
202204 this.queue = run.catch(() => {});
@@ -206,6 +208,7 @@ class BatchProc {
206208 private ensureProc() {
207209 if (this.proc && this.proc.exitCode === null && !this.proc.killed)
208210 return this.proc;
211 noteGitSpawn();
209212 this.proc = Bun.spawn(["git", "cat-file", this.mode], {
210213 cwd: this.dir,
211214 env: gitEnv(),
src/perf/budgets.ts +25 −1
@@ -8,6 +8,21 @@ export interface Budget {
88 readonly p95Ms: number;
99 }
1010
11 /**
12 * Every page the forge serves (#10445): server p95 under 150 ms, and first
13 * paint under 1 s on a phone over 4G. First paint is the browser's Largest
14 * Contentful Paint as the beacon reports it (`web/beacon.ts`), held at p75,
15 * the share web-vitals judges by (web.dev/articles/defining-core-web-vitals-thresholds).
16 */
17 export const PAGE_BUDGET: Budget = { pattern: "page", words: "every page p95 under 150 ms", p95Ms: 150 };
18 export const PAINT_BUDGET = { words: "first paint p75 under 1 s", p75Ms: 1000 } as const;
19
20 /**
21 * What the performance watcher files a finding at (`watch.ts`): looser than
22 * the page budget, so the watcher opens an issue for a page that is badly
23 * slow, not one for every page the day the page budget arrived. Pages
24 * between these and `PAGE_BUDGET` are on the ops view (`page_budgets.ts`).
25 */
1126 export const BUDGETS: readonly Budget[] = [
1227 { pattern: "/", words: "home p95 under 800 ms warm", p95Ms: 800 },
1328 { pattern: "/boards", words: "boards p95 under 800 ms warm", p95Ms: 800 },
@@ -17,7 +32,7 @@ export const BUDGETS: readonly Budget[] = [
1732 { pattern: "/:owner/:name/*", words: "repo sub-page p95 under 1000 ms warm", p95Ms: 1000 },
1833 ];
1934
20 /** The budget a route pattern is held to, or undefined for a route with none. */
35 /** The watcher's budget for a route pattern, or undefined for a route with none. */
2136 export function budgetFor(pattern: string): Budget | undefined {
2237 return (
2338 BUDGETS.find((b) => b.pattern === pattern) ??
@@ -25,6 +40,15 @@ export function budgetFor(pattern: string): Budget | undefined {
2540 );
2641 }
2742
43 /** Not pages: the API, assets, streams, live fragments and the forge's own doors. */
44 const NOT_PAGE = /^\/(?:api|assets|_|mcp|oauth|\.well-known|__forge)(?:\/|$|[a-z_-]*$)|\/(?:stream|live)$/;
45
46 /** Whether a route pattern is a page a person opens. */
47 export const isPagePattern = (pattern: string): boolean => pattern.startsWith("/") && !NOT_PAGE.test(pattern);
48
49 /** The page budget a route pattern is held to: `PAGE_BUDGET` for every page, none for anything else. */
50 export const pageBudgetFor = (pattern: string): Budget | undefined => (isPagePattern(pattern) ? PAGE_BUDGET : undefined);
51
2852 /** A plumbing git subcommand's budget (#8158): a single process past this at p95 is a queue or a loaded machine. */
2953 export const GIT_BUDGET: Budget = { pattern: "git *", words: "each git process p95 under 250 ms", p95Ms: 250 };
3054
src/perf/git.ts +2
@@ -8,6 +8,7 @@
88 * else is `git other`.
99 */
1010
11 import { noteGitSpawn } from "./git_work.ts";
1112 import { createPerfRecord, type PerfRecord } from "./record.ts";
1213
1314 const SUBCOMMANDS = new Set([
@@ -45,5 +46,6 @@ export function createGitRecord(): PerfRecord {
4546
4647 /** Note one finished git process that started at `start` (performance.now()). */
4748 export function noteGit(record: PerfRecord, args: readonly string[], start: number): void {
49 noteGitSpawn();
4850 record.add(gitKey(args), { at: Date.now(), ms: performance.now() - start, signedIn: false, phases: [] });
4951 }
src/perf/git_work.ts added +37
@@ -0,0 +1,37 @@
1 /**
2 * Git work counted, never timed (#10445): every git process the forge
3 * spawned and every object it asked a long-lived `cat-file` reader for, as
4 * two running totals. A page's git cost is the difference across one
5 * request, so an N+1 (a read per build, per repository) shows as a count
6 * that grows with the fixture, whatever the machine's speed.
7 *
8 * Process-wide on purpose: git is shared by every app in the process, as
9 * the reader pool is (`git/read.ts`). Plain numbers, nothing about which
10 * repository or ref.
11 */
12
13 let spawned = 0;
14 let lookups = 0;
15
16 /** One git process started. */
17 export function noteGitSpawn(): void {
18 spawned++;
19 }
20
21 /** One object asked of a `cat-file --batch` reader. */
22 export function noteGitLookup(): void {
23 lookups++;
24 }
25
26 export interface GitWork {
27 readonly spawned: number;
28 readonly lookups: number;
29 }
30
31 /** The totals so far. */
32 export const gitWork = (): GitWork => ({ spawned, lookups });
33
34 /** The git work between `before` and now: processes plus lookups, each one unit. */
35 export function gitWorkSince(before: GitWork): GitWork {
36 return { spawned: spawned - before.spawned, lookups: lookups - before.lookups };
37 }
src/perf/page_budgets.ts added +87
@@ -0,0 +1,87 @@
1 /**
2 * The pages that miss their budget, for the ops view (#10445): one line per
3 * page, and none at all when every page is fast. Two measures, each with
4 * its own rule:
5 *
6 * - Server: the route record's last 15 minutes (`record.ts`), p95 over
7 * `PAGE_BUDGET` with at least `MIN_REQUESTS` requests, naming the
8 * slowest phase that is not a wrapper (as the watcher does, `watch.ts`).
9 * - First paint: the views browsers reported today and yesterday
10 * (`page_vital`, LCP), p75 over `PAINT_BUDGET` with at least
11 * `MIN_VIEWS` views. p75 is exact by counting, never by reading every
12 * value: with nearest rank, p75 is over the budget exactly when fewer than
13 * ⌈0.75·n⌉ views painted within it. A browser that reports no LCP (Safari)
14 * is not counted.
15 *
16 * Plain data in, plain data out; the page draws it.
17 */
18
19 import type { Database } from "bun:sqlite";
20 import { dayOf } from "../web/measure.ts";
21 import { isPagePattern, pageBudgetFor, PAINT_BUDGET } from "./budgets.ts";
22 import { summarize, WINDOW_MS, type PerfRecord } from "./record.ts";
23 import { MIN_REQUESTS, WRAPPERS } from "./watch.ts";
24
25 /** Fewer real views than this say nothing about a page's paint. */
26 export const MIN_VIEWS = 20;
27 const DAY_MS = 86_400_000;
28
29 export interface PageMiss {
30 /** The route pattern: never a path, which could name a private repository. */
31 readonly page: string;
32 /** Server p95 in ms, and its slowest phase, when the server misses. */
33 readonly server?: { readonly p95: number; readonly budget: number; readonly phase: string; readonly n: number } | undefined;
34 /** Views over the paint budget, of how many, when first paint misses. */
35 readonly paint?: { readonly slow: number; readonly n: number } | undefined;
36 }
37
38 /** Whether `slow` of `n` views over the budget puts p75 over it (nearest rank). */
39 export const paintMisses = (slow: number, n: number): boolean => n - slow < Math.ceil(0.75 * n);
40
41 /** Per page: views with an LCP today and yesterday, and how many painted after `PAINT_BUDGET`. */
42 export function paintCounts(db: Database, now: number): Map<string, { slow: number; n: number }> {
43 const out = new Map<string, { slow: number; n: number }>();
44 // One day at a time: `day = ?` walks page_vital_day_route_lcp in route order, so no sort.
45 const read = db.query<{ route: string; n: number; slow: number }, [string, number]>(
46 "SELECT route, COUNT(*) AS n, SUM(lcp > ?2) AS slow FROM page_vital WHERE day = ?1 AND lcp > 0 GROUP BY route",
47 );
48 for (const day of [dayOf(now), dayOf(now - DAY_MS)])
49 for (const row of read.all(day, PAINT_BUDGET.p75Ms)) {
50 const held = out.get(row.route) ?? { slow: 0, n: 0 };
51 out.set(row.route, { slow: held.slow + row.slow, n: held.n + row.n });
52 }
53 return out;
54 }
55
56 /** Every page over its budget now, worst server time first, then most slow paints. */
57 export function pagesOverBudget(db: Database, record: PerfRecord, now: number): PageMiss[] {
58 const misses = new Map<string, { server?: PageMiss["server"]; paint?: PageMiss["paint"] }>();
59 for (const pattern of record.patterns()) {
60 const budget = pageBudgetFor(pattern);
61 if (budget === undefined) continue;
62 const stat = summarize(record.traffic(pattern, now - WINDOW_MS));
63 if (stat === null || stat.n < MIN_REQUESTS || stat.p95 <= budget.p95Ms) continue;
64 const phase = stat.phases.find((p) => !WRAPPERS.has(p.name))?.name ?? "app";
65 misses.set(pattern, { server: { p95: Math.round(stat.p95), budget: budget.p95Ms, phase, n: stat.n } });
66 }
67 for (const [page, c] of paintCounts(db, now)) {
68 if (!isPagePattern(page) || c.n < MIN_VIEWS || !paintMisses(c.slow, c.n)) continue;
69 misses.set(page, { ...misses.get(page), paint: c });
70 }
71 return [...misses]
72 .map(([page, m]) => ({ page, ...m }))
73 .sort(
74 (a, b) =>
75 (b.server?.p95 ?? 0) - (a.server?.p95 ?? 0) ||
76 (b.paint?.slow ?? 0) - (a.paint?.slow ?? 0) ||
77 (a.page < b.page ? -1 : 1),
78 );
79 }
80
81 /** One line in words: `/displays: server p95 412 ms (items), first paint over 1 s in 34 of 120 views`. */
82 export function missLine(m: PageMiss): string {
83 const parts: string[] = [];
84 if (m.server !== undefined) parts.push(`server p95 ${String(m.server.p95)} ms over ${String(m.server.budget)} ms, slowest ${m.server.phase}`);
85 if (m.paint !== undefined) parts.push(`first paint over 1 s in ${String(m.paint.slow)} of ${String(m.paint.n)} views`);
86 return parts.join("; ");
87 }
src/perf/watch.ts +1 −1
@@ -15,7 +15,7 @@ export const MIN_REQUESTS = 20;
1515 /** A phase is a cache when it ever answered `hit`, or it is named like one and has not hit. */
1616 const CACHE_PHASE = /(cache|kpis|panel|vitals)/;
1717 /** `app`, `db` and `route` wrap the others; the blame goes to a part. */
18 const WRAPPERS = new Set(["app", "db", "route", "compress"]);
18 export const WRAPPERS = new Set(["app", "db", "route", "compress"]);
1919
2020 export function evaluate(record: PerfRecord, now: number): Finding[] {
2121 const out: Finding[] = [];
src/tui/screens/entries.ts +2
@@ -41,6 +41,7 @@ import { entry as loopStalls } from "./loopstalls.ts";
4141 import { entry as maintenance } from "./maintenance.ts";
4242 import { modelCeilingEntry, modelGrantEntry } from "./model.ts";
4343 import { entry as orgGrants } from "./orggrants.ts";
44 import { entry as overBudget } from "./overbudget.ts";
4445 import { entry as orgTeams } from "./orgteams.ts";
4546 import { entry as newIssue } from "./newissue.ts";
4647 import { entry as now } from "./nowlist.ts";
@@ -87,6 +88,7 @@ export const ENTRIES: readonly PaletteEntry[] = [
8788 homePins,
8889 system,
8990 loopStalls,
91 overBudget,
9092 telemetry,
9193 resilience,
9294 maintenance,
src/tui/screens/overbudget.ts added +60
@@ -0,0 +1,60 @@
1 /**
2 * Pages over budget in the terminal, the owner's (#10445): one line per page
3 * that misses its server or first-paint budget, the rows `/system` draws
4 * (`pagesOverBudget`, `missLine`). The gate is `/system`'s: a forge admin who
5 * is a person. Read-only.
6 */
7
8 import type { Database } from "bun:sqlite";
9 import { perfRecord } from "../../perf/live.ts";
10 import { missLine, pagesOverBudget, type PageMiss } from "../../perf/page_budgets.ts";
11 import type { Ctx, Screen } from "../app/app.ts";
12 import { binding } from "../app/keys.ts";
13 import type { Session } from "../session.ts";
14 import { style } from "../term/style.ts";
15 import { Scroll, drawLines, hitScroll } from "./common.ts";
16 import type { PaletteEntry } from "./entries.ts";
17
18 const mayRead = (session: Session): boolean => session.user?.isAdmin === true && session.user.kind !== "agent";
19
20 /** A page a line, or one line saying every page is fast; empty for anyone the gate refuses. */
21 export function overBudgetLines(session: Session, misses: readonly PageMiss[]): string[] {
22 if (!mayRead(session)) return [];
23 if (misses.length === 0) return ["Every page is within its budget."];
24 return misses.map((m) => `${m.page} ${missLine(m)}`);
25 }
26
27 const read = (db: Database): PageMiss[] => pagesOverBudget(db, perfRecord, Date.now());
28
29 export function overBudgetScreen(session: Session, ctx: Ctx): Screen {
30 const scroll = new Scroll();
31 // Read once when opened, as the page reads once per load.
32 const lines = overBudgetLines(session, mayRead(session) ? read(session.app.db) : []);
33 return {
34 title: "Pages over budget",
35 keys: [binding(["j", "k"], "scroll"), binding(["esc", "q"], "back")],
36 render(area, s) {
37 const c = ctx.theme.c;
38 const spans = lines.map((text) => [{ text, style: style(c.fg, c.bg) }]);
39 drawLines(s, area, c, scroll, spans);
40 hitScroll(ctx, area, scroll, lines.length);
41 },
42 key(e) {
43 if (scroll.key(e, lines.length)) return true;
44 if (e.key === "esc" || e.key === "q") {
45 ctx.pop();
46 return true;
47 }
48 return false;
49 },
50 };
51 }
52
53 export const entry: PaletteEntry = {
54 id: "over-budget",
55 title: "Pages over budget",
56 hint: "pages missing 150 ms server or 1 s first paint",
57 after: ["stalls", "system"],
58 offered: mayRead,
59 run: (session, ctx) => ctx.push(overBudgetScreen(session, ctx)),
60 };
src/web/pages/display.ts +11 −7
@@ -12,8 +12,7 @@ import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../cor
1212 import { chooseBuilds, choosableBuilds, parseChoice } from "../../actions/displays.ts";
1313 import { displayItems, selectionKeys } from "../../display/items.ts";
1414 import { writesDisplay } from "../../display/selections.ts";
15 import { exactRefOid } from "../../git/branches.ts";
16 import { repoDir } from "../../git/repos.ts";
15 import { DisplayReads } from "../../display/reads.ts";
1716 import { INVALID_TITLE, displayOf } from "../../display/known.ts";
1817 import { launchUrl } from "../../arcade/host.ts";
1918 import { repoShotsUrl } from "../../display/launch.ts";
@@ -45,13 +44,18 @@ export async function displayPage(ctx: Ctx): Promise<Response> {
4544 if (!repo) return timePhase(ctx.request, "render", () => notFoundPage(ctx));
4645 const config = await timePhaseAsync(ctx.request, "display", () => displayOf(ctx.app, repo));
4746 if (config === undefined) return timePhase(ctx.request, "render", () => notFoundPage(ctx));
48 const items = config.ok ? await timePhaseAsync(ctx.request, "items", () => displayItems(ctx.app, repo, config.value, ctx.auth.user)) : [];
47 // One read of each source's pins and tips, shared by the items, the selection and the choices (#10445).
48 const reads = new DisplayReads(ctx.app);
49 const items = config.ok ? await timePhaseAsync(ctx.request, "items", () => displayItems(ctx.app, repo, config.value, ctx.auth.user, reads)) : [];
4950 // The Arcade host plays only the display titled `arcade-builds`; Launch is offered on that one.
50 const launch = await launchUrl(ctx.app, repo, config);
51 const launch = await timePhaseAsync(ctx.request, "launch", () => launchUrl(ctx.app, repo, config));
5152 const writes = config.ok && writesDisplay(ctx.app, repo, ctx.auth.user);
52 const chosen = writes && config.ok ? await selectionKeys(ctx.app, config.value, ctx.auth.user) : [];
53 const choices = writes && config.ok ? await choosableBuilds(ctx.app, config.value, chosen, ctx.auth.user) : [];
54 const tip = writes ? await exactRefOid(repoDir(ctx.app.reposRoot, repo.ownerName, repo.name), `refs/heads/${repo.defaultBranch}`) : undefined;
53 const [chosen, tip] = writes && config.ok
54 ? await timePhaseAsync(ctx.request, "choices", () =>
55 Promise.all([selectionKeys(ctx.app, config.value, ctx.auth.user, reads), reads.tip(repo)]),
56 )
57 : [[], undefined];
58 const choices = writes && config.ok ? await choosableBuilds(ctx.app, config.value, chosen, ctx.auth.user, reads) : [];
5559 const title = config.ok ? config.value.title : INVALID_TITLE;
5660 const body = html`${repoHeader(ctx, repo, "code", { compact: true })}
5761 <div class="page-head mt">
src/web/pages/system.ts +20 −2
@@ -1,5 +1,8 @@
11 /**
2 * /system, for the owner: the machine as it is now — cores, temperatures,
2 * /system, for the owner. First, only when there are any, the pages that
3 * miss their budget (#10445, `perf/page_budgets.ts`): one line each, server
4 * p95 or real browsers' first paint; nothing when every page is fast.
5 * Then the machine as it is now — cores, temperatures,
36 * load, memory, disks, network — and every process grouped by what the
47 * forge launched it for, each with CPU, memory and age. The same test file
58 * in more than one check at once is named at the top. It updates in place
@@ -27,7 +30,8 @@ import { live } from "../../system/live.ts";
2730 import { readerPaces, type ReaderPace } from "../../system/telemetry_reads.ts";
2831 import { stallHour } from "../../metrics/stalls.ts";
2932 import type { RouteStall } from "../../perf/loop.ts";
30 import { loopWatch } from "../../perf/live.ts";
33 import { loopWatch, perfRecord } from "../../perf/live.ts";
34 import { missLine, pagesOverBudget, type PageMiss } from "../../perf/page_budgets.ts";
3135 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
3236 import { notFoundPage, signInFirst } from "../chrome.ts";
3337 import { html, raw, type Html } from "../html.ts";
@@ -162,6 +166,18 @@ export function stallSection(rows: readonly RouteStall[]): Html {
162166 </table></div>`}</section>`;
163167 }
164168
169 /**
170 * The pages over budget (#10445): one row per page that misses, in words;
171 * nothing at all, not even the heading, when every page is fast.
172 */
173 export function overBudgetSection(misses: readonly PageMiss[]): Html | null {
174 if (misses.length === 0) return null;
175 return html`<section class="settings-block" id="over-budget"><h2>Pages over budget</h2>
176 <ul class="list" aria-label="Pages over budget">${misses.map(

The terminal has no equivalent of this section. loopstalls.ts shows the pattern to follow. The section is correct on the page, but the screen is missing.

177 (m) => html`<li data-page="${m.page}"><span class="mono">${m.page}</span> ${missLine(m)}</li>`,
178 )}</ul></section>`;
179 }
180
165181 /** Who read the telemetry routes in the last 10 minutes and how fast (#9069); `readerPaces`' rows. */
166182 export function readersSection(rows: readonly ReaderPace[]): Html {
167183 const secs = (ms: number | null): string => (ms === null ? "—" : `${(ms / 1000).toFixed(1)} s`);
@@ -245,6 +261,7 @@ export function systemPage(ctx: Ctx): Response {
245261 ? html`<p class="small" id="cell-caps" data-by="kernel">Each cell's memory is capped by the kernel (its own cgroup's <code>memory.max</code>): a cell over it is killed alone.</p>`
246262 : html`<p class="small" id="cell-caps" data-by="watch">Each cell's memory is watched once a second and the cell stopped over its cap; the kernel does not cap it (${holder.reason}).</p>`;
247263 const memory = memorySection(ctx);
264 const misses = timePhase(ctx.request, "budgets", () => pagesOverBudget(ctx.app.db, perfRecord, Date.now()));
248265 const stalls = timePhase(ctx.request, "stalls", () => stallHour(loopWatch.tally(performance.now())));
249266 const rendering = performance.now();
250267 const r = regions(v);
@@ -258,6 +275,7 @@ export function systemPage(ctx: Ctx): Response {
258275 <a href="/boards">Boards</a>.
259276 </p>
260277 </div>
278 ${overBudgetSection(misses)}
261279 <div id="sys-dup">${at("sys-dup")}</div>
262280 <section class="settings-block"><h2>Machine</h2>
263281 <div id="summary" class="row-meta">${at("summary")}</div>
test/child-quota.test.ts +6 −1
@@ -13,6 +13,7 @@ import { serve, type Servers } from "../src/http/server.ts";
1313 import { quotaRefusal } from "../src/spawn/quota.ts";
1414 import { usePlacement, useProbe } from "../src/spawn/door.ts";
1515 import { hostedPlacement, memoryBodies } from "../src/spawn/placement.ts";
16 import { SIZES } from "../src/spawn/sizes.ts";
1617 import { quietLogs } from "./helpers/globals.ts";
1718
1819 quietLogs();
@@ -101,7 +102,11 @@ describe("child quota", () => {
101102
102103 test("room for small but not large is refused only for large", async () => {
103104 clear();
104 process.env["FORGE_CHILD_MEMORY_MIB"] = "2000";
105 // Room for one more small beside what is held, short of a large: read from
106 // SIZES so the limit follows the sizes when they change.
107 const held = SIZES.small.memoryMiB; // the first forge spawned above
108 process.env["FORGE_CHILD_MEMORY_MIB"] = String(held + SIZES.small.memoryMiB);
109 expect(held + SIZES.small.memoryMiB).toBeLessThan(held + SIZES.large.memoryMiB);
105110 expect(quotaRefusal(dir, "small")).toBeUndefined();
106111 expect(quotaRefusal(dir, "large")).toContain("memory limit");
107112 });
test/display-git-work.test.ts added +111
@@ -0,0 +1,111 @@
1 /**
2 * A display's page reads git once per source repository, never once per
3 * build (#10445). Counted in git work (`perf/git_work.ts`: processes spawned
4 * plus objects asked of a reader), not a clock: the page costs the same
5 * after each source gains three more builds, and `/displays`, warm, reads no
6 * git at all.
7 */
8 import { afterAll, beforeAll, expect, test } from "bun:test";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { createApp, type App } from "../src/app.ts";
13 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
14 import { getRepo } from "../src/core/repos.ts";
15 import { createSession } from "../src/core/sessions.ts";
16 import { createUser, type User } from "../src/core/users.ts";
17 import { pinBuild } from "../src/git/builds.ts";
18 import { repoDir } from "../src/git/repos.ts";
19 import { serve, type Servers } from "../src/http/server.ts";
20 import { gitWork, gitWorkSince } from "../src/perf/git_work.ts";
21 import { proveAll } from "./helpers/verified.ts";
22
23 const SOURCES = ["arcade-one", "arcade-two"] as const;
24
25 let dataDir = "";
26 let app: App;
27 let servers: Servers;
28 let alice: User;
29 let cookie = "";
30 let at = 1_800_000_000;
31
32 const commit = async (name: string, path: string, content: string): Promise<string> => {
33 const made = await commitToBranch(app, alice, getRepo(app.db, "alice", name)!, { message: path, files: [{ path, content }] });
34 if (!made.ok) throw new Error(JSON.stringify(made.error));
35 return made.value.commitOid;
36 };
37
38 /** Pins `n` showcased builds, none naming its launch, so each item's launch comes from its commit's forge.json. */
39 async function pinBuilds(name: string, n: number): Promise<void> {
40 for (let i = 0; i < n; i++) {
41 const oid = await commit(name, "forge.json", JSON.stringify({ app: { run: "bun run main.ts", pages: ["/", `/level-${String(at)}`] } }));
42 const label = `v${String(at)}`;
43 const pinned = await pinBuild(repoDir(app.reposRoot, "alice", name), {
44 label,
45 oid,
46 note: "",
47 source: "commit",
48 tagger: { name: "alice", email: "[email protected]" },
49 at: at++,
50 demo: { name, version: label, page: "/", showcase: true },
51 });
52 if (!pinned.ok) throw new Error(pinned.error);
53 }
54 }
55
56 /** The git work of one warm request for `path`, read as the display's writer. */
57 async function workOf(path: string): Promise<number> {
58 const get = () => fetch(`${servers.url}${path}`, { headers: { cookie }, redirect: "manual" }).then(async (r) => {
59 expect(r.status).toBe(200);
60 return r.text();
61 });
62 await get();
63 const before = gitWork();
64 await get();
65 const w = gitWorkSince(before);
66 return w.spawned + w.lookups;
67 }
68
69 beforeAll(async () => {
70 dataDir = await mkdtemp(join(tmpdir(), "forge-display-work-"));
71 app = createApp({ dataDir, port: 0 });
72 servers = await serve(app);
73 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
74 if (!made.ok) throw new Error(made.error);
75 alice = made.value;
76 proveAll(app);
77 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
78 for (const name of [...SOURCES, "shelf"]) {
79 const repo = await createRepo(app, alice, { name, visibility: "public" });
80 if (!repo.ok) throw new Error(String(repo.error));
81 }
82 for (const name of SOURCES) await pinBuilds(name, 1);
83 await commit("shelf", "display.json", JSON.stringify({ title: "Games shelf", layout: "shelf", sources: SOURCES.map((s) => `alice/${s}`) }));
84 }, 60_000);
85
86 afterAll(async () => {
87 servers.stop();
88 app.close();
89 await rm(dataDir, { recursive: true, force: true });
90 });
91
92 test("a display page's git work does not grow with its builds", async () => {
93 const page = await (await fetch(`${servers.url}/alice/shelf/display`, { headers: { cookie } })).text();
94 expect(page).toContain("2 builds");
95 const few = await workOf("/alice/shelf/display");
96 for (const name of SOURCES) await pinBuilds(name, 3);
97 const after = await (await fetch(`${servers.url}/alice/shelf/display`, { headers: { cookie } })).text();
98 expect(after).toContain("8 builds");
99 const many = await workOf("/alice/shelf/display");
100 expect({ few, many }).toEqual({ few, many: few });
101 }, 60_000);
102
103 test("a display page reads each source's pins once, for the items, the selection and the choices together", async () => {
104 // Per source: one for-each-ref of its pins. Plus the display's own tip, for the save form.
105 const work = await workOf("/alice/shelf/display");
106 expect(work).toBeLessThanOrEqual(SOURCES.length + 1);
107 });
108
109 test("/displays, warm, reads no git", async () => {
110 expect(await workOf("/displays")).toBe(0);
111 });
test/helpers/swept-pages.ts added +40
@@ -0,0 +1,40 @@
1 /**
2 * The GET pages a sweep walks (#3387, #10445): every pattern the router
3 * source registers, one-line or wrapped, read from `src/http/server.ts` and
4 * `settings_routes.ts` themselves, so a new page is walked the day it is
5 * added. Parameter-free patterns, plus `/:owner/:name/<literal>…` ones.
6 */
7 import { readFileSync } from "node:fs";
8 import { join } from "node:path";
9 import { buildApiRouter, buildRouter } from "../../src/http/server.ts";
10
11 /** Long-lived streams and live fragments: no page, no phases to name. */
12 export const NOT_PAGES = new Set([
13 "/events",
14 "/system/stream",
15 "/settings/teams/stream",
16 "/fleet/graph/live",
17 "/settings/traffic/scrapes/export",
18 "/settings/backups/live",
19 "/:owner/:name/stream",
20 ]);
21
22 /** Every GET pattern the router source registers, one-line or wrapped. */
23 export function registeredGets(): Set<string> {
24 const source = ["server.ts", "settings_routes.ts"]
25 .map((file) => readFileSync(join(import.meta.dir, "../../src/http", file), "utf8"))
26 .join("\n");
27 const found = new Set<string>();
28 for (const m of source.matchAll(/router\.add\(\s*"GET",\s*"([^"]+)"/g)) found.add(m[1]!);
29 return found;
30 }
31
32 /** Parameter-free GET patterns, plus `/:owner/:name/<literal>…` ones. */
33 export function sweptPatterns(): string[] {
34 const registered = new Set([...buildRouter().patterns(), ...buildApiRouter().patterns()]);
35 return [...registeredGets()]
36 .filter((p) => registered.has(p) && !NOT_PAGES.has(p) && !p.startsWith("/assets/"))
37 .filter((p) => !p.replace(/^\/:owner\/:name/, "").includes(":") && !p.includes("*"))
38 .filter((p) => !p.endsWith("/live") && !p.includes("/__forge/"))
39 .sort();
40 }
test/page-budget-walk.test.ts added +177
@@ -0,0 +1,177 @@
1 /**
2 * Every GET page holds its budget (#10445). The walk visits every page the
3 * router registers (`helpers/swept-pages.ts`), on a forge with fixture data —
4 * a repository with showcased builds and a display over them — and holds
5 * each warm request to a budget of work: git (processes spawned plus objects
6 * looked up, `perf/git_work.ts`) and SQL statements. Work, not the clock
7 * (docs/PRINCIPLES.md): the 150 ms server p95 is held where the clock is
8 * real, by the watcher and the ops view (`perf/page_budgets.ts`); here an N+1
9 * or a page that stops using its cache shows in every run, whatever the cell.
10 * A page over budget fails by name, with its counts and its slowest phase.
11 */
12 import { afterAll, beforeAll, expect, test } from "bun:test";
13 import { mkdtempSync, rmSync } from "node:fs";
14 import { tmpdir } from "node:os";
15 import { join } from "node:path";
16 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
17 import { createApp, type App } from "../src/app.ts";
18 import { getRepo } from "../src/core/repos.ts";
19 import { createUser, type User } from "../src/core/users.ts";
20 import { pinBuild } from "../src/git/builds.ts";
21 import { repoDir } from "../src/git/repos.ts";
22 import { serve, type Servers } from "../src/http/server.ts";
23 import { gitWork, gitWorkSince } from "../src/perf/git_work.ts";
24 import { WRAPPERS } from "../src/perf/watch.ts";
25 import { quietLogs } from "./helpers/globals.ts";
26 import { sweptPatterns } from "./helpers/swept-pages.ts";
27 import { proveAll } from "./helpers/verified.ts";
28
29 quietLogs();
30
31 /**
32 * The most git work a warm page may do: a handful of ref reads, never one per
33 * row. Deliberately tuned to the walk's worst today (4) with room for two
34 * more: tight enough that a per-row read on any fixture list fails it.
35 */
36 const PAGE_GIT_MAX = 6;
37 /**
38 * The most SQL statements a warm page may run. Deliberately tuned to the
39 * walk's worst today (79) with about a quarter's headroom: a statement per
40 * row over the fixture lists crosses it.
41 */
42 const PAGE_STATEMENTS_MAX = 100;
43
44 /** Pages over the budget today → the issue that brings each under it. May not outlive its pages. */
45 const OVER: Array<[path: string, issue: string]> = [];
46
47 const RUNS = 3;
48
49 let root = "";
50 let app: App;
51 let servers: Servers;
52 let alice: User;
53
54 beforeAll(async () => {
55 root = mkdtempSync(join(tmpdir(), "forge-budget-walk-"));
56 app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" });
57 const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
58 if (!u.ok) throw new Error(u.error);
59 alice = u.value;
60 proveAll(app);
61 const made = await createRepo(app, alice, { name: "forge" });
62 if (!made.ok) throw new Error(made.error);
63 // Fixture data: four showcased builds and a display over them, so the display pages walk real rows.
64 const repo = () => getRepo(app.db, "alice", "forge")!;
65 for (let i = 0; i < 4; i++) {
66 const c = await commitToBranch(app, alice, repo(), { message: `build ${String(i)}`, files: [{ path: "forge.json", content: JSON.stringify({ app: { run: "bun run main.ts", pages: ["/", `/p${String(i)}`] } }) }] });
67 if (!c.ok) throw new Error(JSON.stringify(c.error));
68 const pinned = await pinBuild(repoDir(app.reposRoot, "alice", "forge"), {
69 label: `v${String(i)}`, oid: c.value.commitOid, note: "", source: "commit",
70 tagger: { name: "alice", email: "[email protected]" }, at: 1_800_000_000 + i,
71 demo: { name: "forge", version: `v${String(i)}`, page: "/", showcase: true },
72 });
73 if (!pinned.ok) throw new Error(pinned.error);
74 }
75 const d = await commitToBranch(app, alice, repo(), { message: "display", files: [{ path: "display.json", content: JSON.stringify({ title: "Shelf", layout: "shelf", sources: ["alice/forge"] }) }] });
76 if (!d.ok) throw new Error(JSON.stringify(d.error));
77 servers = await serve(app);
78 }, 60_000);
79 afterAll(() => {
80 servers.stop();
81 app.close();
82 rmSync(root, { recursive: true, force: true });
83 });
84
85 interface PageCost {
86 readonly path: string;
87 readonly git: number;
88 readonly statements: number;
89 readonly slowest: string;
90 }
91
92 /** The slowest phase a response's Server-Timing names, past the wrappers: `items 3.1 ms`. */
93 function slowestPhase(header: string): string {
94 const phases = [...header.matchAll(/([a-z][\w-]*);dur=([\d.]+)/g)]
95 .map((m) => ({ name: m[1]!, ms: Number(m[2]) }))
96 .filter((p) => !WRAPPERS.has(p.name) && p.name !== "lag" && !p.name.startsWith("forge-"));
97 const top = phases.sort((a, b) => b.ms - a.ms)[0];
98 return top === undefined ? "app" : `${top.name} ${top.ms.toFixed(1)} ms`;
99 }
100
101 /** One page's warm cost: the least of `RUNS` requests, so work another request left behind drops out. */
102 async function costOf(path: string, cookie: string): Promise<PageCost> {
103 const get = () => fetch(`${servers.url}${path}`, { headers: { cookie }, redirect: "manual" });
104 await (await get()).arrayBuffer();
105 const door = app.db as unknown as { query: unknown };
106 const original = app.db.query.bind(app.db);
107 let git = Infinity;
108 let statements = Infinity;
109 let slowest = "app";
110 for (let i = 0; i < RUNS; i++) {
111 let n = 0;
112 door.query = (sql: string) => {
113 n++;
114 return original(sql);
115 };
116 const before = gitWork();
117 try {
118 const res = await get();
119 await res.arrayBuffer();
120 slowest = slowestPhase(res.headers.get("server-timing") ?? "");
121 } finally {
122 door.query = original;
123 }
124 const w = gitWorkSince(before);
125 git = Math.min(git, w.spawned + w.lookups);
126 statements = Math.min(statements, n);
127 }
128 return { path, git, statements, slowest };
129 }
130
131 /** Alice's session cookie, signed in through the form as a browser would. */
132 async function signInAlice(): Promise<string> {
133 const signin = await fetch(`${servers.url}/signin`, {
134 method: "POST",
135 headers: { origin: servers.url },
136 body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }),
137 redirect: "manual",
138 });
139 return (signin.headers.get("set-cookie") ?? "").split(";")[0]!;
140 }
141
142 test("every GET page, warm, is within its budget of git and SQL work", async () => {
143 const cookie = await signInAlice();
144 const paths = sweptPatterns().map((p) => p.replace(":owner", "alice").replace(":name", "forge"));
145 expect(paths).toContain("/displays");
146 expect(paths).toContain("/alice/forge/display");
147
148 const costs: PageCost[] = [];
149 for (const path of paths) costs.push(await costOf(path, cookie));
150
151 const debt = new Set(OVER.map(([p]) => p));
152 const over = costs
153 .filter((c) => c.git > PAGE_GIT_MAX || c.statements > PAGE_STATEMENTS_MAX)
154 .map((c) => `${c.path}: git ${String(c.git)} (max ${String(PAGE_GIT_MAX)}), SQL ${String(c.statements)} (max ${String(PAGE_STATEMENTS_MAX)}), slowest phase ${c.slowest}`);
155 expect(over.filter((line) => !debt.has(line.slice(0, line.indexOf(":")))).sort()).toEqual([]);
156 // The debt may not outlive its pages: one back under budget comes off the list.
157 const within = new Set(costs.filter((c) => c.git <= PAGE_GIT_MAX && c.statements <= PAGE_STATEMENTS_MAX).map((c) => c.path));
158 expect(OVER.filter(([p]) => within.has(p)).map(([p]) => p)).toEqual([]);
159 for (const [, issue] of OVER) expect(issue).toMatch(/^#\d+$/);
160
161 if (process.env.FORGE_WALK_REPORT === "1")
162 for (const c of [...costs].sort((a, b) => b.git - a.git || b.statements - a.statements).slice(0, 25))
163 console.log(`${c.path}\tgit ${String(c.git)}\tsql ${String(c.statements)}\t${c.slowest}`);
164 }, 300_000);
165
166 test("a display page's warm git work is within the page budget with fixture builds", async () => {
167 const cookie = await signInAlice();
168 const page = await (await fetch(`${servers.url}/alice/forge/display`, { headers: { cookie } })).text();
169 expect(page).toContain("4 builds");
170 const cost = await costOf("/alice/forge/display", cookie);
171 expect(cost.git).toBeLessThanOrEqual(PAGE_GIT_MAX);
172 });
173
174 test("the slowest phase is named past the wrappers and the loop's lag", () => {
175 expect(slowestPhase("app;dur=40.0, session;dur=0.2, lag;dur=900.0, items;dur=31.5, render;dur=4.0")).toBe("items 31.5 ms");
176 expect(slowestPhase("app;dur=3.0")).toBe("app");
177 });
test/page-budgets.test.ts added +177
@@ -0,0 +1,177 @@
1 /**
2 * Pages over budget (#10445): every page is held to 150 ms server p95 and a
3 * 1 s first paint at p75; the ops view (/system) lists one line per page that
4 * misses, and nothing — no heading — when every page is fast. Judged by
5 * seeded samples and counts, never the clock.
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 { createToken } from "../src/core/tokens.ts";
13 import { createUser } from "../src/core/users.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { budgetFor, PAGE_BUDGET, pageBudgetFor } from "../src/perf/budgets.ts";
16 import { missLine, paintCounts, paintMisses, pagesOverBudget } from "../src/perf/page_budgets.ts";
17 import { createPerfRecord, WINDOW_MS } from "../src/perf/record.ts";
18 import { recordVital } from "../src/web/measure.ts";
19 import { overBudgetSection } from "../src/web/pages/system.ts";
20 import { createAgent } from "../src/actions/crew.ts";
21 import type { User } from "../src/core/users.ts";
22 import { createForgeApp, forgeCommands, Session } from "../src/tui/forge.ts";
23 import { overBudgetLines } from "../src/tui/screens/overbudget.ts";
24 import { headlessTerminal } from "../src/tui/term/terminal.ts";
25
26 /** A fixed day long past, so the reports seeded for it are never today's. */
27 const NOW = Date.UTC(2024, 10, 9, 12);
28
29 let dir = "";
30 let app: App;
31 let servers: Servers;
32 let token = "";
33 let alice: User;
34
35 beforeAll(async () => {
36 dir = await mkdtemp(join(tmpdir(), "forge-page-budgets-"));
37 app = createApp({ dataDir: dir, port: 0 });
38 servers = await serve(app);
39 const r = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
40 if (!r.ok) throw new Error(r.error);
41 token = createToken(app.db, r.value.id, "t", "admin").token;
42 alice = r.value;
43 });
44 afterAll(async () => {
45 servers.stop();
46 app.close();
47 await rm(dir, { recursive: true, force: true });
48 });
49
50 /** `n` requests to `pattern`, each `ms` long with a `phase` taking most of it, spread over the window. */
51 function seed(record: ReturnType<typeof createPerfRecord>, pattern: string, ms: number, n = 40, phase = "items", at = NOW) {
52 for (let i = 0; i < n; i++)
53 record.add(pattern, {
54 at: at - WINDOW_MS + 1000 + (i * (WINDOW_MS - 2000)) / n,
55 ms,
56 signedIn: true,
57 phases: [{ name: "app", ms, hit: false }, { name: "session", ms: 0.2, hit: false }, { name: phase, ms: ms * 0.8, hit: false }],
58 });
59 }
60
61 const vital = (route: string, lcp: number, at = NOW) =>
62 recordVital(app.db, { path: "/", ttfb: 50, load: lcp, lcp, inp: 0, cls: 0 }, route, "10.0.0.1", "phone", at);
63
64 test("every page is held to 150 ms; the API is not a page; the watcher files only at its own, looser budgets", () => {
65 expect(PAGE_BUDGET.p95Ms).toBe(150);
66 for (const p of ["/", "/displays", "/:owner/:name/display", "/system", "/explore"]) expect(pageBudgetFor(p)?.p95Ms).toBe(150);
67 for (const p of ["/api/v1/health", "/api/v1/repos", "/assets/*", "/_measure", "/system/stream", "/fleet/graph/live"]) expect(pageBudgetFor(p)).toBeUndefined();
68 expect(budgetFor("/")?.p95Ms).toBe(800);
69 expect(budgetFor("/api/v1/health")?.p95Ms).toBe(100);
70 });
71
72 test("p75 by count is the nearest-rank p75: over the budget exactly when fewer than ⌈0.75 n⌉ paint within it", () => {
73 // n = 20: ⌈15⌉ must be fast. 5 slow → 15 fast → p75 is the 15th, fast.
74 expect(paintMisses(5, 20)).toBe(false);
75 expect(paintMisses(6, 20)).toBe(true);
76 // n = 21: ⌈15.75⌉ = 16 must be fast.
77 expect(paintMisses(5, 21)).toBe(false);
78 expect(paintMisses(6, 21)).toBe(true);
79 // Against a sorted list, by hand: 7 of 21 slow.
80 const xs = [...Array<number>(14).fill(400), ...Array<number>(7).fill(2000)].sort((a, b) => a - b);
81 const p75 = xs[Math.ceil(0.75 * xs.length) - 1]!;
82 expect(p75 > 1000).toBe(paintMisses(7, 21));
83 });
84
85 test("all pages fast: nothing listed, and the ops view draws no section at all", async () => {
86 const record = createPerfRecord();
87 seed(record, "/displays", 40);
88 expect(pagesOverBudget(app.db, record, NOW)).toEqual([]);
89 expect(overBudgetSection([])).toBeNull();
90 const body = await (await fetch(`${servers.url}/system`, { headers: { authorization: `Bearer ${token}` } })).text();
91 expect(body).not.toContain("Pages over budget");
92 });
93
94 test("one line per page that misses: server p95 with its slowest phase, first paint with its count", () => {
95 const record = createPerfRecord();
96 seed(record, "/:owner/:name/display", 260, 40, "items");
97 seed(record, "/displays", 40);
98 seed(record, "/api/v1/repos", 900); // not a page
99 seed(record, "/explore", 900, 5); // too few requests
100 for (let i = 0; i < 20; i++) vital("/:owner/:name/display", i < 8 ? 2400 : 600);
101 for (let i = 0; i < 30; i++) vital("/feed", i < 10 ? 1800 : 500, NOW - 86_400_000); // yesterday counts
102 for (let i = 0; i < 10; i++) vital("/board", 5000); // too few views
103 const misses = pagesOverBudget(app.db, record, NOW);
104 expect(misses.map((m) => m.page)).toEqual(["/:owner/:name/display", "/feed"]);
105 const display = misses[0]!;
106 expect(display.server).toEqual({ p95: 260, budget: 150, phase: "items", n: 40 });
107 expect(display.paint).toEqual({ slow: 8, n: 20 });
108 expect(missLine(display)).toBe("server p95 260 ms over 150 ms, slowest items; first paint over 1 s in 8 of 20 views");
109 expect(missLine(misses[1]!)).toBe("first paint over 1 s in 10 of 30 views");
110 const section = overBudgetSection(misses)!.text;
111 expect(section.match(/<li /g)?.length).toBe(2);
112 expect(section).toContain("Pages over budget");
113 });
114
115 test("the ops view lists a page real browsers paint slowly", async () => {
116 // Its own forge: the browsers' reports are per database, and the live route record is shared by the process, so it is left alone.
117 const own = await mkdtemp(join(tmpdir(), "forge-page-budgets-live-"));
118 const live = createApp({ dataDir: own, port: 0 });
119 const at = await serve(live);
120 try {
121 const r = await createUser(live.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
122 if (!r.ok) throw new Error(r.error);
123 const bearer = createToken(live.db, r.value.id, "t", "admin").token;
124 for (let i = 0; i < 24; i++)
125 recordVital(live.db, { path: "/", ttfb: 50, load: 3000, lcp: i < 12 ? 3000 : 400, inp: 0, cls: 0 }, "/:owner/:name/display", "10.0.0.2", "phone");
126 const body = await (await fetch(`${at.url}/system`, { headers: { authorization: `Bearer ${bearer}` } })).text();
127 expect(body).toContain('data-page="/:owner/:name/display"');
128 expect(body).toContain("first paint over 1 s in 12 of 24 views");
129 } finally {
130 at.stop();
131 live.close();
132 await rm(own, { recursive: true, force: true });
133 }
134 });
135
136 test("the paint count reads its index: no scan, no sort", () => {
137 const plan = app.db
138 .query<{ detail: string }, [string, number]>(
139 "EXPLAIN QUERY PLAN SELECT route, COUNT(*) AS n, SUM(lcp > ?2) AS slow FROM page_vital WHERE day = ?1 AND lcp > 0 GROUP BY route",
140 )
141 .all("2026-11-09", 1000)
142 .map((r) => r.detail);
143 expect(plan.join("\n")).toContain("page_vital_day_route_lcp");
144 expect(plan.join("\n")).not.toMatch(/\bSCAN\b|TEMP B-TREE/);
145 expect(paintCounts(app.db, NOW).get("/:owner/:name/display")).toEqual({ slow: 8, n: 20 });
146 });
147
148 const identity = (user?: User) => ({ sshUsername: user?.username ?? "git", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user });
149 const doors = (user?: User): string[] => {
150 const { app: tui, session } = createForgeApp(app, identity(user), { terminal: headlessTerminal(80, 24), exitHooks: false });
151 const ids = forgeCommands(session)(tui.ctx).map((c) => c.id);
152 tui.stop?.();
153 return ids;
154 };
155
156 test("the terminal's Pages over budget screen gives an admin person the lines /system draws; nobody else the screen", async () => {
157 const record = createPerfRecord();
158 seed(record, "/:owner/:name/display", 260, 40, "items");
159 const misses = pagesOverBudget(app.db, record, NOW - 7 * 86_400_000);
160 expect(overBudgetLines(new Session(app, identity(alice)), misses)).toEqual([
161 "/:owner/:name/display server p95 260 ms over 150 ms, slowest items",
162 ]);
163 expect(overBudgetLines(new Session(app, identity(alice)), [])).toEqual(["Every page is within its budget."]);
164 expect(doors(alice)).toContain("over-budget");
165
166 const member = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" });
167 if (!member.ok) throw new Error(member.error);
168 const agent = await createAgent(app, alice, { username: "budgetbot", brief: "x" });
169 if (!agent.ok) throw new Error(String(agent.error));
170 // An admin agent: still not offered the screen, which is a person's.
171 app.db.query("UPDATE user SET is_admin = 1 WHERE id = ?1").run(agent.value.user.id);
172 const bot = { ...agent.value.user, isAdmin: true };
173 for (const who of [member.value, bot, undefined]) {
174 expect(overBudgetLines(new Session(app, identity(who)), misses)).toEqual([]);
175 expect(doors(who)).not.toContain("over-budget");
176 }
177 });
test/palette-roster.test.ts +1
@@ -100,6 +100,7 @@ test("a site-admin person is offered the whole roster", () => {
100100 ["valves", "Valves", "the week's budget as a valve per repository; turn one and the crew obeys"],
101101 ["stalls", "Main-thread stalls", "the last hour's stall ms by route"],
102102 ["telemetry", "Telemetry", "which agents may read flow and system; enter flips the grant"],
103 ["over-budget", "Pages over budget", "pages missing 150 ms server or 1 s first paint"],
103104 ["capacity", "Capacity", "concurrency and guards, live; e changes a settable one"],
104105 ["resilience", "Resilience", "where snapshots go and the mirror; each protection green, red or untested"],
105106 ["maintenance", "Maintenance", "prepare the forge for a power-off, check the drain, or end maintenance"],
test/pin-demo-facts.test.ts added +50
@@ -0,0 +1,50 @@
1 /**
2 * A commit's demo facts are kept by its id once the commit has been read
3 * (#10445), and not before: a pin made before its objects arrive — a fork's,
4 * a push still landing — shows its pages once they do.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { demoFactsAt } from "../src/actions/pin_demos.ts";
11 import type { App } from "../src/app.ts";
12 import type { Repo } from "../src/core/repos.ts";
13 import { repoDir } from "../src/git/repos.ts";
14
15 let root = "";
16 let work = "";
17 let bare = "";
18 let oid = "";
19 const app = () => ({ reposRoot: join(root, "repos") }) as unknown as App;
20 const repo = { ownerName: "alice", name: "late", defaultBranch: "main" } as unknown as Repo;
21
22 function git(cwd: string, ...args: string[]): string {
23 const r = Bun.spawnSync(["git", "-c", "user.name=t", "-c", "user.email=t@t", "-c", "init.defaultBranch=main", ...args], { cwd, stderr: "pipe" });
24 if (r.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr.toString()}`);
25 return r.stdout.toString().trim();
26 }
27
28 beforeAll(async () => {
29 root = await mkdtemp(join(tmpdir(), "forge-pin-facts-"));
30 work = join(root, "work");
31 await mkdir(work);
32 git(work, "init", "-q");
33 await writeFile(join(work, "forge.json"), JSON.stringify({ app: { run: "bun run src/main.ts", pages: ["/play", "/about"] } }));
34 git(work, "add", "forge.json");
35 git(work, "commit", "-qm", "pages");
36 oid = git(work, "rev-parse", "HEAD");
37 bare = repoDir(app().reposRoot, repo.ownerName, repo.name);
38 await mkdir(bare, { recursive: true });
39 git(bare, "init", "-q", "--bare");
40 });
41 afterAll(async () => {
42 await rm(root, { recursive: true, force: true });
43 });
44
45 test("an object not yet in the repository is asked again, and its pages show once it arrives", async () => {
46 // Pinned before the push lands: nothing to read, and nothing kept.
47 expect((await demoFactsAt(app(), repo, oid)).pages).toEqual([]);
48 git(work, "push", "-q", bare, "HEAD:refs/heads/main");
49 expect((await demoFactsAt(app(), repo, oid)).pages).toEqual(["/play", "/about"]);
50 });
test/server-timing-sweep.test.ts +2 −32
@@ -7,7 +7,7 @@
77 * starts naming `session` and `render` must come off it.
88 */
99 import { afterAll, beforeAll, expect, test } from "bun:test";
10 import { mkdtempSync, readFileSync, rmSync } from "node:fs";
10 import { mkdtempSync, rmSync } from "node:fs";
1111 import { tmpdir } from "node:os";
1212 import { join } from "node:path";
1313 import { createRepo } from "../src/actions/repos.ts";
@@ -16,6 +16,7 @@ import { createUser } from "../src/core/users.ts";
1616 import { buildApiRouter, buildRouter, serve, type Servers } from "../src/http/server.ts";
1717 import { proveAll } from "./helpers/verified.ts";
1818 import { collectTimings } from "./helpers/timing-sweep.ts";
19 import { registeredGets, sweptPatterns } from "./helpers/swept-pages.ts";
1920 import { quietLogs } from "./helpers/globals.ts";
2021
2122 quietLogs();
@@ -24,17 +25,6 @@ quietLogs();
2425 const UNNAMED: Array<[path: string, issue: string]> = [
2526 ];
2627
27 /** Long-lived streams and live fragments: no page, no phases to name. */
28 const NOT_PAGES = new Set([
29 "/events",
30 "/system/stream",
31 "/settings/teams/stream",
32 "/fleet/graph/live",
33 "/settings/traffic/scrapes/export",
34 "/settings/backups/live",
35 "/:owner/:name/stream",
36 ]);
37
3828 /** Routes that answer 3xx for the sweep's signed-in user: not pages, no phases. A page may not join by redirecting. */
3929 const REDIRECTS = new Set([
4030 "/.well-known/change-password",
@@ -53,26 +43,6 @@ const REDIRECTS = new Set([
5343 "/sign-in",
5444 ]);
5545
56 /** Every GET pattern the router source registers, one-line or wrapped. */
57 function registeredGets(): Set<string> {
58 const source = ["server.ts", "settings_routes.ts"]
59 .map((file) => readFileSync(join(import.meta.dir, "../src/http", file), "utf8"))
60 .join("\n");
61 const found = new Set<string>();
62 for (const m of source.matchAll(/router\.add\(\s*"GET",\s*"([^"]+)"/g)) found.add(m[1]!);
63 return found;
64 }
65
66 /** Parameter-free GET patterns, plus `/:owner/:name/<literal>…` ones. */
67 function sweptPatterns(): string[] {
68 const registered = new Set([...buildRouter().patterns(), ...buildApiRouter().patterns()]);
69 return [...registeredGets()]
70 .filter((p) => registered.has(p) && !NOT_PAGES.has(p) && !p.startsWith("/assets/"))
71 .filter((p) => !p.replace(/^\/:owner\/:name/, "").includes(":") && !p.includes("*"))
72 .filter((p) => !p.endsWith("/live") && !p.includes("/__forge/"))
73 .sort();
74 }
75
7646 let root = "";
7747 let app: App;
7848 let servers: Servers;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs