Files
Agent-Workbench/docs/workers-final-candidate-verifier.mjs

66 lines
5.6 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';
const read = filename => JSON.parse(fs.readFileSync(filename, 'utf8'));
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
const source = read('.qa/source-package-validation.json'), release = read('releases/latest.json');
const baseline = read('.qa/workers-final-code-baseline.json');
assert.equal(source.success, true); assert.equal(source.version, '1.13.0');
assert.equal(source.tests.tests, 404); assert.equal(source.tests.pass, source.tests.tests);
for (const key of ['fail', 'cancelled', 'skipped', 'todo']) assert.equal(source.tests[key], 0);
assert.deepEqual(source.inputs, baseline.inputs); assert.equal(source.inputFiles, baseline.inputs.length);
assert.equal(source.buildMatchedFiles, baseline.builds.length);
const exact = [], normalized = [];
const permittedLf = new Map(read('docs/model-center-release-identity.json').gitLfOnlyInputs.map(value => [value.path, value]));
const commit = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8', windowsHide: true }).trim();
for (const input of source.inputs) {
for (const root of [process.cwd(), release.sourceRoot, source.sourceRoot]) {
const bytes = fs.readFileSync(path.join(root, input.path)); assert.equal(bytes.length, input.bytes); assert.equal(hash(bytes), input.sha256, input.path);
}
const bytes = fs.readFileSync(input.path), blob = execFileSync('git', ['show', commit + ':' + input.path], { windowsHide: true, maxBuffer: 16 * 1024 * 1024 });
if (bytes.equals(blob)) exact.push(input.path);
else {
const original = permittedLf.get(input.path); assert.ok(original, 'Unexpected input/Git byte mismatch: ' + input.path);
assert.equal(hash(blob), original.gitBlobSha256, 'Previously recorded file must remain unchanged: ' + input.path);
assert.ok(Buffer.from(bytes.toString('utf8').replaceAll('\r\n', '\n')).equals(blob), 'Only previously recorded CRLF/LF differences are permitted.');
normalized.push({ path: input.path, sourceSha256: input.sha256, gitBlobSha256: hash(blob), difference: 'Previously recorded unchanged file; exact bytes match after CRLF to LF only.' });
}
}
for (const input of baseline.builds) for (const root of [process.cwd(), release.portableRoot, source.sourceRoot]) {
const bytes = fs.readFileSync(path.join(root, input.path)); assert.equal(bytes.length, input.bytes); assert.equal(hash(bytes), input.sha256, input.path);
}
const files = (root, relative) => fs.readdirSync(path.join(root, relative), { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)).flatMap(entry => {
assert.equal(entry.isSymbolicLink(), false);
const name = path.join(relative, entry.name); return entry.isDirectory() ? files(root, name) : [name];
});
const compared = [], repackedMetadata = [];
const validationNodeSha256 = hash(fs.readFileSync(process.execPath));
const bundledNodeSha256 = hash(fs.readFileSync(path.join(release.portableRoot, 'runtime/node.exe')));
assert.equal(validationNodeSha256, bundledNodeSha256, 'The source validation and portable host use the same Node binary.');
for (const folder of ['src', 'tests', 'scripts', 'public', 'benchmarks', 'runtime']) {
const names = files(release.sourceRoot, folder); assert.deepEqual(names, files(source.sourceRoot, folder), folder);
for (const filename of names) {
const left = fs.readFileSync(path.join(release.sourceRoot, filename)), right = fs.readFileSync(path.join(source.sourceRoot, filename));
if (!left.equals(right) && filename.replaceAll('\\', '/') === 'runtime/REA-RUNTIME.json') {
const { builtAt: finalBuiltAt, ...a } = JSON.parse(left), { builtAt: testedBuiltAt, ...b } = JSON.parse(right);
assert.ok(Number.isFinite(Date.parse(finalBuiltAt)) && Number.isFinite(Date.parse(testedBuiltAt))); assert.deepEqual(a, b);
repackedMetadata.push({ path: filename.replaceAll('\\', '/'), finalBuiltAt, testedBuiltAt, finalSha256: hash(left), testedSha256: hash(right), difference: 'builtAt only; all other fields and dependency hashes equal.' });
} else assert.ok(left.equals(right), filename);
compared.push(filename.replaceAll('\\', '/'));
}
}
for (const log of source.logs) { const bytes = fs.readFileSync(log.path); assert.equal(bytes.length, log.bytes); assert.equal(hash(bytes), log.sha256, log.stage); }
const result = { success: true, version: source.version, testedAt: new Date().toISOString(), codeCommit: baseline.codeCommit, commit,
finalSourceRoot: release.sourceRoot, finalPortableRoot: release.portableRoot, independentSourceRoot: source.sourceRoot,
tests: source.tests, inputFiles: source.inputFiles, exactBytes: exact.length, normalized, buildMatchedFiles: baseline.builds.length,
comparedFiles: compared.length, comparedFolders: ['src', 'tests', 'scripts', 'public', 'benchmarks', 'runtime'], repackedMetadata,
logsVerified: source.logs.length, validationNode: { path: process.execPath, version: process.version, sha256: validationNodeSha256 },
portableNode: { path: path.join(release.portableRoot, 'runtime/node.exe'), sha256: bundledNodeSha256 },
scope: 'All frozen inputs/builds and native source runtime compared with independently validated source. Git exceptions limited to four previously recorded unchanged CRLF/LF files.' };
fs.writeFileSync('.qa/workers-final-inputs-comparison.json', JSON.stringify(result, null, 2) + '\n');
console.log(JSON.stringify({ success: true, inputs: source.inputFiles, exactGitBytes: exact.length, normalized: normalized.length,
builds: baseline.builds.length, comparedFiles: compared.length, repackedMetadata: repackedMetadata.length, logsVerified: source.logs.length }));