"""Independent patch application and scope check for the job696 audit.""" import argparse from fractions import Fraction import hashlib import json from pathlib import Path import shutil import subprocess import tempfile p=argparse.ArgumentParser() p.add_argument('--inputs',type=Path,required=True) p.add_argument('--bundle',type=Path,required=True) a=p.parse_args() manifest=json.loads((a.bundle/'registry-696-manifest.json').read_text(encoding='utf-8')) hashes={} with tempfile.TemporaryDirectory(prefix='registry696-') as name: root=Path(name) for target,info in manifest['revised_files'].items(): src=a.inputs/'QUESTIONS.md' if target=='research/QUESTIONS.md' else a.inputs/'sources'/target.removeprefix('research/') assert hashlib.sha256(src.read_bytes()).hexdigest()==info['base_sha256'] dst=root/target;dst.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(src,dst) subprocess.run(['patch','-p1','-i',str((a.bundle/'registry-696.patch').resolve())],cwd=root,capture_output=True,check=True) changed=0 for target,info in manifest['revised_files'].items(): content=(root/target).read_bytes() assert content==(a.bundle/info['output']).read_bytes() hashes[target]=hashlib.sha256(content).hexdigest() assert hashes[target]==info['revised_sha256'] old=a.inputs/'QUESTIONS.md' if target=='research/QUESTIONS.md' else a.inputs/'sources'/target.removeprefix('research/') before=old.read_text(encoding='utf-8').splitlines() after=content.decode('utf-8').splitlines() assert len(before)==len(after) differences=[(x,y) for x,y in zip(before,after) if x!=y] changed+=len(differences) if target!='research/QUESTIONS.md': assert len(differences)==1 and all(x.startswith('verdict: ') and y.startswith('verdict: ') for x,y in differences) else: assert len(differences)==4 assert all('Q-head-remainder-0830' in x or 'Q-doubling-killrun-0830' in x for x,y in differences) assert changed==6 # An arithmetic check of the already-sourced truth ratio, not a fresh tile run. truth=Fraction(348,8*66) assert truth==Fraction(29,44) and f'{float(truth):.4f}'=='0.6591' out={'patched_files':3,'changed_lines':6,'owner_body_edits':0,'other_index_rows_changed':0, 'truth_ratio_from_sourced_gaps':str(truth),'revised_sha256':hashes, 'scope':'Finite file/patch invariants and sourced-ratio arithmetic, no sieve run.'} target=a.bundle/'registry-696-verification.json' target.write_text(json.dumps(out,indent=2,sort_keys=True)+'\n',encoding='utf-8') print(json.dumps(out,indent=2,sort_keys=True)) print('sha256',hashlib.sha256(target.read_bytes()).hexdigest())