io-v2-core-probe.mjs (3137B)
1 // Executes bare computation trees privately on native evaluators. Production IO 2 // envelopes and terminal providers remain gated; no JS evaluator substitution. 3 export async function run(_control,backend){ 4 const base=new URL('../',document.querySelector('script[type=module][src]').src); 5 const fixtures=await(await fetch('/zig-negative.json')).json(),checks=[]; 6 let execute,dispose; 7 if(backend==='haskell-wasm'){ 8 const {CompilerWorker}=await import(new URL('runtime/haskell/client.mjs',base)); 9 const client=new CompilerWorker();dispose=()=>client.dispose();await client.request({type:'init',compiler:false}); 10 execute=async fixture=>{await client.request({type:'load',bytes:new Uint8Array(fixture.bytes).buffer,hash:fixture.hash});return JSON.parse((await client.request({type:'io-v2-core',name:'root',arguments:[]})).text);}; 11 }else{ 12 const {WasmRuntime}=await import('/io-v2-runtime.mjs'),{ioV2Private}=await import(new URL('dist/zig-helpers.js',base)); 13 const g=await WasmRuntime.create(new Uint8Array(await(await fetch(new URL('runtime/zig/arboricx.wasm',base))).arrayBuffer())); 14 dispose=()=>g.dispose();let run=0n; 15 await g.scopeAsync(async()=>{ 16 const kept=g.stem(g.leaf),retired=g.stem(g.leaf),owned=g.stats().owned; 17 let rejected=false;try{g.sweepScope([kept,-1n]);}catch(error){rejected=error.message==='Stale interpreter root';} 18 if(!rejected||g.stats().owned!==owned)throw Error('Invalid sweep partially retired handles'); 19 g.sweepScope([kept,g.leaf]); 20 if(g.observe(kept).node.kind!=='stem')throw Error('Live sweep root lost'); 21 rejected=false;try{g.observe(retired);}catch{rejected=true;} 22 if(!rejected)throw Error('Retired native alias stayed usable'); 23 }); 24 checks.push('invalid root sweep is atomic; retired native handles reject'); 25 execute=async fixture=>{ 26 const owned=g.stats().owned; 27 const out=await g.scopeAsync(async()=>{ 28 const action=g.load(new Uint8Array(fixture.bytes),'root'); 29 const tree=ioV2Private.constructors({leaf:g.leaf,observe:g.observe.bind(g),stem:g.stem.bind(g),fork:g.fork.bind(g),app:g.app.bind(g)}); 30 return ioV2Private.runPrivate(tree,action,++run,roots=>g.sweepScope(roots)); 31 }); 32 if(g.stats().owned!==owned)throw Error('Native owned handle history retained');return out; 33 }; 34 } 35 const cases=[['pure','completed'],['bind','completed'],['scope','completed'],['child-0','failed'],['child-1','completed'],['unsupported','completed'],['discard-invalid','completed'],['terminal-2','failed'],['terminal-9','cancelled'],['malformed','failed']]; 36 try{ 37 for(const [name,kind]of cases){ 38 const out=await execute(fixtures['v2-run-'+name]); 39 const expected={kind,value:kind==='completed'?'42':null,cleanup:0,tasks:{scopes:0,tasks:0,hosts:0},resources:{resources:0,admissions:0,leases:0},host:{pending:0,replies:0}}; 40 if(JSON.stringify(out)!==JSON.stringify(expected))throw Error(name+': '+JSON.stringify(out)); 41 checks.push(name); 42 } 43 for(let i=0;i<5;i++)await execute(fixtures['v2-run-scope']); 44 checks.push('repeated run retirement and reuse; native evaluation with per-turn root sweeps'); 45 }finally{dispose();} 46 return {backend,checks}; 47 }