import assert from'node:assert/strict';import{mkdtempSync,writeFileSync,copyFileSync}from'node:fs'; import{read,artifact,physicalCase}from'./smp-frame-service-artifacts.mjs';import{inspectFrameServiceEvidence,inspectFrameServiceSnapshots,inspectFrameServiceInputs,frameServicePhysicalPlan}from'./smp-frame-service-proof.mjs';import{inspectFrameServiceModel,frameStates}from'./smp-frame-service-model-proof.mjs';import{frameServiceInventory}from'./smp-frame-service-ledger-proof.mjs';import{frameAtomicSites}from'./smp-frame-service-inputs-proof.mjs';import{inspectPhysicalCheckpoint}from'./physical-checkpoint-proof.mjs'; assert.equal(process.argv.length,4);const[candidatePath,diagnosticPath]=process.argv.slice(2),directory=mkdtempSync('reports/os-implementation/K2075-smp-frame-service-proof-tests-');copyFileSync('scripts/os-acceptance/test-smp-frame-service-proof.mjs',directory+'/harness.mjs.txt');const report={schema:'turkos-k2075-smp-frame-service-proof-tests/v1',status:'RUNNING',candidate:artifact(candidatePath),diagnostic:artifact(diagnosticPath),harness:artifact(directory+'/harness.mjs.txt'),positive:0,forgedControlsRejected:0,groups:{},guestAccepted:false,productAccepted:false}; try{ const input=physicalCase(read(candidatePath),read(diagnosticPath)),load=c=>Object.fromEntries(Object.entries(c.dumps).map(([n,a])=>[n,input.readArtifact(a)])),base=input.captures.map(load),migration=input.migration.map(load),affinity=input.affinity.map(load),tlb=input.tlb.map(load),asid=input.asid.map(load),snapshots=input.frameService.map(load),u=(b,p)=>b.readBigUInt64LE(p); const inspect=()=>inspectFrameServiceSnapshots(base,migration,affinity,tlb,asid,snapshots,input.memoryMiB,input.bootId,input.symbols,input.compiled,input.bootDtb,4);report.positiveEvidence=inspectFrameServiceEvidence(input);report.positive=1;const inventory=frameServiceInventory(base,snapshots,input.compiled); const reject=(name,run)=>{assert.throws(run,undefined,name);report.forgedControlsRejected++;report.groups[name.split('/')[0]]=(report.groups[name.split('/')[0]]??0)+1;}; function flip(stage,name,offset,coherent=false){const old=snapshots[stage],bytes=Buffer.from(old[name]);if((name==='OS_K2075_GRAPH'&&offset%128===72||name==='OS_K2075_SERVICE'&&[64,72].includes(offset%128))&&u(bytes,offset)!==0n)bytes.writeBigUInt64LE(0n,offset);else bytes[offset]^=1;const changed={...old,[name]:bytes};let run=inspect;if(coherent){if(name.startsWith('clientPort')){const cpu=Number(name.at(-1)),executive=Buffer.from(old['executive'+cpu]),address=u(old.OS_K2075_GRAPH,cpu*128),start=u(old.OS_K2045_EVIDENCE,(33+cpu)*8);bytes.copy(executive,Number(address-start));changed['executive'+cpu]=executive;}else{const primary=Buffer.from(old.OS_K2045_PRIMARY_STACK),field=name==='service'?80:96,at=Number(u(old.OS_K2075_EVIDENCE,field))-input.symbols.OS_K2045_PRIMARY_STACK.address;bytes.copy(primary,at);changed.OS_K2045_PRIMARY_STACK=primary;}run=()=>inspectFrameServiceModel(changed,stage,input.symbols,base[3],inventory);}snapshots[stage]=changed;try{reject((coherent?'private-model':'snapshot')+'/'+stage+'/'+name+'/'+offset,run)}finally{snapshots[stage]=old;}} for(let stage=0;stage<12;stage++){ for(const name of['OS_K2075_EVIDENCE','OS_K2075_ACKS','OS_K2075_GRAPH','OS_K2075_USE','OS_K2075_RELEASE','OS_K2075_SERVICE','OS_K2075_PAGES','OS_K2075_FAILED_REQUESTS','OS_K2075_FAILED_REPLIES','authority'])for(let at=0;at=2)for(let at=0;at<4096;at+=8)flip(stage,'framePage'+cpu,at); for(const name of['mailbox'+cpu,'mailboxLease'+cpu,'irqStack'+cpu,'irqGuard'+cpu,...Array.from({length:5},(_,i)=>'irqLease'+cpu+'-'+i),'local'+cpu])for(const at of[0,Math.floor(snapshots[stage][name].length/16)*8,snapshots[stage][name].length-8])flip(stage,name,at); } for(let at=0;at<48;at+=8)flip(stage,'serverPorts',at,true); if(stage!==11)for(const at of[...Array.from({length:31},(_,i)=>i*8),344])flip(stage,'service',at,true); for(const name of['lifecycle','shared','kernelRoot','kernelL2','deviceL2','gicL3',...Object.keys(snapshots[stage]).filter(n=>n.startsWith('kernelL3-')||n.startsWith('thread'))])for(const at of[0,Math.floor(snapshots[stage][name].length/16)*8,snapshots[stage][name].length-8])flip(stage,name,at); } for(let stage=4;stage<9;stage++)for(const at of[inventory.layout[17],...inventory.slots.flatMap(slot=>[0,8,16].map(i=>inventory.layout[5]+slot*24+i)),inventory.layout[1]]){const old=base[stage],authority=Buffer.from(old.authority);authority[at]^=1;base[stage]={...old,authority};try{reject('parent-history/'+stage+'/'+at,inspect)}finally{base[stage]=old;}} for(const name of['OS_K2075_MODEL_LAYOUT','OS_K2075_BACKEND_LAYOUT','OS_K2075_MAILBOX_LAYOUT','OS_K2075_SLOT_LAYOUT'])for(let at=0;atinspectFrameServiceInputs(bad,input.symbols));} for(const[name,sites]of Object.entries(frameAtomicSites))for(const[at]of sites)for(let i=0;i<4;i++){const bad={...input.compiled,[name]:Buffer.from(input.compiled[name])};bad[name][at+i]^=1;reject('compiled/'+name+'/'+(at+i),()=>inspectFrameServiceInputs(bad,input.symbols));} for(const[name,at]of[['frameCycle',1996],['frameCycle',2348],['frameServe',88],['frameServe',1456],['frameReceive',36]])for(let i=0;i<4;i++){const bad={...input.compiled,[name]:Buffer.from(input.compiled[name])};bad[name][at+i]^=1;reject('compiled/flag-address/'+name+'/'+(at+i),()=>inspectFrameServiceInputs(bad,input.symbols));} for(let i=0;i<12;i++){const capture=input.frameService[i],data=snapshots[i],stage=20810+i,args={capture,expected:frameServicePhysicalPlan(data,input.symbols),gateAddress:input.symbols.OS_K2075_CAPTURE_GATE.address,stage,qmp:input.qmp,timeline:input.timeline,wholeRsp:input.wholeRsp,readArtifact:input.readArtifact};for(const field of['val','size']){const bad=structuredClone(capture);bad.captures[0][field]++;reject('transport/'+i+'/'+field,()=>inspectPhysicalCheckpoint({...args,capture:bad}));}const gate=structuredClone(capture);gate.rsp.find(r=>r.direction==='tx'&&r.payload.startsWith('M')).payload+='00';reject('transport/'+i+'/gate',()=>inspectPhysicalCheckpoint({...args,capture:gate}));const step=input.timeline.find(r=>r.stage===stage&&r.execute==='query-status'),rx=input.qmp.find(r=>r.direction==='rx'&&r.message.id===step.id),old=rx.message.return.status;rx.message.return.status='running';try{reject('transport/'+i+'/paused',()=>inspectPhysicalCheckpoint(args));}finally{rx.message.return.status=old;}} report.status='SMP_FRAME_SERVICE_PROOF_TESTS_PASSED'; }catch(error){report.status='SMP_FRAME_SERVICE_PROOF_TESTS_FAILED';report.error=error.stack?.slice(0,6000);process.exitCode=1;} writeFileSync(directory+'/report.json',JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:directory+'/report.json',positive:report.positive,forgedControlsRejected:report.forgedControlsRejected,groups:report.groups,error:report.error}));