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

The most frequent red checks (client JS/CSS ceilings, batched rows, look prerequisites) are fixed at their cause, no rule loosened #17836

opened this

Merged 4h 9m ago, live in production

scout/forge:scout/17831-the-most-frequent-red-checks-client-js-c → main 0 commits fork 4 threads · 4 unresolved

  • Merged. scout/17831-the-most-frequent-red-checks-client-js-c was merged into main as 339cc69.

Review

Sign in to review this change.
8 files changed +127 −31
src/checks/looks.ts +3 −3
@@ -42,9 +42,9 @@ const whole = (v: unknown): v is number =>
4242
4343 function parseSize(v: unknown): PageSize | undefined {
4444 if (!v || typeof v !== "object") return undefined;
45 const { scrollWidth, scrollHeight, innerWidth, innerHeight } = v as Record<string, unknown>;
46 return whole(scrollWidth) && whole(scrollHeight) && whole(innerWidth) && whole(innerHeight)
47 ? { scrollWidth, scrollHeight, innerWidth, innerHeight }
45 const { scrollWidth, scrollHeight, innerWidth: viewWidth, innerHeight } = v as Record<string, unknown>;
46 return whole(scrollWidth) && whole(scrollHeight) && whole(viewWidth) && whole(innerHeight)
47 ? { scrollWidth, scrollHeight, innerWidth: viewWidth, innerHeight }
4848 : undefined;
4949 }
5050
src/core/repos.ts +18 −10
@@ -1458,6 +1458,13 @@ export function canRead(
14581458 ): boolean {
14591459 if (user?.appId !== undefined && repo.origin !== null && !appMayRead(db, repo, user.appId)) return false;
14601460 if (!ownerInStanding(db, repo)) return user?.isAdmin === true;
1461 return readsInStanding(db, repo, user);
1462 }
1463
1464 /**
1465 * `canRead` for a repository whose owner is known to be in good standing: the rest of its rules.
1466 */
1467 function readsInStanding(db: Database, repo: Repo, user: User | undefined): boolean {
14611468 if (repo.visibility === "public") return true;
14621469 if (personalOrDirect(repo, user)) return true;
14631470 if (onCrew(db, repo, user)) return true;
@@ -1477,24 +1484,25 @@ export function canRead(
14771484 }
14781485
14791486 /**
1480 * The ids among `repos` that `user` may read: the answer of `canRead` for each, with the public
1481 * repositories of owners in good standing settled in one statement however many there are.
1482 * A repository that is not public is asked of `canRead` itself, as its rules are per repository.
1487 * The ids among `repos` that `user` may read: the answer of `canRead` for each, with every owner's
1488 * standing settled in one statement however many there are (#17831: a private repository asked
1489 * it again, one statement each, 33 of the owner's home's 70). The rest of a private repository's
1490 * rules are per repository.
14831491 */
14841492 export function canReadMany(db: Database, repos: readonly Repo[], user: User | undefined): Set<string> {
14851493 const out = new Set<string>();
1486 const publics = repos.filter((r) => r.visibility === "public");
1487 if (publics.length > 0) {
1494 const standing = new Set<string>();
1495 if (repos.length > 0) {
14881496 const rows = db
14891497 .query<{ id: string }, [string]>(
14901498 `SELECT repo.id AS id FROM repo WHERE repo.id IN (SELECT value FROM json_each(?1)) AND ${OWNER_STANDING}`,
14911499 )
1492 .all(JSON.stringify(publics.map((r) => r.id)));
1493 for (const r of rows) out.add(r.id);
1494 // A public repository whose owner is out of standing is read by a site admin alone.
1495 if (user?.isAdmin === true) for (const r of publics) out.add(r.id);
1500 .all(JSON.stringify(repos.map((r) => r.id)));
1501 for (const r of rows) standing.add(r.id);
14961502 }
1497 for (const repo of repos) if (repo.visibility !== "public" && canRead(db, repo, user)) out.add(repo.id);
1503 // A repository whose owner is out of standing is read by a site admin alone.
1504 for (const repo of repos)
1505 if (standing.has(repo.id) ? readsInStanding(db, repo, user) : user?.isAdmin === true) out.add(repo.id);
14981506 if (user?.appId !== undefined)
14991507 for (const repo of repos)
15001508 if (repo.origin !== null && out.has(repo.id) && !appMayRead(db, repo, user.appId)) out.delete(repo.id);
src/design/ratchets.ts +14 −8
@@ -101,11 +101,8 @@ export const WIDTH_SKIP = /\/(vendor|render|og)(\/|$)/;
101101 * Files that read a width in script today, with how many reads: layout must answer to CSS instead.
102102 */
103103 export const WIDTH_READS: Readonly<Record<string, number>> = {
104 "src/looks/capture.ts": 6,
105 "src/looks/readable.ts": 4,
106 "src/actions/runs.ts": 1,
107 "src/checks/looks.ts": 3,
108 "src/checks/summary.ts": 1,
104 // The page's own size, read in the browser for its look (#17831).
outdated

This file reads a width once (checked with widthReads on this head), but the ceiling is 6. Lower it to 1 so five new reads cannot slip in, as the pull description says it did.

105 "src/looks/capture.ts": 1,
109106 // +1: the probe for the page's own bar at the pill's corner (#16310).
110107 "src/apps/toolbar.ts": 6,
111108 // The toolbar lab prints the viewport it measured in (#16310).
@@ -117,13 +114,22 @@ export const WIDTH_READS: Readonly<Record<string, number>> = {
117114 "src/web/pages/map.ts": 2,
118115 "src/web/studio/cards.ts": 1,
119116 "src/keeper/covered.ts": 1,
120 "src/core/looks.ts": 3,
121117 };
122118
123 const WIDTH = /inner[W]idth|client[W]idth|offset[W]idth|matchMedia\(['"]\((min|max)-/g;
119 // A read of the page's width: an element's client/offset width (dotted or destructured), the
120 // window's inner width under any of its names or bare, or a min/max-width media query.
121 const WIDTH = /(?<![\w$])(?:client|offset)[W]idth\b|(?:[wW]indow|\b(?:self|globalThis|top|parent))\??\.inner[W]idth\b|(?<![\w$.])inner[W]idth\b|matchMedia\(['"]\((min|max)-/g;
122 // A property name (type field, object key, aliased destructure, typed parameter) declares; it does
123 // not read (#17831). A look's measured record spells its field like the browser's global, and each
124 // change to that record went red here. The `:` must be on the same line, so a ternary whose `:`
125 // starts the next line still counts. Every match is one the older, wider pattern made too.
outdated

This pattern loosens the rule. (?<![\w$.]) skips self., globalThis. and parent. reads, and (?!\s*[:,}]) skips cond ? innerWidth : 0. Narrow by path (WIDTH_SKIP for the look-record files) or rename the record field, rather than weakening what counts as a read.

126 const KEY = /(^|[{,;(])([ \t]*(?:readonly[ \t]+)?)(?:inner|client|offset)[W]idth(?=[ \t]*\??:)/gm;
127 // Whole-line comments: a sentence about a width is not a read of one.
128 const COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*).*$/gm;
124129
125130 /** How many times the text reads a width in script. */
126 export const widthReads = (text: string): number => (text.match(WIDTH) ?? []).length;
131 export const widthReads = (text: string): number =>
132 (text.replace(COMMENT_LINE, "").replace(KEY, "$1$2").match(WIDTH) ?? []).length;
127133
128134 /** Files that read a width in script beyond WIDTH_READS, and the fix. The behaviors read none. */
129135 export function widthReadsOver(files: readonly Source[]): string[] {
src/looks/capture.ts +3 −3
@@ -292,10 +292,10 @@ export function parseRecorded(raw: unknown): Recorded {
292292 sz !== null && typeof sz === "object"
293293 ? (sz as Record<string, unknown>)
294294 : {};
295 const { scrollWidth, scrollHeight, innerWidth, innerHeight } = from;
295 const { scrollWidth, scrollHeight, innerWidth: viewWidth, innerHeight } = from;
296296 const size =
297 whole(scrollWidth) && whole(scrollHeight) && whole(innerWidth) && whole(innerHeight)
298 ? { scrollWidth, scrollHeight, innerWidth, innerHeight }
297 whole(scrollWidth) && whole(scrollHeight) && whole(viewWidth) && whole(innerHeight)
298 ? { scrollWidth, scrollHeight, innerWidth: viewWidth, innerHeight }
299299 : undefined;
300300 return {
301301 ...(size === undefined ? {} : { size }),
test/app-home-batched.test.ts +19 −4
@@ -92,7 +92,22 @@ beforeAll(async () => {
9292 app = createApp({ dataDir: root, port: 0 });
9393 bob = await account("bob");
9494 servers = await serve(app);
95 // Each test's home is built here, not in the test (#17831): two commits, a fork, two apps and
96 // the wait for the fork to read diverged are git's CPU, not the page's, and the 2 s per-test
97 // budget counts hooks as the file's. The first test paid for them and for the page's first
98 // render, and went red on loaded checks (2.06-2.59 s). One read warms the page; the counts
99 // are statements, so a warm read changes none of them.
100 for (const who of ["ann", "fay", "lea", "sam"]) homes.set(who, await home(who));
101 const warm = homes.get("ann")!;
102 await (await fetch(`${servers.url}${warm.path}`, { headers: { cookie: warm.cookie } })).text();
95103 });
104 const homes = new Map<string, Home>();
105 /** The home `beforeAll` built for `who`. */
106 const built = (who: string): Home => {
107 const h = homes.get(who);
108 if (!h) throw new Error(`no home built for ${who}`);
109 return h;
110 };
96111 afterAll(() => {
97112 servers.stop();
98113 app.close();
@@ -242,14 +257,14 @@ const SLACK = 3;
242257 // Batched reads cost the same however many rows there are: a statement per
243258 // issue, pull or run would make the 200/40/5 page cost more than the 10/2/1 one.
244259 test("now reads its rows in batches with 200 open issues, 40 open pulls and 5 active runs", async () => {
245 const h = await home("ann");
260 const h = built("ann");
246261 const small = await costOf(h.path, h.cookie);
247262 h.grow();
248263 expect(Math.abs((await costOf(h.path, h.cookie)) - small)).toBeLessThanOrEqual(SLACK);
249264 });
250265
251266 test("following stays cheap on a diverged fork (regression guard; this pull does not change it)", async () => {
252 const h = await home("fay");
267 const h = built("fay");
253268 const small = await costOf(h.forkPath, h.cookie);
254269 h.grow();
255270 const res = await fetch(`${servers.url}${h.forkPath}`, { headers: { cookie: h.cookie } });
@@ -269,7 +284,7 @@ const growToLive = (repoId: string, author: string, from: number) =>
269284 })();
270285
271286 test("now costs the same statements on the live shape: 300 open issues, 2,000 closed, 3,500 edges, 20 pillars", async () => {
272 const h = await home("lea");
287 const h = built("lea");
273288 const small = await costOf(h.path, h.cookie);
274289 h.grow();
275290 growToLive(h.vault.id, h.owner.id, 200);
@@ -324,7 +339,7 @@ async function statementsByText(path: string, cookie: string): Promise<Map<strin
324339 }
325340
326341 test("every statement the home runs costs the same with 200 issues, 40 pulls and 5 runs as with a few", async () => {
327 const h = await home("sam");
342 const h = built("sam");
328343 const fewStatements = await statementsByText(h.path, h.cookie);
329344 h.grow();
330345 const manyStatements = await statementsByText(h.path, h.cookie);
test/behaviors-rule.test.ts +49 −1
@@ -9,7 +9,7 @@
99 */
1010 import { expect, test } from "bun:test";
1111 import { join } from "node:path";
12 import { RATCHET_FIX, tsSources, WIDTH_SKIP, widthReads, widthReadsOver } from "../src/design/ratchets.ts";
12 import { RATCHET_FIX, tsSources, WIDTH_READS, WIDTH_SKIP, widthReads, widthReadsOver } from "../src/design/ratchets.ts";
1313
1414 // The width-read table and its pattern live in src/design/ratchets.ts, so `bun
1515 // scripts/design-lint.ts` names the same files before a push (#17521).
@@ -139,6 +139,15 @@ test("no file reads a width in script beyond what the list allows", () => {
139139 expect(widthReadsOver(files)).toEqual([]);
140140 });
141141
142 test("each width ceiling is what its file reads today, no slack above it (#17831)", () => {
143 // A ceiling above the file's reads lets new ones in unseen: capture.ts sat at 6 while reading 1.
144 const reads = new Map(files.map((f) => [f.path, widthReads(f.text)]));
145 const off = Object.entries(WIDTH_READS)
146 .filter(([path, n]) => reads.get(path) !== n)
147 .map(([path, n]) => `${path}: the list allows ${n}, the file reads ${reads.get(path) ?? "nothing (gone)"}; lower its line to match`);
148 expect(off).toEqual([]);
149 });
150
142151 test("the behaviors themselves read no width and the home lanes use them", () => {
143152 const behaviors = files.find((f) => f.path === "src/web/behaviors.ts")?.text ?? "";
144153 expect(widthReads(behaviors)).toBe(0);
@@ -154,3 +163,42 @@ test("each script and width failure ends with the sentence that says which list
154163 expect(over.length).toBe(1);
155164 expect(over[0]!.endsWith(RATCHET_FIX.widthOver)).toBe(true);
156165 });
166
167 test("a property name declares and is not a read; every read of the page's width counts (#17831)", () => {
168 const W = "inner" + "Width", C = "client" + "Width", O = "offset" + "Width";
169 // A look's measured record spells its field like the global: declaring it reads nothing.
170 const declares = [
171 `type Size = { readonly ${W}: number };`,
172 `const row = { ${W}: r.inner_width };`,
173 `const sideways = s.scrollWidth > s.${W};`,
174 `const at = input.size?.${W};`,
175 `const { ${W}: w } = from;`,
176 `function size(${W}: number) {}`,
outdated

Add self.innerWidth and c ? innerWidth : 0 to the reads that must count; today the test locks in the blind spots.

177 `// the window's ${W} says whether it fits`,
178 ` * The page's ${C} in a sentence.`,
179 ];
180 for (const line of declares) expect([line, widthReads(line)]).toEqual([line, 0]);
181 expect(widthReadsOver([{ path: "src/core/looks.ts", text: declares.join("\n") }])).toEqual([]);
182 // Each of these reads the page, once.
183 const reads = [
184 `const w = window.${W};`,
185 `const w = self.${W};`,
186 `const w = globalThis.${W};`,
187 `const w = frame.contentWindow.${W};`,
188 `const w = window?.${W};`,
189 `const n = c ? ${W} : 0;`,
190 `const n = c ? 0 : ${W};`,
191 `resize(${W}, h);`,
192 `post({ w: ${W} });`,
193 `post({ ${W}, innerHeight });`,
194 `const w = ${W}, h = innerHeight;`,
195 `const { ${C} } = el;`,
196 `const { ${O}, offsetHeight } = el;`,
197 `W = root.${C};`,
198 `W = one.${O};`,
199 `if (matchMedia('(max-width: 719px)').matches) go();`,
200 ];
201 for (const line of reads) expect([line, widthReads(line)]).toEqual([line, 1]);
202 // A ternary whose ':' starts the next line still reads.
203 expect(widthReads(`const n = c ? ${W}\n : 0;`)).toBe(1);
204 });
test/can-read-many.test.ts +16
@@ -90,3 +90,19 @@ test("the statements it runs for public repositories do not grow with their numb
9090 expect(count(few)).toBe(1);
9191 expect(count(many)).toBe(count(few));
9292 });
93
94 test("the owner's own private repositories cost one statement however many there are (#17831)", async () => {
95 // Each one asked its owner's standing again, one statement apiece: 33 of the owner's home's 70.
96 const count = (list: Repo[]): number => {
97 const spy = spyOn(app.db, "query");
98 const read = canReadMany(app.db, list, owner);
99 expect(read.size).toBe(list.length);
100 const n = spy.mock.calls.length;
101 spy.mockRestore();
102 return n;
103 };
104 const few = all(await repos("mine-few", 2, "private", owner));
105 const many = all(await repos("mine-many", 25, "private", owner));
106 expect(count(many)).toBe(count(few));
107 expect(count(few)).toBe(1);
108 });
test/home-200.test.ts +5 −2
@@ -36,8 +36,11 @@ test("the second look at home as the owner costs a bounded number of statements,
3636 await look();
3737 const warm = await look();
3838 const slow = warm.phases.filter((p) => p.name !== "app" && p.ms > 20);
39 // Counted, not timed (#7824): 200 repositories and 100 asks are read in batches, not one statement each.
40 expectWork(await statements(f.app.db, async () => void (await look())), { max: 70 });
39 // Counted, not timed (#7824): 200 repositories and 100 asks are read in batches, not one
40 // statement each. 37 since the owner's standing is asked once for every repository, not once per
41 // private one (#17831):
42 // at 70 it sat at the ceiling, and a pull adding one statement anywhere on home went red here.

Non-blocking: the comment breaks mid-sentence at '(#17831):' and then continues; reflow it into one sentence.

43 expectWork(await statements(f.app.db, async () => void (await look())), { max: 45 });
4144 // The page's own total is the sum of named phases: nothing over 20 ms hides in "app" alone.
4245 // Every phase home.ts and the feed time: one missing here fails as a plain expect, which the gate's budget retry never re-runs.
4346 const known = new Set(["app", "spawned", "steps", "session", "repos", "asked", "kpis", "vitals", "waiting", "held", "panels", "feed", "settled", "visit", "since", "moving", "render"]);

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs