import assert from 'node:assert/strict';import {readFileSync,writeFileSync,mkdirSync,mkdtempSync,cpSync,copyFileSync} from 'node:fs';import {resolve} from 'node:path';import {spawnSync} from 'node:child_process'; import {k2100BootOwnerSource} from '/scripts/os-acceptance/k2100-boot-owner-source.mjs'; import {artifact} from '/scripts/os-acceptance/cpu-watchdog-artifacts.mjs'; const d=mkdtempSync('reports/os-implementation/K2100-boot-owner-preparation-'),source=d+'/source';mkdirSync(source+'/src',{recursive:true});mkdirSync(source+'/tests'); const body=k2100BootOwnerSource(readFileSync('os-cpu-lifecycle/src/lifecycle.rs','utf8'))+'\n'+readFileSync('scripts/os-acceptance/k2100-boot-owner-tests.rs','utf8'); writeFileSync(source+'/src/lifecycle.rs',body);writeFileSync(source+'/src/lib.rs','#![no_std]\npub mod lifecycle;\n');copyFileSync('kernel/tests/os/cpu_lifecycle.rs',source+'/tests/cpu_lifecycle.rs'); writeFileSync(source+'/Cargo.toml','[package]\nname="turkos-os-cpu-lifecycle"\nversion="0.1.0"\nedition="2024"\n[workspace]\n'); for(const[f,name]of [['/tmp/turkos-k2100-boot-owner-preparation.mjs','harness.mjs.txt'],['scripts/os-acceptance/k2100-boot-owner.rs','boot-owner.rs'],['scripts/os-acceptance/k2100-boot-owner-source.mjs','transform.mjs.txt'],['scripts/os-acceptance/k2100-boot-owner-tests.rs','tests.rs.txt']])copyFileSync(f,d+'/'+name); const report={status:'RUNNING',guestAccepted:false,productAccepted:false,tests:0,newTests:7,sourceMutantsRejected:0,commands:[],scope:'Host tests of the actual CPU lifecycle model. A nonclone boot-CPU pin issues an odd, nonzero generation, blocks close while retained, returns custody on rejection and reserves a nonwrapping release generation. Hardware inputs are modeled; native profile integration and long-duration acceptance remain.'}; function run(id,manifest,args,expected){const r=spawnSync('cargo',[...args,'--manifest-path',manifest,'--offline'],{env:{...process.env,CARGO_TARGET_DIR:resolve(d,'target')},encoding:'utf8',timeout:180000,maxBuffer:8*1024*1024});const log=d+'/'+id+'.log';writeFileSync(log,(r.stdout??'')+(r.stderr??''));report.commands.push({id,exitCode:r.status,log:artifact(log)});assert.equal(r.status,expected,id);return (r.stdout??'')+(r.stderr??'');} try{ const log=run('host-tests',source+'/Cargo.toml',['test'],0);assert.equal((log.match(/test lifecycle::boot_owner_tests::\w+ \.\.\. ok/g)??[]).length,7);report.tests=[...log.matchAll(/test result: ok\. (\d+) passed/g)].reduce((a,m)=>a+Number(m[1]),0); run('arm64',source+'/Cargo.toml',['build','--lib','--target','aarch64-unknown-none'],0); for(const[id,before,after]of [ ['close-live','if self.records[self.state.boot as usize].generation&1!=0{return Err(Error::Busy)}',''], ['double-pin','if r.generation&1!=0{return Err(Error::Busy)}',''], ['actual-cpu','h.mpidr&AFFINITY_MASK!=r.affinity||',''], ['issuer','token.issuer!=self.state.issuer||token.epoch!=self.state.epoch||token.cpu!=self.state.boot','token.epoch!=self.state.epoch||token.cpu!=self.state.boot'], ['generation','||token.generation!=r.generation',''], ['reserve-release','r.generation.checked_add(2)','r.generation.checked_add(1)'], ['release-publish','self.records[self.state.boot as usize].generation=generation;Ok(())','let _=generation;Ok(())'] ]){ const dir=d+'/mutant-'+id;cpSync(source,dir,{recursive:true});let target=body; // The hardware expression is also used by the unchanged secondary path. const prefix=body.indexOf('// A boot CPU stays online');assert.ok(prefix>0);let tail=body.slice(prefix); if(id==='close-live'){assert.equal(body.split(before).length,2);target=body.replace(before,after);}else{assert.equal(tail.split(before).length,2,id);target=body.slice(0,prefix)+tail.replace(before,after);} writeFileSync(dir+'/src/lifecycle.rs',target);const log=run('mutant-'+id,dir+'/Cargo.toml',['test','--lib'],101);assert.match(log,/test result: FAILED/);assert.doesNotMatch(log,/error\[E/);report.sourceMutantsRejected++; } report.status='BOOT_OWNER_PREPARATION_PASSED'; }catch(e){report.status='BOOT_OWNER_PREPARATION_FAILED';report.error=e.stack;process.exitCode=1;} report.source=artifact(source+'/src/lifecycle.rs');report.harness=artifact(d+'/harness.mjs.txt');report.transform=artifact(d+'/transform.mjs.txt');report.extension=artifact(d+'/boot-owner.rs');report.testsSource=artifact(d+'/tests.rs.txt');writeFileSync(d+'/report.json',JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,tests:report.tests,newTests:7,sourceMutantsRejected:report.sourceMutantsRejected,report:d+'/report.json',error:report.error}));