import{snapshotImageTrustDependencies}from'./image-trust-dependencies.mjs'; import assert from 'node:assert/strict';import {copyFileSync,cpSync,mkdirSync,mkdtempSync,readFileSync,writeFileSync} from 'node:fs';import{createHash}from'node:crypto';import{spawnSync}from'node:child_process';import{join,dirname,relative}from'node:path';import{hashAt,frozenSnapshot}from'./boot-contract-report.mjs'; const root=process.cwd(),directory=mkdtempSync(join(root,'reports/os-implementation/K1985-threat-pipeline-staging-local-')),source=join(directory,'source'),artifact=path=>({path:relative(root,path),sha256:hashAt(root,relative(root,path))}); const report={schema:'turkos-k1985-threat-pipeline-preparation/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources:[],commands:[],frozenBefore:frozenSnapshot(root)}; for(const file of['os-threat-pipeline/Cargo.toml','os-threat-pipeline/Cargo.lock','os-threat-pipeline/src/lib.rs','os-threat-pipeline/src/pipeline.rs','os-image-manifest/src/manifest.rs','os-image-rollback/src/rollback.rs','os-key-rotation/src/rotation.rs','kernel/tests/os/threat_pipeline.rs','kernel/THREAT_PIPELINE_CONTRACT.md',...['threat-pipeline-rust-v1.elf','threat-pipeline-corpus-v1.bin','threat-pipeline-keys-v1.bin','threat-pipeline-damaged-v1.bin'].map(n=>'kernel/tests/os/fixtures/'+n)]){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}),output=(r.stdout??'')+(r.stderr??'');const path=join(directory,name+'.log');writeFileSync(path,output);report.commands.push({name,args,exitCode:r.status,log:artifact(path)});assert.ifError(r.error);assert.equal(r.signal,null);assert.equal(r.status,expected,name);return output;} try{ report.dependencySources=snapshotImageTrustDependencies(root,directory,join(source,'os-threat-pipeline/Cargo.lock')); const manifest=join(source,'os-threat-pipeline/Cargo.toml');const green=run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','threat_pipeline'],0);assert.match(green,/2 passed; 0 failed/);run('target-library-check',['check','--manifest-path',manifest,'--locked','--offline','--lib','--target','aarch64-unknown-none'],0);report.mutants=[]; for(const[name,guard,replacement]of[['revoked','if record.status==2','if false'],['version','||ticket.version!=self.state.version',''],['issuer','ticket.issuer!=self.state.issuer||','']]){ const mutant=join(directory,'mutant-'+name);cpSync(source,mutant,{recursive:true});const path=join(mutant,file),original=readFileSync(path,'utf8');assert.equal(original.split(guard).length,2);writeFileSync(path,original.replace(guard,replacement));report.mutants.push(artifact(path));env={...env,CARGO_TARGET_DIR:join(directory,'mutant-'+name+'-target')};const red=run(name+'-mutant-tests',['test','--manifest-path',join(mutant,'os-threat-pipeline/Cargo.toml'),'--locked','--offline','--test','threat_pipeline'],101);assert.match(red,/os_threat_pipeline_(negative|positive) \.\.\. FAILED/); } report.status='STAGING_LOCAL_PASSED';report.tests=2;report.mutationsRejected=3;report.scope='Integrated manifest, signing registry and volatile sequence policy; stale transactions abort after key changes. Host and no_std checks only; guest pending'; }catch(error){report.status='STAGING_TEST_FAILED';report.error=error.stack;process.exitCode=1;} report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenAfter,report.frozenBefore);report.harness=artifact(join(root,'scripts/os-acceptance/threat-pipeline-preparation.mjs'));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}));