@aglyn/tenant-data-admin 1.0.0-beta.223 → 1.0.0-beta.225

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/render-monitor.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { TENANT_APEX } from '@aglyn/aglyn/app-utils/tenant-apex'\nimport type { OperatorHealthStateDoc } from './operator-health'\n\n/**\n * THE RENDER MONITOR (AGL-3568): does a published site still render a page\n * that was not already in a cache?\n *\n * On 2026-10-05 every uncached page render on the published-site runtime\n * hung to the platform's 60 s limit for 28 minutes, and nothing alerted. The\n * three monitors that existed each missed it for its own reason:\n *\n * - the GitHub uptime probe is scheduled every 15 minutes and GitHub ran it\n * four times that day, none of them during the outage;\n * - its page rows fetch `/`, which the ISR cache answers, so a visitor's\n * page that was cached still loaded and the row stayed green;\n * - `/api/health/render/site` runs the page LOADER in-process and never the\n * site layout, which is where the request was stuck.\n *\n * So this fetches pages over HTTP, from the console (a different deployment\n * from the one it watches), on Cloud Scheduler every five minutes, and asks\n * for two pages no cache can answer:\n *\n * - `dynamic` — `/search`, a route every site has that renders on every\n * request (`force-dynamic`) inside the full site layout. A 200 that is a\n * complete document proves the layout and head ran just now.\n * - `isr` — a path nobody has ever requested, under the catch-all page.\n * A never-seen path is the one request the ISR cache cannot answer, so it\n * renders in the same static context every published page renders in,\n * which is the context the 2026-09-09 outage only appeared in. It is a\n * 404 by construction, and the site's own not-found page is drawn inside\n * the same layout, so a complete 404 document is a pass.\n *\n * A site that fails `threshold` runs in a row raises\n * `system.siteRenderFailing` once; the first passing run after that raises\n * `system.siteRenderRecovered` once. The state lives in\n * `operatorHealthState/render-monitor--<host>`, beside every other health\n * check, so Staff → Operator alerts lists it with its status and since when.\n *\n * Nothing here names an Aglyn host: the default target is the install's own\n * demonstration site label under its own tenant apex, the convention the\n * middleware and the render canaries already follow.\n */\n\n/** The state documents' collection: the health checks' own. */\nconst RENDER_MONITOR_COLLECTION = 'operatorHealthState'\n\n/** Every render monitor document id starts with this. */\nexport const RENDER_MONITOR_CHECK_PREFIX = 'render-monitor--'\n\n/** The beat id `/api/health/crons` reads for the scheduled run. */\nexport const RENDER_MONITOR_JOB_ID = 'render-monitor'\n\n/**\n * How long one page fetch may take. Well under the 60 s a platform function\n * may run, so a hung render is graded here as a timeout rather than waiting\n * for the platform's 504, and well under the route's own `maxDuration`.\n */\nexport const RENDER_MONITOR_TIMEOUT_MS = 25_000\n\n/** Failing runs in a row before the alert goes out. */\nexport const RENDER_MONITOR_DEFAULT_THRESHOLD = 2\n\n/**\n * The prefix of the never-requested path the `isr` probe asks for. A\n * lowercase slug on purpose: it must reach the catch-all page like any\n * screen slug would, not a reserved or rewritten route.\n */\nexport const RENDER_PROBE_ISR_PREFIX = '/aglyn-render-probe-'\n\nexport type RenderProbeKind = 'dynamic' | 'isr'\n\nexport interface RenderProbeSpec {\n kind: RenderProbeKind\n /** The path to request, given a fresh nonce. */\n path: (nonce: string) => string\n /** The status a healthy render answers with. */\n expectStatus: number\n}\n\nexport const RENDER_PROBES: readonly RenderProbeSpec[] = [\n { kind: 'dynamic', path: () => '/search', expectStatus: 200 },\n {\n kind: 'isr',\n path: (nonce) => `${RENDER_PROBE_ISR_PREFIX}${nonce}`,\n expectStatus: 404,\n },\n]\n\n/** One fetched response, reduced to what grading reads. */\nexport interface RenderProbeResponse {\n status: number\n contentType?: string | null\n body?: string\n location?: string | null\n /** `x-vercel-cache` on Vercel, `x-nextjs-cache` on a plain `next start`. */\n cacheState?: string | null\n}\n\nexport interface RenderProbeVerdict {\n ok: boolean\n /** Bot protection answered instead of the site. */\n challenged: boolean\n /** A stable code; `ok` on a pass. */\n code: string\n detail: string\n}\n\n/** Vercel's challenge interstitial, matched on the body like the front doors. */\nconst CHECKPOINT_MARKER = /Vercel Security Checkpoint/i\n\n/**\n * Cache states that mean the bytes came out of a store. A probe meant to\n * prove a render cannot pass on one of these, whatever the status.\n */\nconst CACHED_STATES = new Set(['HIT', 'STALE', 'PRERENDER'])\n\n/**\n * Grade one probe response. Pure.\n *\n * The page markers are the front doors' (`tools/scripts/lib/front-door.mjs`)\n * and for their reason: structural, never site copy. `</html>` proves the\n * document finished streaming, `/_next/static/` that our build produced it.\n */\nexport function gradeRenderProbe(\n response: RenderProbeResponse,\n expectStatus: number,\n): RenderProbeVerdict {\n const body = response.body ?? ''\n if (CHECKPOINT_MARKER.test(body)) {\n return {\n ok: false,\n challenged: true,\n code: 'challenged',\n detail: `bot protection answered (HTTP ${response.status}) instead of the site; check AGLYN_PROBE_TOKEN on the console`,\n }\n }\n if (response.status >= 300 && response.status < 400) {\n return {\n ok: false,\n challenged: false,\n code: 'redirected',\n detail: `redirected to ${response.location ?? '?'}, so no page was rendered`,\n }\n }\n if (response.status !== expectStatus) {\n return {\n ok: false,\n challenged: false,\n code: `http-${response.status}`,\n detail: `HTTP ${response.status}`,\n }\n }\n if (!/text\\/html/i.test(response.contentType ?? '')) {\n return {\n ok: false,\n challenged: false,\n code: 'not-html',\n detail: `not HTML (${response.contentType || 'no content-type'})`,\n }\n }\n if (!body.includes('</html>')) {\n return {\n ok: false,\n challenged: false,\n code: 'incomplete',\n detail: 'the document never finished (no </html>)',\n }\n }\n if (!body.includes('/_next/static/')) {\n return {\n ok: false,\n challenged: false,\n code: 'not-our-render',\n detail: 'no Next asset references, so the app did not render it',\n }\n }\n const cache = String(response.cacheState ?? '').trim().toUpperCase()\n if (CACHED_STATES.has(cache)) {\n return {\n ok: false,\n challenged: false,\n code: 'served-from-cache',\n detail: `served from cache (${cache}), so it proves no render`,\n }\n }\n return { ok: true, challenged: false, code: 'ok', detail: 'rendered' }\n}\n\n/** A configured value as an origin, or '' when it is not one. */\nexport function originOf(value: string | undefined | null): string {\n const trimmed = String(value ?? '').trim().replace(/\\/+$/, '')\n return /^https?:\\/\\/[^/\\s?#]+$/i.test(trimmed) ? trimmed.toLowerCase() : ''\n}\n\n/**\n * Which sites the monitor renders, from the console's environment.\n *\n * 1. The install's demonstration site: `OPERATOR_HEALTH_TENANT_ORIGIN` when\n * set (the one published site the operator alerts tick already watches),\n * else `https://<AGLYN_TENANT_DEMO or demo>.<tenant apex>`.\n * 2. Plus every origin in `RENDER_MONITOR_ORIGINS`, comma separated.\n *\n * `RENDER_MONITOR_ORIGINS=off` watches nothing, for an install with no\n * published site to watch. Values that are not origins are dropped.\n */\nexport function resolveRenderMonitorTargets(\n env: Readonly<Record<string, string | undefined>>,\n): string[] {\n const raw = String(env['RENDER_MONITOR_ORIGINS'] ?? '').trim()\n if (/^(off|none|false|0)$/i.test(raw)) return []\n const apex =\n String(env['NEXT_PUBLIC_TENANT_DOMAIN'] ?? '').trim() || TENANT_APEX\n const label = String(env['AGLYN_TENANT_DEMO'] ?? '').trim() || 'demo'\n const demo =\n originOf(env['OPERATOR_HEALTH_TENANT_ORIGIN']) ||\n originOf(`https://${label}.${apex}`)\n const listed = raw\n .split(/[\\s,]+/)\n .map((value) => originOf(value))\n .filter(Boolean)\n return [...new Set([demo, ...listed].filter(Boolean))]\n}\n\n/** The state document id for one origin. */\nexport function renderMonitorCheckId(origin: string): string {\n return `${RENDER_MONITOR_CHECK_PREFIX}${new URL(origin).host}`\n}\n\n/** One render monitor state document. */\nexport interface RenderMonitorStateDoc extends OperatorHealthStateDoc {\n origin: string\n /** Failing runs in a row, reset by any passing run. */\n consecutiveFailures: number\n /** When the current run of failures began; null while passing. */\n failingSinceMs: number | null\n lastOkAtMs: number | null\n}\n\nexport type RenderMonitorTransition = 'failing' | 'recovered' | null\n\n/**\n * The next state for one site, given what this run saw. Pure.\n *\n * The status turns `degraded` only once `threshold` runs in a row have\n * failed, so one slow cold start or one dropped connection says nothing. It\n * turns back on the first passing run, because a render that works is\n * proof, and a recovery held back is a recovery nobody hears about.\n */\nexport function nextRenderMonitorState(\n prior: Partial<RenderMonitorStateDoc> | null,\n observation: { ok: boolean; detail: string },\n context: { origin: string; label: string; threshold: number; now: number },\n): { next: RenderMonitorStateDoc; transition: RenderMonitorTransition } {\n const { origin, label, threshold, now } = context\n const checkId = renderMonitorCheckId(origin)\n const detail = String(observation.detail ?? '').slice(0, 1_000)\n const wasDegraded = prior?.status === 'degraded'\n if (observation.ok) {\n return {\n next: {\n checkId,\n label,\n origin,\n status: 'ok',\n sinceMs: !wasDegraded && prior?.sinceMs ? prior.sinceMs : now,\n detail,\n updatedAtMs: now,\n consecutiveFailures: 0,\n failingSinceMs: null,\n lastOkAtMs: now,\n },\n transition: wasDegraded ? 'recovered' : null,\n }\n }\n const failures = (prior?.consecutiveFailures ?? 0) + 1\n const failingSinceMs = prior?.failingSinceMs ?? now\n const degraded = wasDegraded || failures >= Math.max(1, threshold)\n return {\n next: {\n checkId,\n label,\n origin,\n status: degraded ? 'degraded' : 'ok',\n sinceMs: degraded\n ? wasDegraded && prior?.sinceMs\n ? prior.sinceMs\n : failingSinceMs\n : (prior?.sinceMs ?? now),\n detail,\n updatedAtMs: now,\n consecutiveFailures: failures,\n failingSinceMs,\n lastOkAtMs: prior?.lastOkAtMs ?? null,\n },\n transition: degraded && !wasDegraded ? 'failing' : null,\n }\n}\n\n/** One probe as the report shows it. */\nexport interface RenderProbeRow extends RenderProbeVerdict {\n kind: RenderProbeKind\n url: string\n ms: number\n httpStatus: number | null\n}\n\n/** One site's result for a run. */\nexport interface RenderMonitorSiteResult {\n origin: string\n ok: boolean\n probes: RenderProbeRow[]\n /** The state written; absent on a dry run or a failed write. */\n status?: 'ok' | 'degraded'\n consecutiveFailures?: number\n transition?: RenderMonitorTransition\n error?: string\n}\n\nexport interface RenderMonitorReport {\n dryRun: boolean\n threshold: number\n sites: RenderMonitorSiteResult[]\n}\n\n/** The read-modify-write of one site's state, as one transaction. */\nexport type RenderMonitorStore = <T>(\n checkId: string,\n work: (prior: Partial<RenderMonitorStateDoc> | null) => {\n next: RenderMonitorStateDoc\n result: T\n },\n) => Promise<T>\n\n/**\n * The default store: one Firestore transaction on the site's document, so\n * two overlapping runs cannot both decide they saw the edge.\n */\nconst firestoreStore: RenderMonitorStore = async (checkId, work) => {\n const { default: firebaseAdmin } = await import('./firebase-admin')\n const db = firebaseAdmin.app().firestore()\n const ref = db.collection(RENDER_MONITOR_COLLECTION).doc(checkId)\n return db.runTransaction(async (transaction: any) => {\n const snapshot = await transaction.get(ref)\n const prior = snapshot.exists\n ? (snapshot.data() as Partial<RenderMonitorStateDoc>)\n : null\n const { next, result } = work(prior)\n transaction.set(ref, next)\n return result\n })\n}\n\ntype RaiseAlert = (\n type: string,\n options: { dedupeKey?: string; context?: Record<string, string | number> },\n) => Promise<unknown>\n\nconst defaultRaise: RaiseAlert = async (type, options) => {\n const { raiseOperatorAlert } = await import('./operator-alerts')\n return raiseOperatorAlert(type, options)\n}\n\nfunction describeDuration(ms: number): string {\n const minutes = Math.max(1, Math.round(ms / 60_000))\n if (minutes < 60) return `${minutes} min`\n const hours = Math.floor(minutes / 60)\n const rest = minutes % 60\n return rest ? `${hours} h ${rest} min` : `${hours} h`\n}\n\nfunction probeLine(row: RenderProbeRow): string {\n return `${row.kind} ${new URL(row.url).pathname}: ${row.detail} (${row.ms} ms)`\n}\n\n/** Fetch and grade one probe. Never throws. */\nasync function runProbe(\n origin: string,\n spec: RenderProbeSpec,\n options: {\n fetcher: typeof fetch\n headers: Record<string, string>\n nonce: string\n timeoutMs: number\n clock: () => number\n },\n): Promise<RenderProbeRow> {\n const url = `${origin}${spec.path(options.nonce)}`\n const startedAt = options.clock()\n try {\n const response = await options.fetcher(url, {\n redirect: 'manual',\n cache: 'no-store',\n headers: {\n 'user-agent': 'aglyn-render-monitor',\n accept: 'text/html,application/xhtml+xml',\n ...options.headers,\n },\n signal: AbortSignal.timeout(options.timeoutMs),\n })\n const body = await response.text()\n const verdict = gradeRenderProbe(\n {\n status: response.status,\n contentType: response.headers.get('content-type'),\n body,\n location: response.headers.get('location'),\n cacheState:\n response.headers.get('x-vercel-cache') ??\n response.headers.get('x-nextjs-cache'),\n },\n spec.expectStatus,\n )\n return {\n kind: spec.kind,\n url,\n ms: options.clock() - startedAt,\n httpStatus: response.status,\n ...verdict,\n }\n } catch (error) {\n const name = (error as { name?: string } | null)?.name\n const timedOut = name === 'TimeoutError' || name === 'AbortError'\n return {\n kind: spec.kind,\n url,\n ms: options.clock() - startedAt,\n httpStatus: null,\n ok: false,\n challenged: false,\n code: timedOut ? 'timeout' : 'unreachable',\n detail: timedOut\n ? `no complete page in ${Math.round(options.timeoutMs / 1_000)} s`\n : `unreachable (${(error as { cause?: { code?: string } } | null)?.cause?.code ?? name ?? 'error'})`,\n }\n }\n}\n\n/**\n * One run: render every target, record each site's state, raise the edge.\n *\n * A dry run fetches and grades and writes nothing, so a person can ask\n * \"what would the monitor see right now\" without moving its state.\n */\nexport async function runRenderMonitor(\n options: {\n dryRun?: boolean\n env?: Readonly<Record<string, string | undefined>>\n fetcher?: typeof fetch\n /** Our own edge's bypass headers, from the caller's environment. */\n headers?: Record<string, string>\n now?: () => number\n nonce?: () => string\n timeoutMs?: number\n store?: RenderMonitorStore\n raise?: RaiseAlert\n } = {},\n): Promise<RenderMonitorReport> {\n const env = options.env ?? process.env\n const clock = options.now ?? Date.now\n const store = options.store ?? firestoreStore\n const raise = options.raise ?? defaultRaise\n const dryRun = Boolean(options.dryRun)\n const parsedThreshold = Number.parseInt(\n String(env['RENDER_MONITOR_FAILURE_THRESHOLD'] ?? ''),\n 10,\n )\n const threshold =\n Number.isFinite(parsedThreshold) && parsedThreshold >= 1\n ? parsedThreshold\n : RENDER_MONITOR_DEFAULT_THRESHOLD\n const nonce =\n options.nonce ??\n (() => `${clock().toString(36)}${Math.random().toString(36).slice(2, 8)}`)\n\n const sites = await Promise.all(\n resolveRenderMonitorTargets(env).map(\n async (origin): Promise<RenderMonitorSiteResult> => {\n const probes = await Promise.all(\n RENDER_PROBES.map((spec) =>\n runProbe(origin, spec, {\n fetcher: options.fetcher ?? fetch,\n headers: options.headers ?? {},\n nonce: nonce(),\n timeoutMs: options.timeoutMs ?? RENDER_MONITOR_TIMEOUT_MS,\n clock,\n }),\n ),\n )\n const ok = probes.every((probe) => probe.ok)\n const site: RenderMonitorSiteResult = { origin, ok, probes }\n if (dryRun) return site\n const host = new URL(origin).host\n const failing = probes.filter((probe) => !probe.ok)\n const detail = ok\n ? 'Fresh renders pass.'\n : failing.map(probeLine).join('; ')\n try {\n const now = clock()\n const { next, transition, failingSinceMs } = await store(\n renderMonitorCheckId(origin),\n (prior) => {\n const decided = nextRenderMonitorState(\n prior,\n { ok, detail },\n {\n origin,\n label: `Page rendering on ${host}`,\n threshold,\n now,\n },\n )\n return {\n next: decided.next,\n result: {\n ...decided,\n // When the failures a recovery ends began.\n failingSinceMs: prior?.failingSinceMs ?? prior?.sinceMs ?? now,\n },\n }\n },\n )\n site.status = next.status\n site.consecutiveFailures = next.consecutiveFailures\n site.transition = transition\n if (transition === 'failing') {\n await raise('system.siteRenderFailing', {\n dedupeKey: host,\n context: {\n site: host,\n failures: next.consecutiveFailures,\n since: describeDuration(now - next.sinceMs),\n detail,\n },\n })\n } else if (transition === 'recovered') {\n await raise('system.siteRenderRecovered', {\n dedupeKey: host,\n context: {\n site: host,\n duration: describeDuration(now - failingSinceMs),\n },\n })\n }\n } catch (error) {\n site.error = error instanceof Error ? error.message : String(error)\n console.error(`[render-monitor] ${host} could not be recorded`, error)\n }\n return site\n },\n ),\n )\n return { dryRun, threshold, sites }\n}\n"],"names":["TENANT_APEX","RENDER_MONITOR_COLLECTION","RENDER_MONITOR_CHECK_PREFIX","RENDER_MONITOR_JOB_ID","RENDER_MONITOR_TIMEOUT_MS","RENDER_MONITOR_DEFAULT_THRESHOLD","RENDER_PROBE_ISR_PREFIX","RENDER_PROBES","kind","path","expectStatus","nonce","CHECKPOINT_MARKER","CACHED_STATES","Set","gradeRenderProbe","response","body","test","ok","challenged","code","detail","status","location","contentType","includes","cache","String","cacheState","trim","toUpperCase","has","originOf","value","trimmed","replace","toLowerCase","resolveRenderMonitorTargets","env","raw","apex","label","demo","listed","split","map","filter","Boolean","renderMonitorCheckId","origin","URL","host","nextRenderMonitorState","prior","observation","context","threshold","now","checkId","slice","wasDegraded","next","sinceMs","updatedAtMs","consecutiveFailures","failingSinceMs","lastOkAtMs","transition","failures","degraded","Math","max","firestoreStore","work","default","firebaseAdmin","db","app","firestore","ref","collection","doc","runTransaction","transaction","snapshot","get","exists","data","result","set","defaultRaise","type","options","raiseOperatorAlert","describeDuration","ms","minutes","round","hours","floor","rest","probeLine","row","url","pathname","runProbe","spec","startedAt","clock","fetcher","redirect","headers","accept","signal","AbortSignal","timeout","timeoutMs","text","verdict","httpStatus","error","name","timedOut","cause","runRenderMonitor","process","Date","store","raise","dryRun","parsedThreshold","Number","parseInt","isFinite","toString","random","sites","Promise","all","probes","fetch","every","probe","site","failing","join","decided","dedupeKey","since","duration","Error","message","console"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,WAAW,QAAQ,qCAAoC;AAGhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsCC,GAED,6DAA6D,GAC7D,MAAMC,4BAA4B;AAElC,uDAAuD,GACvD,OAAO,MAAMC,8BAA8B,mBAAkB;AAE7D,iEAAiE,GACjE,OAAO,MAAMC,wBAAwB,iBAAgB;AAErD;;;;CAIC,GACD,OAAO,MAAMC,4BAA4B,MAAM;AAE/C,qDAAqD,GACrD,OAAO,MAAMC,mCAAmC,EAAC;AAEjD;;;;CAIC,GACD,OAAO,MAAMC,0BAA0B,uBAAsB;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EFsF,KAAKzH,MAAM,GAAGuC,KAAKvC,MAAM;YACzByH,KAAK/E,mBAAmB,GAAGH,KAAKG,mBAAmB;YACnD+E,KAAK5E,UAAU,GAAGA;YAClB,IAAIA,eAAe,WAAW;gBAC5B,MAAM6D,MAAM,4BAA4B;oBACtCmB,WAAWhG;oBACXI,SAAS;wBACPwF,MAAM5F;wBACNiB,UAAUP,KAAKG,mBAAmB;wBAClCoF,OAAOtD,iBAAiBrC,MAAMI,KAAKC,OAAO;wBAC1CzC;oBACF;gBACF;YACF,OAAO,IAAI8C,eAAe,aAAa;gBACrC,MAAM6D,MAAM,8BAA8B;oBACxCmB,WAAWhG;oBACXI,SAAS;wBACPwF,MAAM5F;wBACNkG,UAAUvD,iBAAiBrC,MAAMQ;oBACnC;gBACF;YACF;QACF,EAAE,OAAOuD,OAAO;YACduB,KAAKvB,KAAK,GAAGA,iBAAiB8B,QAAQ9B,MAAM+B,OAAO,GAAG5H,OAAO6F;YAC7DgC,QAAQhC,KAAK,CAAC,CAAC,iBAAiB,EAAErE,KAAK,sBAAsB,CAAC,EAAEqE;QAClE;QACA,OAAOuB;IACT;IAGJ,OAAO;QAAEd;QAAQzE;QAAWgF;IAAM;AACpC"}
1
+ {"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/render-monitor.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { TENANT_APEX } from '@aglyn/aglyn/app-utils/tenant-apex'\nimport type { OperatorHealthStateDoc } from './operator-health'\n\n/**\n * THE RENDER MONITOR (AGL-3568): does a published site still render a page\n * that was not already in a cache?\n *\n * On 2026-10-05 every uncached page render on the published-site runtime\n * hung to the platform's 60 s limit for 28 minutes, and nothing alerted. The\n * three monitors that existed each missed it for its own reason:\n *\n * - the GitHub uptime probe is scheduled every 15 minutes and GitHub ran it\n * four times that day, none of them during the outage;\n * - its page rows fetch `/`, which the ISR cache answers, so a visitor's\n * page that was cached still loaded and the row stayed green;\n * - `/api/health/render/site` runs the page LOADER in-process and never the\n * site layout, which is where the request was stuck.\n *\n * So this fetches pages over HTTP, from the console (a different deployment\n * from the one it watches), on Cloud Scheduler every five minutes, and asks\n * for two pages no cache can answer:\n *\n * - `dynamic` — `/search`, a route every site has that renders on every\n * request (`force-dynamic`) inside the full site layout. A 200 that is a\n * complete document proves the layout and head ran just now.\n * - `isr` — a path nobody has ever requested, under the catch-all page.\n * A never-seen path is the one request the ISR cache cannot answer, so it\n * renders in the same static context every published page renders in,\n * which is the context the 2026-09-09 outage only appeared in. It is a\n * 404 by construction, and the site's own not-found page is drawn inside\n * the same layout, so a complete 404 document is a pass.\n *\n * A site that fails `threshold` runs in a row raises\n * `system.siteRenderFailing` once; the first passing run after that raises\n * `system.siteRenderRecovered` once.\n *\n * ## A challenge is not a failed render (AGL-3580)\n *\n * When bot protection answers instead of the site, this run saw nothing about\n * the site at all. On 2026-10-05 the console had no `AGLYN_PROBE_TOKEN`, every\n * `/search` probe got the 429 checkpoint for three hours, and the monitor told\n * the operator \"Pages are not rendering on aglyn.com\" and then \"rendering\n * again after 3 h\" about a site that was serving pages the whole time. So a\n * run whose only failures are challenges is BLIND: it leaves the site's\n * verdict and failure count exactly as they were, and `threshold` blind runs\n * in a row raise `system.renderMonitorBlind` once, which names the setup\n * problem instead of an outage. A run with a real failure beside a challenge\n * is still a failure, because the real one is evidence. The state lives in\n * `operatorHealthState/render-monitor--<host>`, beside every other health\n * check, so Staff → Operator alerts lists it with its status and since when.\n *\n * Nothing here names an Aglyn host for anyone else: the default target is the\n * install's own demonstration site label under its own tenant apex, the\n * convention the middleware and the render canaries already follow. The one\n * exception is `PLATFORM_RENDER_PAGES` below, which applies on Aglyn's own\n * console deployment and nowhere else.\n *\n * ## Real pages, rendered fresh (AGL-3571)\n *\n * The two probes above draw the layout. They do not draw a page BODY, and\n * beta.223 hung only on page bodies with an image in them (an ancestor walk\n * looped on the page root, AGL-3565): `demo` has no images and the not-found\n * page has no body, so this monitor read green through it. So a third probe,\n * `page`, renders a real published page through the full ISR path — through\n * the tenant's `*.vercel.app` production domain (`RENDER_MONITOR_RENDER_ORIGIN`),\n * which honors `?tenantHost=` and sits behind Deployment Protection, naming\n * the site with a spelling no cache holds (`freshHostSpelling`). The cache key\n * includes the `[host]` segment, and `normalizeHostAlias` resolves every\n * spelling to the same site. Without the automation bypass the render origin\n * answers its sign-in redirect, so no `page` probe is made at all.\n *\n * One page per site per run, rotating through that site's list: a platform\n * break fails every page with an image, so every run, and alerts as fast as\n * before; a break on one page alone is seen on its turn.\n */\n\n/** The state documents' collection: the health checks' own. */\nconst RENDER_MONITOR_COLLECTION = 'operatorHealthState'\n\n/** Every render monitor document id starts with this. */\nexport const RENDER_MONITOR_CHECK_PREFIX = 'render-monitor--'\n\n/** The beat id `/api/health/crons` reads for the scheduled run. */\nexport const RENDER_MONITOR_JOB_ID = 'render-monitor'\n\n/**\n * How long one page fetch may take. Well under the 60 s a platform function\n * may run, so a hung render is graded here as a timeout rather than waiting\n * for the platform's 504, and well under the route's own `maxDuration`.\n */\nexport const RENDER_MONITOR_TIMEOUT_MS = 25_000\n\n/** Failing runs in a row before the alert goes out. */\nexport const RENDER_MONITOR_DEFAULT_THRESHOLD = 2\n\n/**\n * The prefix of the never-requested path the `isr` probe asks for. A\n * lowercase slug on purpose: it must reach the catch-all page like any\n * screen slug would, not a reserved or rewritten route.\n */\nexport const RENDER_PROBE_ISR_PREFIX = '/aglyn-render-probe-'\n\nexport type RenderProbeKind = 'dynamic' | 'isr' | 'page'\n\nexport interface RenderProbeSpec {\n kind: RenderProbeKind\n /** The path to request, given a fresh nonce. */\n path: (nonce: string) => string\n /** The status a healthy render answers with. */\n expectStatus: number\n}\n\nexport const RENDER_PROBES: readonly RenderProbeSpec[] = [\n { kind: 'dynamic', path: () => '/search', expectStatus: 200 },\n {\n kind: 'isr',\n path: (nonce) => `${RENDER_PROBE_ISR_PREFIX}${nonce}`,\n expectStatus: 404,\n },\n]\n\n/** One fetched response, reduced to what grading reads. */\nexport interface RenderProbeResponse {\n status: number\n contentType?: string | null\n body?: string\n location?: string | null\n /** `x-vercel-cache` on Vercel, `x-nextjs-cache` on a plain `next start`. */\n cacheState?: string | null\n}\n\nexport interface RenderProbeVerdict {\n ok: boolean\n /** Bot protection answered instead of the site. */\n challenged: boolean\n /** A stable code; `ok` on a pass. */\n code: string\n detail: string\n}\n\n/** Vercel's challenge interstitial, matched on the body like the front doors. */\nconst CHECKPOINT_MARKER = /Vercel Security Checkpoint/i\n\n/**\n * Cache states that mean the bytes came out of a store. A probe meant to\n * prove a render cannot pass on one of these, whatever the status.\n */\nconst CACHED_STATES = new Set(['HIT', 'STALE', 'PRERENDER'])\n\n/**\n * Grade one probe response. Pure.\n *\n * The page markers are the front doors' (`tools/scripts/lib/front-door.mjs`)\n * and for their reason: structural, never site copy. `</html>` proves the\n * document finished streaming, `/_next/static/` that our build produced it.\n */\nexport function gradeRenderProbe(\n response: RenderProbeResponse,\n expectStatus: number,\n): RenderProbeVerdict {\n const body = response.body ?? ''\n if (CHECKPOINT_MARKER.test(body)) {\n return {\n ok: false,\n challenged: true,\n code: 'challenged',\n detail: `bot protection answered (HTTP ${response.status}) instead of the site; check AGLYN_PROBE_TOKEN on the console`,\n }\n }\n if (response.status >= 300 && response.status < 400) {\n return {\n ok: false,\n challenged: false,\n code: 'redirected',\n detail: `redirected to ${response.location ?? '?'}, so no page was rendered`,\n }\n }\n if (response.status !== expectStatus) {\n return {\n ok: false,\n challenged: false,\n code: `http-${response.status}`,\n detail: `HTTP ${response.status}`,\n }\n }\n if (!/text\\/html/i.test(response.contentType ?? '')) {\n return {\n ok: false,\n challenged: false,\n code: 'not-html',\n detail: `not HTML (${response.contentType || 'no content-type'})`,\n }\n }\n if (!body.includes('</html>')) {\n return {\n ok: false,\n challenged: false,\n code: 'incomplete',\n detail: 'the document never finished (no </html>)',\n }\n }\n if (!body.includes('/_next/static/')) {\n return {\n ok: false,\n challenged: false,\n code: 'not-our-render',\n detail: 'no Next asset references, so the app did not render it',\n }\n }\n const cache = String(response.cacheState ?? '').trim().toUpperCase()\n if (CACHED_STATES.has(cache)) {\n return {\n ok: false,\n challenged: false,\n code: 'served-from-cache',\n detail: `served from cache (${cache}), so it proves no render`,\n }\n }\n return { ok: true, challenged: false, code: 'ok', detail: 'rendered' }\n}\n\n/**\n * The pages Aglyn's own console renders fresh when `RENDER_MONITOR_PAGES` is\n * unset: the canary's real client pages (`tools/scripts/lib/prod-canary.mjs`\n * `DEFAULT_TENANT_HOSTS`), images, reusable components, repeats and a form\n * between them. Applied ONLY on that console deployment (`VERCEL_PROJECT_ID`),\n * so no other install ever requests them.\n */\nexport const PLATFORM_RENDER_PAGES = [\n 'ready-to-roll.aglyn.app/',\n 'edr-construction.aglyn.app/',\n 'edr-construction.aglyn.app/services',\n 'edr-construction.aglyn.app/contact',\n].join(' ')\n\n/** Aglyn's console project on Vercel, the one deployment the list above is for. */\nexport const PLATFORM_CONSOLE_VERCEL_PROJECT_ID = 'prj_gEzxEXc0Lhs81rmaXIg2a1GbsDfl'\n\n/**\n * The tenant's production project domain on Vercel: follows every promote and\n * rollback, honors `?tenantHost=`, and is reachable only with the bypass.\n */\nexport const PLATFORM_RENDER_ORIGIN = 'https://aglyn-tenant-aglyn.vercel.app'\n\n/** The middleware's custom-domain sentinel (`apps/tenant/utils/get-host.ts`). */\nconst CNAME_HOST_PREFIX = 'cname--'\n\n/** Letters past this are never recased, so the bit arithmetic stays in 32 bits. */\nconst MAX_SPELLING_BITS = 30\n\nfunction spellingLetters(host: string): number[] {\n return [...host.toLowerCase()]\n .map((char, index) => (/[a-z]/.test(char) ? index : -1))\n .filter((index) => index >= 0)\n .slice(0, MAX_SPELLING_BITS)\n}\n\n/**\n * Spelling number `n` of one site: a `?tenantHost=` value that resolves to\n * that site and is a `[host]` route segment no other number produces, so the\n * page it asks for has no cache entry to come from.\n *\n * - `{sub}.{apex}` → the full name, letters upper-cased by the bits of `n`,\n * then one trailing dot per exhausted round of cases.\n * - any other name → `cname--{name}`, recased the same way and cycling after\n * `2 ** letters` (that form keeps trailing dots), with the prefix left\n * lower-case because the canonical-domain redirect tests the raw segment.\n *\n * MUST stay identical to `freshHostSpelling` in\n * `tools/scripts/lib/prod-canary.mjs`; `apps/tenant/specs/probe-host-spelling.spec.ts`\n * pins both against `normalizeHostAlias`.\n */\nexport function freshHostSpelling(host: string, n: number, apex: string = TENANT_APEX): string {\n const subdomain = host.endsWith(`.${apex}`)\n const prefix = subdomain ? '' : CNAME_HOST_PREFIX\n const chars = [...host.toLowerCase()]\n const letters = spellingLetters(host)\n const capacity = 2 ** letters.length\n const value = Math.max(0, Math.floor(Number(n) || 0))\n const bits = value % capacity\n letters.forEach((index, bit) => {\n if (Math.floor(bits / 2 ** bit) % 2 === 1) chars[index] = chars[index].toUpperCase()\n })\n const dots = subdomain ? '.'.repeat(Math.floor(value / capacity)) : ''\n return `${prefix}${chars.join('')}${dots}`\n}\n\n/**\n * The spelling number for a run at `nowMs`: minutes since 2026-10-01, so it\n * never repeats on one deployment — a `{sub}.aglyn.app` name of 18 letters\n * gains its first trailing dot after half a year.\n */\nexport function renderSpellingNumber(nowMs: number): number {\n return Math.max(1, Math.floor((nowMs - Date.UTC(2026, 9, 1)) / 60_000))\n}\n\n/** One watched site's real pages. */\nexport interface RenderMonitorPages {\n /** The site's public origin, which keys its state document. */\n origin: string\n host: string\n paths: string[]\n}\n\n/**\n * `\"a.example.app b.example.app/services, https://b.example.app/contact\"` →\n * one entry per host, paths merged, `/` when an entry names none. Entries that\n * are not a hostname are dropped, as `RENDER_MONITOR_ORIGINS` drops them.\n */\nexport function parseRenderMonitorPages(text: string | undefined | null): RenderMonitorPages[] {\n const byHost = new Map<string, string[]>()\n for (const raw of String(text ?? '').split(/[\\s,]+/)) {\n const entry = raw.trim().replace(/^https?:\\/\\//i, '')\n if (!entry) continue\n const slash = entry.indexOf('/')\n const host = (slash === -1 ? entry : entry.slice(0, slash)).toLowerCase()\n if (!/^[a-z0-9-]+(\\.[a-z0-9-]+)+$/.test(host)) continue\n const path = slash === -1 ? '/' : entry.slice(slash).replace(/\\/+$/, '') || '/'\n const paths = byHost.get(host) ?? []\n if (!paths.includes(path)) paths.push(path)\n byHost.set(host, paths)\n }\n return [...byHost].map(([host, paths]) => ({ origin: `https://${host}`, host, paths }))\n}\n\n/**\n * Which real pages the monitor renders, and through which origin.\n *\n * `RENDER_MONITOR_PAGES` (same grammar as the canary's `CANARY_TENANT_HOSTS`)\n * and `RENDER_MONITOR_RENDER_ORIGIN`, else — on Aglyn's own console only —\n * `PLATFORM_RENDER_PAGES` through `PLATFORM_RENDER_ORIGIN`. `off` (here or in\n * `RENDER_MONITOR_ORIGINS`) renders none. No render origin, no pages.\n */\nexport function resolveRenderMonitorPages(\n env: Readonly<Record<string, string | undefined>>,\n): { renderOrigin: string; pages: RenderMonitorPages[] } {\n const off = /^(off|none|false|0)$/i\n const raw = String(env['RENDER_MONITOR_PAGES'] ?? '').trim()\n if (off.test(raw) || off.test(String(env['RENDER_MONITOR_ORIGINS'] ?? '').trim())) {\n return { renderOrigin: '', pages: [] }\n }\n const platform = env['VERCEL_PROJECT_ID'] === PLATFORM_CONSOLE_VERCEL_PROJECT_ID\n const renderOrigin =\n originOf(env['RENDER_MONITOR_RENDER_ORIGIN']) || (platform ? PLATFORM_RENDER_ORIGIN : '')\n const pages = parseRenderMonitorPages(raw || (platform ? PLATFORM_RENDER_PAGES : ''))\n return renderOrigin ? { renderOrigin, pages } : { renderOrigin: '', pages: [] }\n}\n\n/** A configured value as an origin, or '' when it is not one. */\nexport function originOf(value: string | undefined | null): string {\n const trimmed = String(value ?? '').trim().replace(/\\/+$/, '')\n return /^https?:\\/\\/[^/\\s?#]+$/i.test(trimmed) ? trimmed.toLowerCase() : ''\n}\n\n/**\n * Which sites the monitor renders, from the console's environment.\n *\n * 1. The install's demonstration site: `OPERATOR_HEALTH_TENANT_ORIGIN` when\n * set (the one published site the operator alerts tick already watches),\n * else `https://<AGLYN_TENANT_DEMO or demo>.<tenant apex>`.\n * 2. Plus every origin in `RENDER_MONITOR_ORIGINS`, comma separated.\n *\n * `RENDER_MONITOR_ORIGINS=off` watches nothing, for an install with no\n * published site to watch. Values that are not origins are dropped.\n */\nexport function resolveRenderMonitorTargets(\n env: Readonly<Record<string, string | undefined>>,\n): string[] {\n const raw = String(env['RENDER_MONITOR_ORIGINS'] ?? '').trim()\n if (/^(off|none|false|0)$/i.test(raw)) return []\n const apex =\n String(env['NEXT_PUBLIC_TENANT_DOMAIN'] ?? '').trim() || TENANT_APEX\n const label = String(env['AGLYN_TENANT_DEMO'] ?? '').trim() || 'demo'\n const demo =\n originOf(env['OPERATOR_HEALTH_TENANT_ORIGIN']) ||\n originOf(`https://${label}.${apex}`)\n const listed = raw\n .split(/[\\s,]+/)\n .map((value) => originOf(value))\n .filter(Boolean)\n // A site whose pages are rendered is watched whole: its layout probes too.\n const paged = resolveRenderMonitorPages(env).pages.map((page) => page.origin)\n return [...new Set([demo, ...listed, ...paged].filter(Boolean))]\n}\n\n/** The state document id for one origin. */\nexport function renderMonitorCheckId(origin: string): string {\n return renderMonitorHostCheckId(new URL(origin).host)\n}\n\n/** The state document id for one host, as {@link renderMonitorCheckId} names it. */\nexport function renderMonitorHostCheckId(host: string): string {\n return `${RENDER_MONITOR_CHECK_PREFIX}${host}`\n}\n\n/** One render monitor state document. */\nexport interface RenderMonitorStateDoc extends OperatorHealthStateDoc {\n origin: string\n /** Failing runs in a row, reset by any passing run. */\n consecutiveFailures: number\n /** When the current run of failures began; null while passing. */\n failingSinceMs: number | null\n lastOkAtMs: number | null\n /** Runs in a row where bot protection answered instead of the site. */\n consecutiveBlindRuns?: number\n /** When the current blind stretch began; null while the monitor can see. */\n blindSinceMs?: number | null\n /** When a run last saw the site itself: a pass or a real failure. */\n lastObservedAtMs?: number | null\n}\n\nexport type RenderMonitorTransition = 'failing' | 'recovered' | 'blind' | null\n\n/**\n * The next state for one site, given what this run saw. Pure.\n *\n * The status turns `degraded` only once `threshold` runs in a row have\n * failed, so one slow cold start or one dropped connection says nothing. It\n * turns back on the first passing run, because a render that works is\n * proof, and a recovery held back is a recovery nobody hears about.\n *\n * A BLIND observation (every failure was a bot-protection challenge) changes\n * nothing about the site: its status, failure count and since-time carry over\n * untouched, and only the blind count moves. It transitions to `blind` once,\n * on the `threshold`-th blind run in a row.\n */\nexport function nextRenderMonitorState(\n prior: Partial<RenderMonitorStateDoc> | null,\n observation: { ok: boolean; detail: string; blind?: boolean },\n context: { origin: string; label: string; threshold: number; now: number },\n): { next: RenderMonitorStateDoc; transition: RenderMonitorTransition } {\n const { origin, label, threshold, now } = context\n const checkId = renderMonitorCheckId(origin)\n const detail = String(observation.detail ?? '').slice(0, 1_000)\n const wasDegraded = prior?.status === 'degraded'\n if (!observation.ok && observation.blind) {\n const blindRuns = (prior?.consecutiveBlindRuns ?? 0) + 1\n return {\n next: {\n checkId,\n label,\n origin,\n status: wasDegraded ? 'degraded' : 'ok',\n sinceMs: prior?.sinceMs ?? now,\n detail: `The monitor cannot see this site: ${detail}`.slice(0, 1_000),\n updatedAtMs: now,\n consecutiveFailures: prior?.consecutiveFailures ?? 0,\n failingSinceMs: prior?.failingSinceMs ?? null,\n lastOkAtMs: prior?.lastOkAtMs ?? null,\n consecutiveBlindRuns: blindRuns,\n blindSinceMs: prior?.blindSinceMs ?? now,\n lastObservedAtMs: prior?.lastObservedAtMs ?? null,\n },\n transition: blindRuns === Math.max(1, threshold) ? 'blind' : null,\n }\n }\n const sighted = { consecutiveBlindRuns: 0, blindSinceMs: null, lastObservedAtMs: now }\n if (observation.ok) {\n return {\n next: {\n checkId,\n label,\n origin,\n status: 'ok',\n sinceMs: !wasDegraded && prior?.sinceMs ? prior.sinceMs : now,\n detail,\n updatedAtMs: now,\n consecutiveFailures: 0,\n failingSinceMs: null,\n lastOkAtMs: now,\n ...sighted,\n },\n transition: wasDegraded ? 'recovered' : null,\n }\n }\n const failures = (prior?.consecutiveFailures ?? 0) + 1\n const failingSinceMs = prior?.failingSinceMs ?? now\n const degraded = wasDegraded || failures >= Math.max(1, threshold)\n return {\n next: {\n checkId,\n label,\n origin,\n status: degraded ? 'degraded' : 'ok',\n sinceMs: degraded\n ? wasDegraded && prior?.sinceMs\n ? prior.sinceMs\n : failingSinceMs\n : (prior?.sinceMs ?? now),\n detail,\n updatedAtMs: now,\n consecutiveFailures: failures,\n failingSinceMs,\n lastOkAtMs: prior?.lastOkAtMs ?? null,\n ...sighted,\n },\n transition: degraded && !wasDegraded ? 'failing' : null,\n }\n}\n\n/** One probe as the report shows it. */\nexport interface RenderProbeRow extends RenderProbeVerdict {\n kind: RenderProbeKind\n url: string\n ms: number\n httpStatus: number | null\n}\n\n/** One site's result for a run. */\nexport interface RenderMonitorSiteResult {\n origin: string\n ok: boolean\n /** Every failure this run was a challenge, so it saw nothing of the site. */\n blind: boolean\n probes: RenderProbeRow[]\n /** The state written; absent on a dry run or a failed write. */\n status?: 'ok' | 'degraded'\n consecutiveFailures?: number\n transition?: RenderMonitorTransition\n error?: string\n}\n\nexport interface RenderMonitorReport {\n dryRun: boolean\n threshold: number\n sites: RenderMonitorSiteResult[]\n}\n\n/** The read-modify-write of one site's state, as one transaction. */\nexport type RenderMonitorStore = <T>(\n checkId: string,\n work: (prior: Partial<RenderMonitorStateDoc> | null) => {\n next: RenderMonitorStateDoc\n result: T\n },\n) => Promise<T>\n\n/**\n * The default store: one Firestore transaction on the site's document, so\n * two overlapping runs cannot both decide they saw the edge.\n */\nconst firestoreStore: RenderMonitorStore = async (checkId, work) => {\n const { default: firebaseAdmin } = await import('./firebase-admin')\n const db = firebaseAdmin.app().firestore()\n const ref = db.collection(RENDER_MONITOR_COLLECTION).doc(checkId)\n return db.runTransaction(async (transaction: any) => {\n const snapshot = await transaction.get(ref)\n const prior = snapshot.exists\n ? (snapshot.data() as Partial<RenderMonitorStateDoc>)\n : null\n const { next, result } = work(prior)\n transaction.set(ref, next)\n return result\n })\n}\n\ntype RaiseAlert = (\n type: string,\n options: { dedupeKey?: string; context?: Record<string, string | number> },\n) => Promise<unknown>\n\nconst defaultRaise: RaiseAlert = async (type, options) => {\n const { raiseOperatorAlert } = await import('./operator-alerts')\n return raiseOperatorAlert(type, options)\n}\n\nfunction describeDuration(ms: number): string {\n const minutes = Math.max(1, Math.round(ms / 60_000))\n if (minutes < 60) return `${minutes} min`\n const hours = Math.floor(minutes / 60)\n const rest = minutes % 60\n return rest ? `${hours} h ${rest} min` : `${hours} h`\n}\n\n/**\n * The `page` probe's URL for one run: this run's page of the site's list\n * (rotating every five-minute slot), under this run's spelling of the site.\n */\nexport function pageProbeUrl(\n renderOrigin: string,\n site: RenderMonitorPages,\n slot: number,\n spelling: number,\n): string {\n const path = site.paths[Math.abs(slot) % site.paths.length] ?? '/'\n const url = new URL(path, renderOrigin)\n url.searchParams.set('tenantHost', freshHostSpelling(site.host, spelling))\n return url.toString()\n}\n\nfunction probeLine(row: RenderProbeRow): string {\n return `${row.kind} ${new URL(row.url).pathname}: ${row.detail} (${row.ms} ms)`\n}\n\n/** Fetch and grade one probe. Never throws. */\nasync function runProbe(\n url: string,\n spec: { kind: RenderProbeKind; expectStatus: number },\n options: {\n fetcher: typeof fetch\n headers: Record<string, string>\n timeoutMs: number\n clock: () => number\n },\n): Promise<RenderProbeRow> {\n const startedAt = options.clock()\n try {\n const response = await options.fetcher(url, {\n redirect: 'manual',\n cache: 'no-store',\n headers: {\n 'user-agent': 'aglyn-render-monitor',\n accept: 'text/html,application/xhtml+xml',\n ...options.headers,\n },\n signal: AbortSignal.timeout(options.timeoutMs),\n })\n const body = await response.text()\n const verdict = gradeRenderProbe(\n {\n status: response.status,\n contentType: response.headers.get('content-type'),\n body,\n location: response.headers.get('location'),\n cacheState:\n response.headers.get('x-vercel-cache') ??\n response.headers.get('x-nextjs-cache'),\n },\n spec.expectStatus,\n )\n return {\n kind: spec.kind,\n url,\n ms: options.clock() - startedAt,\n httpStatus: response.status,\n ...verdict,\n }\n } catch (error) {\n const name = (error as { name?: string } | null)?.name\n const timedOut = name === 'TimeoutError' || name === 'AbortError'\n return {\n kind: spec.kind,\n url,\n ms: options.clock() - startedAt,\n httpStatus: null,\n ok: false,\n challenged: false,\n code: timedOut ? 'timeout' : 'unreachable',\n detail: timedOut\n ? `no complete page in ${Math.round(options.timeoutMs / 1_000)} s`\n : `unreachable (${(error as { cause?: { code?: string } } | null)?.cause?.code ?? name ?? 'error'})`,\n }\n }\n}\n\n/**\n * One run: render every target, record each site's state, raise the edge.\n *\n * A dry run fetches and grades and writes nothing, so a person can ask\n * \"what would the monitor see right now\" without moving its state.\n */\nexport async function runRenderMonitor(\n options: {\n dryRun?: boolean\n env?: Readonly<Record<string, string | undefined>>\n fetcher?: typeof fetch\n /** Our own edge's bypass headers, from the caller's environment. */\n headers?: Record<string, string>\n now?: () => number\n nonce?: () => string\n timeoutMs?: number\n store?: RenderMonitorStore\n raise?: RaiseAlert\n } = {},\n): Promise<RenderMonitorReport> {\n const env = options.env ?? process.env\n const clock = options.now ?? Date.now\n const store = options.store ?? firestoreStore\n const raise = options.raise ?? defaultRaise\n const dryRun = Boolean(options.dryRun)\n const parsedThreshold = Number.parseInt(\n String(env['RENDER_MONITOR_FAILURE_THRESHOLD'] ?? ''),\n 10,\n )\n const threshold =\n Number.isFinite(parsedThreshold) && parsedThreshold >= 1\n ? parsedThreshold\n : RENDER_MONITOR_DEFAULT_THRESHOLD\n const nonce =\n options.nonce ??\n (() => `${clock().toString(36)}${Math.random().toString(36).slice(2, 8)}`)\n\n const probeOptions = {\n fetcher: options.fetcher ?? fetch,\n headers: options.headers ?? {},\n timeoutMs: options.timeoutMs ?? RENDER_MONITOR_TIMEOUT_MS,\n clock,\n }\n // Real pages need the render origin AND the bypass that gets past its\n // Deployment Protection; without both a `page` probe could only fail.\n const { renderOrigin, pages } = resolveRenderMonitorPages(env)\n const canRenderPages = Boolean(\n renderOrigin && probeOptions.headers['x-vercel-protection-bypass'],\n )\n const runAt = clock()\n const slot = Math.floor(runAt / 300_000)\n\n const sites = await Promise.all(\n resolveRenderMonitorTargets(env).map(\n async (origin): Promise<RenderMonitorSiteResult> => {\n const layoutProbes = RENDER_PROBES.map((spec) =>\n runProbe(`${origin}${spec.path(nonce())}`, spec, probeOptions),\n )\n const site = canRenderPages ? pages.find((page) => page.origin === origin) : undefined\n const pageProbe = site\n ? [\n runProbe(\n pageProbeUrl(renderOrigin, site, slot, renderSpellingNumber(runAt)),\n { kind: 'page', expectStatus: 200 },\n probeOptions,\n ),\n ]\n : []\n const probes = await Promise.all([...layoutProbes, ...pageProbe])\n const ok = probes.every((probe) => probe.ok)\n const failing = probes.filter((probe) => !probe.ok)\n const blind = !ok && failing.every((probe) => probe.challenged)\n const result: RenderMonitorSiteResult = { origin, ok, blind, probes }\n if (dryRun) return result\n const host = new URL(origin).host\n const detail = ok\n ? 'Fresh renders pass.'\n : failing.map(probeLine).join('; ')\n try {\n const now = clock()\n const { next, transition, failingSinceMs } = await store(\n renderMonitorCheckId(origin),\n (prior) => {\n const decided = nextRenderMonitorState(\n prior,\n { ok, detail, blind },\n {\n origin,\n label: `Page rendering on ${host}`,\n threshold,\n now,\n },\n )\n return {\n next: decided.next,\n result: {\n ...decided,\n // When the failures a recovery ends began.\n failingSinceMs: prior?.failingSinceMs ?? prior?.sinceMs ?? now,\n },\n }\n },\n )\n result.status = next.status\n result.consecutiveFailures = next.consecutiveFailures\n result.transition = transition\n if (transition === 'failing') {\n await raise('system.siteRenderFailing', {\n dedupeKey: host,\n context: {\n site: host,\n failures: next.consecutiveFailures,\n since: describeDuration(now - next.sinceMs),\n detail,\n },\n })\n } else if (transition === 'blind') {\n await raise('system.renderMonitorBlind', {\n dedupeKey: host,\n context: {\n site: host,\n runs: next.consecutiveBlindRuns ?? threshold,\n detail,\n },\n })\n } else if (transition === 'recovered') {\n await raise('system.siteRenderRecovered', {\n dedupeKey: host,\n context: {\n site: host,\n duration: describeDuration(now - failingSinceMs),\n },\n })\n }\n } catch (error) {\n result.error = error instanceof Error ? error.message : String(error)\n console.error(`[render-monitor] ${host} could not be recorded`, error)\n }\n return result\n },\n ),\n )\n return { dryRun, threshold, sites }\n}\n\n/**\n * One watched site as `/api/health/pages` reports it (AGL-3580).\n *\n * The shape of every other health check — `ok`, `ms`, a stable `code` — plus\n * when the monitor last saw the site, so a reader can tell a fresh verdict\n * from an old one without a second request.\n */\nexport interface RenderPagesCheck {\n ok: boolean\n ms: number\n code?: string\n /** When a run last rendered or failed this site's pages, ISO. */\n lastObservedAt: string | null\n /** When the site's current status began, ISO. */\n since: string | null\n}\n\n/**\n * A verdict older than this says the monitor stopped, not that the site did.\n * Four missed five-minute runs.\n */\nexport const RENDER_PAGES_STALE_MS = 20 * 60_000\n\n/** How many sites one `?site=` may ask about. */\nexport const RENDER_PAGES_MAX_SITES = 10\n\n/**\n * Which watched hosts a `?site=` asks about. Pure.\n *\n * Absent or empty asks about every watched site. A host that is not watched\n * is kept, so the caller can answer it red: a public monitor pointed at a\n * site nobody renders is a monitor that can never go red, which is the\n * mistake this door exists to undo.\n */\nexport function requestedRenderPagesHosts(\n query: string | null | undefined,\n watchedOrigins: readonly string[],\n): string[] {\n const asked = String(query ?? '')\n .split(/[\\s,]+/)\n .map((entry) => entry.trim().toLowerCase().replace(/^https?:\\/\\//, '').replace(/\\/.*$/, ''))\n .filter((entry) => /^[a-z0-9-]+(\\.[a-z0-9-]+)+$/.test(entry))\n const hosts = asked.length ? asked : watchedOrigins.map((origin) => new URL(origin).host)\n return [...new Set(hosts)].slice(0, RENDER_PAGES_MAX_SITES)\n}\n\n/**\n * The public verdict on real pages, per site, from the render monitor's\n * stored state (AGL-3580). Pure.\n *\n * This is what the status page's `Published sites` and `Marketing site`\n * monitors read. Before it they read `/api/health/render/*`, which builds a\n * node tree inside a route handler and never draws a page, and both stayed at\n * 100% through two outages on 2026-10-05 in which every fresh page hung.\n *\n * Red is reserved for the one thing a visitor would notice:\n *\n * - `not-rendering` — the monitor saw the site fail to render pages it could\n * not have served from a cache, `threshold` runs in a row.\n * - `not-watched` — the monitor renders nothing for this host, so this row\n * could never go red; fix the monitor's config or the URL.\n * - `state-unavailable` — the verdicts could not be read at all.\n *\n * Green with a code is a verdict this door cannot improve on, and is never\n * reported as an outage, because it is not one:\n *\n * - `awaiting-first-run` — a site added since the monitor last ran.\n * - `monitor-blind` — bot protection answered the monitor; the site's last\n * real verdict stands, and the operator alert says what to fix.\n * - `observation-stale` — no run in {@link RENDER_PAGES_STALE_MS}; the\n * monitor's own `render-monitor` row on `/api/health/crons` is what goes\n * red for that, so a stopped scheduler is never told as a site outage.\n */\nexport function renderPagesHealth(\n hosts: readonly string[],\n watchedOrigins: readonly string[],\n states: ReadonlyMap<string, Partial<RenderMonitorStateDoc>> | null,\n ms: number,\n now: number = Date.now(),\n): Record<string, RenderPagesCheck> {\n const watched = new Set(watchedOrigins.map((origin) => new URL(origin).host))\n const iso = (value: number | null | undefined) =>\n typeof value === 'number' && Number.isFinite(value) ? new Date(value).toISOString() : null\n const checks: Record<string, RenderPagesCheck> = {}\n if (!hosts.length) {\n checks['sites'] = { ok: false, ms, code: 'no-sites-watched', lastObservedAt: null, since: null }\n return checks\n }\n for (const host of hosts) {\n const blank = { ms, lastObservedAt: null, since: null }\n if (!watched.has(host)) {\n checks[host] = { ...blank, ok: false, code: 'not-watched' }\n continue\n }\n if (states === null) {\n checks[host] = { ...blank, ok: false, code: 'state-unavailable' }\n continue\n }\n const state = states.get(renderMonitorHostCheckId(host))\n if (!state) {\n checks[host] = { ...blank, ok: true, code: 'awaiting-first-run' }\n continue\n }\n const observed = state.lastObservedAtMs ?? state.lastOkAtMs ?? null\n const row = { ms, lastObservedAt: iso(observed), since: iso(state.sinceMs) }\n if (state.status === 'degraded') {\n checks[host] = { ...row, ok: false, code: 'not-rendering' }\n } else if ((state.consecutiveBlindRuns ?? 0) > 0) {\n checks[host] = { ...row, ok: true, code: 'monitor-blind' }\n } else if (now - (state.updatedAtMs ?? 0) > RENDER_PAGES_STALE_MS) {\n checks[host] = { ...row, ok: true, code: 'observation-stale' }\n } else {\n checks[host] = { ...row, ok: true }\n }\n }\n return checks\n}\n\n/**\n * Read the stored verdicts for `hosts`, one batched read. Null when the read\n * fails, which {@link renderPagesHealth} reports red rather than calm.\n */\nexport async function readRenderMonitorStates(\n hosts: readonly string[],\n): Promise<Map<string, Partial<RenderMonitorStateDoc>> | null> {\n try {\n const { default: firebaseAdmin } = await import('./firebase-admin')\n const db = firebaseAdmin.app().firestore()\n const refs = hosts.map((host) =>\n db.collection(RENDER_MONITOR_COLLECTION).doc(renderMonitorHostCheckId(host)),\n )\n const snapshots = refs.length ? await db.getAll(...refs) : []\n const states = new Map<string, Partial<RenderMonitorStateDoc>>()\n for (const snapshot of snapshots) {\n if (snapshot.exists) states.set(snapshot.id, snapshot.data() as Partial<RenderMonitorStateDoc>)\n }\n return states\n } catch (error) {\n console.error('[render-monitor] stored verdicts could not be read', error)\n return null\n }\n}\n"],"names":["TENANT_APEX","RENDER_MONITOR_COLLECTION","RENDER_MONITOR_CHECK_PREFIX","RENDER_MONITOR_JOB_ID","RENDER_MONITOR_TIMEOUT_MS","RENDER_MONITOR_DEFAULT_THRESHOLD","RENDER_PROBE_ISR_PREFIX","RENDER_PROBES","kind","path","expectStatus","nonce","CHECKPOINT_MARKER","CACHED_STATES","Set","gradeRenderProbe","response","body","test","ok","challenged","code","detail","status","location","contentType","includes","cache","String","cacheState","trim","toUpperCase","has","PLATFORM_RENDER_PAGES","join","PLATFORM_CONSOLE_VERCEL_PROJECT_ID","PLATFORM_RENDER_ORIGIN","CNAME_HOST_PREFIX","MAX_SPELLING_BITS","spellingLetters","host","toLowerCase","map","char","index","filter","slice","freshHostSpelling","n","apex","subdomain","endsWith","prefix","chars","letters","capacity","length","value","Math","max","floor","Number","bits","forEach","bit","dots","repeat","renderSpellingNumber","nowMs","Date","UTC","parseRenderMonitorPages","text","byHost","Map","raw","split","entry","replace","slash","indexOf","paths","get","push","set","origin","resolveRenderMonitorPages","env","off","renderOrigin","pages","platform","originOf","trimmed","resolveRenderMonitorTargets","label","demo","listed","Boolean","paged","page","renderMonitorCheckId","renderMonitorHostCheckId","URL","nextRenderMonitorState","prior","observation","context","threshold","now","checkId","wasDegraded","blind","blindRuns","consecutiveBlindRuns","next","sinceMs","updatedAtMs","consecutiveFailures","failingSinceMs","lastOkAtMs","blindSinceMs","lastObservedAtMs","transition","sighted","failures","degraded","firestoreStore","work","default","firebaseAdmin","db","app","firestore","ref","collection","doc","runTransaction","transaction","snapshot","exists","data","result","defaultRaise","type","options","raiseOperatorAlert","describeDuration","ms","minutes","round","hours","rest","pageProbeUrl","site","slot","spelling","abs","url","searchParams","toString","probeLine","row","pathname","runProbe","spec","startedAt","clock","fetcher","redirect","headers","accept","signal","AbortSignal","timeout","timeoutMs","verdict","httpStatus","error","name","timedOut","cause","runRenderMonitor","process","store","raise","dryRun","parsedThreshold","parseInt","isFinite","random","probeOptions","fetch","canRenderPages","runAt","sites","Promise","all","layoutProbes","find","undefined","pageProbe","probes","every","probe","failing","decided","dedupeKey","since","runs","duration","Error","message","console","RENDER_PAGES_STALE_MS","RENDER_PAGES_MAX_SITES","requestedRenderPagesHosts","query","watchedOrigins","asked","hosts","renderPagesHealth","states","watched","iso","toISOString","checks","lastObservedAt","state","blank","observed","readRenderMonitorStates","refs","snapshots","getAll","id"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,WAAW,QAAQ,qCAAoC;AAGhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuEC,GAED,6DAA6D,G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AAO;YACL6M,MAAM,CAAC1L,KAAK,GAAG,aAAK0H;gBAAK/I,IAAI;;QAC/B;IACF;IACA,OAAO+M;AACT;AAEA;;;CAGC,GACD,OAAO,eAAeK,wBACpBX,KAAwB;IAExB,IAAI;QACF,MAAM,EAAE3F,SAASC,aAAa,EAAE,GAAG,MAAM,MAAM,CAAC;QAChD,MAAMC,KAAKD,cAAcE,GAAG,GAAGC,SAAS;QACxC,MAAMmG,OAAOZ,MAAMlL,GAAG,CAAC,CAACF,OACtB2F,GAAGI,UAAU,CAACtI,2BAA2BuI,GAAG,CAAClC,yBAAyB9D;QAExE,MAAMiM,YAAYD,KAAKhL,MAAM,GAAG,MAAM2E,GAAGuG,MAAM,IAAIF,QAAQ,EAAE;QAC7D,MAAMV,SAAS,IAAIpJ;QACnB,KAAK,MAAMiE,YAAY8F,UAAW;YAChC,IAAI9F,SAASC,MAAM,EAAEkF,OAAO1I,GAAG,CAACuD,SAASgG,EAAE,EAAEhG,SAASE,IAAI;QAC5D;QACA,OAAOiF;IACT,EAAE,OAAO5C,OAAO;QACdmC,QAAQnC,KAAK,CAAC,sDAAsDA;QACpE,OAAO;IACT;AACF"}
@@ -137,6 +137,13 @@ export declare const MEDIA_CDN_STABLE_EDGE_BYPASS_CACHE_CONTROL = "private, max-
137
137
  * the stable policy, for the AGL-1515 reasons above.
138
138
  */
139
139
  export declare const MEDIA_CDN_VERSIONED_CACHE_CONTROL = "public, max-age=31536000, s-maxage=3600, stale-while-revalidate=86400, immutable";
140
+ /**
141
+ * The generated width that answers a `?w=` request (AGL-3486): the width
142
+ * itself when the asset has it, otherwise the smallest larger one, and `null`
143
+ * when every variant is narrower than asked — then the display copy or the
144
+ * original is the closest picture there is.
145
+ */
146
+ export declare function mediaCdnVariantFor(variants: readonly unknown[], width: number): number | null;
140
147
  /**
141
148
  * Whether a versioned request may be answered with
142
149
  * {@link MEDIA_CDN_VERSIONED_CACHE_CONTROL} (AGL-3485).
@@ -20,6 +20,7 @@ import { BANDWIDTH_CAP_RETRY_AFTER_SECONDS, bandwidthCapApplies, bandwidthCapEng
20
20
  import { MEDIA_CDN_SEGMENT, mediaCdnScopeRefusal, parseMediaCdnScope } from "@aglyn/aglyn/app-utils/media-cdn-scope";
21
21
  import { mediaDeliveryProvider } from "@aglyn/aglyn/plugin-manager/media-delivery-provider";
22
22
  import { parseSiteIconSpec, SITE_ICON_BACKGROUND_PARAM, SITE_ICON_PARAM, SITE_ICON_VERSION_PARAM } from "@aglyn/aglyn/app-utils/site-icon-set";
23
+ import { scheduleAfterResponse } from "./after-response.js";
23
24
  import { analyticsDayExpiresAt } from "./analytics-retention.js";
24
25
  import { firebaseAdmin } from "./firebase-admin.js";
25
26
  import { recordMediaServe } from "./media-serve-count.js";
@@ -173,6 +174,21 @@ const SEGMENT = MEDIA_CDN_SEGMENT;
173
174
  * hour, which is what `s-maxage` is for. Images only — every other type keeps
174
175
  * the stable policy, for the AGL-1515 reasons above.
175
176
  */ export const MEDIA_CDN_VERSIONED_CACHE_CONTROL = 'public, max-age=31536000, s-maxage=3600, stale-while-revalidate=86400, immutable';
177
+ /**
178
+ * The generated width that answers a `?w=` request (AGL-3486): the width
179
+ * itself when the asset has it, otherwise the smallest larger one, and `null`
180
+ * when every variant is narrower than asked — then the display copy or the
181
+ * original is the closest picture there is.
182
+ */ export function mediaCdnVariantFor(variants, width) {
183
+ if (!(width > 0)) return null;
184
+ let best = null;
185
+ for (const candidate of variants){
186
+ if (typeof candidate !== 'number' || !Number.isInteger(candidate)) continue;
187
+ if (candidate === width) return width;
188
+ if (candidate > width && (best === null || candidate < best)) best = candidate;
189
+ }
190
+ return best;
191
+ }
176
192
  /**
177
193
  * Whether a versioned request may be answered with
178
194
  * {@link MEDIA_CDN_VERSIONED_CACHE_CONTROL} (AGL-3485).
@@ -617,23 +633,6 @@ const lockPending = new Map();
617
633
  contentType
618
634
  };
619
635
  }
620
- /**
621
- * Runs `task` once the response has been sent, through Next's `after()`.
622
- * Required when first asked for rather than imported, for the reason
623
- * `capture-email-check.ts` gives; false where there is no request to run
624
- * after — a spec, a script — and the task is then not run at all.
625
- */ function scheduleAfterResponse(task) {
626
- try {
627
- const loaded = require('next/server');
628
- if (typeof (loaded == null ? void 0 : loaded.after) !== 'function') return false;
629
- loaded.after(()=>task().catch((error)=>{
630
- console.error('[media-cdn] after-response task failed', error);
631
- }));
632
- return true;
633
- } catch (unused) {
634
- return false;
635
- }
636
- }
637
636
  /**
638
637
  * Whether `?poster=1` was asked for (AGL-2743).
639
638
  *
@@ -1180,9 +1179,10 @@ export function parseMediaCdnRange(header, size) {
1180
1179
  *
1181
1180
  * ## Falling back to the master is right for a WIDTH and wrong for a POSTER
1182
1181
  *
1183
- * `?w=` on an asset with no such variant serves the original, and that has
1184
- * always been safe because both answers are the same KIND of thing: an
1185
- * image, larger than asked for. A rendition inherits it for the same
1182
+ * `?w=` on an asset with no such variant serves the next larger variant,
1183
+ * or the display copy or original when none is larger, and that is safe
1184
+ * because every answer is the same KIND of thing: an image, larger than
1185
+ * asked for. A rendition inherits it for the same
1186
1186
  * reason — an unknown `?r=` serves the master, which is the same video in
1187
1187
  * more bytes.
1188
1188
  *
@@ -1237,7 +1237,18 @@ export function parseMediaCdnRange(header, size) {
1237
1237
  // handed out without it would be reused across representations.
1238
1238
  res.setHeader('Vary', 'Accept');
1239
1239
  }
1240
- const useVariant = !usePoster && !rendition && Boolean(width) && variants.includes(width);
1240
+ /*
1241
+ * The variant that answers `?w=`: the width itself, or the smallest one
1242
+ * above it the asset has (AGL-3486). An asset made by an older encoder
1243
+ * lacks steps the page now asks for — generation 1 had 320/640/1280/1920
1244
+ * and srcsets ask for 768 — and answering those from the original sent
1245
+ * every phone the full-size JPEG. The next width up is the same picture,
1246
+ * a little larger than asked for, as a WebP. Only an exact width is a
1247
+ * `variantServed` for the year-long policy: a stand-in is replaced by the
1248
+ * real width once the asset is regenerated.
1249
+ */ const variantWidth = !usePoster && !rendition && width > 0 ? mediaCdnVariantFor(variants, width) : null;
1250
+ const useVariant = variantWidth !== null;
1251
+ const exactVariant = variantWidth === width;
1241
1252
  // Read only here, past every gate — a refusal above returns before the
1242
1253
  // parameter is ever looked at, so `?download=1` can never be the reason
1243
1254
  // a response happens (AGL-1411).
@@ -1272,7 +1283,7 @@ export function parseMediaCdnRange(header, size) {
1272
1283
  * revalidates. Between an image variant and its original that is a wrong
1273
1284
  * size; between a poster and its video it is a 40 KB still standing in
1274
1285
  * for a 40 MB film, or the reverse.
1275
- */ const representation = usePoster ? `-poster${usePosterVariant ? `-w${width}` : ''}` : rendition ? `-r${rendition.key}` : useVariant ? `-w${width}${encoderTag}` : useDisplay ? `-d${encoderTag}` : '';
1286
+ */ const representation = usePoster ? `-poster${usePosterVariant ? `-w${width}` : ''}` : rendition ? `-r${rendition.key}` : useVariant ? `-w${variantWidth}${encoderTag}` : useDisplay ? `-d${encoderTag}` : '';
1276
1287
  // Stable URL: revalidate against an ETag so a replaced asset is picked
1277
1288
  // up (a conditional GET returns 304 while the content is unchanged).
1278
1289
  //
@@ -1298,7 +1309,7 @@ export function parseMediaCdnRange(header, size) {
1298
1309
  token: parseMediaCdnVersionToken(req.query[MEDIA_CDN_VERSION_PARAM]),
1299
1310
  currentHash,
1300
1311
  widthRequested: Boolean(width),
1301
- variantServed: useVariant,
1312
+ variantServed: exactVariant,
1302
1313
  otherRepresentation: usePoster || Boolean(rendition),
1303
1314
  documentEncoderVersion: snapshot.get(MEDIA_VARIANT_ENCODER_VERSION_FIELD)
1304
1315
  });
@@ -1443,7 +1454,7 @@ export function parseMediaCdnRange(header, size) {
1443
1454
  [MEDIA_VARIANT_ENCODER_VERSION_FIELD]: snapshot.get(MEDIA_VARIANT_ENCODER_VERSION_FIELD),
1444
1455
  variantRegeneration: snapshot.get('variantRegeneration')
1445
1456
  })) {
1446
- scheduleAfterResponse(async ()=>{
1457
+ await scheduleAfterResponse(async ()=>{
1447
1458
  const regenerationPath = mediaStoragePathInScope({
1448
1459
  storagePath: snapshot.get('storagePath'),
1449
1460
  base: `${isOrg ? 'orgs' : 'hosts'}/${scopeId}`,
@@ -1458,7 +1469,7 @@ export function parseMediaCdnRange(header, size) {
1458
1469
  if (outcome === 'failed') {
1459
1470
  console.error('[media-cdn] delivery copies could not be regenerated', scopeSegment, mediaId);
1460
1471
  }
1461
- });
1472
+ }, '[media-cdn]');
1462
1473
  }
1463
1474
  if (!hashed) {
1464
1475
  setCacheControl(stableCacheControlFor(docServedType));
@@ -1497,7 +1508,7 @@ export function parseMediaCdnRange(header, size) {
1497
1508
  // and `parseMediaRendition` refuses a key or extension that is not
1498
1509
  // `[a-z0-9-]`. So no representation can address an object the master
1499
1510
  // could not.
1500
- const objectPath = usePoster ? usePosterVariant ? `${basePath}${MEDIA_POSTER_OBJECT_SUFFIX}__w${width}.webp` : mediaPosterObjectPath(basePath) : rendition ? mediaRenditionObjectPath(basePath, rendition) : useVariant ? `${basePath}__w${width}.webp` : useDisplay ? mediaDisplayObjectPath(basePath) : basePath;
1511
+ const objectPath = usePoster ? usePosterVariant ? `${basePath}${MEDIA_POSTER_OBJECT_SUFFIX}__w${width}.webp` : mediaPosterObjectPath(basePath) : rendition ? mediaRenditionObjectPath(basePath, rendition) : useVariant ? `${basePath}__w${variantWidth}.webp` : useDisplay ? mediaDisplayObjectPath(basePath) : basePath;
1501
1512
  let file = bucket.file(objectPath);
1502
1513
  // The caller's count (AGL-2812), asked here and nowhere earlier: past every
1503
1514
  // gate and the 304 exit, where the Storage reads begin. It starts before the