Skip to content
Merged
88 changes: 56 additions & 32 deletions config/scripts/build-mobile-web-app-bundle.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import {
import {
MOBILE_WEB_APP_BUNDLE_MAX_ENTRY_BYTES,
MOBILE_WEB_APP_BUNDLE_MAX_TOTAL_BYTES,
MOBILE_WEB_APP_BUNDLE_ROUTE_SCRIPT_SPREAD,
MOBILE_WEB_APP_BUNDLE_SCRIPT_MARGIN,
MOBILE_WEB_APP_BUNDLE_SCRIPT_SWEEP,
MOBILE_WEB_APP_SOURCE_DIRS,
assertAssetCeilingFitsShell,
mobileWebAppBundleMaxAssets,
Expand Down Expand Up @@ -431,6 +434,12 @@ describe('the Phase C budget', () => {
mobileWebAppBundleMaxAssets(routeKeys.length, imageCount)
)
expect(chunkCount).toBeLessThanOrEqual(mobileWebAppBundleMaxChunks(routeKeys.length))
// The other side of the same fence: an envelope further than the margin above the build is
// one nobody re-derived, so it fails here rather than surviving as headroom for a bump.
expect(
mobileWebAppBundleMaxChunks(routeKeys.length) - chunkCount,
'the chunk envelope is looser than this build; re-measure MOBILE_WEB_APP_BUNDLE_SCRIPT_SWEEP'
).toBeLessThanOrEqual(MOBILE_WEB_APP_BUNDLE_SCRIPT_MARGIN)
expect(entryStaticBytes).toBeLessThanOrEqual(MOBILE_WEB_APP_BUNDLE_MAX_ENTRY_BYTES)
},
120_000
Expand Down Expand Up @@ -461,34 +470,48 @@ describe('the Phase C budget', () => {
)
})

it('derives the chunk ceiling from the route count, not from a measured number', async () => {
// A chunk is emitted per distinct set of importers, so the count is not a function of the
// route count alone. Re-measured on this head, by copying the route tree and dropping routes
// from the end of the sorted key list -- both siblings of each, because deleting a .web.tsx
// alone leaves the native file for the builder to resolve and measures a different closure.
// The 14-route reading is the real tree and includes the one script the deferred mermaid
// artifact costs.
for (const [routes, measured] of [
[8, 32],
[10, 43],
[12, 61],
[14, 69]
]) {
expect(mobileWebAppBundleMaxChunks(routes), `${String(routes)} routes`).toBeGreaterThan(
measured
itBundling('sweeps the tree the envelope is anchored on', async () => {
// The sweep is the fence's only input, so it has to be about this tree: a route added anywhere
// in the sorted list fails here, which is the re-measure the plan asks for instead of a bump.
expect(MOBILE_WEB_APP_BUNDLE_SCRIPT_SWEEP.map(([key]) => key)).toEqual(
await collectMobileWebAppRouteKeys(join(projectDir, 'mobile', 'app'))
)
})

it('bounds every prefix the sweep measured, and the spread it reports', () => {
// An upper envelope of the measurement, not a fit: the count rises with the prefix, so the
// number above its top row is above all fifteen.
for (const [key, measured] of MOBILE_WEB_APP_BUNDLE_SCRIPT_SWEEP) {
expect(
mobileWebAppBundleMaxChunks(MOBILE_WEB_APP_BUNDLE_SCRIPT_SWEEP.length),
key
).toBeGreaterThanOrEqual(measured)
}
// 1 to 9 per route, which is why four per route was a bound rather than a fit and why the
// envelope cannot be a line through the measurement either.
expect(Math.min(...MOBILE_WEB_APP_BUNDLE_ROUTE_SCRIPT_SPREAD)).toBe(1)
expect(Math.max(...MOBILE_WEB_APP_BUNDLE_ROUTE_SCRIPT_SPREAD)).toBe(9)
})

it('sits exactly one margin over the swept tree and grants the worst route beyond it', () => {
const swept = MOBILE_WEB_APP_BUNDLE_SCRIPT_SWEEP.length
const measured = MOBILE_WEB_APP_BUNDLE_SCRIPT_SWEEP.at(-1)[1]
expect(mobileWebAppBundleMaxChunks(swept)).toBe(measured + MOBILE_WEB_APP_BUNDLE_SCRIPT_MARGIN)
// Past the swept tree each route is allowed the worst the sweep saw, so the next route breaches
// this only by costing more than any route measured.
for (const beyond of [1, 2, 9]) {
expect(mobileWebAppBundleMaxChunks(swept + beyond) - mobileWebAppBundleMaxChunks(swept)).toBe(
Math.max(...MOBILE_WEB_APP_BUNDLE_ROUTE_SCRIPT_SPREAD) * beyond
)
}
expect(mobileWebAppBundleMaxChunks(14)).toBe(72)
expect(mobileWebAppBundleMaxChunks(15) - mobileWebAppBundleMaxChunks(14)).toBe(4)
// Between four and nine more per route above, so the ceiling is a bound and not a fit -- and
// at 14 routes it is a close one. 69 measured against 72, with the last two routes having cost
// the 8 the ceiling grants for two: the next route that shares less than its neighbours fails
// here, which is what this is for.
expect(mobileWebAppBundleMaxChunks(14) - mobileWebAppBundleMaxChunks(12)).toBe(8)
// Flat below it: the fence is only ever asked about the real tree, and a shorter prefix is
// already under the top row it is anchored on.
expect(mobileWebAppBundleMaxChunks(swept - 1)).toBe(mobileWebAppBundleMaxChunks(swept))
})

it('refuses an engine chunked along its own lazy boundaries, and passes one artifact', () => {
// The two builds this ceiling has to tell apart, both measured at 14 routes.
// The two builds this ceiling has to tell apart, both measured at 14 routes and both still
// told apart by the envelope, which is tighter than the 4r + 16 they were first read against.
//
// The page reaches mermaid through one pre-bundled artifact and the bundle emits 69 scripts
// (68 of them the page's own split, one the deferred engine). Importing the package instead
Expand All @@ -507,9 +530,9 @@ describe('the Phase C budget', () => {

it('derives the asset ceiling so the chunk ceiling is always the one that trips first', () => {
// A bundle's assets are its chunks, its images and the document. Asserting one constant under
// another did not say that: with 42 images, 4 * 18 + 16 chunks plus 42 plus the document is
// 131 assets, over the flat 128 the ceiling used to be, so from 18 routes on the asset count
// failed first and named the wrong thing.
// another did not say that: with 42 images, the 98 chunks 18 routes are allowed plus 42 plus
// the document is 141 assets, over the flat 128 the ceiling used to be, so from 18 routes on
// the asset count failed first and named the wrong thing.
for (const routeCount of [14, 18, 24, 40]) {
for (const imageCount of [0, 42, 120]) {
const chunks = mobileWebAppBundleMaxChunks(routeCount)
Expand Down Expand Up @@ -542,13 +565,14 @@ describe('the Phase C budget', () => {

it('fails the build when the derived ceiling passes what the phone will accept', async () => {
// The shell hands back null for a manifest over its own ceiling, so a derived ceiling above
// that ships a green build no device can open. At the 42 images the tree carries, 4r + 16 +
// 42 + 1 crosses 256 at 50 routes, which Phase C reaches. A deferred engine kept to one
// artifact leaves that where it is; the 103-script version of it moved the crossing to 24.
// that ships a green build no device can open. At the 42 images the tree carries, the envelope
// plus 42 plus the document crosses 256 at 31 routes, which Phase C reaches. The crossing came
// in from 50 with the envelope: it grants the worst swept route to each one past the sweep,
// where `4r + 16` granted four, so re-measuring a tree whose routes share more moves it out.
expect(await readMobileWebBundleMaxAssets()).toBe(MOBILE_WEB_BUNDLE_MAX_ASSETS)
expect(assertAssetCeilingFitsShell(49, 42, MOBILE_WEB_BUNDLE_MAX_ASSETS)).toBe(255)
expect(() => assertAssetCeilingFitsShell(50, 42, MOBILE_WEB_BUNDLE_MAX_ASSETS)).toThrow(
/259 .*256/
expect(assertAssetCeilingFitsShell(30, 42, MOBILE_WEB_BUNDLE_MAX_ASSETS)).toBe(249)
expect(() => assertAssetCeilingFitsShell(31, 42, MOBILE_WEB_BUNDLE_MAX_ASSETS)).toThrow(
/258 .*256/
)
})
})
Expand Down
11 changes: 6 additions & 5 deletions config/scripts/mobile-web-app-page-grant-call-sites.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import ts from 'typescript-api'
*
* Grants are resolved once, from the route the shell opened, and carried for the life of the
* session: a route that reaches a seam it did not declare is a page whose action is refused at the
* host with nothing on screen to say why. Six of the session route's fourteen grants were pinned
* only by the list they were copied from (ruling 33.3); this is the rule the other eight already
* had, written once and driven over every row.
* host with nothing on screen to say why. Ruling 33.3 wrote that rule once and drove it over every
* row rather than leaving a grant pinned by the list it was copied from. Six rows pin eight of the
* session route's thirteen grants here; the other five have censuses of their own, as does the
* optional grant declared beside them.
*
* Parsed, not matched. A regex over source text finds the seam named in a comment, in a string and
* in an import it does not call, and the first two are exactly what a census must not count.
Expand All @@ -24,9 +25,9 @@ const importRow = (grants, specifier, why) => ({ kind: 'import', grants, specifi
* One row per grant the page can ask for through a call site of its own.
*
* `haptics` and `screencastBinary` have their own files (`mobile-web-app-haptics-seam.test.mjs`,
* `mobile-web-app-screencast-lane-grant.test.mjs`), the four audio grants have
* `mobile-web-app-screencast-lane-grant.test.mjs`), the three audio grants have
* `mobile-web-app-session-dictation-capture.test.mjs` and `externalNavigation` has
* `mobile-web-app-external-navigation-grant.test.mjs`, so those nine are not repeated here. The
* `mobile-web-app-external-navigation-grant.test.mjs`, so those six are not repeated here. The
* media three share one seam and one row: `useMediaPicker` is the only way in, and `canPickMedia`
* is `pick && read && release`, so a route reaching it needs all three or none of them.
*
Expand Down
85 changes: 80 additions & 5 deletions config/scripts/mobile-web-app-page-grant-call-sites.test.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
* The six grants that were pinned only by the list they were copied from (ruling 33.3).
* The eight grants this file pins, in six rows covering them (ruling 33.3).
*
* `haptics`, `screencastBinary`, `externalNavigation` and the four audio grants already have
* censuses of their own; these six did not, so removing any of them from a manifest entry reddened
* nothing. Each row below gets its own named case, and each case's control is the same rule driven
* over the entry that route would have had with the grant struck out.
* `haptics`, `screencastBinary`, `externalNavigation` and the three audio grants already have
* censuses of their own; these eight did not, so removing any of them from a manifest entry
* reddened nothing. Each row below gets its own named case, and each case's control is the same
* rule driven over the entry that route would have had with the grant struck out.
*/
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { mobileWebAppRouteClosure } from './build-mobile-web-app-bundle.mjs'
Expand All @@ -15,6 +17,7 @@ import {
pageRouteModulesCoverTheManifest
} from './mobile-web-app-page-route-modules.mjs'
import { MOBILE_WEB_PAGE_ROUTES } from './mobile-web-page-routes.mjs'
import { spelledCountsAgainstTables } from './spelled-count-census.mjs'
import {
PAGE_GRANT_CALL_SITES,
grantCallSites,
Expand All @@ -41,10 +44,82 @@ function closureOf(pathname) {
return held
}

const scriptsDir = import.meta.dirname

const sessionGrants = MOBILE_WEB_PAGE_ROUTES.filter((route) => route.pathname === SESSION).flatMap(
(route) => route.grants
)
const pinnedHere = PAGE_GRANT_CALL_SITES.flatMap((row) => row.grants)
const pinnedHereForSession = pinnedHere.filter((grant) => sessionGrants.includes(grant))
const sessionOptionalGrants = MOBILE_WEB_PAGE_ROUTES.filter(
(route) => route.pathname === SESSION
).flatMap((route) => route.optionalGrants ?? [])
const pinnedElsewhere = sessionGrants.filter((grant) => !pinnedHere.includes(grant))
const censusedElsewhere = [...sessionGrants, ...sessionOptionalGrants].filter(
(grant) => !pinnedHere.includes(grant)
)

/**
* The split both this file and its census state in prose, counted off the two tables instead.
*
* The rows here pin some of the session route's grants and named censuses pin the rest; the
* sentences that say how many were written when a fourteenth grant existed and did not move when
* #22072 removed it.
*
* Two counts, and which one a row takes is what its sentence is about. Exactly one row takes the
* intersection: the `.mjs` sentence for how many of the session route's grants these rows cover,
* which a row pinning a grant no route declares -- the shell can serve a verb before a screen asks
* for it -- would otherwise make the census demand a comment overstate the session route's list.
*
* Of the rows that could take either, every other one counts the rows here, the title about
* reaching them through the session route included. That title names the session route but is not
* a claim about it: the assertion under it compares `grantsNeeded` with every row's grants, so it
* has to move when the rows move and not when the route does. A census holding it at the
* intersection would have kept the title below the assertion it heads. The remaining rows count
* neither: they read the route's own list, or the grants on it that no row pins.
*
* The rows read the whole file, titles included: a count in a JSDoc and the same count in an `it`
* title go stale together, and pinning only the first leaves a green suite describing itself
* wrongly to whoever reads the run.
*/
const SPELLED_COUNTS = {
'mobile-web-app-page-grant-call-sites.mjs': [
{ precedes: 'grants', counted: sessionGrants.length },
{ precedes: 'audio grants', counted: sessionGrants.filter(isAudio).length },
{ precedes: 'rows pin', counted: PAGE_GRANT_CALL_SITES.length },
{ precedes: 'of the session', counted: pinnedHereForSession.length },
{ precedes: 'have censuses of their own', counted: pinnedElsewhere.length },
// One more than the row above: C8.1's optional grant is censused elsewhere as well, and the
// sentence here counts what has a census rather than what the route requires.
{ precedes: 'are not repeated here', counted: censusedElsewhere.length }
],
'mobile-web-app-page-grant-call-sites.test.mjs': [
// Both the header's "eight grants this file pins" and the title's "eight grants".
{ precedes: 'grants', counted: pinnedHere.length },
{ precedes: 'rows covering', counted: PAGE_GRANT_CALL_SITES.length },
{ precedes: 'audio grants', counted: sessionGrants.filter(isAudio).length },
{ precedes: 'did not', counted: pinnedHere.length },
{ precedes: 'through the session route', counted: pinnedHere.length }
]
}

function isAudio(grant) {
return grant.startsWith('native.audio.')
}

describe('the call-site reader', () => {
const navigate = PAGE_GRANT_CALL_SITES[0]
const storage = PAGE_GRANT_CALL_SITES[1]

it('spells the split off the two tables, in this file and in the one it reads', async () => {
for (const [name, rows] of Object.entries(SPELLED_COUNTS)) {
const source = await readFile(join(scriptsDir, name), 'utf8')
for (const { precedes, spelled, counts } of spelledCountsAgainstTables(source, rows)) {
expect(spelled, `${name}: ${precedes}`).toEqual(counts)
}
}
})

it('counts a call and not an import that never calls it', () => {
expect(
moduleReachesGrantRow(
Expand Down
29 changes: 28 additions & 1 deletion config/scripts/mobile-web-page-route-hop-coverage.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFile } from 'node:fs/promises'
import { describe, expect, it } from 'vitest'
import { mobileAppNavigationTargets } from './mobile-app-navigation-targets.mjs'
import { MOBILE_WEB_PAGE_ROUTES } from './mobile-web-page-routes.mjs'
import { spelledCountsAgainstTables } from './spelled-count-census.mjs'

/**
* Every in-page hop between page routes, and whether the opener's grants cover the target.
Expand Down Expand Up @@ -73,7 +75,7 @@ function sameRoute(pushed, declared) {
* a changed-file row and its diff.
*
* The seven C7 rows are the same rule with the arrows all one way: every one `X -> session`, one
* from each other page route. The session screen's fourteen grants are a strict
* from each other page route. The session screen's thirteen grants are a strict
* superset of every other route's, so nothing can reach it under the grants it was opened with —
* and nothing it pushes to leaves, because its own seven targets each declare a subset. A row in
* the other direction would mean a route had grown a grant the session lacks.
Expand All @@ -100,7 +102,32 @@ const HANDED_OFF = [
'/h/[hostId]/tasks -> /h/[hostId]/session/[worktreeId]'
]

/**
* The one count the note above spells out, counted off the list it is about.
*
* The superset claim is what the seven session rows rest on, so the number in it is load-bearing:
* it read fourteen through #22072, which removed a grant and moved nothing here.
*/
const SPELLED_COUNTS = [
{
precedes: 'grants are a strict',
counted: MOBILE_WEB_PAGE_ROUTES.filter(
(route) => route.pathname === '/h/[hostId]/session/[worktreeId]'
).flatMap((route) => route.grants).length
}
]

describe('in-page hops between page routes', () => {
it("spells the session route's grant count off the table it is claiming about", async () => {
const source = await readFile(import.meta.filename, 'utf8')
for (const { precedes, spelled, counts } of spelledCountsAgainstTables(
source,
SPELLED_COUNTS
)) {
expect(spelled, precedes).toEqual(counts)
}
})

it('finds the hops the app actually builds, so the census is not empty', () => {
const { targets } = mobileAppNavigationTargets()
// The sidebar's tasks push is the hop this lane exists for; if the census stops seeing it the
Expand Down
13 changes: 7 additions & 6 deletions config/scripts/mobile-web-page-routes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const MOBILE_WEB_PAGE_ROUTES = [
pathname: '/h/[hostId]/review/[worktreeId]',
grants: ['navigate', 'storage', 'externalLink', 'haptics', 'native.clipboard.write']
},
// The session screen: terminal and chat. Fourteen grants, every one of them read off a call site
// The session screen: terminal and chat. Thirteen grants, every one of them read off a call site
// in this route's own closure rather than carried from the design, and it is the only route that
// asks for the media verbs, the audio verbs or the screencast lane.
//
Expand All @@ -127,12 +127,13 @@ export const MOBILE_WEB_PAGE_ROUTES = [
// asks the shell through the grants `init` carried. Without it the pane subscribes without
// `wantsBinary` against a shell that would have encoded the frames.
//
// The four audio verbs are dictation's, and they are this route's alone: C7.10 PR D put the
// The three audio verbs are dictation's, and they are this route's alone: C7.10 PR D put the
// capture seam on the page and `mobile-web-app-session-dictation-capture.test.mjs` derives the
// list from the closure, which reaches `dictation-capture.web.ts` from the composer. All four or
// none — a route granted three records with the screen free to lock, and a lock mid-processing
// suspends the app and loses the transcript. Ruling 4's degradation is retired with them: the
// page no longer falls back to the vendored module's denied microphone.
// list from the closure, which reaches `dictation-capture.web.ts` from the composer. All three or
// none — a route granted two opens a microphone it has no verb to close, and the device side
// holds the screen awake for as long as one is open (#22072 moved that lock off the page, which
// is why the fourth verb this list carried is gone). Ruling 4's degradation is retired with them:
// the page no longer falls back to the vendored module's denied microphone.
//
// `externalNavigation` is the one optional grant in this list, and it is C8.1's. The HTML preview
// renders an agent's artifact in a sealed frame, and a tap on a link inside it becomes a top-frame
Expand Down
Loading
Loading