import assert from'node:assert/strict';import{copyFileSync,cpSync,mkdirSync,mkdtempSync,readFileSync,writeFileSync}from'node:fs';import{join,dirname,relative}from'node:path';import{spawnSync}from'node:child_process';import{hashAt,frozenSnapshot}from'./boot-contract-report.mjs'; const root=process.cwd(),directory=mkdtempSync(join(root,'reports/os-implementation/K2040-cpu-lifecycle-staging-local-')),source=join(directory,'source'),artifact=path=>({path:relative(root,path),sha256:hashAt(root,relative(root,path))});const report={schema:'turkos-k2040-cpu-lifecycle-preparation/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources:[],commands:[],frozenBefore:frozenSnapshot(root)};for(const file of['os-cpu-lifecycle/Cargo.toml','os-cpu-lifecycle/Cargo.lock','os-cpu-lifecycle/src/lib.rs','os-cpu-lifecycle/src/lifecycle.rs','kernel/tests/os/cpu_lifecycle.rs','kernel/CPU_LIFECYCLE_CONTRACT.md']){const out=join(source,file);mkdirSync(dirname(out),{recursive:true});copyFileSync(file,out);report.sources.push(artifact(out));}let env={...process.env,CARGO_TARGET_DIR:join(directory,'target')};delete env.RUSTFLAGS;delete env.CARGO_ENCODED_RUSTFLAGS;function run(name,args,expected){const r=spawnSync('cargo',args,{cwd:root,env,encoding:'utf8',timeout:600000,maxBuffer:16*1024*1024}),log=join(directory,name+'.log');writeFileSync(log,(r.stdout??'')+(r.stderr??''));report.commands.push({name,args,exitCode:r.status,log:artifact(log)});assert.ifError(r.error);assert.equal(r.signal,null);assert.equal(r.status,expected,name);return readFileSync(log,'utf8');} try{const manifest=join(source,'os-cpu-lifecycle/Cargo.toml');const tests=run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','cpu_lifecycle'],0);assert.match(tests,/3 passed; 0 failed/);run('target',['check','--manifest-path',manifest,'--locked','--offline','--lib','--target','aarch64-unknown-none'],0);report.mutants=[]; for(const[name,guard,replacement,occurrences]of [['issuer','issuer!=self.state.issuer||','',1],['generation','generation!=r.generation||','',1],['boot-cpu','if cpu as u64==self.state.boot{return Err(Error::BootCpu)}','',1],['online-firmware','||h.firmware!=0','',1],['owners','if h.live_tasks!=0||h.live_timers!=0||h.live_irqs!=0','if false',1],['timer-enabled','h.timer_control&1!=0||','',1],['offline-confirmation','if raw!=1{return Err(Error::Firmware)}let offline','let offline',1],['live-close','if self.records[..self.state.count as usize].iter().enumerate().any(|(i,r)|i as u64!=self.state.boot&&r.phase!=1)','if false',1]]){const mutant=join(directory,'mutant-'+name);cpSync(source,mutant,{recursive:true});const path=join(mutant,'os-cpu-lifecycle/src/lifecycle.rs'),text=readFileSync(path,'utf8');assert.equal(text.split(guard).length,occurrences+1);writeFileSync(path,text.replaceAll(guard,replacement));report.mutants.push(artifact(path));env={...env,CARGO_TARGET_DIR:join(directory,'mutant-'+name+'-target')};const red=run(name+'-tests',['test','--manifest-path',join(mutant,'os-cpu-lifecycle/Cargo.toml'),'--locked','--offline','--test','cpu_lifecycle'],101);assert.match(red,/os_cpu_lifecycle_(negative|positive|boundaries) \.\.\. FAILED/);} report.status='STAGING_LOCAL_PASSED';report.tests=3;report.mutationsRejected=8;report.scope='CPU lifecycle with boot CPU protection, firmware OFF before start, distinct starting and online acknowledgements, quarantined errors, private generations, reversible drain, zero live owners and genuine firmware OFF before reuse. Real CPU_ON/OFF guest evidence remains pending';}catch(error){report.status='STAGING_TEST_FAILED';report.error=error.stack;process.exitCode=1;}report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenBefore,report.frozenAfter);copyFileSync('scripts/os-acceptance/cpu-lifecycle-preparation.mjs',join(directory,'harness.mjs.txt'));report.harness=artifact(join(directory,'harness.mjs.txt'));const path=join(directory,'report.json');writeFileSync(path,JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:relative(root,path),tests:report.tests,mutationsRejected:report.mutationsRejected,error:report.error}));