Files
Agent-Workbench/scripts/verify-live-delivery-v.mjs

56 lines
8.4 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 root=path.resolve('docs/live-delivery-v-evidence');
const sha=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=>sha(JSON.stringify(canonical(value)));
const file=name=>{assert.equal(typeof name,'string');assert.ok(!/[\\\x00\r\n]/.test(name)&&!name.split('/').includes('..'));const target=path.resolve(root,name);assert.ok(target.startsWith(root+path.sep));return target;};
const bytes=name=>fs.readFileSync(file(name)),read=name=>JSON.parse(bytes(name));
const manifest=read('manifest.json');assert.equal(manifest.kind,'owned-live-delivery-v-evidence');
const names=new Set();let totalBytes=0;
for(const row of manifest.files){assert.ok(!names.has(row.path));names.add(row.path);const value=bytes(row.path);assert.equal(value.length,row.bytes);assert.equal(sha(value),row.sha256,row.path);totalBytes+=value.length;}
const list=(directory=root)=>fs.readdirSync(directory,{withFileTypes:true}).flatMap(entry=>{assert.equal(entry.isSymbolicLink(),false);const target=path.join(directory,entry.name);return entry.isDirectory()?list(target):[path.relative(root,target).replaceAll('\\','/')];});
assert.deepEqual(list().sort(),['manifest.json',...names].sort());
assert.ok([...names].every(name=>!name.startsWith('run/preflight/')&&!name.startsWith('run/node/')));
const owned=name=>{assert.ok(names.has(name));return read(name);};
const initial=owned(manifest.references.initial),network=owned(manifest.references.network),audit=owned(manifest.references.networkAudit),final=owned(manifest.references.final),proof=owned(manifest.references.ownership),readback=owned(manifest.references.readback),frozen=owned('contract/manifest.json');
const gitBytes=(commit,relative)=>execFileSync('git',['show',commit+':'+relative],{maxBuffer:4*1024*1024,windowsHide:true});
assert.equal(sha(bytes('run/current.json')),frozen.originalBaselineSha256);assert.equal(sha(bytes('provenance/frozen-plan.md')),frozen.planSha256);
assert.deepEqual(bytes('contract/manifest.json'),gitBytes(manifest.experimentPlanCommit,'docs/live-delivery-v-contract/manifest.json'));
for(const row of frozen.files){assert.equal(sha(bytes('contract/'+row.path)),row.sha256);assert.deepEqual(bytes('contract/'+row.path),gitBytes(manifest.experimentPlanCommit,'docs/live-delivery-v-contract/'+row.path));}
for(const row of frozen.inputs){assert.equal(sha(bytes('contract/'+row.capturedPath)),row.sha256);assert.equal(row.sha256,frozen.originalElevenFileHashes[row.path]);}
for(const row of frozen.dependencySources)assert.equal(sha(gitBytes(manifest.experimentBaseCommit,row.path)),row.sha256);
assert.equal(sha(bytes('provenance/release-1.24.8.json')),frozen.releaseIdentitySha256);assert.equal(owned('provenance/release-1.24.8.json').sourceCommit,manifest.releaseSourceCommit);
assert.equal(frozen.modelRequestsAtFreeze,0);assert.equal(frozen.maxNetworkJobs,1);assert.equal(frozen.maxPreflightRequests,1);assert.equal(frozen.maxNodeRequests,8);assert.equal(frozen.maxNodeTokens,90000);assert.equal(frozen.maxNodeDurationMs,600000);assert.equal(frozen.contextLimit,16000);assert.equal(frozen.maxOutputTokens,2048);assert.equal(frozen.safetyReserveTokens,1600);assert.equal(frozen.goalMatchesTExactly,true);
assert.equal(network.status,'partial');assert.equal(network.connectionRevision,9);assert.equal(network.stages.dns.status,'passed');assert.equal(network.httpRequests,0);assert.equal(network.modelRequests,0);assert.equal(network.credentialAccess,false);assert.equal(network.route.modelApiRouteVerified,false);
assert.deepEqual(network.trials.map(row=>[row.mode,row.status]),[['auto','passed'],['ipv4','failed'],['ipv6','passed']]);assert.equal(network.trials[1].code,'TLS_HANDSHAKE_TIMEOUT');
assert.equal(audit.passed,true);assert.equal(audit.readOnlyReconciliation,true);assert.equal(audit.firstDriverFailurePreserved,true);assert.equal(owned('run/network/driver-error.json').jobId,network.id);
const previous=owned('run/network/history-before.json'),next=owned('run/network/history-after.json');
const strip=({stale,currentConnectionRevision,...raw})=>raw;
const oldRaw=previous.jobs.map(strip).reverse(),newRaw=next.jobs.map(strip).reverse();
assert.equal(oldRaw.length,2);assert.deepEqual(newRaw,[...oldRaw,strip(network)]);assert.equal(next.active,0);
const snapshot=owned('run/final/snapshot.json'),before=initial.baseline;
for(const field of ['pid','version','runtimeRoot','configSha256','projectsSha256','sessionsSha256','eventCursor','modelCenterSha256'])assert.deepEqual(snapshot[field],before[field],field);
assert.deepEqual(snapshot.exports,before.exports);assert.deepEqual(snapshot.modelCenter,before.modelCenter);
assert.deepEqual(snapshot.persistent.artifacts,before.persistent.artifacts);assert.deepEqual(snapshot.persistent.files,before.persistent.files);
for(const [name,table] of Object.entries(before.database.tables)){
assert.equal(snapshot.database.tables[name].count,table.count);assert.equal(snapshot.database.tables[name].schemaSha256,table.schemaSha256);
const without=rows=>rows.filter(row=>name!=='metadata'||row.id!=='modelNetworkDiagnostics');assert.deepEqual(without(snapshot.database.tables[name].rows),without(table.rows));
}
for(const [snapshotValue,raw]of[[before,oldRaw],[snapshot,newRaw]])assert.equal(snapshotValue.database.tables.metadata.rows.find(row=>row.id==='modelNetworkDiagnostics').sha256,digest({key:'modelNetworkDiagnostics',value:JSON.stringify(raw)}));
const attempts=owned('run/initial-attempts.json');assert.equal(attempts.length,225);assert.equal(snapshot.database.tables.metadata.rows.find(row=>row.id==='modelAttempts').sha256,digest({key:'modelAttempts',value:JSON.stringify(attempts)}));
for(const rows of Object.values(proof.additions))assert.deepEqual(rows,[]);assert.equal(proof.projectInsertionOffset,0);assert.ok(proof.projects.every(row=>row.actualSha256===row.originalSha256&&row.normalizedSha256===row.originalSha256));
assert.equal(final.passed,true);assert.equal(final.pid,36552);assert.equal(final.version,'1.24.8');assert.equal(final.modelRequests,0);assert.equal(final.reportedTokens,0);assert.equal(final.unknownRequests,0);assert.equal(final.originalProjects,65);assert.equal(final.originalSessions,81);assert.equal(final.originalArtifacts,3720);assert.equal(final.originalAttempts,225);assert.equal(final.nodeDelivered,false);assert.equal(final.purgeBenefitTested,false);assert.equal(final.productSourceChanged,false);assert.equal(final.keyReadOrResaved,false);assert.deepEqual(final.connectionRevision,[9,9]);assert.deepEqual(final.legacyDisplayRevisionFollowsGlobalConfigurationRevision,[73,73]);assert.equal(final.credentialRevision,1);
assert.equal(readback.originalIdentityExact,true);assert.equal(readback.inspectorClosed,true);assert.equal(readback.modelCenterExactSinceRestoration,true);
let gitIndex;
if(process.argv.includes('--index')){
const prefix=path.relative(process.cwd(),root).replaceAll('\\','/'),manifestBytes=bytes('manifest.json'),entries=[...manifest.files,{path:'manifest.json',bytes:manifestBytes.length,sha256:sha(manifestBytes)}];
const output=execFileSync('git',['cat-file','--batch'],{input:entries.map(row=>':'+prefix+'/'+row.path).join('\n')+'\n',maxBuffer:256*1024*1024,windowsHide:true});let offset=0,indexBytes=0;
for(const entry of entries){const end=output.indexOf(10,offset),header=output.subarray(offset,end).toString('utf8'),match=header.match(/^[a-f0-9]{40} blob (\d+)$/);assert.ok(match,header);const size=Number(match[1]);offset=end+1;const value=output.subarray(offset,offset+size);offset+=size;assert.equal(output[offset++],10);assert.equal(size,entry.bytes);assert.equal(sha(value),entry.sha256,entry.path);indexBytes+=size;}
assert.equal(offset,output.length);gitIndex={passed:true,files:entries.length,bytes:indexBytes,exactOriginalBytes:true};
}
console.log(JSON.stringify({success:true,files:manifest.files.length,bytes:totalBytes,manifestSha256:sha(bytes('manifest.json')),diagnosticStatus:'partial',stoppedBeforePreflight:true,modelRequests:0,nodeDelivered:false,purgeBenefitTested:false,allOriginalStateExceptSingleDiagnosticAppendExact:true,...(gitIndex?{gitIndex}:{}),scope:'Offline bytes, frozen contract/source and exact state digests. No network, model calls, credential access or host mutation.'}));