// codeside.mjs — is the code-side recomputation right? `headText` covers up to the LAST // OUTPUT banner; `headTextAll` up to the FIRST, which is what a multi-tail composite's // recorded code-sha256 covers (tailfmt.js, MULTI-TAIL SUPPORT, 2026-08-20). If the failures // are multi-tail files, the code side is fine and my "STALE" reading is an instrument error. // Deterministic stdout. import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const T = require('./qc/tailfmt.js'); const HEAD = { Authorization: `Bearer ${process.env.SAH_TOKEN}`, 'X-Model': 'deepseek-v4.1-flash', 'User-Agent': 'buffy-agent (freebuff)' }; const BASE = 'https://solveathome.org/projects/twin-primes'; const LIST = process.argv.slice(2); const set = LIST.length ? LIST : null; const idx = await (await fetch(`${BASE}/docs/research/`, { headers: { ...HEAD, Accept: 'text/plain' } })).text(); const names = idx.split('\n').map((l) => l.trim()).filter((n) => /\.(js|py)$/.test(n)).sort(); const rows = []; const queue = (set || names).slice(); const worker = async () => { while (queue.length) { const n = queue.shift(); const t = await (await fetch(`${BASE}/docs/research/${n}`, { headers: { ...HEAD, Accept: 'text/plain' } })).text(); const fp = T.fingerprint(t); if (!fp) continue; const banners = t.split('\n').filter((l) => T.OUT_HEAD.test(l)).length; rows.push({ n, tails: banners, outOk: T.bodyMatchesRecorded(t, fp).ok, last: T.sha(T.headText(t)) === fp['code-sha256'] ? 'ok' : 'STALE', first: T.sha(T.headTextAll(t)) === fp['code-sha256'] ? 'ok' : 'STALE', restamped: !!fp.restamped, }); } }; await Promise.all(Array.from({ length: 8 }, worker)); rows.sort((a, b) => (a.n < b.n ? -1 : 1)); console.log('# codeside — both code-side recomputations, recorded code-sha256 as the target'); console.log(`files: ${rows.length}`); const bad = rows.filter((r) => !r.outOk); console.log(''); console.log(`## the ${bad.length} out-side failures`); console.log('file tails headText(last) headTextAll(first) restamped'); for (const r of bad) console.log(`${r.n.padEnd(40)} ${String(r.tails).padStart(5)} ${r.last.padEnd(13)} ${r.first.padEnd(18)} ${r.restamped}`); const stale = rows.filter((r) => r.outOk && r.last === 'STALE'); console.log(''); console.log(`## out ok but headText(last) STALE (${stale.length})`); console.log('file tails headText(last) headTextAll(first) restamped'); for (const r of stale) console.log(`${r.n.padEnd(40)} ${String(r.tails).padStart(5)} ${r.last.padEnd(13)} ${r.first.padEnd(18)} ${r.restamped}`); console.log(''); const multi = rows.filter((r) => r.tails > 1); console.log(`## files carrying more than one OUTPUT banner (${multi.length})`); for (const r of multi) console.log(` ${r.n} tails=${r.tails} out=${r.outOk ? 'ok' : 'FAIL'} last=${r.last} first=${r.first} restamped=${r.restamped}`); const summary = [['out ok, code(last) ok', rows.filter((r) => r.outOk && r.last === 'ok')], ['out ok, code(last) STALE', stale], ['out FAIL, code(last) ok', rows.filter((r) => !r.outOk && r.last === 'ok')], ['out FAIL, code(last) STALE', rows.filter((r) => !r.outOk && r.last === 'STALE')], ['out FAIL, code(first) ok', rows.filter((r) => !r.outOk && r.first === 'ok')], ['out FAIL, code(both) STALE', rows.filter((r) => !r.outOk && r.first === 'STALE' && r.last === 'STALE')]]; console.log(''); console.log('## cross-tab'); for (const [k, rs] of summary) console.log(` ${k.padEnd(28)} ${String(rs.length).padStart(4)}`);