|
| 1 | +// Negative-control matrix for the /export html document build (PR #12191). |
| 2 | +// Portable (Windows/macOS/Linux): mutations are exact String.replace calls that |
| 3 | +// throw when the target text is missing, so a silent no-op mutation is impossible. |
| 4 | +// Usage: node doc-matrix.mjs <repoRoot> <outDir> |
| 5 | +import { spawnSync } from 'node:child_process'; |
| 6 | +import { createHash } from 'node:crypto'; |
| 7 | +import { |
| 8 | + copyFileSync, |
| 9 | + existsSync, |
| 10 | + mkdirSync, |
| 11 | + readFileSync, |
| 12 | + rmSync, |
| 13 | + writeFileSync, |
| 14 | +} from 'node:fs'; |
| 15 | +import { createRequire } from 'node:module'; |
| 16 | +import { dirname, join, resolve } from 'node:path'; |
| 17 | + |
| 18 | +const root = resolve(process.argv[2] ?? '.'); |
| 19 | +const out = resolve(process.argv[3] ?? 'probe-out'); |
| 20 | +mkdirSync(out, { recursive: true }); |
| 21 | +const wt = join(root, 'packages/web-templates'); |
| 22 | +const B = join(wt, 'src/export-html/build.mjs'); |
| 23 | +const ws = join(root, 'packages/web-shell'); |
| 24 | +const T = join(ws, 'dist/transcript.js'); |
| 25 | +const PKG = join(ws, 'package.json'); |
| 26 | +const CAND_WARN = process.env.CAND_WARN ?? '1_880_000'; |
| 27 | +const CAND_MAX = process.env.CAND_MAX ?? '1_930_000'; |
| 28 | + |
| 29 | +const sha = (p) => createHash('sha256').update(readFileSync(p)).digest('hex'); |
| 30 | +const pristine = { |
| 31 | + build: readFileSync(B, 'utf8'), |
| 32 | + transcript: readFileSync(T), |
| 33 | + pkg: readFileSync(PKG, 'utf8'), |
| 34 | +}; |
| 35 | +const pristineSha = { build: sha(B), transcript: sha(T), pkg: sha(PKG) }; |
| 36 | + |
| 37 | +const MUTATIONS = { |
| 38 | + 'no-engagement': [ |
| 39 | + ['if (!hasDocumentMcpAppBridgeStubInput(', 'if (false && !hasDocumentMcpAppBridgeStubInput('], |
| 40 | + ], |
| 41 | + 'no-stub': [ |
| 42 | + ['{ filter: /^@modelcontextprotocol\\/ext-apps\\/app-bridge$/ }', '{ filter: /^__never__$/ }'], |
| 43 | + ], |
| 44 | + 'no-forbidden-mcp': [ |
| 45 | + [ |
| 46 | + 'pattern: /(^|\\/)node_modules\\/@modelcontextprotocol\\/(ext-apps|sdk)\\//,', |
| 47 | + 'pattern: /__never__/,', |
| 48 | + ], |
| 49 | + ], |
| 50 | + 'tight-caps': [ |
| 51 | + ['const DOCUMENT_RUNTIME_WARNING_BYTES = 1_970_000;', `const DOCUMENT_RUNTIME_WARNING_BYTES = ${CAND_WARN};`], |
| 52 | + ['const MAX_DOCUMENT_RUNTIME_BYTES = 2_030_000;', `const MAX_DOCUMENT_RUNTIME_BYTES = ${CAND_MAX};`], |
| 53 | + ], |
| 54 | +}; |
| 55 | + |
| 56 | +function applyMutations(src, names) { |
| 57 | + for (const name of names) { |
| 58 | + for (const [from, to] of MUTATIONS[name]) { |
| 59 | + const count = src.split(from).length - 1; |
| 60 | + if (count !== 1) throw new Error(`mutation ${name}: expected 1 match, got ${count} for ${from}`); |
| 61 | + src = src.replace(from, to); |
| 62 | + } |
| 63 | + } |
| 64 | + return src; |
| 65 | +} |
| 66 | + |
| 67 | +// 1. A transcript entry with the MCP bridge pre-bundled: drop ext-apps from the |
| 68 | +// manifest (externals derive from it) and rebuild only the transcript mode. |
| 69 | +const req = createRequire(PKG); |
| 70 | +const viteBin = join(dirname(req.resolve('vite/package.json')), 'bin/vite.js'); |
| 71 | +const preDir = join(out, 'prebundled-transcript'); |
| 72 | +rmSync(preDir, { recursive: true, force: true }); |
| 73 | +const pkg = JSON.parse(pristine.pkg); |
| 74 | +delete pkg.dependencies['@modelcontextprotocol/ext-apps']; |
| 75 | +writeFileSync(PKG, JSON.stringify(pkg, null, 2) + '\n'); |
| 76 | +const vb = spawnSync( |
| 77 | + process.execPath, |
| 78 | + [viteBin, 'build', '--config', 'vite.lib.config.ts', '--mode', 'transcript', '--outDir', preDir, '--emptyOutDir'], |
| 79 | + { cwd: ws, encoding: 'utf8' }, |
| 80 | +); |
| 81 | +writeFileSync(PKG, pristine.pkg); |
| 82 | +writeFileSync(join(out, 'prebundle-vite.log'), `${vb.stdout}\n${vb.stderr}`); |
| 83 | +const preT = join(preDir, 'transcript.js'); |
| 84 | +if (vb.status !== 0 || !existsSync(preT)) throw new Error(`prebundle build failed: ${vb.status}`); |
| 85 | +const preText = readFileSync(preT, 'utf8'); |
| 86 | +const healthyText = pristine.transcript.toString('utf8'); |
| 87 | +const bridgeImport = /from\s*["']@modelcontextprotocol\/ext-apps\/app-bridge["']/; |
| 88 | +const prebundleInfo = { |
| 89 | + healthyImportsBridge: bridgeImport.test(healthyText), |
| 90 | + prebundledImportsBridge: bridgeImport.test(preText), |
| 91 | + healthyBytes: Buffer.byteLength(healthyText), |
| 92 | + prebundledBytes: Buffer.byteLength(preText), |
| 93 | +}; |
| 94 | +console.log('PROBE_JSON ' + JSON.stringify({ kind: 'prebundle', ...prebundleInfo })); |
| 95 | +if (prebundleInfo.prebundledImportsBridge || !prebundleInfo.healthyImportsBridge) { |
| 96 | + throw new Error('prebundle control is not a pre-inlined bridge'); |
| 97 | +} |
| 98 | + |
| 99 | +// 2. The matrix. |
| 100 | +const CASES = [ |
| 101 | + ['C0-intact', 'healthy', []], |
| 102 | + ['C0t-intact-candidate-caps', 'healthy', ['tight-caps']], |
| 103 | + ['C1-prebundled', 'prebundled', []], |
| 104 | + ['C2-prebundled-no-engagement', 'prebundled', ['no-engagement']], |
| 105 | + ['C2t-prebundled-no-engagement-candidate-caps', 'prebundled', ['no-engagement', 'tight-caps']], |
| 106 | + ['C3-no-stub', 'healthy', ['no-stub']], |
| 107 | + ['C4-no-stub-no-engagement', 'healthy', ['no-stub', 'no-engagement']], |
| 108 | + ['C5-no-stub-no-engagement-no-forbidden', 'healthy', ['no-stub', 'no-engagement', 'no-forbidden-mcp']], |
| 109 | +]; |
| 110 | +for (const [name, transcript, muts] of CASES) { |
| 111 | + writeFileSync(B, applyMutations(pristine.build, muts)); |
| 112 | + if (transcript === 'healthy') writeFileSync(T, pristine.transcript); |
| 113 | + else copyFileSync(preT, T); |
| 114 | + const metafile = join(out, `${name}.metafile.json`); |
| 115 | + const r = spawnSync(process.execPath, ['src/export-html/build.mjs'], { |
| 116 | + cwd: wt, |
| 117 | + encoding: 'utf8', |
| 118 | + env: { ...process.env, EXPORT_HTML_METAFILE: metafile }, |
| 119 | + }); |
| 120 | + const log = `${r.stdout}\n${r.stderr}`; |
| 121 | + writeFileSync(join(out, `${name}.log`), log); |
| 122 | + const bytes = log.match(/renderer JS is (\d+) bytes/)?.[1] ?? null; |
| 123 | + const err = log.match(/Error: ([^\n]{0,160})/)?.[1] ?? null; |
| 124 | + const forbidden = log.match(/reached (\d+) forbidden input/)?.[1] ?? null; |
| 125 | + const warned = /exceeds the \d+-byte warning threshold/.test(log); |
| 126 | + let meta = null; |
| 127 | + if (existsSync(metafile)) { |
| 128 | + const keys = Object.keys(JSON.parse(readFileSync(metafile, 'utf8')).inputs); |
| 129 | + meta = { |
| 130 | + inputs: keys.length, |
| 131 | + backslashKeys: keys.filter((k) => k.includes('\\')).length, |
| 132 | + stubKey: keys.find((k) => k.includes('document-mcp-app-bridge-stub')) ?? null, |
| 133 | + mcpKeys: keys.filter((k) => k.includes('@modelcontextprotocol')).length, |
| 134 | + sampleNodeModulesKey: keys.find((k) => k.includes('node_modules')) ?? null, |
| 135 | + }; |
| 136 | + rmSync(metafile); |
| 137 | + } |
| 138 | + console.log( |
| 139 | + 'PROBE_JSON ' + |
| 140 | + JSON.stringify({ kind: 'case', name, transcript, muts, rc: r.status, bytes, warned, forbidden, err, meta }), |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +// 3. Restore and rebuild the healthy document; prove the tree is byte-exact. |
| 145 | +writeFileSync(B, pristine.build); |
| 146 | +writeFileSync(T, pristine.transcript); |
| 147 | +const rebuild = spawnSync(process.execPath, ['src/export-html/build.mjs'], { cwd: wt, encoding: 'utf8' }); |
| 148 | +const status = spawnSync('git', ['status', '--porcelain'], { cwd: root, encoding: 'utf8' }).stdout.trim(); |
| 149 | +const restored = { |
| 150 | + kind: 'restore', |
| 151 | + rebuildRc: rebuild.status, |
| 152 | + build: sha(B) === pristineSha.build, |
| 153 | + transcript: sha(T) === pristineSha.transcript, |
| 154 | + pkg: sha(PKG) === pristineSha.pkg, |
| 155 | + gitStatus: status, |
| 156 | +}; |
| 157 | +console.log('PROBE_JSON ' + JSON.stringify(restored)); |
| 158 | +if (!restored.build || !restored.transcript || !restored.pkg || rebuild.status !== 0) process.exit(1); |
0 commit comments