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 / translation / python_selftest.py

368 lines · 25757 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Python lane self-test (#627; spec/universal/python-u64-v1.md, translation-compose-v1.md). Author-run TESTED
3evidence, not QUALIFIED.
4
5Same-language (Python -> Python), for every F2-python positive and accepted boundary control: native import;
6structural decode of the Python-member unit (header/op bytes, obligations, spans, sidecar); the #621 codec
7(`bash tools/source-aware/sa identity` in --sa-root) accepts it and agrees with the SHA256 identity; export;
8the semantic-only envelope exports the same bytes; re-import; compose section 4.1 identity
9N(strip(U_B)) = N(strip(U_A)); the F2 rewrite has the same N(strip()) as the C import of its F1 source;
10python3 runs the original and the exported source on boundary and seeded random vectors and both must equal
11the bigint evaluation of the records (REFERENCE + DIFFERENTIAL).
12
13Compositions: C -> Python on every F1 positive and accepted C boundary control (C importer, original C by cc,
14generated Python by python3). The native realization leg (translation-v1 section 5 lowering) is NOT_RUN.
15Python -> C runs only with --c-export (a translation-v1 section 6 exporter CLI); that exporter accepts the C
16member only, so the Python unit is retargeted R(U, C) first. Without it the direction is NOT_RUN.
17
18Also: every refusal fixture (exact status, no stdout), exporter family acceptance (R(U, C/Rust/Python) export
19identically), exporter refusals (profiles 3/5/6, division, malformed, limit, bad name), constructed
20non-NORMALIZED / permuted-slot / shared-SSA units, and the public-command identity guards."""
21from pathlib import Path
22import argparse,hashlib,itertools,json,os,random,shutil,struct,subprocess,sys,tempfile
23ROOT=Path(__file__).resolve().parents[3];sys.dont_write_bytecode=True
24MASK=(1<<64)-1
25BOUNDARY=[0,1,2,(1<<32)-1,1<<32,(1<<63)-1,1<<63,MASK-1,MASK]
26IMPORTER=ROOT/'build/L627/import/python-import';EXPORTER=ROOT/'build/L627/export/python-export'
27ICLI=ROOT/'tools/adapters/python-import';ECLI=ROOT/'tools/adapters/python-export'
28SEMHASH=hashlib.sha256((ROOT/'spec/universal/semantics.md').read_bytes()).digest()
29FAMILY={'C':(1,1,1,1),'Rust':(2,2,2,1),'Python':(3,4,3,5)}
30CFLAGS=['-std=c17','-O2','-Wall','-Werror','-Wno-unused-parameter']
31PYORACLE='''import sys
32path,name,n=sys.argv[1],sys.argv[2],int(sys.argv[3])
33ns={}
34exec(compile(open(path,'rb').read().decode('ascii'),path,'exec'),ns)
35f=ns[name]
36for line in sys.stdin:
37 xs=[int(t) for t in line.split()]
38 assert len(xs)==n
39 v=f(*xs)
40 assert type(v) is int and 0<=v<2**64,(xs,v)
41 print(v)
42'''
43
44class Fail(Exception):pass
45
46def check(cond,msg):
47 if not cond:raise Fail(msg)
48
49def run(cmd,data=b'',**kw):
50 return subprocess.run(cmd,input=data,capture_output=True,timeout=60,**kw)
51
52def semantic(env):
53 check(env[:5]==b'OMSB\x01','bad envelope magic');ns,np=struct.unpack_from('<II',env,5);check(len(env)==13+ns+np,'envelope length')
54 return env[13:13+ns]
55
56def semonly(sem):
57 return b'OMSB\x01'+struct.pack('<II',len(sem),0)+sem
58
59def records(sem):
60 count=struct.unpack_from('<H',sem,10)[0]
61 return [struct.unpack_from('<8BHHQ',sem,44+20*i) for i in range(count)]
62
63def ops_of(sem):
64 return [(op,a,b,imm) for (d,v,op,t,attr,e,al,ob,a,b,imm) in records(sem)]
65
66def retarget(sem,member):
67 """compose section 2: R(U, M) rewrites language/profile bytes, op dialect bytes, arithmetic obligations."""
68 lang,prof,dia,obl=FAMILY[member];b=bytearray(sem);b[5]=lang;b[6]=prof
69 for i in range(struct.unpack_from('<H',sem,10)[0]):
70 o=44+20*i;b[o]=dia
71 if b[o+2] in (2,3,4):b[o+7]=obl
72 return bytes(b)
73
74def normalized(ops):
75 n=len(ops)
76 return n>=3 and ops[n-2]==(1,65535,65535,0) and ops[n-1][0]==2 and ops[n-1][2]==n-2 and ops[n-1][1]<n-2 and ops[n-1][3]==0
77
78def strip(sem):
79 ops=ops_of(sem)
80 if not normalized(ops):return sem
81 n=len(ops)-2;return sem[:10]+struct.pack('<H',n)+sem[12:44+20*n]
82
83def N(sem):
84 return retarget(sem,'C')
85
86def evaluate(ops,args):
87 values=[]
88 for op,a,b,imm in ops:
89 if op==9:v=args[imm]
90 elif op==1:v=imm
91 elif op==2:v=values[a]+values[b]
92 elif op==3:v=values[a]-values[b]
93 elif op==4:v=values[a]*values[b]
94 else:raise Fail('non-S1 op %d'%op)
95 values.append(v&MASK)
96 return values[-1]
97
98def decode_python(env,src,arity):
99 """Independent structural decode of one python-import envelope (python-u64-v1.md, source-aware-v1.md)."""
100 ns,np=struct.unpack_from('<II',env,5);sem=semantic(env);p=env[13+ns:]
101 check(sem[:10]==b'OMSA\x01\x03\x04\x01\x00\x01','wrong Python member header')
102 count=struct.unpack_from('<H',sem,10)[0];check(1<=count<=64 and len(sem)==44+20*count,'operation count')
103 check(sem[12:44]==SEMHASH,'wrong semantic contract digest')
104 check(p[:6]==b'OMSP\x01\x01' and struct.unpack_from('<I',p,6)[0]==len(src),'sidecar header (encoding 1)')
105 check(p[10:42]==hashlib.sha256(sem).digest() and p[42:74]==hashlib.sha256(src).digest(),'provenance binding')
106 check(len(p)==74+8*count+len(src) and p[74+8*count:]==src,'source replay bytes')
107 slots=[];ops=[]
108 for i,(d,v,op,t,attr,eff,al,ob,a,b,imm) in enumerate(records(sem)):
109 lo,hi=struct.unpack_from('<II',p,74+8*i);check(lo<=hi<=len(src),'span bounds');text=src[lo:hi]
110 check(d==3 and v==1 and t==1 and eff==0 and al==0,'op %d fixed fields'%i)
111 if op==9:
112 check(attr==0 and ob==1 and a==b==65535 and imm<arity,'input record');slots.append(imm)
113 check(text[:1].isalpha() and text.replace(b'_',b'').isalnum(),'input span is not the parameter name')
114 elif op==1:
115 check(attr==1 and ob==0 and a==b==65535,'constant record')
116 check(text==str(imm).encode() or (i==count-2 and (lo,hi)==(0,0) and imm==0),'constant span')
117 else:
118 check(op in (2,3,4) and attr==1 and ob==5 and a<i and b<i and imm==0,'arithmetic record (obligations 5)')
119 check(text=={2:b'+',3:b'-',4:b'*'}[op] or (i==count-1 and (lo,hi)==(0,0)),'operator span')
120 ops.append((op,a,b,imm))
121 check(sorted(slots)==list(range(arity)) and slots==list(range(arity)),'parameter slots')
122 check(normalized(ops),'importer declares the +0 normalization pair')
123 return ops
124
125def vectors(nargs,rng):
126 if nargs==0:return [()]
127 if nargs<=2:vs=list(itertools.product(BOUNDARY,repeat=nargs))
128 else:vs=[tuple(BOUNDARY[(j+k)%len(BOUNDARY)] for k in range(nargs)) for j in range(len(BOUNDARY))]+[tuple(x if k==s else 1 for k in range(nargs)) for s in range(nargs) for x in BOUNDARY]
129 return vs+[tuple(rng.getrandbits(64) for _ in range(nargs)) for _ in range(64)]
130
131def payload(vs):
132 return ''.join(' '.join(map(str,v))+'\n' for v in vs).encode()
133
134def python_oracle(out,tag,src,name,nargs,vs):
135 path=out/(tag+'.py');path.write_bytes(src)
136 r=run([sys.executable,'-I','-B',str(out/'pyoracle.py'),str(path),name,str(nargs)],payload(vs))
137 check(r.returncode==0,tag+' python3 oracle failed: '+r.stderr.decode(errors='replace')[-400:])
138 return [int(x) for x in r.stdout.split()]
139
140def c_harness(name,nargs):
141 if not nargs:return '\n#include <stdio.h>\n#include <inttypes.h>\nint main(void){printf("%" PRIu64 "\\n",'+name+'());return 0;}\n'
142 scan=' '.join(['%" SCNu64 "']*nargs);ptrs=','.join('&x['+str(j)+']' for j in range(nargs));args=','.join('x['+str(j)+']' for j in range(nargs))
143 return '\n#include <stdio.h>\n#include <inttypes.h>\nint main(void){uint64_t x[6]={0};while(scanf("'+scan+'",'+ptrs+')=='+str(nargs)+')printf("%" PRIu64 "\\n",'+name+'('+args+'));return 0;}\n'
144
145def c_oracle(out,tag,src,name,nargs,vs):
146 c=out/(tag+'.c');exe=out/tag;c.write_bytes(src+c_harness(name,nargs).encode())
147 r=run([os.environ.get('CC','cc')]+CFLAGS+[str(c),'-o',str(exe)]);check(r.returncode==0,tag+' cc failed: '+r.stderr.decode(errors='replace')[-400:])
148 r=run([str(exe)],payload(vs));check(r.returncode==0,tag+' C run failed')
149 return [int(x) for x in r.stdout.split()]
150
151class Ctx:
152 def __init__(s,a,out):
153 s.out=out;s.sa_root=Path(a.sa_root).resolve();s.cimport=Path(a.c_import).resolve();s.c_export=a.c_export
154 s.rng=random.Random(627624);s.observations=0;s.sa_requests=0
155 def sa(s,cmd,data):
156 s.sa_requests+=1;return run(['bash','tools/source-aware/sa',cmd],data,cwd=str(s.sa_root))
157 def identity(s,env):
158 r=s.sa('identity',env);check(r.returncode==0 and len(r.stdout)==32,'sa identity refused: rc=%d %s'%(r.returncode,r.stderr.decode(errors='replace')))
159 check(r.stdout==hashlib.sha256(b'omega.source.semantic.v1'+semantic(env)).digest(),'sa identity differs from SHA256 semantic identity')
160 return r.stdout.hex()
161 def pyimport(s,src):
162 r=run([str(IMPORTER)],src);check(r.returncode==0 and r.stdout,'python import refused rc=%d %s'%(r.returncode,r.stderr.decode(errors='replace')))
163 return r.stdout
164 def pyexport(s,env,name=None):
165 r=run([sys.executable,str(ECLI)]+([name] if name else []),env)
166 check(r.returncode==0 and r.stdout,'python export refused rc=%d %s'%(r.returncode,r.stderr.decode(errors='replace')))
167 return r.stdout
168 def cimp(s,src):
169 r=run([str(s.cimport)],src);check(r.returncode==0,'C import refused rc=%d'%r.returncode);return r.stdout
170 def agree(s,tag,ops,nargs,legs):
171 """legs: name -> list of outputs; all must equal the REFERENCE evaluation of ops."""
172 vs=legs.pop('_vectors');ref=[evaluate(ops,v) for v in vs]
173 for leg,vals in legs.items():
174 if vals!=ref:
175 bad=[(list(v),x,y) for v,x,y in zip(vs,vals,ref) if x!=y][:5]
176 raise Fail('%s: %s disagrees with REFERENCE on %d vectors, e.g. %s'%(tag,leg,sum(x!=y for x,y in zip(vals,ref))+abs(len(vals)-len(ref)),bad))
177 s.observations+=len(vs)*len(legs);return len(vs)
178
179def same_language(ctx,case,cases_c):
180 tag=case['id'];src=case['source'].encode();nargs=case['inputs'];name=case['name'];out=ctx.out
181 check(hashlib.sha256(src).hexdigest()==case['sha256'],tag+' fixture sha256')
182 ua=ctx.pyimport(src)
183 cli=run([sys.executable,str(ICLI)],src);check(cli.returncode==0 and cli.stdout==ua,'public python-import differs from native')
184 ops=decode_python(ua,src,nargs);ident=ctx.identity(ua)
185 srcb=ctx.pyexport(ua)
186 check(ctx.pyexport(semonly(semantic(ua)))==srcb,'semantic-only envelope exports differently (replayed, not generated)')
187 native=run([str(EXPORTER)],b'\x00'+ua);check(native.returncode==0 and native.stdout==srcb,'native exporter differs from public command')
188 ub=ctx.pyimport(srcb);decode_python(ub,srcb,nargs);ctx.identity(ub)
189 check(N(strip(semantic(ub)))==N(strip(semantic(ua))),'compose 4.1 identity N(strip(U_B)) != N(strip(U_A))')
190 exact=semantic(ub)==semantic(ua)
191 row={'id':tag,'inputs':nargs,'ops':len(ops),'source_bytes':len(src),'identity':ident,'reimport_semantic_bytes_equal':exact,
192 'exported_sha256':hashlib.sha256(srcb).hexdigest()}
193 csrc=cases_c.get(case.get('f1') or case.get('control'))
194 if csrc is not None:
195 uc=ctx.cimp(csrc);check(N(strip(semantic(uc)))==N(strip(semantic(ua))),'F2 rewrite is not the F1 expression: N(strip()) differs from the C import')
196 check(N(semantic(uc))==N(semantic(ua)),'F2 rewrite N() differs from the C import');row['same_N_as_F1_C_import']=True
197 vs=vectors(nargs,ctx.rng)
198 legs={'_vectors':vs,'python3(original)':python_oracle(out,tag+'-orig',src,name,nargs,vs),'python3(exported)':python_oracle(out,tag+'-exp',srcb,'calc',nargs,vs)}
199 row['vectors']=ctx.agree(tag,ops,nargs,legs);row['result']='PASS';(out/(tag+'.exported.py')).write_bytes(srcb)
200 return row,srcb
201
202def c_to_python(ctx,tag,csrc,name,nargs):
203 out=ctx.out;ua=ctx.cimp(csrc);ctx.identity(ua);ops=ops_of(semantic(ua))
204 srcb=ctx.pyexport(ua);check(ctx.pyexport(semonly(semantic(ua)))==srcb,'C->Python: semantic-only export differs')
205 for m in ['Rust','Python']:
206 env=semonly(retarget(semantic(ua),m));ctx.identity(env)
207 check(ctx.pyexport(env)==srcb,'family: R(U,%s) exports differently'%m)
208 ub=ctx.pyimport(srcb);decode_python(ub,srcb,nargs)
209 check(N(strip(semantic(ub)))==N(strip(semantic(ua))),'C->Python identity N(strip(U_B)) != N(strip(U_A))')
210 vs=vectors(nargs,ctx.rng)
211 legs={'_vectors':vs,'cc(original C)':c_oracle(out,tag+'-c',csrc,name,nargs,vs),'python3(generated)':python_oracle(out,tag+'-py',srcb,'calc',nargs,vs)}
212 n=ctx.agree(tag,ops,nargs,legs);(out/(tag+'.generated.py')).write_bytes(srcb)
213 return {'id':tag,'identity':'PASS','differential':'PASS','legs':['REFERENCE','cc(original C)','python3(generated Python)'],
214 'native_realization':'NOT_RUN','vectors':n,'generated_sha256':hashlib.sha256(srcb).hexdigest(),'result':'PASS'},srcb
215
216def python_to_c(ctx,case):
217 out=ctx.out;tag='P2C-'+case['id'];src=case['source'].encode();nargs=case['inputs']
218 ua=ctx.pyimport(src);ops=ops_of(semantic(ua))
219 # The section 6 C exporter (G1) accepts the C member only: retarget R(U, C) first.
220 rc=semonly(retarget(semantic(ua),'C'));ctx.identity(rc)
221 r=run([sys.executable,ctx.c_export],rc);check(r.returncode==0,'C export refused R(U,C) rc=%d %s'%(r.returncode,r.stderr.decode(errors='replace')))
222 srcb=r.stdout;ub=ctx.cimp(srcb)
223 check(N(strip(semantic(ub)))==N(strip(semantic(ua))),'Python->C identity N(strip(U_B)) != N(strip(U_A))')
224 vs=vectors(nargs,ctx.rng)
225 legs={'_vectors':vs,'python3(original Python)':python_oracle(out,tag+'-py',src,case['name'],nargs,vs),'cc(generated C)':c_oracle(out,tag+'-c',srcb,'calc',nargs,vs)}
226 return {'id':tag,'identity':'PASS','differential':'PASS','retargeted_to_C_first':True,'native_realization':'NOT_RUN','vectors':ctx.agree(tag,ops,nargs,legs),'result':'PASS'}
227
228def unit(recs,lang=1,profile=1,runtime=0,abi=1,dialect=None,types=None,obl_arith=None,contract=SEMHASH):
229 """Semantic-only envelope from (op,a,b,imm) records with registry attributes for u64 members."""
230 dialect=lang if dialect is None else dialect;body=b''
231 if obl_arith is None:obl_arith=5 if profile in (4,6) else 1
232 for op,a,b,imm in recs:
233 attr=0 if op==9 else 1;obl=1 if op==9 else 0 if op==1 else (obl_arith|2 if op==5 else obl_arith)
234 body+=struct.pack('<8BHHQ',dialect,1,op,1,attr,0,0,obl,a,b,imm)
235 return semonly(b'OMSA\x01'+bytes([lang,profile,1,runtime,abi])+struct.pack('<H',len(recs))+contract+body)
236
237def bigint_unit():
238 """Profile 3 (cpython-int-pure-v1): type 2 inputs/constants, bigint add (effects 20, alias 255, obligations 8)."""
239 body=struct.pack('<8BHHQ',3,1,9,2,0,0,0,1,65535,65535,0)+struct.pack('<8BHHQ',3,1,9,2,0,0,0,1,65535,65535,1)+struct.pack('<8BHHQ',3,1,2,2,2,20,255,8,0,1,0)
240 return semonly(b'OMSA\x01'+bytes([3,3,1,1,1])+struct.pack('<H',3)+SEMHASH+body)
241
242def exporter_checks(ctx):
243 rows=[];out=ctx.out
244 def refuse(name,env,want,args=()):
245 r=run([sys.executable,str(ECLI)]+list(args),env)
246 check(r.returncode==want and r.stdout==b'' and b'PY_EXPORT_' in r.stderr,'%s: want %d got %d %r'%(name,want,r.returncode,r.stderr))
247 rows.append({'name':name,'exit':r.returncode,'result':'PASS'})
248 add=[(9,65535,65535,0),(9,65535,65535,1),(2,0,1,0)]
249 for name,env in [('profile6-same-records',unit(add,lang=3,profile=6,runtime=1,abi=2)),('profile3-bigint',bigint_unit()),('osc-profile5',unit(add,lang=4,profile=5))]:
250 d=ctx.sa('decode',env);check(d.returncode==0 and d.stdout==env,name+': constructed unit is not codec-valid')
251 refuse(name,env,68)
252 div=unit([(9,65535,65535,0),(9,65535,65535,1),(5,0,1,0)])
253 d=ctx.sa('decode',div);check(d.returncode==0,'division unit not codec-valid');refuse('u64-division',div,68)
254 good=unit(add)
255 refuse('truncated',good[:-1],65);refuse('trailing-byte',good+b'\x00',65);refuse('bad-magic',b'X'+good[1:],65)
256 refuse('wrong-contract',unit(add,contract=b'\x00'*32),65);refuse('empty',b'',65)
257 blow=[(9,65535,65535,0)]+[(4,k,k,0) for k in range(6)]
258 refuse('tree-expansion-over-64-operations',unit(blow),67)
259 for bad in ['Calc','def','x'*33,'1abc','a-b']:refuse('bad-name-'+bad[:8],good,64,[bad])
260 refuse('two-arguments',good,64,['a','b'])
261 # Constructed units: not NORMALIZED, permuted input slots, shared SSA results; evaluation equality.
262 for name,recs,n in [('not-normalized',add,2),('permuted-slots',[(9,65535,65535,1),(9,65535,65535,0),(3,0,1,0)],2),
263 ('shared-ssa',[(9,65535,65535,0),(9,65535,65535,1),(4,0,1,0),(2,2,2,0)],2),
264 ('const-root-not-normalized',[(1,65535,65535,MASK)],0),('input-root-last-slot',[(9,65535,65535,0),(9,65535,65535,1)],2),
265 ('capacity-61-chain-not-normalized',[(9,65535,65535,0),(9,65535,65535,1)]+[(2,0 if k==0 else k+1,0,0) for k in range(60)],2)]:
266 env=unit(recs);ctx.identity(env);src=ctx.pyexport(env);ub=ctx.pyimport(src);decode_python(ub,src,n)
267 ops=ops_of(semantic(env));vs=vectors(n,ctx.rng)
268 nv=ctx.agree(name,ops,n,{'_vectors':vs,'reimport':[evaluate(ops_of(semantic(ub)),v) for v in vs],'python3(exported)':python_oracle(out,'ctor-'+name,src,'calc',n,vs)})
269 rows.append({'name':name,'result':'PASS','vectors':nv,'identity_equal_after_reimport':N(strip(semantic(ub)))==N(strip(semantic(env))),
270 'note':'not NORMALIZED or not import-shaped: re-import adds the pair / reorders inputs; checked by equal evaluation (translation-v1 section 4)'})
271 env=unit(add);src=ctx.pyexport(env,'omega_fold');check(src.startswith(b'def omega_fold(i0, i1):\n'),'named export');ctx.pyimport(src)
272 rows.append({'name':'named-export','result':'PASS'})
273 return rows
274
275def refusals(ctx,fx):
276 rows=[]
277 for r in fx['refusals']:
278 src=bytes.fromhex(r['source_hex']) if 'source_hex' in r else r['source'].encode()
279 check(hashlib.sha256(src).hexdigest()==r['sha256'],r['id']+' fixture sha256')
280 p=run([str(IMPORTER)],src);ok=p.returncode==r['status'] and p.stdout==b'' and p.stderr.startswith(b'PY_IMPORT_')
281 rows.append({'id':r['id'],'want':r['status'],'exit':p.returncode,'stderr':p.stderr.decode(errors='replace').strip(),'result':'PASS' if ok else 'FAIL'})
282 return rows
283
284def public_checks(ctx):
285 rows=[];src=b'def calc(a, b):\n return (a + b) & 0xFFFFFFFFFFFFFFFF\n'
286 r=run([sys.executable,str(ICLI),'x'],src);check(r.returncode==64 and not r.stdout and b'PY_IMPORT_BAD_COMMAND' in r.stderr,'import args');rows.append('import-arguments-refused')
287 r=subprocess.run([str(IMPORTER)],stdin=subprocess.DEVNULL,stdout=subprocess.PIPE,stderr=subprocess.PIPE,preexec_fn=lambda:os.close(0),timeout=10)
288 check(r.returncode==77 and not r.stdout and r.stderr==b'PY_IMPORT_IO\n','closed stdin');rows.append('import-closed-stdin')
289 env=ctx.pyimport(src)
290 for tool,rels in [('python-import',['src/source-aware/vendor/sha256.om','src/adapters/python-u64-import.om','build/L627/import/build.json','build/L627/import/python-import']),
291 ('python-export',['src/source-aware/vendor/sha256.om','src/source-aware/core.om','src/adapters/python-u64-export.om','build/L627/export/build.json','build/L627/export/python-export'])]:
292 for fault in ['source','binary']:
293 with tempfile.TemporaryDirectory(prefix='closed-build-',dir=ctx.out) as td:
294 t=Path(td)
295 for rel in ['tools/adapters/'+tool]+rels:
296 p=t/rel;p.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(ROOT/rel,p);p.chmod(0o700 if rel.endswith(tool) else 0o600)
297 victim=t/(rels[-3] if fault=='source' else rels[-1]);victim.write_bytes(victim.read_bytes()+b'changed')
298 r=run([sys.executable,str(t/'tools/adapters'/tool)],src if tool=='python-import' else env)
299 check(r.returncode==78 and not r.stdout and b'STALE_OR_MISSING_BUILD' in r.stderr,tool+' '+fault+' identity change not refused');rows.append(tool+'-'+fault+'-identity-change-refused')
300 return rows
301
302def main():
303 ap=argparse.ArgumentParser();ap.add_argument('--out',required=True)
304 ap.add_argument('--sa-root',default=str(ROOT),help='checkout whose tools/source-aware/sa (build/L574/sa) is the reference codec')
305 ap.add_argument('--c-import',default=str(ROOT/'build/L589/c-import'))
306 ap.add_argument('--c-export',default='',help='translation-v1 section 6 C exporter CLI (tools/adapters/c-export); empty = Python->C NOT_RUN')
307 a=ap.parse_args();out=Path(a.out).resolve();out.mkdir(parents=True,exist_ok=False);(out/'pyoracle.py').write_text(PYORACLE)
308 ctx=Ctx(a,out)
309 for p in [IMPORTER,EXPORTER,ctx.cimport]:check(p.read_bytes()[:4]==b'\x7fELF','missing native artifact '+str(p))
310 fx=json.loads((ROOT/'tests/universal/translation/fixtures-f2-python.json').read_text())
311 f1=json.loads((ROOT/'tests/universal/translation/fixtures-f1.json').read_text())
312 cases_c={f['id']:f['source'].encode() for f in f1['positives']}
313 H='#include <stdint.h>\n';base=(H+'uint64_t calc(uint64_t a,uint64_t b){return a+b;}').encode()
314 controls_c={'exact-source-capacity':base+b' '*(4096-len(base)),'comment-at-capacity':base+b'//'+b'x'*(4096-len(base)-2),
315 'operation-capacity':(H+'uint64_t calc(uint64_t a,uint64_t b){return '+'+'.join(['a']*61)+';}').encode(),
316 'nesting-limit':(H+'uint64_t calc(uint64_t a,uint64_t b){return '+'('*16+'a'+')'*16+';}').encode()}
317 cases_c.update(controls_c)
318 report={'schema':'omega.python-u64.selftest.v1','lane':'#627','classification':'TESTED (author self-test), not QUALIFIED','proof':False,
319 'source_commit':subprocess.check_output(['git','-C',str(ROOT),'rev-parse','HEAD'],text=True).strip(),
320 'importer_sha256':hashlib.sha256(IMPORTER.read_bytes()).hexdigest(),'exporter_sha256':hashlib.sha256(EXPORTER.read_bytes()).hexdigest(),
321 'c_importer':str(ctx.cimport),'c_importer_sha256':hashlib.sha256(ctx.cimport.read_bytes()).hexdigest(),
322 'sa_binary_sha256':hashlib.sha256((ctx.sa_root/'build/L574/sa').read_bytes()).hexdigest(),'python3':sys.version.split()[0]}
323 failures=[];same=[];c2p=[];p2c=[];shown={}
324 for case in fx['positives']+fx['controls']:
325 try:
326 row,srcb=same_language(ctx,case,cases_c);same.append(row)
327 if case.get('f1') in ('F1-P05','F1-P13'):shown['python->python '+case['id']]=srcb.decode()
328 except Fail as e:failures.append({'case':case['id'],'failure':str(e)});same.append({'id':case['id'],'result':'FAIL','failure':str(e)})
329 for f in f1['positives']:
330 try:
331 row,srcb=c_to_python(ctx,'C2P-'+f['id'],f['source'].encode(),f['name'],f['inputs']);c2p.append(row)
332 if f['id'] in ('F1-P05','F1-P13'):shown['C->Python '+f['id']]=srcb.decode()
333 except Fail as e:failures.append({'case':'C2P-'+f['id'],'failure':str(e)})
334 for cid,csrc in controls_c.items():
335 try:c2p.append(c_to_python(ctx,'C2P-ctl-'+cid,csrc,'calc',2)[0])
336 except Fail as e:failures.append({'case':'C2P-ctl-'+cid,'failure':str(e)})
337 if a.c_export:
338 for case in fx['positives']+fx['controls']:
339 try:p2c.append(python_to_c(ctx,case))
340 except Fail as e:failures.append({'case':'P2C-'+case['id'],'failure':str(e)})
341 p2c_status={'status':'RUN','c_export':a.c_export,'note':'R(U, C) applied before the C exporter (it accepts the C member only)'}
342 else:
343 p2c_status={'status':'NOT_RUN','reason':'no --c-export given (translation-v1 section 6 exporter is not on this base)'}
344 try:exp=exporter_checks(ctx)
345 except Fail as e:failures.append({'case':'exporter-checks','failure':str(e)});exp=[]
346 neg=refusals(ctx,fx);failures+=[{'case':r['id'],'failure':'refusal status %d != %d'%(r['exit'],r['want'])} for r in neg if r['result']!='PASS']
347 try:pub=public_checks(ctx)
348 except Fail as e:failures.append({'case':'public-commands','failure':str(e)});pub=[]
349 report.update({'result':'FAIL' if failures else 'PASS','same_language':{'executed':len(same),'passed':sum(r.get('result')=='PASS' for r in same),'cases':same},
350 'not_in_profile':fx['not_in_profile'],
351 'compositions':{'C->Python':{'executed':len(c2p),'passed':sum(r['result']=='PASS' for r in c2p),'native_realization':'NOT_RUN (translation-v1 section 5 lowering is not on this base)','cases':c2p},
352 'Python->C':dict(p2c_status,executed=len(p2c),passed=sum(r['result']=='PASS' for r in p2c),cases=p2c)},
353 'refusals':{'executed':len(neg),'passed':sum(r['result']=='PASS' for r in neg),'cases':neg},
354 'exporter_checks':{'executed':len(exp),'passed':len(exp),'cases':exp},'public_command_checks':pub,
355 'observations':ctx.observations,'source_aware_requests':ctx.sa_requests,'shown_exports':shown,'failures':failures})
356 (out/'result.json').write_text(json.dumps(report,indent=2)+'\n')
357 for k,v in shown.items():print('--- '+k+'\n'+v,end='')
358 summary={'result':report['result'],'same_language':'%d/%d'%(report['same_language']['passed'],len(same)),
359 'C->Python':'%d/%d (native realization NOT_RUN)'%(report['compositions']['C->Python']['passed'],len(c2p)),
360 'Python->C':p2c_status['status']+(' %d/%d'%(len(p2c),len(p2c)) if a.c_export and not any(f['case'].startswith('P2C') for f in failures) else ''),
361 'refusals':'%d/%d'%(report['refusals']['passed'],len(neg)),'exporter_checks':len(exp),'public_command_checks':len(pub),
362 'not_in_profile':len(fx['not_in_profile']),'observations':ctx.observations,'sa_requests':ctx.sa_requests}
363 print(json.dumps(summary,indent=1))
364 for f in failures[:20]:print('FAIL',f['case'],f['failure'][:300])
365 print(('PASS' if not failures else 'FAIL')+' python-u64 selftest; report '+str(out/'result.json'))
366 return 1 if failures else 0
367
368if __name__=='__main__':raise SystemExit(main())