import assert from 'node:assert/strict';import {copyFileSync,cpSync,mkdirSync,mkdtempSync,readFileSync,writeFileSync} from 'node:fs';import{createHash}from'node:crypto';import{spawnSync}from'node:child_process';import{join,dirname,relative}from'node:path';import{hashAt,frozenSnapshot}from'./boot-contract-report.mjs'; const root=process.cwd(),directory=mkdtempSync(join(root,'reports/os-implementation/K1940-sandbox-quotas-staging-local-')),source=join(directory,'source'),artifact=path=>({path:relative(root,path),sha256:hashAt(root,relative(root,path))}); const report={schema:'turkos-k1925-sandbox-quotas-preparation/v1',status:'RUNNING',guestAccepted:false,productAccepted:false,sources:[],commands:[],frozenBefore:frozenSnapshot(root)}; for(const file of['os-sandbox-quotas/Cargo.toml','os-sandbox-quotas/Cargo.lock','os-sandbox-quotas/src/lib.rs','os-sandbox-quotas/src/quotas.rs','kernel/tests/os/sandbox_quotas.rs','kernel/SANDBOX_QUOTAS_CONTRACT.md']){const out=join(source,file);mkdirSync(dirname(out),{recursive:true});copyFileSync(file,out);report.sources.push(artifact(out));} let env={...process.env,CARGO_TARGET_DIR:join(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}),output=(r.stdout??'')+(r.stderr??'');const path=join(directory,name+'.log');writeFileSync(path,output);report.commands.push({name,args,exitCode:r.status,log:artifact(path)});assert.ifError(r.error);assert.equal(r.signal,null);assert.equal(r.status,expected,name);return output;} try{ const manifest=join(source,'os-sandbox-quotas/Cargo.toml');const green=run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test','sandbox_quotas'],0);assert.match(green,/2 passed; 0 failed/);run('target-library-check',['check','--manifest-path',manifest,'--locked','--offline','--lib','--target','aarch64-unknown-none'],0);report.mutants=[]; for(const[name,guard,replacement]of[['principal','if handle.principal!=self.accounts[i].principal','if false'],['quota','if !next.within(self.accounts[i].limit)','if false'],['cpu','a.cpu_used>=a.cpu_ticks','a.cpu_used>a.cpu_ticks']]){ const mutant=join(directory,'mutant-'+name);cpSync(source,mutant,{recursive:true});const path=join(mutant,'os-sandbox-quotas/src/quotas.rs'),original=readFileSync(path,'utf8');assert.equal(original.split(guard).length,2);writeFileSync(path,original.replace(guard,replacement));report.mutants.push(artifact(path));env={...env,CARGO_TARGET_DIR:join(directory,'mutant-'+name+'-target')};const red=run(name+'-mutant-tests',['test','--manifest-path',join(mutant,'os-sandbox-quotas/Cargo.toml'),'--locked','--offline','--test','sandbox_quotas'],101);assert.match(red,/os_sandbox_quotas_(negative|positive) \.\.\. FAILED/); } report.status='STAGING_LOCAL_PASSED';report.tests=2;report.mutationsRejected=3;report.scope='Per-principal observed backing and task/page/channel limits, thousand immutable quota refusals with other service progress, real retained host channel bytes, terminal resource release, and trusted timer receipt ledger. Physical EL0 preemption remains pending. Host and no_std target checks only; guest pending'; }catch(error){report.status='STAGING_TEST_FAILED';report.error=error.stack;process.exitCode=1;} report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenAfter,report.frozenBefore);report.harness=artifact(join(root,'scripts/os-acceptance/sandbox-quotas-preparation.mjs'));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),tests:report.tests,mutationsRejected:report.mutationsRejected}));