import assert from 'node:assert/strict'; import {copyFileSync,mkdirSync,mkdtempSync,readdirSync,readFileSync,writeFileSync,cpSync} from 'node:fs'; import {dirname,join,relative} from 'node:path'; import {spawnSync} from 'node:child_process'; import {frozenSnapshot,hashAt} from './boot-contract-report.mjs'; const root=process.cwd(),directory=mkdtempSync(join(root,'reports/os-implementation/K1775-exec-environment-staging-local-')); const before=frozenSnapshot(root),source=join(directory,'source'),sources=[]; const artifact=p=>({path:relative(root,p),sha256:hashAt(root,relative(root,p))}); const copy=path=>{const target=join(source,path);mkdirSync(dirname(target),{recursive:true});copyFileSync(join(root,path),target);sources.push(artifact(target));}; function tree(path){for(const entry of readdirSync(join(root,path),{withFileTypes:true})){const child=path+'/'+entry.name;if(entry.isDirectory())tree(child);else copy(child);}} for(const component of ['os-exec-environment']){copy(component+'/Cargo.toml');tree(component+'/src');} for(const path of ['os-exec-environment/Cargo.lock','kernel/tests/os/exec_environment.rs','kernel/EXEC_ENVIRONMENT_CONTRACT.md','scripts/os-acceptance/exec-environment-prepare.mjs'])copy(path); const env={...process.env,CARGO_TARGET_DIR:join(directory,'target')};delete env.RUSTFLAGS;delete env.CARGO_ENCODED_RUSTFLAGS; const report={schema:'turkos-k1775-exec-environment-staging/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources,tests:{},frozenBefore:before,scope:'Complete count, per-string, embedded-NUL and total-byte preflight before constructing a private512-byte stack packet. Turkish UTF-8 copied unchanged using the existing process-image recipe ABI. Exactly256bytes accepted,257 rejected while old publication stays byte-identical. Host-only preparation; native argument-register and stack proof pending.'}; const run=(name,args,expected)=>{const result=spawnSync('cargo',args,{env,cwd:root,encoding:'utf8',timeout:600000,maxBuffer:64*1024*1024});const log=join(directory,name+'.log');writeFileSync(log,(result.stdout??'')+(result.stderr??''));report.tests[name]={exitCode:result.status,log:artifact(log)};assert.equal(result.error,undefined);assert.equal(result.signal,null);assert.equal(result.status,expected,name);return readFileSync(log,'utf8');}; try{ const manifest=join(source,'os-exec-environment/Cargo.toml'); const green=run('host',['test','--manifest-path',manifest,'--offline','--locked','--test','environment'],0);assert.match(green,/2 passed; 0 failed/); run('aarch64',['check','--manifest-path',manifest,'--offline','--locked','--lib','--target','aarch64-unknown-none'],0); const mutant=join(directory,'mutant');cpSync(source,mutant,{recursive:true}); const model=join(mutant,'os-exec-environment/src/environment.rs'),text=readFileSync(model,'utf8'); const old='if total>MAX_TEXT_BYTES{return Err(Error::Total);}';assert.equal(text.split(old).length,2); writeFileSync(model,text.replace(old,'if total>MAX_TEXT_BYTES+1{return Err(Error::Total);}')); report.mutant=artifact(model);env.CARGO_TARGET_DIR=join(directory,'mutant-target'); const red=run('limit-plus-one-mutant',['test','--manifest-path',join(mutant,'os-exec-environment/Cargo.toml'),'--offline','--locked','--test','environment'],101); assert.match(red,/os_exec_environment_negative \.\.\. FAILED/); report.status='STAGING_LOCAL_PASSED'; }catch(error){report.status='STAGING_LOCAL_FAILED';report.error=error.stack;process.exitCode=1;} report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenAfter,before); const path=join(directory,'report.json');writeFileSync(path,JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({status:report.status,report:relative(root,path)}));