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
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
8 files
changed +127
−31
src/checks/looks.ts +3 −3
| @@ -42,9 +42,9 @@ const whole = (v: unknown): v is number => | ||
| 42 | 42 | |
| 43 | 43 | function parseSize(v: unknown): PageSize | undefined { |
| 44 | 44 | 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 } | |
| 48 | 48 | : undefined; |
| 49 | 49 | } |
| 50 | 50 |
src/core/repos.ts +18 −10
| @@ -1458,6 +1458,13 @@ export function canRead( | ||
| 1458 | 1458 | ): boolean { |
| 1459 | 1459 | if (user?.appId !== undefined && repo.origin !== null && !appMayRead(db, repo, user.appId)) return false; |
| 1460 | 1460 | 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 { | |
| 1461 | 1468 | if (repo.visibility === "public") return true; |
| 1462 | 1469 | if (personalOrDirect(repo, user)) return true; |
| 1463 | 1470 | if (onCrew(db, repo, user)) return true; |
| @@ -1477,24 +1484,25 @@ export function canRead( | ||
| 1477 | 1484 | } |
| 1478 | 1485 | |
| 1479 | 1486 | /** |
| 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. | |
| 1483 | 1491 | */ |
| 1484 | 1492 | export function canReadMany(db: Database, repos: readonly Repo[], user: User | undefined): Set<string> { |
| 1485 | 1493 | 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) { | |
| 1488 | 1496 | const rows = db |
| 1489 | 1497 | .query<{ id: string }, [string]>( |
| 1490 | 1498 | `SELECT repo.id AS id FROM repo WHERE repo.id IN (SELECT value FROM json_each(?1)) AND ${OWNER_STANDING}`, |
| 1491 | 1499 | ) |
| 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); | |
| 1496 | 1502 | } |
| 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); | |
| 1498 | 1506 | if (user?.appId !== undefined) |
| 1499 | 1507 | for (const repo of repos) |
| 1500 | 1508 | 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)(\/|$)/; | ||
| 101 | 101 | * Files that read a width in script today, with how many reads: layout must answer to CSS instead. |
| 102 | 102 | */ |
| 103 | 103 | 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). | |
| 105 | "src/looks/capture.ts": 1, | |
| 109 | 106 | // +1: the probe for the page's own bar at the pill's corner (#16310). |
| 110 | 107 | "src/apps/toolbar.ts": 6, |
| 111 | 108 | // The toolbar lab prints the viewport it measured in (#16310). |
| @@ -117,13 +114,22 @@ export const WIDTH_READS: Readonly<Record<string, number>> = { | ||
| 117 | 114 | "src/web/pages/map.ts": 2, |
| 118 | 115 | "src/web/studio/cards.ts": 1, |
| 119 | 116 | "src/keeper/covered.ts": 1, |
| 120 | "src/core/looks.ts": 3, | |
| 121 | 117 | }; |
| 122 | 118 | |
| 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. | |
| 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; | |
| 124 | 129 | |
| 125 | 130 | /** 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; | |
| 127 | 133 | |
| 128 | 134 | /** Files that read a width in script beyond WIDTH_READS, and the fix. The behaviors read none. */ |
| 129 | 135 | export function widthReadsOver(files: readonly Source[]): string[] { |
src/looks/capture.ts +3 −3
| @@ -292,10 +292,10 @@ export function parseRecorded(raw: unknown): Recorded { | ||
| 292 | 292 | sz !== null && typeof sz === "object" |
| 293 | 293 | ? (sz as Record<string, unknown>) |
| 294 | 294 | : {}; |
| 295 | const { scrollWidth, scrollHeight, innerWidth, innerHeight } = from; | |
| 295 | const { scrollWidth, scrollHeight, innerWidth: viewWidth, innerHeight } = from; | |
| 296 | 296 | 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 } | |
| 299 | 299 | : undefined; |
| 300 | 300 | return { |
| 301 | 301 | ...(size === undefined ? {} : { size }), |
test/app-home-batched.test.ts +19 −4
| @@ -92,7 +92,22 @@ beforeAll(async () => { | ||
| 92 | 92 | app = createApp({ dataDir: root, port: 0 }); |
| 93 | 93 | bob = await account("bob"); |
| 94 | 94 | 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(); | |
| 95 | 103 | }); |
| 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 | }; | |
| 96 | 111 | afterAll(() => { |
| 97 | 112 | servers.stop(); |
| 98 | 113 | app.close(); |
| @@ -242,14 +257,14 @@ const SLACK = 3; | ||
| 242 | 257 | // Batched reads cost the same however many rows there are: a statement per |
| 243 | 258 | // issue, pull or run would make the 200/40/5 page cost more than the 10/2/1 one. |
| 244 | 259 | 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"); | |
| 246 | 261 | const small = await costOf(h.path, h.cookie); |
| 247 | 262 | h.grow(); |
| 248 | 263 | expect(Math.abs((await costOf(h.path, h.cookie)) - small)).toBeLessThanOrEqual(SLACK); |
| 249 | 264 | }); |
| 250 | 265 | |
| 251 | 266 | 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"); | |
| 253 | 268 | const small = await costOf(h.forkPath, h.cookie); |
| 254 | 269 | h.grow(); |
| 255 | 270 | const res = await fetch(`${servers.url}${h.forkPath}`, { headers: { cookie: h.cookie } }); |
| @@ -269,7 +284,7 @@ const growToLive = (repoId: string, author: string, from: number) => | ||
| 269 | 284 | })(); |
| 270 | 285 | |
| 271 | 286 | 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"); | |
| 273 | 288 | const small = await costOf(h.path, h.cookie); |
| 274 | 289 | h.grow(); |
| 275 | 290 | growToLive(h.vault.id, h.owner.id, 200); |
| @@ -324,7 +339,7 @@ async function statementsByText(path: string, cookie: string): Promise<Map<strin | ||
| 324 | 339 | } |
| 325 | 340 | |
| 326 | 341 | 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"); | |
| 328 | 343 | const fewStatements = await statementsByText(h.path, h.cookie); |
| 329 | 344 | h.grow(); |
| 330 | 345 | const manyStatements = await statementsByText(h.path, h.cookie); |
test/behaviors-rule.test.ts +49 −1
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | import { expect, test } from "bun:test"; |
| 11 | 11 | 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"; | |
| 13 | 13 | |
| 14 | 14 | // The width-read table and its pattern live in src/design/ratchets.ts, so `bun |
| 15 | 15 | // 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", () => { | ||
| 139 | 139 | expect(widthReadsOver(files)).toEqual([]); |
| 140 | 140 | }); |
| 141 | 141 | |
| 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 | ||
| 142 | 151 | test("the behaviors themselves read no width and the home lanes use them", () => { |
| 143 | 152 | const behaviors = files.find((f) => f.path === "src/web/behaviors.ts")?.text ?? ""; |
| 144 | 153 | expect(widthReads(behaviors)).toBe(0); |
| @@ -154,3 +163,42 @@ test("each script and width failure ends with the sentence that says which list | ||
| 154 | 163 | expect(over.length).toBe(1); |
| 155 | 164 | expect(over[0]!.endsWith(RATCHET_FIX.widthOver)).toBe(true); |
| 156 | 165 | }); |
| 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) {}`, | |
| 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 | ||
| 90 | 90 | expect(count(few)).toBe(1); |
| 91 | 91 | expect(count(many)).toBe(count(few)); |
| 92 | 92 | }); |
| 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, | ||
| 36 | 36 | await look(); |
| 37 | 37 | const warm = await look(); |
| 38 | 38 | 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. | |
| 43 | expectWork(await statements(f.app.db, async () => void (await look())), { max: 45 }); | |
| 41 | 44 | // The page's own total is the sum of named phases: nothing over 20 ms hides in "app" alone. |
| 42 | 45 | // 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. |
| 43 | 46 | const known = new Set(["app", "spawned", "steps", "session", "repos", "asked", "kpis", "vitals", "waiting", "held", "panels", "feed", "settled", "visit", "since", "moving", "render"]); |
new above
offline
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.