import { setPriority, getPriority } from 'node:os'; import { spawn } from 'node:child_process'; import { appendFileSync, readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; const file=process.env.P5_PRIORITY_TRACE; const capture=(phase,pid=0)=>{let priority=null;try{priority=getPriority(pid);}catch{};const row={phase,pid:pid||process.pid,priority};appendFileSync(file,JSON.stringify(row)+'\n');return priority;}; try{setPriority(0,19);}catch{process.stderr.write('UNVERIFIED/STOP: own wrapper priority setup failed\n');process.exit(78);} if(capture('wrapper-before')!==19)process.exit(78); const child=spawn(process.execPath,['--import',fileURLToPath(new URL('./priority-guard.mjs',import.meta.url)),'--test','--test-concurrency=1',...process.argv.slice(2)],{ env:{...process.env,P5_PRIORITY_PARENT:String(process.pid)},stdio:['ignore','inherit','inherit','ipc'], }); child.on('message',msg=>{ if(!['before','after'].includes(msg?.p5Priority))return; const priority=capture(`direct-child-${msg.p5Priority}`,child.pid); child.send({p5Priority:`${msg.p5Priority}-ack`,priority}); }); child.on('error',()=>{process.stderr.write('UNVERIFIED/STOP: native child setup failed\n');process.exitCode=78;}); child.on('close',(code,signal)=>{ capture('wrapper-after'); const rows=readFileSync(file,'utf8').trim().split('\n').map(JSON.parse); const drift=rows.some(r=>r.priority!==19); const before=rows.filter(r=>r.phase==='before-modules'); const complete=before.length>0&&before.every(r=>rows.some(x=>x.pid===r.pid&&x.phase==='exit')); if(drift||!complete||signal){process.stderr.write(`UNVERIFIED/STOP: priority drift or incomplete capture; child=${code}, signal=${signal}\n`);process.exitCode=78;} else{process.stderr.write(`native strict19 verified: ${before.length} Node processes; direct-child before/after readbacks=19; behavior exit=${code}\n`);process.exitCode=code??78;} });