kevin-wiki / ui/scripts
Purpose
Build- and check-time scripts for the viewer: bundle the wiki/ markdown into a
form the Next app serves, and enforce the wiki's quality invariants (inline
citations, gate passwords) so broken content can't ship.
Mental model & key files
bundle-wiki.mjs— packages wiki content plus referenced public image/video assets for the app to render and emits the public-only static search index; private-page-only data and assets stay out of public deployment artifacts.export-okf.mjs+lib/okf-export.mjs— deterministically project the same public corpus into an OKF v0.2 Markdown tree served at/api/okf.enforce-citations.ts+lib/citation-scan.ts— fail the build when facts lack[Source: ...]citations;test-citations.tscovers the scanner.test-gate-passwords.ts— validates gated-content password config.test-privacy-gating.ts— locks regression cases for people, career, finance, Dedalus, generated packs, the mail and cloud tool pages, operational logs, and the public stable objects on/system.test-public-search-index.ts— proves the CDN search artifact includes every public entry, excludes every private entry, and contains no privacy metadata.test-public-graph.ts— proves every number in the anonymous graph view matches the links it ships, and that no private slug or title is in it.test-public-counts.ts— proves home's page, category and tag counts equal what the sidebar shows an anonymous viewer.test-public-build-output.ts+lib/privacy-scan.ts— scans the build output, or a running server with--base, for private slugs anywhere and private titles where the site prints a title, in seven encodings, and checks that a private slug answers exactly like a missing one. With--baseit also scans the headers of every crawled response and of each redirect followed to it, name and value (a name also from each of its--joined words on), for slugs and for titles anywhere, as undici gives them and decoded as UTF-8, with eachLinkandLocationURL resolved against the URL that answered; a finding names a header only from a fixed list (NAMED_HEADERS). It holds every document, from its bytes andContent-Type, and the twin and the 404s for unlocked viewers, to the font rules inlib/font-heads.ts. The document test errs red (lib/served-page.ts): on a navigation an answer is a document unless each value of itsContent-Type, split and parsed as WHATWG does, is a type no browser renders as one (text, Markdown, JSON, CSS, JavaScript, a font, an image, a download, an event stream or an RSC payload); an XML type counts when its body names the XHTML namespace. An RSC answer is one only in an HTML type. The same test decides which URLs the crawl fetches again as RSC, and a page that answers 200 in a type that renders none is a finding: a sitemap URL but its files (SITEMAP_FILES), a fixed page (CRAWL_PAGES), or a public README or_indexpage. The unlocked views need the server's key: run it with the server'sWIKI_GATE_SECRET, or pass--skip-unlocked-viewsto view those pages anonymously alone (a crawl of production); without either it exits 1. Each view must give its answer, by status and by whether the page carries the 404 body, found by NotFoundBody's heading (NOT_FOUND_SENTENCEinsrc/lib/constants.ts, as rendered HTML, since every route's RSC payload carries the 404 as data). An anonymous viewer gets the 404 for a missing page, pack or path and for the private page. An unlocked viewer gets the private page they unlocked, without the 404 body, and the 404 body at 200 from the twin for a missing page; for a missing pack too, but a viewer with one page unlocked gets the password gate for every pack name. A missing path is a 404 to every viewer. For the viewer with one page unlocked, a pack and a made-up pack name of its length (both the password gate), and a private page not unlocked and a made-up page in its folder and of its length (both the 404 body), must agree in the redirects followed to them (each status, and eachLocationnormalized for its path with its_rscvalue masked), status, headers and canonical body for GET, RSC and HEAD, as an anonymous viewer's pairs must; the normalizer reads Next's segment lists under/wiki/and/packs/, the twin's/unlockedincluded. Without a local build (.next/server) it says so and holds crawled pages to every font rule but the 400 and 600 placement, which needs the build's routes. It sends a crawled URL as RSC only when it answers a document (Vercel answers an RSC request to a file with its 404 page), compares an HTML answer to an RSC request as HTML, and treats Vercel'sx-vercel-id,x-vercel-cacheandageas headers that change per request. At most 4 requests are in flight, redirects included (lib/held-fetch.ts): the crawl follows each hop itself, reads its body before the next opens, and drops the cookie and authorization headers on a hop to another origin. Each hop times out after 30 seconds, and a network error or timeout is retried twice.pnpm test:script-libs(prebuild) runslib/*.test.ts:held-fetch.test.tschecks the hops against two stub servers, andserved-page.test.tsthe document test against a stub site whose pages differ only inContent-Type, and for XML in body.test-font-heads.ts—pnpm test:font-heads(prebuild) runs the font rules' reader on hand-written documents and prints, per case, the verdict the check must give (red when any engine may fetch a font against the rules; "false red" and "React's shape" mark the red cases no engine misfetches) and the reader's: quoting, case, character references, NUL, comments, raw text,<noscript>,<template>,<svg>and<math>, the head's end, the error page's id, Link headers, byte order marks and labels, origins andcrossorigin. It then reads malformed megabyte pages and Link headers with runs of 100,000 spaces or tabs within a time bound, and checks that a finding never prints an href or a tag name outside its fixed list.test-font-output.ts+lib/font-heads.ts+lib/woff2-cmap.ts— postbuild check of the site fonts: file hashes, Geist Mono's cmap, list and range, no font hint in any RSC payload, the font rules on every prerendered page from its bytes, and no PP Mori core file in a client chunk (ui/SKILL.md, Fonts).test-case-paths.ts— names public wiki paths that differ only in case before the build; a warning on a case-insensitive disk, a failure undervercel build(VERCEL=1), which would otherwise stop with EEXIST.measure-diagrams.ts—pnpm measure:diagramsdraws each public page's Mermaid diagrams headless (agent-browser) for both themes intosrc/generated/diagrams/<key>.light.svgand.dark.svg, whichmarkdown.tsinlines, and their sizes intosrc/lib/diagram-sizes.json; rerun after a public diagram changes.measure-tweets.ts—pnpm measure:tweets --base <url> [--missing]measures each public tweet's opened card on a running site at a 1440px, 430px and 360px screen (headless agent-browser) intosrc/lib/tweet-sizes.json.check-embed-sizes.ts+lib/public-embeds.ts—pnpm check:embed-sizes(prebuild) warns about a public diagram with no drawing or size or a public tweet with no size (--failexits 1), and fails on a size key or drawing no public page uses.test-content-css.ts— postbuild: every class in a prerendered page's rendered Markdown (code and inline SVG aside) has a rule in the emitted CSS; a class only a page's raw HTML uses goes in globals.css's@source inlinelist (--listprints that set).test-readme-title.ts— a folder page prints exactly one h1, whether or not its README opens with a comment.check-ui-tokens.mjs— reports type and tracking classes outside the token scale in.tsxundersrc;--failexits 1 on any.test-vercel-upload.mjs+lib/trace-reads.mjs— runs the build scripts that read outsideui/, records every file they open, and fails when.vercelignorewould leave one out of the upload. The build scripts write into its scratch folder (bundle-wiki.mjs --out), so it never writesui/, and a run stopped by a signal stops its script and removes the folder.
Patterns to follow / invariants
- ESM;
.mjsfor plain Node scripts,.tsrun via tsx. - Keep scan/validation logic in
lib/(pure) and the entrypoints thin + testable. - A failing citation/gate check should block the build — these are guardrails.
Common tasks → first action
- Re-bundle after wiki edits → run
bundle-wiki.mjs, thenpnpm build:public-links: the bundle deletes.content, and every anonymous article reads.content/public-links.jsonand.content/public-mentions.json. - Add a content guardrail → put the predicate in
lib/, call it from anenforce-*.tsentry, add atest-*.ts. - Verify the portable agent bundle →
pnpm test:okfandnode scripts/export-okf.mjs --check.
Gotchas
- These run in the
ui/pnpm workspace, not the repo-root tsx environment. - Citation enforcement mirrors
wiki/conventions — keep it in sync withconventions-qualityif the citation format changes.