import fs from 'node:fs'; import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { execFileSync } from 'node:child_process'; const hash = bytes => createHash('sha256').update(bytes).digest('hex'); const git = args => execFileSync('git', args, { windowsHide: true, maxBuffer: 20000000 }); const baseline = JSON.parse(fs.readFileSync('docs/model-protocols-release-code-baseline.json', 'utf8')); const tree = new Map(git(['ls-tree', '-r', '-z', baseline.codeCommit]).toString('utf8').split('\0').filter(Boolean).map(line => { const [header, name] = line.split('\t'); return [name, header.split(' ')[2]]; })); const exact = [], normalized = [], generated = []; const generator = 'examples/delivery-report/scripts/build.mjs'; assert.deepEqual(fs.readFileSync(generator), git(['show', baseline.codeCommit + ':' + generator])); for (const item of baseline.inputs) { const bytes = fs.readFileSync(item.path); assert.equal(hash(bytes), item.sha256); assert.equal(bytes.length, item.bytes); if (tree.has(item.path)) { const committed = git(['show', baseline.codeCommit + ':' + item.path]); if (bytes.equals(committed)) exact.push(item.path); else { const normalizedBytes = Buffer.from(bytes.toString('utf8').replaceAll('\r\n', '\n')); assert.deepEqual(Buffer.from(bytes.toString('utf8')), bytes, 'Only valid UTF-8 text may normalize.'); assert.deepEqual(normalizedBytes, committed, 'Difference beyond CRLF/LF: ' + item.path); const attr = git(['check-attr', 'text', 'eol', '--', item.path]).toString('utf8'); assert.doesNotMatch(attr, /: text: unset/); assert.match(attr, /: eol: (lf|crlf)/); normalized.push({ ...item, gitBytes: committed.length, gitSha256: hash(committed), attributes: attr.trim().split('\n'), scope: 'Only repository-declared CRLF/LF normalization; full normalized buffer matches committed blob.' }); } } else { assert.ok(/^examples\/delivery-report\/dist\/(?:[^/]+\.mjs|package\.json)$/.test(item.path), 'Unexpected uncommitted frozen input: ' + item.path); const source = item.path.endsWith('/package.json') ? null : item.path.replace('/dist/', '/src/'); const expected = source ? git(['show', baseline.codeCommit + ':' + source]) : Buffer.from('{"type":"module"}\n'); assert.deepEqual(bytes, expected); generated.push({ ...item, source, generator, generatorSha256: hash(fs.readFileSync(generator)) }); } } assert.equal(generated.length, 5); const result = { success: true, at: new Date().toISOString(), codeCommit: baseline.codeCommit, inputFiles: baseline.inputs.length, exactInputs: exact.length, normalized, generated, scope: 'Every frozen source/QA/build input tied to the committed source or exact generated example derivation; unchanged build bytes independently recorded.' }; fs.writeFileSync('.qa/protocol-f-code-git-audit.json', JSON.stringify(result, null, 2) + '\n', { flag: 'wx' }); console.log(JSON.stringify({ success: true, inputs: baseline.inputs.length, exact: exact.length, normalized: normalized.map(item => item.path), generated: generated.length }));