interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / universal / source-aware / gate.mjs

125 lines · 15834 bytes Raw Blame History
1// Independent development gate. Node is orchestration/reference only, never production.
2import fs from 'node:fs';
3import path from 'node:path';
4import assert from 'node:assert/strict';
5import {spawnSync,execFileSync} from 'node:child_process';
6import {createHash} from 'node:crypto';
7import {hash,unit,op,u32,expected,parent} from './fixtures.mjs';
8import {inspect,legality} from './reference.mjs';
9import {seed,valid,damaged,categories} from './corpus.mjs';
10const root='build/L574',run=path.join(root,'runs',new Date().toISOString().replaceAll(':','-'));
11fs.mkdirSync(run,{recursive:true});fs.rmSync(path.join(root,'PASS.json'),{force:true});
12const report={schema:'omega.source-aware.development.v1',fidelity:'DIFFERENTIAL_COMPATIBILITY',host:'HOST_NATIVE',seed,run,sourceHEAD:execFileSync('git',['rev-parse','HEAD']).toString().trim(),base625:'69790fb4ad1e803ca79245430745b96129d1dcf4',review:'PENDING',signed:false,notRun:['full local judge','independent API/core review','translation proof','general language adapters','GPU/hardware targets'],steps:[]};
13let compiler=path.join(root,'toolchain/build/bootstrap/omc'),core=fs.readFileSync('src/source-aware/core.om','utf8'),vendor=fs.readFileSync('src/source-aware/vendor/sha256.om','utf8'),batch=fs.readFileSync('tests/universal/source-aware/batch.om','utf8');
14const digest=b=>hash(b).toString('hex');
15const filehash=p=>digest(fs.readFileSync(p));
16function command(exe,args=[],input,options={}){const r=spawnSync(exe,args,{input,maxBuffer:512*1024*1024,timeout:180000,...options});if(r.error&&!(options.allowEarlyExit&&r.error.code==='EPIPE'))throw r.error;return r;}
17function compile(source,name){const src=path.join(run,name+'.om'),exe=path.join(run,name);fs.writeFileSync(src,source);let r=command(compiler,[],Buffer.from(source));fs.writeFileSync(src+'.stderr',r.stderr);assert.equal(r.status,0,`${name} compilation: ${r.stderr}`);assert.ok(r.stdout.length>0);fs.writeFileSync(exe,r.stdout,{mode:0o700});let elf=command('sh',[path.join(root,'toolchain/tools/verify-elf.sh'),exe]);assert.equal(elf.status,0,'AArch64 ELF required');return {exe,sourceSHA256:digest(source),binarySHA256:digest(r.stdout),compileStatus:r.status};}
18function runBatch(exe,requests,{strict=true}={}){
19 let input=Buffer.concat(requests.map(r=>Buffer.concat([Buffer.of(r.mode),u32(r.input.length),r.input])));const result=command(exe,[],input,{allowEarlyExit:!strict});let out=result.stdout,pos=0,seen=0,wrong=[],executedCategories={};
20 for(let [i,r] of requests.entries()){
21 if(pos+37>out.length){wrong.push({i,why:'missing response',remaining:out.length-pos});break;}
22 let code=out[pos],len=out.readUInt32LE(pos+1),consumed=out.subarray(pos+5,pos+37),d=hash(Buffer.concat([Buffer.of(r.mode),r.input]));pos+=37;
23 assert.ok(consumed.equals(d),`consumed input mismatch ${i}`);assert.ok(pos+len<=out.length,'truncated response');let bytes=out.subarray(pos,pos+len);pos+=len;seen++;let category=r.category??'other';executedCategories[category]=(executedCategories[category]??0)+1;
24 if(code!==r.code||!bytes.equals(r.want))wrong.push({i,category:r.category,expectedCode:r.code,code,expectedBytes:r.want.length,bytes:len});
25 }
26 if(result.status!==0||pos!==out.length||seen!==requests.length)wrong.push({status:result.status,signal:result.signal,seen,requested:requests.length,pos,outLength:out.length,stderr:result.stderr.toString()});
27 const row={requested:requests.length,executed:seen,executedCategories,mismatches:wrong.length,requestStreamSHA256:digest(input),responseStreamSHA256:digest(out),status:result.status,sampleFailures:wrong.slice(0,8)};
28 if(strict)assert.equal(wrong.length,0,JSON.stringify(row));return row;
29}
30const request=(mode,input,code=0,want)=>({mode,input,code,want:want??(code?Buffer.alloc(0):expected(mode,input))});
31function malformedRequest(input){const ref=inspect(input);assert.notEqual(ref.code,0);return request(2,input,ref.code);}
32try{
33 report.toolchain={compilerSHA256:filehash(compiler),compilerSourceSHA256:filehash(path.join(root,'toolchain/seed/omc/omc.om')),bridgeCommit:'0fc7d8662cb799e7c206b17c18ea4c3ec2b8fea9',seedCommit:'29b12760f4c962606411d6bf25032aedef063301',imports:filehash('tools/source-aware/imports.sha256'),nativeSourceSHA256:digest(vendor+core+fs.readFileSync('src/source-aware/main.om')),nativeBinarySHA256:filehash(path.join(root,'sa')),oracle:'Node '+process.version+' Buffer, TextDecoder and OpenSSL SHA256'};
34 assert.equal(report.toolchain.compilerSHA256,'191b776ef98821597c3e30655da6d680a9b66edbc9ceaca3c39c61ea6df098e9');
35 const prod=compile(vendor+core+batch,'batch');report.batch=prod;
36 let validUnits=[],bad=[],requests=[],counts={},samples={},validDigest=createHash('sha256'),badDigest=createHash('sha256');const uniqueV=new Set(),uniqueB=new Set();
37 for(let i=0;i<10000;i++){
38 let b=valid(i),ref=inspect(b);assert.equal(ref.code,0,`valid membership ${i}: ${ref.why}`);let h=digest(b);assert.ok(!uniqueV.has(h),'valid corpus duplicate');uniqueV.add(h);validDigest.update(b);validUnits.push(b);requests.push({...request(i%2+1,b),category:'valid roundtrip'});
39 requests.push({...request(3,b),category:'valid semantic identity'});requests.push({...request(5,b),category:'valid replay'});
40 if(i%10===0)requests.push({...request(4,b),category:'valid provenance identity'});
41 }
42 for(let i=0;i<100000;i++){
43 let d=damaged(i),ref=inspect(d.input);assert.equal(ref.code,d.code,`malformed membership ${i} ${d.category}: ${ref.why}`);assert.notEqual(ref.code,0);let h=digest(d.input);assert.ok(!uniqueB.has(h),'malformed duplicate '+d.category);uniqueB.add(h);badDigest.update(d.input);
44 counts[d.category]=(counts[d.category]??0)+1;if(!samples[d.category])samples[d.category]={expectedCode:d.code,hex:d.input.toString('hex'),SHA256:h};bad.push(d);requests.push({...request(i%2+1,d.input,d.code),category:d.category});
45 }
46 assert.equal(Object.keys(counts).length,categories.length);assert.equal(Object.values(counts).reduce((a,b)=>a+b),100000);
47 fs.writeFileSync(path.join(run,'counterexamples.json'),JSON.stringify(samples,null,2)+'\n');
48 console.log('Generated and independently classified 10000 unique valid and 100000 unique malformed units. Running native core.');
49 let structural=runBatch(prod.exe,requests);for(let [category,n] of Object.entries(counts))assert.equal(structural.executedCategories[category],n);report.structural={...structural,validUnits:validUnits.length,uniqueValid:uniqueV.size,malformedUnits:bad.length,uniqueMalformed:uniqueB.size,categoryCounts:counts,validCorpusSHA256:validDigest.digest('hex'),malformedCorpusSHA256:badDigest.digest('hex'),perRequestConsumedSHA256Checked:true,validRoundtripExecuted:structural.executedCategories['valid roundtrip'],semanticDigestsIndependentlyChecked:structural.executedCategories['valid semantic identity'],exactReplayExecuted:structural.executedCategories['valid replay'],provenanceDigestsIndependentlyChecked:structural.executedCategories['valid provenance identity']};
50 report.steps.push('structural');fs.writeFileSync(path.join(run,'progress.json'),JSON.stringify(report,null,2));
51 // Every typed operation/profile and capability combination, including retained operations.
52 let legalRequests=[];
53 for(let b of validUnits.slice(0,60))for(let target of [1,2])for(let runtime of [0,1,2])for(let abi of [1,2])for(let effects of [0,17,63]){
54 const caps=Buffer.from([target,runtime,abi,effects]),ref=inspect(b);legalRequests.push(request(6,Buffer.concat([caps,b]),0,legality(ref,caps)));
55 }
56 for(let caps of [[0,0,1,0],[3,0,1,0],[1,3,1,0],[1,0,0,0],[1,0,3,0],[1,0,1,64]])legalRequests.push(request(6,Buffer.concat([Buffer.from(caps),unit()]),68));
57 report.legality=runBatch(prod.exe,legalRequests);
58 // Identity field separation and profile/behavior variation checked against independent SHA256.
59 const s1=unit({source:Buffer.from('a')}),s2=unit({source:Buffer.from(' b\r\n'),spans:[[1,2]]});
60 assert.ok(expected(3,s1).equals(expected(3,s2)));assert.ok(!expected(4,s1).equals(expected(4,s2)));
61 const b2=unit({ops:[op({imm:1n})]});assert.ok(!expected(3,s1).equals(expected(3,b2)));
62 const missing=unit({missing:true});let edge=[request(3,s1),request(3,s2),request(4,s1),request(4,s2),request(3,b2),request(1,missing),request(5,missing,73)];
63 // UTF8 controls and source encodings at their finite bounds, plus every source byte.
64 const all=unit({source:Buffer.from(Array.from({length:256},(_,i)=>i))});edge.push(request(5,all));
65 for(let source of [Buffer.alloc(0),Buffer.from([0]),Buffer.alloc(4096,255),Buffer.from('\uFEFF\0\r\n\tμ😀'),Buffer.from([0xf4,0x8f,0xbf,0xbf])]){
66 let b=unit({source,encoding:source.every(x=>x===255)?0:1});assert.equal(inspect(b).code,0);for(let mode of [1,2,3,4,5])edge.push(request(mode,b));
67 }
68 // Structural deep negatives on actually typed bigint/dynamic/memory inputs.
69 for(let b of validUnits.slice(0,24)){
70 let ref=inspect(b),off=13+b.readUInt32LE(5);for(let i=0;i<ref.ops.length;i++)for(let at of [2,3,4,5,6,7,8,10,12]){
71 let d=Buffer.from(b);d[57+i*20+at]^=255;let r=inspect(d);if(r.code!==0)edge.push(request(2,d,r.code));else edge.push(request(2,d));
72 }
73 let d=Buffer.from(b);d[off+42]^=1;edge.push(request(5,d,74));
74 }
75 report.edges=runBatch(prod.exe,edge);
76 // Real public commands, direct native entry and named failures. No redirection artifact claims.
77 let publicRows=[];
78 for(let [name,mode] of [['encode',1],['decode',2],['identity',3],['provenance',4],['replay',5]]){
79 let r=command('sh',['tools/source-aware/sa',name],all);assert.equal(r.status,0);assert.ok(r.stdout.equals(expected(mode,all)));publicRows.push({name,status:r.status,bytes:r.stdout.length});
80 }
81 for(let sample of Object.values(samples))for(let name of ['decode','replay']){
82 let r=command('sh',['tools/source-aware/sa',name],Buffer.from(sample.hex,'hex'));assert.equal(r.status,sample.expectedCode);assert.equal(r.stdout.length,0);assert.ok(r.stderr.length>0);publicRows.push({name,expectedCode:sample.expectedCode,status:r.status,bytes:0});
83 }
84 for(let [input,code,label] of [[missing,73,'PROVENANCE_MISSING'],[Buffer.alloc(8193),67,'LIMIT']]){
85 let r=command('sh',['tools/source-aware/sa','replay'],input);assert.equal(r.status,code);assert.equal(r.stdout.length,0);assert.ok(r.stderr.toString().includes(label));publicRows.push({name:label,status:r.status,bytes:0});
86 }
87 for(let args of [[],['wat'],['decode','extra']]){let r=command('sh',['tools/source-aware/sa',...args],all);assert.equal(r.status,64);assert.equal(r.stdout.length,0);publicRows.push({name:'BAD_COMMAND',status:r.status});}
88 const caps=Buffer.of(2,0,1,63),r=command('sh',['tools/source-aware/sa','legality'],Buffer.concat([caps,s1]));assert.equal(r.status,0);assert.ok(r.stdout.equals(legality(inspect(s1),caps)));publicRows.push({name:'legality',status:r.status});
89 // Direct core caller succeeds then fails, across direct decode/perform entry points.
90 let caller='fn main to int\nconstants\n';for(let [i,c] of s1.entries())caller+=`put Ib ${i} ${c}\n`;
91 caller+=`code = perform 1 ${s1.length}\nif ne code 0\nret 1\nend\nif eq get El 0 0\nret 2\nend\ncode = decode 0\nif ne code 65\nret 3\nend\nif ne get El 0 0\nret 4\nend\nput El 0 12\ncode = perform 6 8193\nif ne code 67\nret 5\nend\nif ne get El 0 0\nret 6\nend\nput El 0 12\ncode = perform 0 0\nif ne code 64\nret 7\nend\nif ne get El 0 0\nret 8\nend\nret 0\nend\n`;
92 let direct=compile(vendor+core+caller,'direct');let dr=command(direct.exe);assert.equal(dr.status,0);assert.equal(dr.stdout.length,0);report.directCore=direct;report.publicCommands={requested:publicRows.length,executed:publicRows.length,rows:publicRows};
93 // Actual build failure after a prior success marker, in an isolated copy of owned sources.
94 const scratch=path.join(run,'build-failure-probe');fs.mkdirSync(scratch,{recursive:true});
95 for(let p of ['tools/source-aware','src/source-aware','spec/universal'])fs.cpSync(p,path.join(scratch,p),{recursive:true});
96 const sb=path.join(scratch,'build/L574');fs.mkdirSync(sb,{recursive:true});
97 fs.cpSync(path.join(root,'toolchain'),path.join(sb,'toolchain'),{recursive:true});
98 fs.writeFileSync(path.join(sb,'PASS.json'),'stale success');fs.writeFileSync(path.join(sb,'sa'),'stale executable');
99 fs.appendFileSync(path.join(scratch,'src/source-aware/core.om'),'\nINVALID_OMEGA_SYNTAX\n');
100 let failedBuild=command('bash',[path.join(scratch,'tools/source-aware/build')]);fs.writeFileSync(path.join(run,'expected-build-failure.log'),failedBuild.stderr);
101 assert.notEqual(failedBuild.status,0);for(let p of ['PASS.json','sa','sa.tmp'])assert.ok(!fs.existsSync(path.join(sb,p)),'stale artifact '+p);
102 report.failureLifecycle={actualBuildFailureStatus:failedBuild.status,partialExecutableAbsent:true,staleExecutableAbsent:true,stalePASSAbsent:true};
103 report.steps.push('legality/identity/edges/public/direct');console.log('Structural, capabilities, public commands and direct transactional checks passed. Compiling semantic mutants.');
104 // Small decisive corpus for each independently compiled semantic mutant.
105 let controls=[...requests.slice(0,310),...edge,...bad.slice(0,categories.length).map(d=>request(2,d.input,d.code)),...legalRequests];
106 let mutants=[
107 ['profile-identity',s=>s.replace('part 13 get M 5\ndigout','put Ib 19 1\npart 13 get M 5\ndigout')],
108 ['bigint-wrapped-legality',s=>s.replace('if or eq get M 1 3 eq get M 1 6\nret 4','if or eq get M 1 3 eq get M 1 6\nret 3')],
109 ['source-hash-bypass',s=>s.replace('if eq hashed start n add off 42 false','if false')],
110 ['span-bound-bypass',s=>s.replace('if or gt a b gt b n','if false')],
111 ['constant-effects-bypass',s=>s.replace('ret pureop i','ret 0')],
112 ['output-reset-missing',s=>s.replaceAll('put El 0 0','note mutant output reset omitted')],
113 ['dropped-obligation-encoding',s=>s.replace('ebyte get Oob i','ebyte 0')],
114 ['unknown-version-admitted',s=>s.replace('if ne get Ib 17 1','if false')],
115 ['UTF8-overlong-admitted',s=>s.replace('if lt v low','if false')],
116 ];
117 report.mutants=[];
118 for(let [name,mutate] of mutants){let changed=mutate(core);assert.notEqual(changed,core);let built=compile(vendor+changed+batch,'mutant-'+name);let result=runBatch(built.exe,controls,{strict:false});assert.ok(result.mismatches>0,'survived '+name);assert.ok(result.executed>0,'never executed '+name);report.mutants.push({name,...built,...result,caught:true});}
119 report.steps.push('mutants');
120 // Lower API compatibility is a separate test-only legacy host lane.
121 const legacyScript=path.resolve('tests/universal/source-aware/legacy.mjs');let lr=command(process.execPath,[legacyScript,run]);fs.writeFileSync(path.join(run,'legacy.log'),Buffer.concat([lr.stdout,lr.stderr]));assert.equal(lr.status,0,'legacy lane '+lr.stderr);report.legacy=JSON.parse(fs.readFileSync(path.join(run,'legacy.json')));
122 report.steps.push('lower compatibility');
123 report.fixtureFiles={};for(let p of ['spec/universal/source-aware-v1.md','src/source-aware/core.om','src/source-aware/main.om','tests/universal/source-aware/fixtures.mjs','tests/universal/source-aware/reference.mjs','tests/universal/source-aware/corpus.mjs','tests/universal/source-aware/batch.om','tests/universal/source-aware/gate.mjs','tests/universal/source-aware/legacy.c','tests/universal/source-aware/legacy.mjs'])report.fixtureFiles[p]=filehash(p);
124 report.verdict='PASS';fs.writeFileSync(path.join(run,'receipt.json'),JSON.stringify(report,null,2)+'\n');fs.writeFileSync(path.join(root,'PASS.json'),JSON.stringify(report,null,2)+'\n');console.log(JSON.stringify({verdict:report.verdict,valid:report.structural.validUnits,malformed:report.structural.malformedUnits,requests:report.structural.executed,mutants:report.mutants.length,run}));
125}catch(e){report.verdict='FAIL';report.failure={message:e.message,stack:e.stack};fs.writeFileSync(path.join(run,'failure.json'),JSON.stringify(report,null,2)+'\n');fs.rmSync(path.join(root,'PASS.json'),{force:true});console.error(e.stack);process.exitCode=1;}