Files
Agent-Workbench/docs/model-protocols-f-release-evidence/071-protocol-f-first-seal-script.mjs
T
2026-11-01 00:19:52 +08:00

112 lines
13 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { publicSnapshot, compareOriginalState } from './protocol-f-state.mjs';
const read=file=>JSON.parse(fs.readFileSync(file,'utf8'));
const hash=bytes=>createHash('sha256').update(bytes).digest('hex');
const baseline=read('docs/model-protocols-release-code-baseline.json'), release=read('releases/latest.json');
assert.equal(baseline.version,'1.17.0'); assert.equal(release.installer.version,'1.17.0');
const phases=['archives','source','portable','portable-companion','portable-protocols','prepare-install','install','formal-stage','formal-upgrade','formal-activate','formal-ui','formal-verify','formal-artifacts'];
const receipts=phases.map(phase=>{const file='.qa/protocol-f-'+phase+'-result.json',value=read(file);assert.equal(value.success,true,phase);assert.equal(value.unchangedInputs,true);assert.equal(value.release.setupSha256,release.installer.sha256);return {file,value};});
const source=read('.qa/protocol-f-source-original-validation.json'), portable=read('.qa/protocol-f-portable-original-validation.json');
assert.equal(source.success,true); assert.equal(source.tests.tests,558); assert.equal(source.tests.pass,558);
for(const key of ['fail','cancelled','skipped','todo'])assert.equal(source.tests[key],0);
const sorted=rows=>rows.slice().sort((a,b)=>a.path.localeCompare(b.path));
assert.deepEqual(sorted(source.inputs),sorted(baseline.inputs));
assert.deepEqual(sorted(source.buildFiles),sorted(baseline.builds.map(({path,sha256})=>({path,sha256}))));
assert.equal(source.actualDesktop.window.results.length,7); assert.equal(source.actualDesktop.ui.assertions.length,9); assert.equal(source.actualDesktop.projects.cases.length,16);
assert.deepEqual(source.actualDesktop.projects.results.map(x=>x.parentCheck.results[0].tests.passed),[8,9,10]);
assert.equal(source.actualCompanion.passed.length,7); assert.equal(source.actualProtocols.passed.length,5);
assert.equal(source.actualProtocolEngineering.success,true); assert.equal(source.actualProtocolEngineering.cases.length,13);
assert.deepEqual(source.actualProtocolEngineering.results.map(x=>x.parentCheck.results[0].tests.passed),[8,9,10]);
assert.equal(portable.appRoot,release.portableRoot); assert.equal(portable.actualControlled.success,true);
const ui=read('docs/model-protocols-all-ui-validation.json'); assert.equal(ui.success,true); assert.equal(ui.results.length,25); assert.equal(ui.groups,191);
const installLocation=read('.qa/protocol-f-install-location.json'), install=read(installLocation.result);
assert.equal(install.success,true); assert.equal(install.cases.length,7); assert.equal(install.installer.productSetup.sha256,release.installer.sha256);
assert.equal(install.installer.samePayloadManifestSha256,hash(fs.readFileSync(path.join(release.portableRoot,'MANIFEST.json'))));
const formal=read('.qa/protocol-f-formal-verify.json'), before=read('.qa/protocol-f-formal-before.json');
assert.equal(formal.success,true); assert.equal(formal.identity.pid,36552); assert.equal(formal.currentProviderPreserved,true); assert.equal(formal.inspectorClosed,true);
const current=await publicSnapshot('http://127.0.0.1:4318',formal.dataRoot),retained=compareOriginalState(before,current);
assert.equal(current.version,'1.17.0'); assert.equal(current.runtimeRoot,formal.candidate);
const smoke=read('.qa/protocol-f-formal-ui-validation.json'), artifacts=read('.qa/protocol-f-formal-artifacts.json'), native=read('.qa/protocol-f-formal-native.json');
assert.equal(smoke.success,true); assert.equal(smoke.results.length,20); assert.equal(artifacts.success,true); assert.equal(artifacts.checked,artifacts.total);
assert.equal(native.success,true); assert.equal(native.version,'1.17.0'); assert.equal(native.hostPid,36552);
for(const key of ['pageErrors','requestFailures','httpErrors','blockedWrites'])assert.deepEqual(smoke[key],[]);
for(const item of [...baseline.inputs,...baseline.builds,...baseline.benchmarks]){const bytes=fs.readFileSync(item.path);assert.equal(bytes.length,item.bytes);assert.equal(hash(bytes),item.sha256,item.path);}
for(const item of [release.portable,release.source,release.installer])assert.equal(hash(fs.readFileSync(item.path)),item.sha256);
const gitAudit=read('.qa/protocol-f-code-git-audit.json');assert.equal(gitAudit.success,true);assert.equal(gitAudit.codeCommit,baseline.codeCommit);assert.equal(gitAudit.inputFiles,baseline.inputs.length);
const license=read('.qa/protocol-f-license-audit.json');assert.equal(license.success,true);
const completed=read('.qa/protocol-f-log-completions/index.json');assert.equal(completed.success,true);
const directory='docs/model-protocols-f-release-evidence';assert.equal(fs.existsSync(directory),false);
const queue=[],seen=new Map();
function bytesCopy(original,bytes,expected={}){
const absolute=path.resolve(original),exists=seen.get(absolute);if(exists){if(expected.sha256)assert.equal(exists.sha256,expected.sha256);return exists;}
const sha256=hash(bytes);if(expected.sha256)assert.equal(sha256,expected.sha256,original);if(expected.bytes!==undefined)assert.equal(bytes.length,expected.bytes,original);
const name=String(queue.length+1).padStart(3,'0')+'-'+path.basename(original).replace(/\.log$/,'-log.txt');
const row={original:absolute,archived:directory+'/'+name,bytes:bytes.length,sha256};queue.push({row,bytes});seen.set(absolute,row);return row;
}
function copy(file,expected={}){
assert.doesNotMatch(file,/(?:\.sqlite(?:-(?:wal|shm))?|\.(?:db|key|pem|pfx|p12))$/i,'No private database or certificate material');
const stat=fs.lstatSync(file);assert.equal(stat.isSymbolicLink(),false);assert.equal(stat.isFile(),true);return bytesCopy(file,fs.readFileSync(file),expected);
}
function installerLog(file,sha256){
const bytes=fs.readFileSync(file);if(hash(bytes)===sha256)return copy(file,{sha256});
const item=completed.completions.find(x=>x.source===path.resolve(file));assert.ok(item);assert.equal(item.original.sha256,sha256);
copy(item.original.path,item.original);return copy(file,item.completed);
}
function surface(root){if(!root||!fs.existsSync(root))return;for(const e of fs.readdirSync(root,{withFileTypes:true}))if(e.isFile()&&/\.(?:json|png|jpg|html|txt|log|tap|mjs|cjs|sse)$/.test(e.name))copy(path.join(root,e.name));}
function screenshots(value){
if(!value||typeof value!=='object')return;if(Array.isArray(value)){for(const x of value)screenshots(x);return;}
if(typeof value.file==='string'&&/\.(png|jpg)$/.test(value.file))copy(value.file,value.sha256?{sha256:value.sha256}:{});
for(const [key,x]of Object.entries(value)){
if(typeof x==='string'&&/screenshot$/i.test(key)&&/\.(png|jpg)$/.test(x))copy(x,value.screenshotSha256?{sha256:value.screenshotSha256}:{});
if(key==='screenshots'&&Array.isArray(x))for(const file of x)if(typeof file==='string')copy(file);
if(x&&typeof x==='object')screenshots(x);
}
}
function projectTree(root){if(!fs.existsSync(root))return;for(const e of fs.readdirSync(root,{withFileTypes:true})){if(['.git','node_modules','__pycache__','.pytest_cache','.venv'].includes(e.name))continue;const f=path.join(root,e.name);if(e.isDirectory())projectTree(f);else copy(f);}}
for(const {file,value}of receipts){copy(file);copy(value.logPath,{bytes:value.logBytes,sha256:value.logSha256});}
for(const name of fs.readdirSync('.qa'))if(/^(?:protocol-.*\.(?:json|log|tap|mjs)|(?:prepare|freeze|package|run|shorten|deploy|verify|audit|seal|record|write|finalize)-protocol-.*\.mjs)$/.test(name))copy(path.join('.qa',name));
for(const name of ['docs/model-protocols-release-code-baseline.json','docs/model-protocols-all-ui-validation.json','releases/latest.json'])copy(name);
for(const name of fs.readdirSync('docs'))if(/^model-protocols-.*\.(?:json|txt)$/.test(name))copy(path.join('docs',name));
for(const name of fs.readdirSync('.qa'))if(/^model-protocols-.*\.(?:json|log)$/.test(name))copy(path.join('.qa',name));
for(const row of source.logs)copy(row.path,row);copy(path.join(source.directory,'inputs.json'));copy(path.join(source.directory,'result.json'));
for(const row of source.logs.filter(x=>['contained companion gateway and UI','contained model protocols and mobile streams'].includes(x.stage))){const root=fs.readFileSync(row.path,'utf8').match(/EVIDENCE ([^\r\n]+)/)?.[1];assert.ok(root);surface(root);}
for(const row of install.cases){if(row.processLog)copy(row.processLog,{sha256:row.processLogSha256});if(row.setupLog)installerLog(row.setupLog,row.setupLogSha256);if(row.uninstallLog)installerLog(row.uninstallLog,row.uninstallLogSha256);}
copy(installLocation.result);copy(release.installer.source);copy(path.join(path.dirname(release.installer.source),'compile.txt'));copy(install.installer.source);copy(path.join(path.dirname(install.installer.source),'compile.txt'));
copy('.qa/protocol-f-formal-setup.log',{sha256:read('.qa/protocol-f-formal-stage.json').setupLogSha256});
surface('.qa/protocol-native-before');surface('.qa/protocol-native-after');surface('.qa/protocol-first-ui-environment-failure');surface('.qa/protocol-f-log-completions');
for(const name of fs.readdirSync('.qa'))if(name.startsWith('model-protocols-ui-'))surface(path.join('.qa',name));
surface(source.actualDesktop.window.root);projectTree(path.join(source.actualDesktop.window.root,'owned-project'));
for(const name of ['desktop-controlled-ui','desktop-projects','responses-engineering']){
const file=path.join(source.sourceRoot,'.qa',name+'-location.json'),location=read(file);copy(file);copy(location.result);surface(location.directory);
if(name==='desktop-controlled-ui')projectTree(path.join(location.directory,'original-fixture','project'));
else for(const kind of ['node','react','python']){const root=path.join(location.directory,kind);surface(root);projectTree(path.join(root,'original-fixture','project'));}
}
for(const name of ['.qa/controlled-projects-cf832704-bb40-4563-b086-23b93ec6aeb8','.qa/controlled-projects-c0121328-f10f-4c33-8960-e4c1db7df1f7']){
surface(name);for(const kind of ['node','react','python']){surface(path.join(name,kind));projectTree(path.join(name,kind,'original-fixture','project'));}
}
for(const value of [source.actualDesktop,source.actualCompanion,source.actualProtocols,smoke,native])screenshots(value);
for(const phase of ['source','portable-companion','portable-protocols']){
const log=phase==='source'?null:fs.readFileSync('.qa/protocol-f-'+phase+'.log','utf8');
if(log){const root=log.match(/EVIDENCE ([^\r\n]+)/)?.[1];assert.ok(root);surface(root);}
}
for(const value of [source.actualCompanion,source.actualProtocols])surface(value.directory);
const manifest={success:true,version:'1.17.0',codeCommit:baseline.codeCommit,sealedAt:new Date().toISOString(),files:queue.map(x=>x.row),
bytes:queue.reduce((sum,x)=>sum+x.bytes.length,0),sourceTests:source.tests,ui:{classes:25,groups:191},scope:'Original bytes of release and original failure evidence. Formal snapshots are public hashes only; no private SQLite, model Key, certificate, or QA device identity is sealed.'};
fs.mkdirSync(directory);fs.writeFileSync(directory+'/.gitattributes','* -text -whitespace\n',{flag:'wx'});
for(const {row,bytes}of queue)fs.writeFileSync(row.archived,bytes,{flag:'wx'});
fs.writeFileSync(directory+'/manifest.json',JSON.stringify(manifest,null,2)+'\n',{flag:'wx'});
const identity={success:true,version:'1.17.0',codeCommit:baseline.codeCommit,immutableRelease:path.basename(release.portableRoot),release,
frozen:{inputs:baseline.inputs.length,builds:baseline.builds.length,benchmarks:baseline.benchmarks.length,gitAudit},
independentSource:{sourceRoot:source.sourceRoot,tests:source.tests,inputs:source.inputFiles,buildMatchedFiles:source.buildMatchedFiles,
desktopWindow:7,desktopUi:9,desktopEngineering:16,responsesEngineering:13,parentTests:[8,9,10],containedCompanion:7,containedProtocols:5},
ui:{classes:25,groups:191,formalPanels:20},samePayloadInstall:{cases:7,setupSha256:install.installer.sha256,productSetupSha256:release.installer.sha256},
formal:{pid:formal.identity.pid,runtimeRoot:formal.candidate,dataRoot:formal.dataRoot,counts:formal.counts,artifactRecords:artifacts.artifactRecords,blobFiles:artifacts.blobFiles,
currentProviderPreserved:true,retained,inspectorClosed:true,companion:formal.companion,native},
evidence:{manifest:directory+'/manifest.json',files:queue.length,bytes:manifest.bytes},realModelRequests:0,
limitations:['Real configured-model engineering and Purge benefit remain separately deferred.','No new actual iPhone 1.17 stream acceptance; paired Edge/TLS viewports are separate from 1.16 user-confirmed iPhone evidence.','Vision/audio are capability declarations only.','Unknown WorkBuddy formats and model-independent computer control remain planned.'],sealedAt:manifest.sealedAt};
fs.writeFileSync('docs/model-protocols-release-identity.json',JSON.stringify(identity,null,2)+'\n',{flag:'wx'});
console.log(JSON.stringify({success:true,files:queue.length,bytes:manifest.bytes,identity:'docs/model-protocols-release-identity.json'}));