Files
faunosteo/scripts/browser/compare-imports.mjs
T
bchanot c84186b032 test(tests): R4.2e browser measurements of the Rust import (module worker page, same perimeter as the TS baseline)
r4-import.mjs runs the 226 baseline files and the hostile classes through a disposable
module worker calling SheetHandle.import_pdf in Chromium 156, Firefox 157 and WebKit 27.2
under the RSS watchdog (not a guaranteed limit); compare-imports.mjs prints TS vs Rust;
serve.mjs gains opt-in mounts (/r4/, /pkg/). Results 2026-10-18-r4-import.json: 666/678
agreements, the 12 others are the 4 declared files; W4 met (max 68 MiB linear); the
1 GiB ceiling traps in every engine.
2026-10-18 16:58:42 +02:00

314 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// TypeScript import baseline vs Rust import (R4.2e): markdown tables from
// a results file of import-baseline.mjs / ts-baseline.mjs and one of
// r4-import.mjs, on the same perimeter (same files, groups, engines, RSS
// metrics). Per engine and group: median durations, max summed and content
// RSS growth, the per-file outcome agreement (TS `editor` ↔ Rust `ok`, a
// TS refusal ↔ a Rust refusal or worker failure); every disagreeing file is
// listed with both outcomes, the declared differences (plan §A.3, OD-3,
// OD-4) apart, and any other one flagged loudly. A finer check compares
// the message each side would show (TS alert text ↔ Rust kind through the
// plan §B.1.8 table).
//
// node scripts/browser/compare-imports.mjs <ts.json> <r4-import.json>
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { REPO_ROOT } from './lib/paths.mjs'
import { median } from './lib/stats.mjs'
const MiB = 1024 * 1024
const FIXTURES = 'apps/app/tests/fixtures/'
const FR_DICT = join(REPO_ROOT, 'apps/app/src/i18n/fr.ts')
/** Plan §A.3: the only accepted outcome differences, by file slug. */
const DECLARED = {
'values-1m': 'OD-3 value cap: a token-dense attachment is refused',
'surrogate-content-overwritten':
'OD-4: overwritten duplicate with a lone surrogate → lone_surrogate',
'surrogate-version-overwritten':
'OD-4: overwritten duplicate with a lone surrogate → lone_surrogate',
'two-files-decoy-first':
'OD-4: overwritten duplicate with a lone surrogate → lone_surrogate',
}
/** Plan §B.1.8: the message key each Rust outcome shows, per route. */
const PDF_KEYS = {
invalid_pdf: 'error_invalid_pdf',
file_too_large: 'error_invalid_pdf',
no_attachment: 'error_no_attachment',
attachment_stripped: 'error_attachment_stripped',
incompatible_version: 'error_incompatible_version',
lone_surrogate: 'error_lone_surrogate',
invalid: 'error_generic',
internal: 'error_core_unavailable',
core_unavailable: 'error_core_unavailable',
}
const COPY_REFUSALS = [
'file_too_large',
'invalid_utf8',
'invalid',
'unsupported_version',
'lone_surrogate',
]
const mb = (bytes) => (bytes === null ? '–' : (bytes / MiB).toFixed(1))
const ms = (value) => (value === null ? '–' : value.toFixed(0))
const shortFile = (file) => file.replace(FIXTURES, '')
const slugOf = (file) =>
file
.split('/')
.pop()
.replace(/\.(pdf|bin|gen\.json)$/, '')
function table(header, rows) {
const line = (cells) => `| ${cells.join(' | ')} |`
const rule = line(header.map(() => '---'))
return [line(header), rule, ...rows.map(line)].join('\n')
}
/** French message text → its `home.import.*` key ({version} as a wildcard). */
async function messageKeys() {
const { fr } = await import(pathToFileURL(FR_DICT).href)
const entries = Object.entries(fr).filter(([k]) =>
k.startsWith('home.import.error_'),
)
return entries.map(([key, text]) => {
const parts = text.split('{version}').map(RegExp.escape)
return {
key: key.slice('home.import.'.length),
re: new RegExp(`^${parts.join('.*')}$`),
}
})
}
function tsVerdict(run, keys) {
if (run.outcome === 'editor') return { verdict: 'accepted', label: 'editor' }
if (run.outcome === 'error' && run.errorText) {
const key = keys.find((k) => k.re.test(run.errorText))?.key ?? '?'
return { verdict: 'refused', label: `error ${key}`, key }
}
return { verdict: `harness ${run.outcome}`, label: run.outcome }
}
function rustKey(run) {
if (run.route === 'copy') {
if (run.outcome === 'refused' && COPY_REFUSALS.includes(run.resultKind)) {
return 'error_invalid_copy'
}
return 'error_core_unavailable'
}
if (run.outcome === 'failed') return 'error_generic'
return PDF_KEYS[run.resultKind] ?? `?${run.resultKind}`
}
function rustVerdict(run) {
if (run.outcome === 'ok') return { verdict: 'accepted', label: 'ok' }
if (run.outcome === 'refused' || run.outcome === 'failed') {
const what = run.outcome === 'failed' ? `failed ${run.phase}` : 'refused'
const label = `${what} ${run.resultKind ?? run.detail ?? ''}`.trim()
return { verdict: 'refused', label, key: rustKey(run) }
}
return { verdict: `harness ${run.outcome}`, label: run.outcome }
}
/** One row per (engine, file) present on either side. */
function pairs(tsRuns, rustRuns, keys) {
const index = (runs) => new Map(runs.map((r) => [`${r.engine} ${r.file}`, r]))
const ts = index(tsRuns)
const rust = index(rustRuns)
const all = new Set([...ts.keys(), ...rust.keys()])
return [...all].map((id) => {
const t = ts.get(id)
const r = rust.get(id)
const run = t ?? r
return {
engine: run.engine,
group: run.group,
file: run.file,
ts: t ?? null,
rust: r ?? null,
tsV: t ? tsVerdict(t, keys) : null,
rustV: r ? rustVerdict(r) : null,
}
})
}
const agrees = (p) => p.tsV && p.rustV && p.tsV.verdict === p.rustV.verdict
const growth = (r) => r.rssPeakBytes - r.rssBeforeBytes
const contentGrowth = (r) => r.rssPeakContentBytes - r.rssBeforeContentBytes
const maxOf = (list) => (list.length ? Math.max(...list) : null)
const finite = (list) => list.filter(Number.isFinite)
function groupRow([key, list]) {
const [engine, group] = key.split(' ')
const ts = list.map((p) => p.ts).filter(Boolean)
const rust = list.map((p) => p.rust).filter(Boolean)
const agreeing = list.filter(agrees).length
return [
engine,
group,
String(list.length),
String(agreeing),
String(list.length - agreeing),
ms(median(finite(ts.map((r) => r.durationMs)))),
ms(median(finite(rust.map((r) => r.durationMs)))),
ms(median(finite(rust.map((r) => r.pageMs)))),
mb(maxOf(finite(ts.map(growth)))),
mb(maxOf(finite(rust.map(growth)))),
mb(maxOf(finite(ts.map(contentGrowth)))),
mb(maxOf(finite(rust.map(contentGrowth)))),
]
}
function groupTable(rows) {
const groups = new Map()
for (const p of rows) {
const key = `${p.engine} ${p.group}`
groups.set(key, [...(groups.get(key) ?? []), p])
}
const header = [
'Engine',
'Group',
'Files',
'Agree',
'Disagree',
'TS median ms',
'Rust median ms',
'Rust median page ms',
'TS max RSS growth MiB',
'Rust max RSS growth MiB',
'TS max content growth MiB',
'Rust max content growth MiB',
]
return table(header, [...groups].map(groupRow))
}
/** Disagreements grouped by file and outcome pair, engines listed. */
function disagreementLines(rows) {
const byFile = new Map()
for (const p of rows.filter((row) => !agrees(row))) {
const tsLabel = p.tsV?.label ?? 'not run'
const what = `TS ${tsLabel} / Rust ${p.rustV?.label ?? 'not run'}`
const key = `${p.file}\u0000${what}`
byFile.set(key, [...(byFile.get(key) ?? []), p.engine])
}
return [...byFile].map(([key, engines]) => {
const [file, what] = key.split('\u0000')
const declared = DECLARED[slugOf(file)]
const tag = declared ? `declared (${declared})` : 'NOT DECLARED'
return {
file,
line: `- ${shortFile(file)} [${engines.join(', ')}]: ${what}: ${tag}`,
declared,
}
})
}
/** Every declared difference must show up, in every engine. */
function declaredCheck(rows, engines) {
return Object.keys(DECLARED).map((slug) => {
const cells = engines.map((engine) => {
const p = rows.find((r) => r.engine === engine && slugOf(r.file) === slug)
if (!p) return `${engine}: missing`
if (agrees(p)) return `${engine}: AGREES (expected a difference)`
const tsLabel = p.tsV?.label ?? 'not run'
return `${engine}: TS ${tsLabel} / Rust ${p.rustV?.label ?? 'not run'}`
})
return `- ${slug}: ${cells.join('; ')}`
})
}
/** Both sides refuse, but would not show the same message. */
function messageLines(rows) {
const differ = rows.filter(
(p) =>
agrees(p) && p.tsV.verdict === 'refused' && p.tsV.key !== p.rustV.key,
)
const byFile = new Map()
for (const p of differ) {
const what = `TS ${p.tsV.key} / Rust ${p.rustV.key} (${p.rustV.label})`
const key = `${p.file}\u0000${what}`
byFile.set(key, [...(byFile.get(key) ?? []), p.engine])
}
return [...byFile].map(([key, engines]) => {
const [file, what] = key.split('\u0000')
return `- ${shortFile(file)} [${engines.join(', ')}]: ${what}`
})
}
function perimeterLines(rows) {
const onlyTs = rows.filter((p) => !p.rust)
const onlyRust = rows.filter((p) => !p.ts)
const lines = []
for (const p of onlyTs)
lines.push(`- only in TS: ${p.engine} ${shortFile(p.file)}`)
for (const p of onlyRust)
lines.push(`- only in Rust: ${p.engine} ${shortFile(p.file)}`)
return lines
}
function headLines(ts, rust, rows) {
const versions = (r) =>
Object.entries(r.engines)
.map(([e, v]) => `${e} ${v}`)
.join(', ')
const agreeing = rows.filter(agrees).length
return [
`TS baseline: started ${ts.startedAt}, ${versions(ts)}, ` +
`build ${ts.build?.headCommit ?? '?'}.`,
`Rust import: started ${rust.startedAt}, ${versions(rust)}, ` +
`module sha256 ${rust.module.pkg.wasmSha256} ` +
`(HEAD ${rust.module.headCommit}).`,
`Perimeter: ${rust.perimeter}.`,
`Watchdog: ${rust.watchdog.caveat}`,
`Outcome agreement: ${agreeing} of ${rows.length} (engine, file) pairs.`,
]
}
function readResults([tsPath, rustPath]) {
if (!tsPath || !rustPath) {
throw new Error(
'usage: compare-imports.mjs <ts-baseline.json> <r4-import.json>',
)
}
const ts = JSON.parse(readFileSync(tsPath, 'utf8'))
const rust = JSON.parse(readFileSync(rustPath, 'utf8'))
return { ts, rust }
}
function comparison(ts, rust, rows) {
const engines = [...new Set(rows.map((p) => p.engine))]
const disagreements = disagreementLines(rows)
const undeclared = disagreements.filter((d) => !d.declared)
const perimeter = perimeterLines(rows)
const messages = messageLines(rows)
const parts = [
...headLines(ts, rust, rows),
'TS vs Rust by engine and group:',
groupTable(rows),
perimeter.length
? `PERIMETER DIFFERS (${perimeter.length}):\n${perimeter.join('\n')}`
: 'Perimeter: every (engine, file) pair is on both sides.',
`Disagreements (${disagreements.length}; ` +
`not declared: ${undeclared.length}):`,
disagreements.map((d) => d.line).join('\n') || 'none',
'Declared differences, per engine:',
declaredCheck(rows, engines).join('\n'),
`Both refuse, different message (${messages.length}):`,
messages.join('\n') || 'none',
]
if (undeclared.length) {
const count = `${undeclared.length} disagreement(s)`
parts.push(`WARNING: ${count} outside the declared differences.`)
}
return parts
}
async function main() {
const { ts, rust } = readResults(process.argv.slice(2))
const rows = pairs(ts.import.runs, rust.import.runs, await messageKeys())
console.log(comparison(ts, rust, rows).join('\n\n'))
}
await main()