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/K2010-psci-call-staging-local-')),source=join(directory,'source'),artifact=path=>({path:relative(root,path),sha256:hashAt(root,relative(root,path))});const report={schema:'turkos-k2010-psci-call-preparation/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources:[],commands:[],frozenBefore:frozenSnapshot(root)};for(const file of['os-psci-call/Cargo.toml','os-psci-call/Cargo.lock','os-psci-call/src/lib.rs','os-psci-call/src/call.rs','kernel/tests/os/psci_call.rs','kernel/PSCI_CALL_CONTRACT.md','os-runtime-platform/tests/fixtures/qemu-virt-512.dtb']){const out=join(source,file);mkdirSync(dirname(out),{recursive:true});copyFileSync(file,out);report.sources.push(artifact(out));}for(const file of ['Cargo.toml','src/lib.rs','src/platform.rs','src/ram.rs','src/features.rs']){const from='os-runtime-platform/'+file,to=join(source,from);mkdirSync(dirname(to),{recursive:true});copyFileSync(from,to);report.sources.push(artifact(to));}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-psci-call/Cargo.toml');const tests=run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','psci_call'],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','request.issuer!=self.state.issuer||','',1],['generation','request.generation!=self.state.generation||','',1],['entry','if entry%4!=0||entry=self.entry_end','if false',1]]){const mutant=join(directory,'mutant-'+name);cpSync(source,mutant,{recursive:true});const path=join(mutant,'os-psci-call/src/call.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-psci-call/Cargo.toml'),'--locked','--offline','--test','psci_call'],101);assert.match(red,/os_psci_call_(negative|positive) \.\.\. FAILED/);} report.status='STAGING_LOCAL_PASSED';report.tests=3;report.mutationsRejected=3;report.scope='A kernel-owned PSCI request gate binds issuer, generation, accepted target, physical entry, context and conduit. Host tests verify exact arguments, signed low-32-bit result handling, busy/foreign/stale/closed refusals, abort and DTB conduit discovery. Three mutants are rejected; actual secondary execution 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/psci-call-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}));