// compare.js — job #45: out-xchan.txt (brief's 2>&1 capture) against the embedded OUTPUT block // of research/xchan-at29-01-segmented.js. Usage: node compare.js [out-xchan.txt] 'use strict'; const fs = require('fs'), crypto = require('crypto'); const T = require('./research/qc/tailfmt'); const SCRIPT = 'research/xchan-at29-01-segmented.js'; const OUT = process.argv[2] || 'out-xchan.txt'; // ---- embedded block: body = body-lines lines after the fingerprint's closing rule const src = fs.readFileSync(SCRIPT, 'utf8').split('\n'); const iBan = src.findIndex(l => l.startsWith('// OUTPUT — EMBEDDED')); const fp = {}; let j = iBan + 1; for (; !src[j].startsWith('// ====='); j++) { const m = src[j].match(/^\/\/\s+([a-z0-9-]+):\s+(.*)$/); if (m) fp[m[1]] = m[2].trim(); } const nBody = Number(fp['body-lines']); const body = src.slice(j + 1, j + 1 + nBody).map(l => l.replace(/^\/\/ ?/, '')); const bodyText = body.join('\n') + '\n'; const bodySha = T.sha(T.normalize(bodyText)); // ---- run capture: stderr progress lines are " seg N/M [t s]"; everything else is stdout const rawBuf = fs.readFileSync(OUT); const rawSha = crypto.createHash('sha256').update(rawBuf).digest('hex'); const SEG = /^ seg \d+\/\d+ \[\d+\.\ds\]$/; const lines = rawBuf.toString('utf8').split('\n'); const stderrLines = lines.filter(l => SEG.test(l)); const stdoutText = lines.filter(l => !SEG.test(l)).join('\n'); const runSha = T.sha(T.normalize(stdoutText)); console.log(`embedded out-sha256 ${fp['out-sha256']}`); console.log(`embedded body, normalised ${bodySha} ${bodySha === fp['out-sha256'] ? 'matches' : 'DIFFERS'}`); console.log(`run stdout, normalised ${runSha} ${runSha === fp['out-sha256'] ? 'matches' : 'DIFFERS'}`); console.log(`raw ${OUT} sha256 ${rawSha} (${lines.length - 1} lines, ${stderrLines.length} stderr progress lines removed for the stdout hash)`); // ---- normalised line diff const a = T.normalize(bodyText).split('\n'), b = T.normalize(stdoutText).split('\n'); let nDiff = 0; for (let i = 0; i < Math.max(a.length, b.length); i++) { if (a[i] !== b[i]) { nDiff++; if (nDiff <= 40) console.log(` line ${i + 1}\n embedded: ${a[i]}\n this run: ${b[i]}`); } } console.log(`normalised line differences: ${nDiff} (embedded ${a.length} lines, run ${b.length} lines)`); // ---- field comparison per level const PAT = { head: /===== @(\d+): W=(\d+) N̄\(formula\)=(\d+) K=(\d+) \((\d+)\.\.(\d+)\)/, work: /WORK COUNTS \(the code's own loop counts\): (\d+) slots, (\d+) scour marks, (\d+) segments/, nbar: /N̄ counted = (\d+) \(formula (\d+): (PASS|FAIL)\) max \|D_a\| = (\d+), max \|D_b\| = (\d+)/, b3: /B_3 FROM THE \(a,b\) COUNTS = (\d+) split \(3,0\)=(\d+) \(2,1\)=(\d+) \(1,2\)=(\d+) \(0,3\)=(\d+)/, ident: /INTERNAL IDENTITY sub\+sup = B_3 class by class: (\S+)/, sub: /SUB-W \(∏QW\}1\/∏Q = ([\d.]+) \(e₃ = miss \+ sub = ([\d.]+) vs esym ([\d.]+); C\(K,3\) = (\d+) vs (\d+)\)/, al: /ALIGNED obs = (\d+) \[P1 predicts exactly 0: (PASS|FAIL)\] CRT=([\d.]+)/, mx: /MIXED obs = (\d+) CRT=([\d.]+) by orientation \(2,1\) = (\d+) and \(1,2\) = (\d+)/, J: />>> JOINT DEFICIT J = (-?[\d.]+) 1−J = (-?[\d.]+) σ_J = ([\d.]+)/, slot: /σ_J SLOT-CLUSTERED \(the sample unit is the slot\) = ([\d.]+) inflation ×([\d.]+)/, gate: /GATE vs natal-cap-39 embedded OUTPUT \((\d+) integers \+ (\d+) rounded reals\): (.*)$/, }; function fields(ls) { const out = {}; let cur = null; for (const l of ls) { const h = l.match(PAT.head); if (h) { cur = h[1]; out[cur] = { head: h.slice(2) }; continue; } if (!cur || l.startsWith('===== SUMMARY')) { if (l.startsWith('===== SUMMARY')) cur = null; continue; } for (const [k, re] of Object.entries(PAT)) { if (k === 'head') continue; const m = l.match(re); if (m) out[cur][k] = m.slice(1); } } return out; } const E = fields(a), R = fields(b); let nField = 0, nMis = 0; for (const x of Object.keys(E)) { for (const k of Object.keys(PAT)) { const e = E[x][k], r = R[x] && R[x][k]; if (!e && !r) continue; const ej = JSON.stringify(e), rj = JSON.stringify(r); nField += e ? e.length : 0; if (ej !== rj) { nMis++; console.log(` FIELD MISMATCH @${x} ${k}: embedded ${ej} run ${rj}`); } } } console.log(`field groups compared at levels ${Object.keys(E).join(', ')}: ${nField} values, ${nMis} mismatching groups`); fs.writeFileSync('levels.json', JSON.stringify({ embedded: E, run: R }, null, 1));