Files
Agent-Workbench/docs/milestone-audit-evidence-seal-verifier.mjs
T

100 lines
9.5 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';
import { DatabaseSync } from 'node:sqlite';
const read = filename => JSON.parse(fs.readFileSync(filename, 'utf8'));
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
const canonical = value => Array.isArray(value) ? value.map(canonical) : value && typeof value === 'object'
? Object.fromEntries(Object.keys(value).sort().map(key => [key, canonical(value[key])])) : value;
const digest = value => hash(JSON.stringify(canonical(value)));
const source = read('.qa/source-package-validation.json'), compared = read('.qa/milestone-final-inputs-comparison.json');
const release = read('releases/latest.json'), archive = read('.qa/archives-validation.json'), portable = read('.qa/release-validation.json');
const deployed = read('docs/milestone-audit-upgrade-live-validation.json'), smoke = read('.qa/milestone-audit-deployed-ui-validation.json');
const ui = read('docs/milestone-audit-all-ui-validation.json'), security = read('docs/milestone-audit-security-ui-validation.json');
const baseline = read('docs/milestone-audit-code-baseline.json'), first = read('docs/milestone-audit-first-candidate-baseline.json');
for (const [name, report] of Object.entries({ source, compared, archive, portable, deployed, smoke, ui, security })) {
assert.equal(report.success, true, name); assert.equal(report.version, '1.12.1', name);
}
assert.equal(source.tests.pass, source.tests.tests); assert.ok(source.tests.tests > 357);
for (const key of ['fail', 'cancelled', 'skipped', 'todo']) assert.equal(source.tests[key], 0);
assert.equal(source.inputFiles, baseline.inputs.length); assert.deepEqual(source.inputs, baseline.inputs);
assert.equal(source.buildMatchedFiles, baseline.builds.length); assert.equal(compared.inputFiles, source.inputFiles);
assert.equal(ui.groups, 150); assert.equal(ui.results.length, 20); assert.equal(security.assertions.length, 8);
assert.equal(smoke.results.length, 8); assert.equal(smoke.pid, deployed.pid); assert.equal(deployed.preservesCurrentObservedProcess, true);
assert.equal(deployed.allRowsAndExportsPreserved, true); assert.equal(deployed.connectionPreserved, true);
assert.equal(deployed.modelCenterPreserved, true);
assert.equal(deployed.realModelRequests, 0); assert.equal(smoke.modelRequests, 0); assert.equal(security.realModelRequests, 0);
for (const field of ['pageErrors', 'requestFailures', 'httpErrors', 'blockedWrites']) assert.deepEqual(smoke[field], []);
const images = smoke.results.map(result => {
const bytes = fs.readFileSync(result.screenshot); assert.equal(hash(bytes), result.screenshotSha256);
assert.ok(bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])));
return { path: result.screenshot, bytes: bytes.length, sha256: result.screenshotSha256 };
});
for (const native of [source.actualRea, source.actualEngine, source.actualBrowser]) assert.equal(native.success, true);
assert.equal(source.actualEngine.modelRequests, 0); assert.equal(source.actualBrowser.modelRequests, 0);
assert.equal(fs.realpathSync.native(portable.appRoot), fs.realpathSync.native(release.portableRoot));
assert.equal(fs.realpathSync.native(deployed.appRoot), fs.realpathSync.native(release.portableRoot));
for (const kind of ['source', 'portable']) {
const item = archive.archives.find(value => value.kind === kind); assert.equal(item.sha256, release[kind].sha256);
const bytes = fs.readFileSync(release[kind].path); assert.equal(hash(bytes), release[kind].sha256); assert.equal(bytes.length, release[kind].bytes);
}
const logs = [];
for (const log of source.logs) { const bytes = fs.readFileSync(log.path); assert.equal(bytes.length, log.bytes); assert.equal(hash(bytes), log.sha256); logs.push(log); }
for (const result of ui.results) {
assert.equal(result.success, true); const bytes = fs.readFileSync(result.log);
logs.push({ stage: result.script, path: result.log, bytes: bytes.length, sha256: hash(bytes) });
}
const git = execFileSync('git', ['diff', '--name-only', baseline.codeCommit, 'HEAD', '--', 'src', 'tests', 'scripts', 'package.json', 'package-lock.json', 'vite.config.ts', 'tsconfig.json', 'index.html'], { windowsHide: true, encoding: 'utf8' }).trim();
assert.equal(git, '', 'Implementation inputs changed after the validated code commit.');
for (const input of source.inputs) { const bytes = fs.readFileSync(input.path); assert.equal(bytes.length, input.bytes); assert.equal(hash(bytes), input.sha256, input.path); }
for (const original of first.originals) { const bytes = fs.readFileSync(original.saved); assert.equal(bytes.length, original.bytes); assert.equal(hash(bytes), original.sha256, 'Initial evidence must remain unchanged.'); }
const db = new DatabaseSync(path.join(deployed.dataRoot, 'workbench.sqlite'), { readOnly: true }); db.exec('BEGIN');
try {
for (const [table, order] of [['projects', 'id'], ['sessions', 'id'], ['records', 'kind,id'], ['events', 'seq']]) {
const rows = db.prepare('SELECT * FROM ' + table + ' ORDER BY ' + order).all();
assert.equal(rows.length, deployed.database[table].count, table); assert.equal(digest(rows), deployed.database[table].sha256, table + ' changed during deployed UI smoke');
}
} finally { db.exec('ROLLBACK'); db.close(); }
await assert.rejects(fetch('http://127.0.0.1:9229/json/list', { signal: AbortSignal.timeout(1000) }));
const health = await (await fetch('http://127.0.0.1:4318/api/health')).json(); assert.equal(health.version, '1.12.1'); assert.equal(health.pid, deployed.pid);
const copies = [
['.qa/archives-validation.json', 'docs/milestone-audit-archives-validation.json'],
['.qa/milestone-source-candidate-archives-validation.json', 'docs/milestone-audit-source-candidate-archives-validation.json'],
['.qa/milestone-source-tested-candidate.json', 'docs/milestone-audit-source-tested-candidate.json'],
['.qa/source-package-validation.json', 'docs/milestone-audit-source-package-validation.json'],
['.qa/release-validation.json', 'docs/milestone-audit-release-validation.json'],
['.qa/milestone-audit-deployed-ui-validation.json', 'docs/milestone-audit-deployed-ui-validation.json'],
['.qa/milestone-final-inputs-comparison.json', 'docs/milestone-audit-git-inputs.json'],
[path.join(source.directory, 'tests.tap'), 'docs/milestone-audit-final-package-tests.tap'],
...source.logs.filter(log => path.basename(log.path) !== 'tests.tap').map(log => [log.path, 'docs/milestone-audit-source-' + path.basename(log.path).replace(/\.log$/, '-log.txt')]),
['.qa/finish-milestone-deployment.mjs', 'docs/milestone-audit-deployment-verifier.mjs'],
['.qa/smoke-milestone-deployed.mjs', 'docs/milestone-audit-deployed-ui-verifier.mjs'],
['.qa/compare-milestone-final-candidate.mjs', 'docs/milestone-audit-final-candidate-verifier.mjs'],
['.qa/seal-milestone-evidence.mjs', 'docs/milestone-audit-evidence-seal-verifier.mjs'],
];
const originals = [];
for (const [from, to] of copies) {
const bytes = fs.readFileSync(from); originals.push({ path: from, destination: to, bytes: bytes.length, sha256: hash(bytes) });
fs.writeFileSync(to, bytes.toString('utf8').replaceAll('\r\n', '\n'));
}
const names = [...new Set([...fs.readdirSync('docs').filter(name => name.startsWith('milestone-audit-') && name !== 'milestone-audit-release-identity.json').map(name => 'docs/' + name),
'docs/MILESTONE-AUDIT.md', 'docs/NEXT-STEPS.md', 'docs/QA-1.12.1.md', 'docs/VALIDATION.md', 'docs/CAPABILITIES.md', 'docs/PRODUCT-PLAN.md', 'docs/RESEARCH-ROADMAP.md', 'docs/deployment.json'])].sort();
const identity = { success: true, version: '1.12.1', releaseId: path.basename(release.portableRoot).match(/(\d{17})$/)[1], sealedAt: new Date().toISOString(),
codeCommit: baseline.codeCommit, auditPlanCommit: '057d91f', evidenceCommitBase: execFileSync('git', ['rev-parse', 'HEAD'], { windowsHide: true, encoding: 'utf8' }).trim(),
release, independentlyTestedCandidate: read('.qa/milestone-source-tested-candidate.json'), independentSourceRoot: source.sourceRoot,
sourceInputs: source.inputFiles, gitExactInputBytes: compared.exactBytes, gitLfOnlyInputs: compared.normalized, buildMatchedFiles: source.buildMatchedFiles,
testStatistics: source.tests, uiGroups: ui.groups, securityGroups: security.assertions.length, deployedUiPages: smoke.results.length,
currentPid: deployed.pid, historicalBaselinePid: deployed.historicalReleasePid, allRowsAndExportsPreserved: true, postSmokeDatabaseUnchanged: true, inspectorClosed: true,
repackedMetadata: compared.repackedMetadata, validationNode: compared.validationNode, portableNode: compared.portableNode,
originals, logs, logsVerified: logs.length, images, imagesVerified: images.length, originalFirstCandidateEvidencePreserved: true,
reports: names.map(filename => { const bytes = fs.readFileSync(filename); return { path: filename, bytes: bytes.length, sha256: hash(bytes) }; }),
realModelAcceptance: { status: 'deferred_by_user', requests: 0 },
scope: 'Milestone audit and confirmed fixes; no new worker, desktop/mobile or external-engine project execution. Immutable release keeps packaging-time records; repository reports seal final deployment.' };
fs.writeFileSync('docs/milestone-audit-release-identity.json', JSON.stringify(identity, null, 2) + '\n');
console.log(JSON.stringify({ success: true, version: identity.version, releaseId: identity.releaseId, tests: identity.testStatistics,
inputs: identity.sourceInputs, sealedReports: identity.reports.length, logsVerified: logs.length, imagesVerified: images.length,
pid: deployed.pid, postSmokeDatabaseUnchanged: true, inspectorClosed: true }));