Files
Agent-Workbench/docs/verify-task-agreement-1.25.mjs

175 lines
22 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';
// Offline only: no HTTP, installer, credential access or driver replay.
const root = path.resolve('docs/task-agreement-1.25-evidence'), index = process.argv.includes('--index');
assert.ok(process.argv.slice(2).every(value => value === '--index'));
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
const blob = bytes => createHash('sha1').update(Buffer.from('blob ' + bytes.length + '\0')).update(bytes).digest('hex');
const read = name => JSON.parse(fs.readFileSync(path.join(root, name), 'utf8').replace(/^\uFEFF/, ''));
const bytes = name => fs.readFileSync(path.join(root, name));
const manifest = read('manifest.json'), names = new Set(); let total = 0;
for (const row of manifest.entries) {
assert.ok(row.path && !row.path.includes('\\') && !row.path.split('/').includes('..') && !path.isAbsolute(row.path));
assert.equal(names.has(row.path), false); names.add(row.path);
const data = bytes(row.path); assert.equal(data.length, row.bytes); assert.equal(hash(data), row.sha256, row.path); total += data.length;
if (row.path.endsWith('.png')) assert.equal(data.subarray(0, 8).toString('hex'), '89504e470d0a1a0a');
if (/\.jpe?g$/.test(row.path)) assert.equal(data.subarray(0, 3).toString('hex'), 'ffd8ff');
}
const walk = directory => fs.readdirSync(directory, { withFileTypes: true }).flatMap(row => {
const file = path.join(directory, row.name); assert.equal(fs.lstatSync(file).isSymbolicLink(), false);
return row.isDirectory() ? walk(file) : [path.relative(root, file).replaceAll('\\', '/')];
});
assert.deepEqual(walk(root).filter(name => name !== 'manifest.json').sort(), [...names].sort());
assert.equal(manifest.count, names.size); assert.equal(manifest.bytes, total); assert.equal(manifest.realModelRequests, 0);
const supplementRoot=path.resolve('docs/task-agreement-1.25-supplement'),supplement=JSON.parse(fs.readFileSync(path.join(supplementRoot,'manifest.json')));assert.equal(supplement.kind,'task-agreement-1.25-verifier-correction');
const supplementaryNames=new Set();let supplementaryBytes=0;for(const row of supplement.entries){assert.ok(row.path&&!row.path.includes('\\')&&!row.path.split('/').includes('..')&&!path.isAbsolute(row.path));assert.equal(supplementaryNames.has(row.path),false);supplementaryNames.add(row.path);const data=fs.readFileSync(path.join(supplementRoot,row.path));assert.equal(data.length,row.bytes);assert.equal(hash(data),row.sha256);supplementaryBytes+=data.length;}
assert.equal(supplement.count,supplementaryNames.size);assert.equal(supplement.bytes,supplementaryBytes);assert.equal(supplement.originalManifestSha256,hash(bytes('manifest.json')));assert.equal(hash(fs.readFileSync(path.join(supplementRoot,'verifier-original.mjs.txt'))),hash(bytes('receipts/verify-seal.mjs.txt')));assert.equal(hash(fs.readFileSync('docs/verify-task-agreement-1.25.mjs')),hash(fs.readFileSync(path.join(supplementRoot,'verifier-corrected.mjs.txt'))));
const freeze = read('receipts/freeze.json'), release = read('receipts/candidate.json'), identity = read('release-identity.json');
assert.equal(freeze.sourceCommit, manifest.sourceCommit); assert.equal(identity.sourceCommit, freeze.sourceCommit);
assert.equal(release.documentation.sourceCommit, freeze.sourceCommit); assert.equal(identity.version, '1.25.0');
assert.equal(identity.candidate, path.basename(release.portableRoot)); assert.equal(identity.formalPid, 36552);
assert.deepEqual(identity.installer, release.installer);
for (const kind of ['portable', 'source', 'evidence']) assert.deepEqual(identity.archives[kind], release[kind]);
const sourceTree = execFileSync('git', ['ls-tree', '-r', '-z', '--full-tree', freeze.sourceCommit, '--', 'src', 'public', 'scripts', 'tests', 'examples', 'benchmarks', 'research',
'package.json', 'package-lock.json', 'vite.config.ts', 'tsconfig.json', 'index.html', 'mobile.html'], { encoding: 'utf8', maxBuffer: 8 * 1024 * 1024 }).split('\0').filter(Boolean);
const sourceBlobs = new Map(sourceTree.map(row => { const [meta, file] = row.split('\t'); return [file, meta.split(' ')[2]]; }));
for (const row of freeze.inputs) {
const data = bytes('source-bytes/' + row.path); assert.equal(hash(data), row.sha256);
const oid = sourceBlobs.get(row.path); assert.ok(oid, row.path);
if (blob(data) !== oid) {
assert.equal(data.includes(0), false); assert.ok(Buffer.from(data.toString('utf8')).equals(data));
assert.equal(blob(Buffer.from(data.toString('utf8').replaceAll('\r\n', '\n'))), oid, row.path + ' fixed Git source');
}
}
for (const row of freeze.compiled) assert.equal(hash(bytes('compiled/' + row.path)), row.sha256);
assert.equal(hash(bytes('portable-manifest.json')), identity.portableManifestSha256);
assert.equal(hash(bytes('source-manifest.json')), identity.sourceManifestSha256);
const harnessReview=read('receipts/harness-review.json');assert.equal(harnessReview.success,true);assert.equal(harnessReview.sourceCommit,freeze.sourceCommit);for(const row of harnessReview.entries){assert.equal(hash(bytes('source-bytes/'+row.original)),row.originalSha256);assert.equal(hash(bytes('receipts/'+path.basename(row.corrected)+'.txt')),row.correctedSha256);}
const recheckReview=read('receipts/harness-review-recheck.json');assert.equal(recheckReview.success,true);assert.equal(recheckReview.originalOuterWaitMs,300000);assert.equal(recheckReview.newOuterWaitMs,600000);for(const row of recheckReview.entries){assert.equal(hash(bytes('receipts/'+path.basename(row.original)+'.txt')),row.originalSha256);assert.equal(hash(bytes('receipts/'+path.basename(row.corrected)+'.txt')),row.correctedSha256);}
assert.equal(bytes('receipts/verify-desktop-install-reviewed.mjs.txt').toString('utf8').replace('timeout: 300000','timeout: 600000'),bytes('receipts/verify-desktop-install-longwait.mjs.txt').toString('utf8'));
const stages = ['rebuild', 'archives', 'portable', 'install', 'source-a', 'source-a-desktop', 'source-b', 'source-b-desktop', 'portable-a', 'portable-a-desktop', 'portable-b', 'portable-b-desktop', 'portable-files', 'source-c', 'source-c-desktop', 'source-d', 'source-d-desktop', 'portable-c', 'portable-c-desktop', 'portable-d', 'portable-d-desktop'];
for (const stage of stages) {
const receipt = read('receipts/receipts/' + (stage === 'source-a' ? 'source-a-recheck' : stage === 'install' ? 'install-recheck' : stage) + '.json'); assert.equal(receipt.success, true, stage);
assert.equal(receipt.sourceCommit, freeze.sourceCommit); assert.equal(receipt.candidate, release.portableRoot);
assert.equal(hash(bytes('receipts/logs/' + path.basename(receipt.log))), receipt.logSha256);
}
const build = read('validation/rebuild/result.json'); assert.equal(build.success, true); assert.equal(build.sourceCommit, freeze.sourceCommit);
assert.equal(build.compiled.length, freeze.compiled.length); assert.deepEqual(build.mismatches, []);
const frozenNames = new Set(freeze.inputs.map(row => row.path)), extras = build.inputs.filter(row => !frozenNames.has(row.path));
assert.equal(extras.length, 5); assert.equal(build.inputCount, freeze.inputs.length + extras.length);
for (const row of extras) { assert.ok(row.path.startsWith('examples/delivery-report/dist/')); assert.equal(hash(bytes('source-bytes/' + row.path)), row.sha256); }
for (const row of freeze.inputs) assert.equal(build.inputs.find(value => value.path === row.path)?.sha256, row.sha256, row.path);
for (const row of build.compiled) assert.deepEqual(row.actual, row.expected);
for (const kind of ['portable', 'source', 'evidence']) {
const archive = read('validation/archives.json').archives.find(row => row.kind === kind); assert.ok(archive.crcPassed && archive.manifestAndDirectoryMatch);
assert.equal(archive.sha256, identity.archives[kind].sha256);
}
assert.equal(read('validation/portable.json').success, true);
const installation = read('validation/install/result.json'); assert.equal(installation.success, true); assert.equal(installation.version, '1.25.0');
assert.equal(installation.results.length, 12); assert.ok(installation.results.every(row => row.passed));
assert.equal(installation.installedUI.proofs.length, 10); assert.equal(installation.realModelRequests, 0);
let uiRuns = 0, originalArtifacts = 0, originalSnapshots = 0;
for (const scope of ['source', 'portable', 'installed']) for (const [suite, count] of [['a', 7], ['a-desktop', 3], ['b', 9], ['b-desktop', 9], ['c', 6], ['c-desktop', 6], ['d', 5], ['d-desktop', 5]]) {
const directory = 'validation/' + scope + '-' + suite, proof = read(directory + '/result.json');
assert.equal(proof.success, true); assert.equal(proof.version, '1.25.0'); assert.equal(proof.passed.length, count);
assert.equal(proof.realModelRequests, 0); assert.deepEqual(proof.errors, []); uiRuns++;
if (scope === 'portable') assert.equal(proof.appRoot, release.portableRoot);
if (scope === 'installed') assert.equal(proof.appRoot, installation.installedUI.installedRoot);
if (!suite.startsWith('b')) continue;
const artifacts = new Map(read(directory + '/source-artifacts.json').map(row => [row.id, row]));
const artifact = id => {
const row = artifacts.get(id); assert.ok(row, id); const data = bytes(directory + '/' + row.path);
assert.equal(hash(data), row.sha256); assert.equal(data.length, row.bytes); return { row, data };
};
for (const id of artifacts.keys()) { artifact(id); originalArtifacts++; }
for (const file of proof.snapshots) {
const snapshot = read(directory + '/' + file), { session, execution } = snapshot, run = execution.run, shadow = run.progressShadow; originalSnapshots++;
assert.equal(shadow.mode, 'shadow'); assert.deepEqual(shadow.scope, { projectId: session.projectId, sessionId: session.id, taskId: run.taskId, runId: run.id });
assert.equal(shadow.turns.length, run.progressShadowArtifactIds.length);
for (const [i, turn] of shadow.turns.entries()) {
const original = artifact(run.progressShadowArtifactIds[i]); assert.equal(original.row.kind, 'turn-progress-shadow');
assert.deepEqual(JSON.parse(original.data), turn); assert.equal(turn.taskId, run.taskId); assert.equal(turn.runId, run.id);
for (const ref of turn.tools) {
if (ref.tool === 'model_request') {
const event = session.events.find(row => row.id === ref.source_event_id && row.runId === run.id); assert.ok(event?.diagnostic);
assert.equal(hash(JSON.stringify(event.diagnostic)), ref.diagnostic_sha256);
} else {
const event = session.events.find(row => row.invocationId === ref.id && row.runId === run.id && row.taskId === run.taskId);
assert.ok(event); assert.equal(event.tool, ref.tool); assert.equal(event.artifactId, ref.result_artifact_id);
const call = session.providerMessages.flatMap(row => row.tool_calls ?? []).find(row => row.id === event.toolCallId); assert.ok(call);
assert.equal(call.function.name, ref.tool); assert.equal(hash(String(call.function.arguments)), ref.args_sha256);
}
if (ref.result_artifact_id) assert.equal(artifact(ref.result_artifact_id).row.sha256, ref.result_sha256);
}
}
}
}
for (const [scope, suite, count] of [['portable', 'files', 9], ['installed', 'files', 9], ['installed', 'results', 16]]) {
const proof = read('validation/' + scope + '-' + suite + '/result.json'); assert.equal(proof.success, true); assert.equal(proof.checks.length, count); assert.deepEqual(proof.errors, []);
}
const initial = read('receipts/formal/state-before.json');
assert.equal(initial.version, '1.24.9'); assert.equal(initial.hasKey, true);
for (const stage of ['upgrade', 'final']) {
const proof = read('receipts/formal/' + stage + '.json'); assert.equal(proof.success, true); assert.equal(proof.version, '1.25.0');
assert.equal(proof.pid, 36552); assert.equal(proof.inspectorClosed, true); assert.equal(proof.desktopWindows, 0);
assert.equal(proof.realModelRequests, 0); assert.equal(proof.keyReadOrResaved, false);
assert.deepEqual(proof.retention, { originalRows: 'exact', originalExports: 'exact', originalArtifactBytes: 'exact', originalModelCenter: 'exact' });
assert.equal(proof.runtimeRoot, identity.formalRuntimeRoot); assert.equal(proof.operation.state, 'completed');
const after = read('receipts/formal/' + stage + '-state-after.json');
for (const key of ['pid', 'hasKey', 'configSha256', 'projectsSha256', 'sessionsSha256', 'eventCursor', 'exports', 'persistent', 'modelCenterSha256']) assert.deepEqual(after[key], initial[key], key);
assert.deepEqual(after.database.tables, initial.database.tables); assert.deepEqual(after.database.sessionIds, initial.database.sessionIds);
}
assert.equal(read('receipts/formal/before.json').success, true);
assert.equal(read('receipts/formal/installer-exit.json').code, 0);
const web = read('receipts/review/result.json'), native = read('receipts/native/result.json'),stableWeb=read('receipts/review-stable/result.json');
assert.equal(stableWeb.success,true);assert.equal(stableWeb.version,'1.25.0');assert.equal(stableWeb.pid,36552);assert.equal(stableWeb.realModelRequests,0);assert.deepEqual(stableWeb.errors,[]);assert.deepEqual(stableWeb.rejectedWrites,[]);assert.deepEqual(stableWeb.views,web.views);
for (const proof of [web, native]) { assert.equal(proof.success, true); assert.equal(proof.version, '1.25.0'); assert.equal(proof.pid, 36552); assert.equal(proof.realModelRequests, 0); }
assert.deepEqual(web.errors, []); assert.deepEqual(web.rejectedWrites, []);
assert.deepEqual(web.views.map(row => [row.width, row.height, row.requirementItems, row.legacyShadowPanels]), [[1440, 1080, 0, 0], [390, 844, 0, 0]]);
for (const row of native.screenshots) { const data = bytes('receipts/native/' + row.path); assert.equal(data.length, row.bytes); assert.equal(hash(data), row.sha256); }
assert.match(bytes('receipts/native/main.txt').toString('utf8'), /release=1\.25\.0/);
assert.match(bytes('receipts/native/requirements.txt').toString('utf8'), /尚未逐项记录要求。模型的完成答复保留在原会话中。/);
const post=read('receipts/formal/post-review.json'),postState=read('receipts/formal/post-review-state.json');assert.equal(post.success,true);assert.equal(post.version,'1.25.0');assert.equal(post.pid,36552);assert.equal(post.desktopWindows,0);assert.equal(post.inspectorClosed,true);assert.deepEqual(post.retention,read('receipts/formal/final.json').retention);for(const key of ['pid','hasKey','configSha256','projectsSha256','sessionsSha256','eventCursor','exports','persistent','modelCenterSha256'])assert.deepEqual(postState[key],initial[key],key);assert.deepEqual(postState.database.tables,initial.database.tables);assert.deepEqual(postState.database.sessionIds,initial.database.sessionIds);
const cost = read('receipts/update-cost.json'), operation = read('receipts/formal/upgrade.json').operation;
assert.equal(cost.operationElapsedMs, Date.parse(operation.updatedAt) - Date.parse(operation.startedAt));
assert.equal(cost.historyElapsedMs, operation.history.reduce((sum, row) => sum + row.elapsedMs, 0));
const first = bytes('development/closure-related-first.tap').toString('utf8');assert.match(first,/pass 62\b/);assert.match(first,/fail 3\b/);
for(const [file,count] of [['closure-recheck.tap',6],['diagnostic-tools-recheck.tap',9],['release-related-first.tap',25],['release-upgrade-first.tap',2]]){const log=bytes('development/'+file).toString('utf8');assert.match(log,new RegExp('pass '+count+'\\b'));assert.match(log,/fail 0\b/);assert.match(log,/cancelled 0\b/);}
const firstInstallRoot='receipts/install-first-reconciled/',firstResolution=read(firstInstallRoot+'resolution.json'),firstOperation=read(firstInstallRoot+'operation-final.json'),firstProgress=read(firstInstallRoot+'progress-final.json');assert.equal(firstResolution.success,true);assert.equal(firstResolution.originalUpdateDriverCode,-1);assert.equal(firstResolution.originalOuterTimeoutMs,300000);assert.equal(firstResolution.observedOperationElapsedMs,300503);assert.equal(firstProgress.state,'completed');assert.equal(firstOperation.state,'activated');assert.equal(firstOperation.hostRetained,true);assert.deepEqual(read(firstInstallRoot+'data-after-cleanup.json'),firstOperation.snapshot);assert.equal(firstResolution.ownedHostClosed,true);assert.equal(firstResolution.ownedUninstallerExit,0);
for(const item of read(firstInstallRoot+'result.json').results){if(item.log&&item.sha256)assert.equal(hash(bytes(firstInstallRoot+path.basename(item.log))),item.sha256);}
assert.equal(read('receipts/receipts/install.json').success,false);assert.equal(read('receipts/install-first-reconciled/resolution.json').success,true);assert.equal(read('receipts/install-first/resolution.json').success,false);assert.equal(read('receipts/install-first-reconciled/result.json').results.length,6);
assert.equal(read('receipts/receipts/source-a.json').success,false);assert.equal(read('validation/first-source-a/result.json').success,false);assert.equal(read('validation/first-source-a/result.json').passed.length,6);
assert.equal(hash(bytes('receipts/logs/source-a.log')),read('receipts/receipts/source-a.json').logSha256);
const rawRoot='receipts/raw-contracts/',raw=read(rawRoot+'result.json'),rawSession=read(rawRoot+'session.json'),rawBefore=read(rawRoot+'before.json');
assert.equal(raw.success,true);assert.equal(raw.version,'1.25.0');assert.equal(raw.sourceCommit,freeze.sourceCommit);assert.equal(raw.candidate,release.portableRoot);assert.equal(raw.hasKey,false);assert.equal(raw.realModelRequests,0);
const rawArtifacts=read(rawRoot+'source-artifacts.json');assert.equal(rawArtifacts.length,raw.artifactCount);assert.equal(rawArtifacts.length,7);
const rawPayload=kind=>{const row=rawArtifacts.find(row=>row.kind===kind);assert.ok(row,kind);return{row,value:JSON.parse(bytes(rawRoot+row.path))};};
for(const row of rawArtifacts){const data=bytes(rawRoot+row.path);assert.equal(data.length,row.bytes);assert.equal(hash(data),row.sha256);const exported=rawSession.execution.artifacts.find(item=>item.id===row.id);assert.ok(exported);assert.equal(exported.sha256,row.sha256);assert.equal(exported.bytes,row.bytes);}
const diagnostic=rawPayload('diagnostic-context'),draft=rawPayload('requirement-draft'),contract=rawPayload('task-requirements'),proof=rawPayload('requirement-proof'),report=rawPayload('delivery-report'),readResult=rawPayload('tool-result');
for(const entry of [diagnostic,draft,contract,proof]){assert.equal(entry.value.sessionId,rawSession.session.id);assert.equal(entry.value.projectId,rawSession.project.id);assert.equal(entry.value.taskId,rawSession.execution.task.id);assert.equal(entry.value.runId,raw.diagnosticRunId);}
assert.equal(diagnostic.value.originalRunId,raw.originalRunId);assert.equal(diagnostic.value.failure.id,raw.originalFailureId);assert.equal(diagnostic.value.failure.value.source,'qa_fixture');assert.equal(hash(JSON.stringify(diagnostic.value.failure.value)),diagnostic.value.failure.sha256);
const failureEvent=rawSession.session.events.find(row=>row.id===diagnostic.value.failure.eventId);assert.ok(failureEvent);assert.equal(failureEvent.runId,raw.originalRunId);assert.deepEqual(failureEvent.diagnostic,diagnostic.value.failure.value);
assert.equal(draft.value.proposal.items.length,4);assert.ok(draft.value.proposal.items.every(item=>item.role==='unknown'));assert.equal(contract.value.draftId,draft.value.id);assert.equal(contract.value.draftSha256,draft.row.sha256);
for(const item of draft.value.proposal.items){const original=draft.value.sources.find(row=>row.id===item.source.id);assert.ok(original);assert.equal(original.text.slice(item.source.start,item.source.end),item.source.quote);assert.equal(hash(JSON.stringify({kind:original.kind,id:original.id,text:original.text})),item.source.sourceSha256);}
assert.deepEqual(contract.value.items.map(item=>item.role),['requirement','reference','prohibition','requirement']);assert.equal(proof.value.requirementRevision,1);assert.equal(proof.value.status,'verified');assert.equal(proof.value.evidenceId,readResult.value.tool_invocation_id);assert.equal(proof.value.outcome.runId,raw.originalRunId);assert.equal(proof.value.outcome.artifacts[0].sha256,readResult.row.sha256);assert.equal(hash(readResult.value.content),readResult.value.sha256);
const rawStatus=read(rawRoot+'status.json');assert.equal(rawStatus.diagnostics[0].context.evidenceVerified,true);assert.equal(rawStatus.diagnostics[0].context.sha256,diagnostic.row.sha256);assert.equal(rawStatus.diagnostics[0].context.artifactId,diagnostic.row.id);
assert.equal(report.value.status,'scope_verified');assert.equal(report.value.checkPolicy,'not_requested');assert.equal(report.value.verifiedCheckJobId,null);assert.equal(report.value.task.id,rawSession.execution.task.id);assert.equal(report.value.runId,raw.diagnosticRunId);assert.deepEqual(report.value.followUp,rawStatus.followUp);assert.deepEqual(report.value.requirements.items.map(item=>item.status),['evidence_verified','reference_recorded','boundary_recorded','not_requested']);assert.equal(rawBefore.execution.checks.length,0);assert.equal(rawSession.execution.checks.length,0);
assert.deepEqual(rawBefore.session.changes,rawSession.session.changes);
for (const ref of read('original-references.json')) assert.equal(hash(fs.readFileSync(ref.path)), ref.sha256, ref.path);
if (index) {
const rows = execFileSync('git', ['ls-files', '--stage', '-z', '--', 'docs/task-agreement-1.25-evidence'], { encoding: 'utf8', maxBuffer: 8 * 1024 * 1024 }).split('\0').filter(Boolean);
const blobs = new Map(rows.map(row => { const [meta, file] = row.split('\t'); return [file, meta.split(' ')[1]]; }));
for (const name of [...names, 'manifest.json']) {
const data = bytes(name), oid = blob(data);
assert.equal(blobs.get('docs/task-agreement-1.25-evidence/' + name), oid, name + ' indexed bytes');
}
const supplementalRows=execFileSync('git',['ls-files','--stage','-z','--','docs/task-agreement-1.25-supplement'],{encoding:'utf8',maxBuffer:1024*1024}).split('\0').filter(Boolean), supplementalBlobs=new Map(supplementalRows.map(row=>{const[meta,file]=row.split('\t');return[file,meta.split(' ')[1]];}));
for(const name of [...supplementaryNames,'manifest.json']){const data=fs.readFileSync(path.join(supplementRoot,name));assert.equal(supplementalBlobs.get('docs/task-agreement-1.25-supplement/'+name),blob(data),name+' supplemental indexed bytes');}
}
console.log(JSON.stringify({ success: true, files: names.size, bytes: total, sourceCommit: freeze.sourceCommit, inputs: freeze.inputs.length, compiled: freeze.compiled.length,
independentInputs: build.inputCount, rawArtifacts: rawArtifacts.length, uiRuns, originalArtifacts, originalSnapshots, isolatedInstallGroups: 12, supplementFiles:supplementaryNames.size,supplementBytes:supplementaryBytes,realModelRequests: 0, indexChecked: index }));