import assert from 'node:assert/strict'; import {copyFileSync,cpSync,readdirSync,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'; import {verifyMigrationHostSources} from './thread-migration-source.mjs'; const root=process.cwd(),directory=mkdtempSync('reports/os-implementation/K2055-thread-migration-staging-local-'),source=join(directory,'source'),artifact=p=>({path:relative(root,p),sha256:hashAt(root,relative(root,p))}),report={schema:'turkos-k2055-thread-migration-preparation/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources:[],commands:[],mutants:[],frozenBefore:frozenSnapshot(root)}; function copy(path){const to=join(source,path);mkdirSync(dirname(to),{recursive:true});copyFileSync(path,to);report.sources.push(artifact(to));} function walk(path){for(const entry of readdirSync(path,{withFileTypes:true})){const child=join(path,entry.name);if(entry.isDirectory())walk(child);else{assert.ok(entry.isFile());copy(child);}}} verifyMigrationHostSources(); for(const component of ['os-thread-migration','os-exceptions']){copy(component+'/Cargo.toml');copy(component+'/Cargo.lock');walk(component+'/src');} for(const path of ['os-threads/src/threads.rs','os-context-switch/src/context.rs','os-runqueue/src/runqueue.rs','os-percpu-runqueue/src/local.rs','scripts/os-acceptance/thread-migration-source.mjs','kernel/THREAD_MIGRATION_CONTRACT.md'])copy(path); walk('os-thread-migration/tests'); const env={...process.env,CARGO_TARGET_DIR:join(root,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-thread-migration/Cargo.toml');assert.match(run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','migration'],0),/6 passed; 0 failed/);run('target',['check','--manifest-path',manifest,'--locked','--offline','--lib','--target','aarch64-unknown-none'],0); for(const [name,file,guard,replacement,count]of [ ['source-same-cpu','threads','target==self.cpu || ','',1], ['source-inactive-sp','threads','if s.layout.contains(e.sp)','if false',1], ['source-stack-validation','threads','self.verify_slot(i,true)?;','',1], ['target-cpu','threads','if destination!=self.cpu || (!rollback&&ticket.source==self.cpu)','if false',1], ['target-stack-validation','threads','Self::verify_observation(observed,r.layout,true)?;','',1], ['target-inactive-sp','threads','if r.layout.contains(e.sp)','if false',1], ['initial-thread-generation','threads','Ok((i,self.slots[i].generation.checked_add(1).ok_or(Error::Exhausted)?))','Ok((i,self.slots[i].generation))',1], ['queue-context','runqueue','self.audit()?;\n let i=self.index(h)?;\n let o=self.observed(i)?;','let i=self.index(h)?;\n let o=self.observed(i)?;',1], ['context-execution','context','let e=self.execution(false)?;\n let i=self.target(h)?;','let e=self.backend.execution();\n let i=self.target(h)?;',1], ['context-inactive-sp','context','e.sp>=r.layout.guard()&&e.sp<=r.layout.base+r.layout.bytes','false',1], ['route-online','migration','!source.online || !target.online || ','',1], ['route-issuer','migration','source.issuer!=target.issuer || ','',1], ['route-epoch','migration','source.epoch!=target.epoch','false',1], ['route-generation','migration','target.generation==0 || ','',1], ['route-target','migration','target.cpu!=thread.target()','false',1], ['initial-image','migration','argument==r.argument','true',1], ['root-alignment','migration','root%4096!=0 || ','',1], ['claim-binding','migration','if binding!=if recall{self.source}else{self.target}','if false',1], ['claim-context','migration','if e.cpu!=binding.cpu || !e.masked || e.in_irq || root!=self.root','if false',1] ]){const mutant=join(directory,'mutant-'+name);cpSync(source,mutant,{recursive:true});const path=join(mutant,'os-thread-migration/src/'+file+'.rs'),s=readFileSync(path,'utf8');assert.equal(s.split(guard).length,count+1,name);writeFileSync(path,s.replaceAll(guard,replacement));report.mutants.push(artifact(path));assert.match(run(name+'-tests',['test','--manifest-path',join(mutant,'os-thread-migration/Cargo.toml'),'--locked','--offline','--test','migration'],101),/os_thread_migration_\w+ \.\.\. FAILED/);} report.status='STAGING_LOCAL_PASSED';report.tests=6;report.mutationsRejected=report.mutants.length; }catch(error){report.status='STAGING_TEST_FAILED';report.error=error.stack?.slice(0,6000);process.exitCode=1;} report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenAfter,report.frozenBefore);copyFileSync('scripts/os-acceptance/thread-migration-preparation.mjs',directory+'/harness.mjs.txt');report.harness=artifact(directory+'/harness.mjs.txt');writeFileSync(directory+'/report.json',JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:directory+'/report.json',tests:report.tests,mutationsRejected:report.mutationsRejected,error:report.error}));