import assert from'node:assert/strict';import{readFileSync,writeFileSync,mkdtempSync,copyFileSync}from'node:fs';import{artifact,read}from'/scripts/os-acceptance/cpu-watchdog-artifacts.mjs'; const matrix='reports/os-implementation/K2100-service-el0-staging-WFQ0JV',dir=matrix+'/product-one-bank-direct-256-mode-210',runner='scripts/os-acceptance/.k2100-service-el0-staging-generated.mjs',d=mkdtempSync('reports/os-implementation/K2100-el0-flags-review-');const life=read(matrix+'/scoped-lifecycle.json');assert.equal(life.status,'RUNNER_FAILED');assert.equal(life.sourceInputsStable,true);assert.equal(life.allOwnedGuestsStopped,true);copyFileSync(runner,d+'/original-runner.mjs.txt');copyFileSync('/tmp/turkos-k2100-service-el0-smoke-child.log',d+'/original-child.log');copyFileSync('/tmp/turkos-k2100-service-el0-smoke.log',d+'/original-smoke.log'); const before='assert.equal(c[7],960);',after='assert.ok(Number.isSafeInteger(c[7])&&c[7]>=0&&c[7]<=0xffffffff);assert.equal(c[7]&0x0fffffff,960);';const source=readFileSync(runner,'utf8');assert.equal(source.split(before).length,2); const old=(assert,c)=>{assert.equal(c[7],960);};const fixed=new Function('assert','c',after);const bytes=readFileSync(dir+'/OS_K2100_EL0_CALLS.bin');assert.equal(bytes.length,640);const calls=Array.from({length:5},(_,i)=>Array.from({length:16},(_,j)=>Number(bytes.readBigUInt64LE(i*128+j*8))));let originalRefusals=0;for(const c of calls){try{old(assert,c);}catch{originalRefusals++;}fixed(assert,c);}assert.ok(originalRefusals>0);let rejected=0;for(let bit=0;bit<28;bit++){const c=[...calls[0]];c[7]=(960^(1<>>0;assert.throws(()=>fixed(assert,c));rejected++;}for(const invalid of [-1,0x1000003c0,Number.MAX_SAFE_INTEGER,960.5]){const c=[...calls[0]];c[7]=invalid;assert.throws(()=>fixed(assert,c));rejected++;}for(let flags=0;flags<16;flags++){const c=[...calls[0]];c[7]=flags*2**28+960;fixed(assert,c);}writeFileSync(runner,source.replace(before,after));copyFileSync(runner,d+'/corrected-runner.mjs.txt');copyFileSync('/tmp/turkos-k2100-el0-flags-review.mjs',d+'/harness.mjs.txt');writeFileSync(d+'/report.json',JSON.stringify({status:'EL0_NZCV_HOST_CHECK_CORRECTED_AND_TESTED',guestAccepted:false,productAccepted:false,scope:'SPSR NZCV flags are produced by actual userspace arithmetic. Check EL0t/DAIF and all other low 28 bits exactly; allow only four architectural NZCV bits. No native source or compiled artifact changed.',matrix,lifecycle:artifact(matrix+'/scoped-lifecycle.json'),capturedCalls:artifact(dir+'/OS_K2100_EL0_CALLS.bin'),originalRunner:artifact(d+'/original-runner.mjs.txt'),correctedRunner:artifact(d+'/corrected-runner.mjs.txt'),harness:artifact(d+'/harness.mjs.txt'),actualFlags:calls.map(c=>c[7]),originalFalseRefusals:originalRefusals,positiveFlagCombinations:16,invalidFlagsRejected:rejected},null,2)+'\n');console.log(d+'/report.json');