import contextlib,difflib,hashlib,io,json,math,pathlib,sys,traceback,types P=pathlib.Path(__file__).parent def execute(source,name): m=types.ModuleType('reviewed');exec(compile(source,'job1444-threeway.py','exec'),m.__dict__) saved=sys.argv;sys.argv=['review','--out',str(P/name),'--bank',str(P/'L-grid-622.json')] try: with contextlib.redirect_stdout(io.StringIO()),contextlib.redirect_stderr(io.StringIO()):m.main() finally:sys.argv=saved return m,json.loads((P/name).read_text()) def compare(a,b,path=''): if path.endswith('elapsed_s'):return 0 if isinstance(a,dict): assert set(a)==set(b),(path,set(a)^set(b));return 1+sum(compare(a[k],b[k],path+'/'+k) for k in a) if isinstance(a,list): assert len(a)==len(b);return 1+sum(compare(v,w,path) for v,w in zip(a,b)) assert a==b,(path,a,b);return 1 def reference(seq,p,D): best=0 for start in range(D): phases=set(range(p)) for j in range(start,min(start+D,len(seq))): r=seq[j]%p;phases&={r,(r-2)%p} if not phases:break best=max(best,j-start+1) return best try: source=P/'845308fe329a-job1444-threeway.py';raw=source.read_bytes();assert hashlib.sha256(raw).hexdigest()=='845308fe329a9a804812ad3a816c771759124cc544cd7717b1452ac26b0526e7' filed=P/'ad1c072f593d-job1444-threeway.json';assert hashlib.sha256(filed.read_bytes()).hexdigest()=='ad1c072f593d6d4f99f8e9853c4b81426fb91c158130de6583ee5f428b1783c5' bank=P/'L-grid-622.json';bank_sha=hashlib.sha256(bank.read_bytes()).hexdigest() assert bank_sha=='b7451a99e8bae8f69fd0db7e13d5771dc70c9e3bc627d602a72f48082935f3c9' old=raw.decode();m,reproduced=execute(old,'reproduced.json');nodes=compare(reproduced,json.loads(filed.read_text())) target='return [r for r in range(M) if all(r % q for q in qs)]';assert old.count(target)==1 new=old.replace(target,'return [r for r in range(M) if all(r % q and (r + 2) % q for q in qs)]') pm,fixed=execute(new,'corrected-object.json') assert fixed['summary']['cells_compared_to_bank']==fixed['summary']['bank_matches_literal']==126 assert len(reproduced['summary']['bank_mismatches'])==122 and not reproduced['differing_cells'] expected={(7,11),(11,31),(11,37),(11,191)} assert {(c['x'],c['p']) for c in fixed['differing_cells']}==expected checked=0;geometry=[] for x,t in fixed['levels'].items(): M=t['M'];slots=[r for r in range(M) if math.gcd(r,M)==math.gcd(r+2,M)==1] assert slots==pm.admissible(int(x),M) units=[r for r in range(M) if math.gcd(r,M)==1];assert units==m.admissible(int(x),M) geometry.append({'x':int(x),'M':M,'unit_count':len(units),'twin_count':len(slots)}) for row in (c for c in fixed['cells'] if c['x']==int(x)): p=row['p'];true=reference(slots+[r+M for r in slots],p,len(slots));naive=reference(slots+slots,p,len(slots)) assert true==row['literal']==row['true']==row['bank'] and naive==row['naive'];checked+=1 witness={'T5':[11,17,29],'rejected_unit13':{'gcd13_30':math.gcd(13,30),'gcd15_30':math.gcd(15,30)},'T5_p11_true':next(c for c in fixed['cells'] if c['x']==5 and c['p']==11),'T7_p11_true':next(c for c in fixed['cells'] if c['x']==7 and c['p']==11),'T7_seam':{'true_values':[209,221],'true_residues':[209%11,221%11],'naive_values':[209,11],'naive_residues':[209%11,11%11]}} (P/'twin-admissibility.patch').write_text(''.join(difflib.unified_diff(old.splitlines(True),new.splitlines(True),fromfile='a/job1444-threeway.py',tofile='b/job1444-threeway.py')),encoding='utf-8') out={'original_compared_nodes':nodes,'source_sha256':hashlib.sha256(raw).hexdigest(),'filed_output_sha256':hashlib.sha256(filed.read_bytes()).hexdigest(),'bank_sha256':bank_sha,'original_matches':reproduced['summary']['bank_matches_literal'],'original_mismatches':len(reproduced['summary']['bank_mismatches']),'corrected_bank_matches':126,'independent_phase_intersection_cells':checked,'geometry':geometry,'corrected_seam_differences':fixed['differing_cells'],'witness':witness,'scope':'126 small cells only; no folded producer rerun, no additional large tile construction; true field is an alias of literal, not an independent instrument.'} (P/'independent-checks.json').write_text(json.dumps(out,indent=2),encoding='utf-8') except BaseException: (P/'check-error.txt').write_text(traceback.format_exc(),encoding='utf-8');raise