import assert from'node:assert/strict';import{readFileSync,writeFileSync,mkdtempSync,mkdirSync,copyFileSync}from'node:fs';import{spawnSync}from'node:child_process';import{resolve}from'node:path';import{createHash}from'node:crypto';import{k2100ProcessGateSource}from'/scripts/os-acceptance/k2100-process-gate-source.mjs'; const dir=mkdtempSync('reports/os-implementation/K2100-process-gate-preparation-'),root=process.cwd(),target=resolve(dir,'target'); const art=p=>({path:p,sha256:createHash('sha256').update(readFileSync(p)).digest('hex')}); const inputPaths=['scripts/os-acceptance/k1120-process-owners.rs','scripts/os-acceptance/k2100-process-gate-source.mjs','scripts/os-acceptance/k2100-process-gate-tests.rs','os-process-guest-groundwork/tests/owners.rs','os-process-guest-groundwork/Cargo.toml','scripts/os-acceptance/k2100-process-gate-host.rs'];const before=inputPaths.map(art); copyFileSync('/tmp/turkos-k2100-process-gate-preparation.mjs',dir+'/harness.mjs.txt');copyFileSync(inputPaths[1],dir+'/transform.mjs.txt'); const host=k2100ProcessGateSource(readFileSync(inputPaths[0],'utf8'))+'\n'+readFileSync(inputPaths[5],'utf8'),native=k2100ProcessGateSource(readFileSync('reports/os-implementation/K2100-smp-timers-preflight-wY31sh/sources/kernel/src/runtime/process_guest.rs','utf8'));writeFileSync(dir+'/native-process-guest.rs',native); const fixture=readFileSync(inputPaths[3],'utf8')+'\n'+readFileSync(inputPaths[2],'utf8'); let manifest=readFileSync(inputPaths[4],'utf8').replaceAll(/path = "\.\.\/([^"]+)"/g,(_,p)=>'path = "'+resolve(root,p)+'"');manifest+='\n[lib]\npath = "src/lib.rs"\n'; const commands=[];function run(id,args){const r=spawnSync('cargo',args,{encoding:'utf8',timeout:300000,maxBuffer:32*1024*1024,env:{...process.env,CARGO_TARGET_DIR:target}});writeFileSync(dir+'/'+id+'.log',(r.stdout??'')+(r.stderr??''));const row={id,args,exitCode:r.status,log:art(dir+'/'+id+'.log')};commands.push(row);return row;} function source(name,text){const d=dir+'/'+name;mkdirSync(d+'/src',{recursive:true});mkdirSync(d+'/tests');writeFileSync(d+'/Cargo.toml',manifest);writeFileSync(d+'/src/lib.rs','#![no_std]\npub mod owners;\n');writeFileSync(d+'/src/owners.rs',text);writeFileSync(d+'/tests/owners.rs',fixture);return d+'/Cargo.toml';} let status='PROCESS_GATE_PREPARATION_FAILED',error=null;const mutants=[]; try{ const base=source('source',host);assert.equal(run('host-tests',['test','--manifest-path',base,'--offline','--features','host-fixture','--test','owners']).exitCode,0); assert.equal(run('arm64',['build','--manifest-path',base,'--offline','--lib','--target','aarch64-unknown-none']).exitCode,0); const cases=[ ['expected-cpu','expected_cpu > 3 || r.cpu != expected_cpu','expected_cpu > 3','smp_gate_rejects_wrong_cpu_at_construction_without_hardware_access'], ['cpu-range','expected_cpu > 3 || r.cpu != expected_cpu','r.cpu != expected_cpu','smp_gate_rejects_unsupported_cpu_and_affinity_values'], ['capture-cpu','if r.cpu != self.original.cpu','if false','smp_gate_cpu_drift_refuses_before_owner_access_and_recovers'], ['main-cpu','if r.cpu != self.original.cpu','if false','smp_gate_foreign_landing_reads_no_owner_and_valid_landing_runs',true], ['process-cpu','cpu: self.original.cpu as u16,','cpu: 0,','smp_gate_binds_all_four_cpus_and_returns_all_actual_resources'], ['thread-cpu','self.gate.original.cpu as u32','0','smp_gate_binds_all_four_cpus_and_returns_all_actual_resources'], ['legacy-cpu','Self::new_on_cpu(hardware, primary, 0)','Self::new_on_cpu(hardware, primary, hardware.registers().cpu)','smp_gate_preserves_legacy_cpu0_constructor'], ]; for(const[name,needle,replacement,test,last]of cases){const at=last?host.lastIndexOf(needle):host.indexOf(needle);assert.ok(at>=0);const text=host.slice(0,at)+replacement+host.slice(at+needle.length),m=source('mutant-'+name,text),r=run('mutant-'+name,['test','--manifest-path',m,'--offline','--features','host-fixture','--test','owners',test,'--','--exact']);assert.equal(r.exitCode,101,name);assert.ok(readFileSync(r.log.path,'utf8').includes('test result: FAILED'),name+' must reach assertion');mutants.push({name,test,source:art(dir+'/mutant-'+name+'/src/owners.rs'),...r});} assert.deepEqual(inputPaths.map(art),before);status='PROCESS_GATE_PREPARATION_PASSED'; }catch(e){error=e.stack;} const log=readFileSync(dir+'/host-tests.log','utf8'),count=Number(log.match(/test result: ok\. (\d+) passed/)?.[1]??0);writeFileSync(dir+'/report.json',JSON.stringify({status,guestAccepted:false,productAccepted:false,scope:'Host fixture with actual Rust process/thread/space/frame owners. New unsafe constructor binds one trusted online CPU 0..3, preserves CPU0-only legacy constructor, rejects foreign CPU entry and landing before touching owners. Hardware remains modeled. Native integration and per-CPU EL0 execution are not established.',tests:count,newTests:6,sourceMutantsRejected:mutants.length,inputsBefore:before,inputsAfter:inputPaths.map(art),source:art(dir+'/source/src/owners.rs'),native:art(dir+'/native-process-guest.rs'),harness:art(dir+'/harness.mjs.txt'),transform:art(dir+'/transform.mjs.txt'),commands,mutants,error},null,2)+'\n');console.log(JSON.stringify({status,report:dir+'/report.json',tests:count,mutants:mutants.length,error}));if(error)process.exitCode=1;