import assert from'node:assert/strict';import{cpSync,mkdtempSync,readFileSync,writeFileSync,copyFileSync}from'node:fs';import{spawnSync}from'node:child_process';import{join}from'node:path';import{artifact,read}from'/scripts/os-acceptance/cpu-watchdog-artifacts.mjs'; const root=process.cwd(),basePath='reports/os-implementation/K2095-cpu-watchdog-candidate-3i89qX/report.json',base=read(basePath),directory=mkdtempSync('reports/os-implementation/K2100-smp-workers-preflight-'),sources=directory+'/sources',src=sources+'/kernel/src/'; cpSync(base.sourceDirectory,sources,{recursive:true,filter:p=>!p.split('/').includes('target')});let main=readFileSync(src+'main.rs','utf8');const before='if mode>210 || (144..=209).contains(&mode) {return Err("policy-probe-mode");}\n Ok(mode)',after='if mode!=210 {return Err("policy-probe-mode");}\n Ok(210)';assert.equal(main.split(before).length,2);main=main.replace(before,after);const head='fn os_k1100_product_probe_mode(boot:&BootData)';assert.equal(main.split(head).length,2);main=main.replace(head,'#[inline(always)]\n'+head);const roundCall='k2095_cpu_watchdog::run(boot,tables,&mut memory.allocator)?';assert.equal(main.split(roundCall).length,2);main=main.replace(roundCall,roundCall+';os_k2100_process_round(&mut memory.allocator,tables)?;write_line("TURKOS/OS/K2100-PROCESS-COMPONENT: ROUND=1 STALE_REFUSED=1 WAKE=1 RETIRED=2");write_line("TURKOS/OS/K2100-SERVICE-COMPONENT: IPC=2 SHELL=1 VFS=1 CHILD_REAPED=1")');main+='\ninclude!("k2100-process-round.rs");\ncore::arch::global_asm!(include_str!("k2100-process-round.S"));\n';let round=readFileSync('scripts/os-acceptance/k2100-process-round.rs','utf8');const action=' _=>Err("k2100-process-command"),';assert.equal(round.split(action).length,2);round=round.replace(action,' 3..=5=>os_k2100_service_action(table,pid,command),\n'+action);const serviceCall=' os_k2100_process_call(table,current,1)?;';assert.equal(round.split(serviceCall).length,2);round=round.replace(serviceCall,' os_k2100_service_round(table,current)?;\n'+serviceCall);writeFileSync(src+'k2100-process-round.rs',round);copyFileSync('scripts/os-acceptance/k2100-service-component.rs',src+'k2100-service-component.rs');main+='\ninclude!("k2100-service-component.rs");\n';copyFileSync('scripts/os-acceptance/k2100-process-round.S',src+'k2100-process-round.S');let processRound=readFileSync(src+'k2100-process-round.rs','utf8');const command=' 3..=5=>os_k2100_service_action(table,pid,command),';assert.equal(processRound.split(command).length,2);processRound=processRound.replace(command,' 6=>os_k2100_service_enter(table as*mut _,pid),\n'+command);assert.equal(processRound.split('OS_K1105_TRUSTED_ELF').length,3);processRound=processRound.replaceAll('OS_K1105_TRUSTED_ELF','OS_K2100_USER_ELF');writeFileSync(src+'k2100-process-round.rs',processRound);let service=readFileSync(src+'k2100-service-component.rs','utf8');const childCall='os_k2100_process_call(table,child,4)?;';assert.equal(service.split(childCall).length,2);service=service.replace(childCall,'os_k2100_process_call(table,child,6)?;');writeFileSync(src+'k2100-service-component.rs',service);copyFileSync('scripts/os-acceptance/k2100-service-el0.rs',src+'k2100-service-el0.rs');copyFileSync('scripts/os-acceptance/os_k2100_service_vectors.S',src+'os_k2100_service_vectors.S');const user=read('reports/os-implementation/K2100-service-user-elf-plF0Ks/report.json');assert.equal(user.status,'COMPILED_K2100_SERVICE_USER_ELF');assert.equal(artifact(user.elf.path).sha256,user.elf.sha256);copyFileSync(user.elf.path,src+'k2100-service-user.elf');main+='\ninclude!("k2100-service-el0.rs");\n'+"global_asm!(include_str!(\"os_k2100_service_vectors.S\"),\n frame_bytes=const os_k920_exception::FRAME_BYTES,\n sp_offset=const os_k920_exception::FRAME_SP_OFFSET,\n sp_el0_offset=const os_k920_exception::FRAME_SP_EL0_OFFSET,\n elr_offset=const os_k920_exception::FRAME_ELR_OFFSET,\n spsr_offset=const os_k920_exception::FRAME_SPSR_OFFSET,\n esr_offset=const os_k920_exception::FRAME_ESR_OFFSET,\n far_offset=const os_k920_exception::FRAME_FAR_OFFSET,\n slot_offset=const os_k920_exception::FRAME_SLOT_OFFSET,\n q_offset=const os_k920_exception::FRAME_Q_OFFSET,\n fpcr_offset=const os_k920_exception::FRAME_FPCR_OFFSET,\n fpsr_offset=const os_k920_exception::FRAME_FPSR_OFFSET,\n current_el_offset=const os_k920_exception::FRAME_CURRENT_EL_OFFSET,\n canary_offset=const os_k920_exception::FRAME_CANARY_OFFSET,\n padding_offset=const os_k920_exception::FRAME_PADDING_OFFSET,\n);"+'\n';const marker='write_line("TURKOS/OS/K2100-SERVICE-COMPONENT: IPC=2 SHELL=1 VFS=1 CHILD_REAPED=1")';assert.equal(main.split(marker).length,2);main=main.replace(marker,marker+';write_line("TURKOS/OS/K2100-EL0-COMPONENT: CALLS=5 SHELL=1 IPC=2 VFS=1 EXIT=0")');copyFileSync('scripts/os-acceptance/k2100-timer-component.rs',src+'k2100-timer-component.rs');copyFileSync('scripts/os-acceptance/os_k2100_timer.S',src+'os_k2100_timer.S');main+='\ninclude!("k2100-timer-component.rs");\ncore::arch::global_asm!(include_str!("os_k2100_timer.S"));\n';const processStart='os_k2100_process_round(&mut memory.allocator,tables)?;';assert.equal(main.split(processStart).length,2);main=main.replace(processStart,'os_k2100_timer_bind(boot,tables)?;'+processStart+'write_line("TURKOS/OS/K2100-TIMER-COMPONENT: PPI=30 FIRED=1 RESTORED=1 WAKE_AFTER_IRQ=1");');let timerRound=readFileSync(src+'k2100-process-round.rs','utf8');const slept=' os_k2100_process_call(table,current,2)?;';assert.equal(timerRound.split(slept).length,2);timerRound=timerRound.replace(slept,slept+'\n os_k2100_timer_wait()?;');writeFileSync(src+'k2100-process-round.rs',timerRound);copyFileSync('scripts/os-acceptance/k2100-network-component.rs',src+'k2100-network-component.rs');main+='\n#[path="k2100-network-component.rs"]mod k2100_network;\n';const networkCall='os_k2100_process_round(&mut memory.allocator,tables)?;';assert.equal(main.split(networkCall).length,2);main=main.replace(networkCall,'if let Err(error)=k2100_network::run(boot,tables,&mut memory.allocator){write_str("TURKOS/OS/K2100: REJECTED ");write_line(error);return Err(error);};');let networkService=readFileSync(src+'k2100-service-component.rs','utf8');const requestCall='channel.send(pid,parent_handle,b"echo \'smp-load\'")';assert.equal(networkService.split(requestCall).length,2);networkService=networkService.replace(requestCall,'channel.send(pid,parent_handle,&k2100_network::request(pid)?)');const replyCheck='if reply.sender!=child||reply.payload.as_slice()!=b"smp-load"{return Err("k2100-service-reply-provenance")}';assert.equal(networkService.split(replyCheck).length,2);networkService=networkService.replace(replyCheck,replyCheck+'\n k2100_network::reply(pid,reply.payload.as_slice())?;');writeFileSync(src+'k2100-service-component.rs',networkService);copyFileSync('scripts/os-acceptance/k2100-repeat-component.rs',src+'k2100-repeat-component.rs');main+='\ninclude!("k2100-repeat-component.rs");\n';const repeatCall='k2100_network::run(boot,tables,&mut memory.allocator)';assert.equal(main.split(repeatCall).length,2);main=main.replace(repeatCall,'os_k2100_repeat(boot,tables,&mut memory.allocator)');for(const name of ['k2100-smp-workers.rs','k2100-smp-workers.S'])copyFileSync('scripts/os-acceptance/'+name,src+name); let parent=readFileSync(src+'k2095-cpu-watchdog.rs','utf8');parent+='\n#[path="k2100-smp-workers.rs"]mod smp_workers;\n'; for(const [old,neu]of [['watchdog::cycle(&q,shared,c)?;','watchdog::cycle(&q,shared,c)?;smp_workers::cycle(&q,shared,c)?;'],['watchdog::drive(&shared)?;','watchdog::drive(&shared)?;smp_workers::drive(&shared)?;']]){assert.equal(parent.split(old).length,2);parent=parent.replace(old,neu);}writeFileSync(src+'k2095-cpu-watchdog.rs',parent); let wg=readFileSync(src+'k2095-cpu-watchdog-guest.rs','utf8');assert.equal(wg.split('if cmd==3{return Ok(())}').length,2);wg=wg.replace('if cmd==3{return Ok(())}','if cmd==120{return Ok(())}');writeFileSync(src+'k2095-cpu-watchdog-guest.rs',wg); writeFileSync(src+'main.rs',main);writeFileSync(src+'runtime/process_table.rs',readFileSync(src+'runtime/process_table.rs','utf8')+'\n'+readFileSync('os-smp-process-wake/src/wake.rs','utf8')+'\n'+readFileSync('os-smp-process-sleep/src/sleep.rs','utf8'));writeFileSync(src+'runtime/process_guest.rs',readFileSync(src+'runtime/process_guest.rs','utf8')+'\n'+readFileSync('scripts/os-acceptance/k2100-process-owner-wait.rs','utf8'));writeFileSync(src+'runtime/smp_workload.rs',readFileSync('os-smp-workload/src/workload.rs','utf8'));writeFileSync(src+'runtime/mod.rs',readFileSync(src+'runtime/mod.rs','utf8')+'\npub mod smp_workload;\n');copyFileSync('/tmp/turkos-k2100-smp-workers-preflight.mjs',directory+'/harness.mjs.txt'); for(const crate of ['os-shell-words','os-shell-dispatch'])cpSync(crate,sources+'/'+crate,{recursive:true,filter:p=>!p.split('/').includes('target')});const manifest=sources+'/kernel/Cargo.toml';let cargo=readFileSync(manifest,'utf8');assert.equal(cargo.split('[dependencies]').length,2);cargo=cargo.replace('[dependencies]','[dependencies]\nturkos-os-shell-words = {path = "../os-shell-words"}\nturkos-os-shell-dispatch = {path = "../os-shell-dispatch"}');writeFileSync(manifest,cargo);const env={...process.env,CARGO_TARGET_DIR:join(root,directory,'target'),RUSTFLAGS:'-C opt-level=s -C llvm-args=--enable-machine-outliner=always -C relocation-model=static -C link-arg=-T'+join(root,sources,'kernel/linker.ld')+' -C link-arg=--build-id=none -A warnings'};delete env.CARGO_ENCODED_RUSTFLAGS;const build=spawnSync('cargo',['build','--manifest-path',sources+'/kernel/Cargo.toml','--offline','--release','--target','aarch64-unknown-none','--bin','turkos-kernel','--no-default-features','--features','os-runtime-product'],{env,encoding:'utf8',timeout:600000,maxBuffer:128*1024*1024});writeFileSync(directory+'/compile.log',(build.stdout??'')+(build.stderr??''));const report={status:build.status===0?'NATIVE_MODELS_COMPILED':'NATIVE_MODELS_COMPILE_FAILED',guestAccepted:false,productAccepted:false,sourceExtensions:{processWake:artifact(src+'runtime/process_table.rs'),nativeOwners:artifact(src+'runtime/process_guest.rs'),processRound:artifact(src+'k2100-process-round.rs'),serviceComponent:artifact(src+'k2100-service-component.rs'),el0Service:artifact(src+'k2100-service-el0.rs'),el0Vectors:artifact(src+'os_k2100_service_vectors.S'),repeat:artifact(src+'k2100-repeat-component.rs'),network:artifact(src+'k2100-network-component.rs'),timer:artifact(src+'k2100-timer-component.rs'),timerAssembly:artifact(src+'os_k2100_timer.S'),userElf:artifact(src+'k2100-service-user.elf'),processAssembly:artifact(src+'k2100-process-round.S'),workload:artifact(src+'runtime/smp_workload.rs'),modules:artifact(src+'runtime/mod.rs')},scope:'Three secondary private-stack threads with actual activation and shell/Tmpfs on four CPUs. Initial concurrent worker component; no SMP process/EL0 network or duration acceptance. Timer component compile. Genuine CPU0 PPI30 must complete before the stale-PID refusal and healthy wake; original hardware state must restore. No SMP/two-hour acceptance. Compile-only integration of current ProcessTable sleep/wake, actual Owners/ThreadTable adapters and workload monitor. One real process component and actual parent/child IPC shell-parser Tmpfs round are wired after the inherited watchdog run. The child runs the actual Rust shell parser/dispatcher at EL0 with bounded IPC/VFS syscalls. Combined SMP, networking, timers and duration are not yet established. It does not satisfy combined workload or long duration. Mode helper is explicitly inlined. Exact mode 210 is validated from the same DTB; other modes are rejected. No accepted candidate or guest equivalence claim.',baseCandidate:artifact(basePath),sourceDirectory:sources,sourceMain:artifact(src+'main.rs'),harness:artifact(directory+'/harness.mjs.txt'),transform:{before,after},exitCode:build.status,log:artifact(directory+'/compile.log'),imageSpan:null};if(build.status===0){const elf=directory+'/product.elf';copyFileSync(directory+'/target/aarch64-unknown-none/release/turkos-kernel',elf);const sys=spawnSync('rustc',['--print','sysroot'],{encoding:'utf8'}).stdout.trim(),host=spawnSync('rustc',['-vV'],{encoding:'utf8'}).stdout.match(/^host: (.+)$/m)[1],bin=sys+'/lib/rustlib/'+host+'/bin/';let r=spawnSync(bin+'llvm-objcopy',['-O','binary',elf,directory+'/product.Image'],{encoding:'utf8'});assert.equal(r.status,0);r=spawnSync(bin+'llvm-nm',['-n','-S',elf],{encoding:'utf8',maxBuffer:64*1024*1024});assert.equal(r.status,0);writeFileSync(directory+'/product.nm',r.stdout);const address=n=>parseInt(r.stdout.match(new RegExp('^([a-f0-9]{16}) [a-f0-9]{16} [A-Za-z] '+n+'$','m'))[1],16);report.imageSpan={before:base.imageSpan.bytes,bytes:address('_image_end')-address('_image_start'),maximum:8388608};report.imageSpan.saved=report.imageSpan.before-report.imageSpan.bytes;report.artifacts={elf:artifact(elf),image:artifact(directory+'/product.Image'),nm:artifact(directory+'/product.nm')};} writeFileSync(directory+'/report.json',JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:directory+'/report.json',exitCode:build.status,imageSpan:report.imageSpan}));if(build.status!==0)process.exitCode=1;