#!/usr/bin/env node 'use strict'; // ============================================================================ // REPRODUCE-BLOCK — does a bound script's embedded OUTPUT block reproduce HERE, // and if it does not, is the HOST LOCALE the reason? // ============================================================================ // `research/qc/embed.js --check` re-runs the recorded invocation and compares the // normalised capture with the tail's `out-sha256`. On a script whose numbers are // formatted with a bare `toLocaleString()`, that comparison is a function of the // host locale: v24 on a French machine prints U+202F ("510 510") where the block // pasted from a comma-grouping host says "510,510", and the gate reports // out-sha256 DIFFERS — this run does not reproduce the pasted block // on a file nobody touched. This runs the SAME normalisation twice — once plain, // once with grouping pinned to en-US through a preload shim — so the two possible // causes can be told apart: a locale difference moves the plain verdict only, a // real content difference moves both. // // node reproduce-block.js --repo [more.js ...] // // Exit 0 if every script reproduces under at least one of the two runs. // ============================================================================ const fs = require('fs'); const os = require('os'); const path = require('path'); const cp = require('child_process'); const STDERR_MARK = '\u2500\u2500\u2500\u2500\u2500 stderr \u2500\u2500\u2500\u2500\u2500'; // embed.js:270 const args = process.argv.slice(2); const repoAt = args.indexOf('--repo'); if (repoAt === -1 || !args[repoAt + 1]) { console.error('usage: node reproduce-block.js --repo [more.js ...]'); process.exit(2); } const REPO = path.resolve(args[repoAt + 1]); const targets = args.filter((a, i) => i !== repoAt + 1 && a !== '--repo'); if (!targets.length) { console.error('usage: node reproduce-block.js --repo [more.js ...]'); process.exit(2); } const T = require(path.join(REPO, 'research', 'qc', 'tailfmt')); // The shim is a HARNESS variation, not a source change: it exists so the two causes // of a DIFFERS can be separated, and it is written to a temp file, never to the tree. const SHIM = path.join(os.tmpdir(), 'pin-grouping-en-us.js'); fs.writeFileSync(SHIM, [ "'use strict';", "// Pin the default number grouping to en-US so a run is not a function of the host locale.", "const orig = Number.prototype.toLocaleString;", "Number.prototype.toLocaleString = function (locales, options) {", " return orig.call(this, 'en-US', options);", "};", '', ].join('\n')); const capture = (mode, stdout, stderr) => (mode === 'stdout+stderr' && String(stderr).trim()) ? String(stdout).replace(/\n*$/, '\n') + STDERR_MARK + '\n' + stderr : stdout; let bad = 0; for (const t of targets) { const abs = path.isAbsolute(t) ? t : path.resolve(REPO, t); const src = fs.readFileSync(abs, 'utf8'); const fp = T.fingerprint(src); const rel = path.relative(REPO, abs).split(path.sep).join('/'); console.log(rel); console.log(` host locale ${Intl.DateTimeFormat().resolvedOptions().locale} ` + `grouping of 510510: ${JSON.stringify((510510).toLocaleString())}`); if (!fp) { console.log(' LEGACY tail — no fingerprint, nothing to reproduce'); bad++; continue; } const mode = fp.streams || 'stdout'; console.log(` out-sha256 ${fp['out-sha256']}`); let anyOk = false; for (const [label, nodeFlags] of [['plain', []], ['grouping pinned to en-US', ['-r', SHIM]]]) { const r = cp.spawnSync(process.execPath, [...nodeFlags, rel], { cwd: REPO, encoding: 'utf8', maxBuffer: 512 * 1024 * 1024, }); if (r.status !== 0) { console.log(` ${label.padEnd(24)} FAILED (exit ${r.status})`); continue; } const sha = T.sha(T.normalize(capture(mode, r.stdout, r.stderr))); const ok = sha === fp['out-sha256']; anyOk = anyOk || ok; console.log(` ${label.padEnd(24)} ${sha} ${ok ? 'MATCHES the pasted block' : 'does not reproduce'}`); } if (!anyOk) bad++; } process.exit(bad ? 1 : 0);