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/K2030-percpu-irq-staging-local-')),source=join(directory,'source'),artifact=path=>({path:relative(root,path),sha256:hashAt(root,relative(root,path))});const report={schema:'turkos-k2030-percpu-irq-preparation/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources:[],commands:[],frozenBefore:frozenSnapshot(root)};for(const file of['os-percpu-irq/Cargo.toml','os-percpu-irq/Cargo.lock','os-percpu-irq/src/lib.rs','os-percpu-irq/src/irq.rs','kernel/tests/os/percpu_irq.rs','kernel/PERCPU_IRQ_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-percpu-irq/Cargo.toml');const tests=run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','percpu_irq'],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','false',1],['register','h.vbar!=c.vector || ','',1],['payload','if bytes!=self.descriptor(cpu).bytes()','if false',2],['live-close','if self.records[..self.state.count as usize].iter().any(|r| r.phase==2 || r.phase==3)','if false',1],['frame','h.irq_sp!=c.irq+4096-16-FRAME_BYTES || ','',1],['overlap','if cpus[..i].iter().any(|old| old.affinity==c.affinity || old.redistributor==c.redistributor || regions(*old).iter().any(|a| current.iter().any(|b| overlap(*a,*b))))','if false',1]]){const mutant=join(directory,'mutant-'+name);cpSync(source,mutant,{recursive:true});const path=join(mutant,'os-percpu-irq/src/irq.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-percpu-irq/Cargo.toml'),'--locked','--offline','--test','percpu_irq'],101);assert.match(red,/os_percpu_irq_(negative|positive|boundaries) \.\.\. FAILED/);} report.status='STAGING_LOCAL_PASSED';report.tests=3;report.mutationsRejected=7;report.scope='Private per-CPU vector and IRQ/execution stack ownership; hardware register restoration, exact exception-frame position, canaries, interrupt completion, full descriptor identity and delayed reclamation. Actual four-CPU guest IRQ 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/percpu-irq-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}));