"""Audit two stale source summaries and their generated registry copies.""" import argparse import difflib import hashlib import json from pathlib import Path import re p = argparse.ArgumentParser() p.add_argument('--inputs', type=Path, required=True) p.add_argument('--output-dir', type=Path, required=True) args = p.parse_args() dest = args.output_dir dest.mkdir(parents=True, exist_ok=True) def digest(text): return hashlib.sha256(text.encode('utf-8')).hexdigest() def cells(line): return [c.strip() for c in re.split(r'(?', text, re.S) block = next(b for b in blocks if re.search(r'^id:\s*'+re.escape(identifier)+r'\s*$', b, re.M)) status = re.search(r'^status:\s*(.*)$', block, re.M).group(1).strip() verdict = re.search(r'^verdict:\s*(.*)$', block, re.M).group(1).strip() owners.append({'source':source, 'sha256':digest(text), 'status':status, 'matches_index':status==c[2] and verdict==c[3].replace('\\|','|')}) assert any(o['matches_index'] for o in owners), identifier selected.append({'id':identifier,'status':c[2],'owners':owners}) assert len(selected)==15 targets = { 'Q-head-remainder-0830': 'history/staging/attack-0830-head-remainder.md', 'Q-doubling-killrun-0830': 'history/staging/attack-0830-doubling-killrun.md', } revisions = {} verdicts = {} for identifier, source in targets.items(): text = (args.inputs/'sources'/source).read_text(encoding='utf-8') old = re.search(r'^verdict: (.*)$', text, re.M).group(1) if identifier == 'Q-head-remainder-0830': clause = 'At half-decade resolution HL\'s pipeline prices Delta to 1.2 and 0.4 percent at the top two windows, inside one standard error, and the sign of the miss alternates.' new_clause = 'Per verify-0830-record-defects.md section 3.3 and the hl3 rider, that half-decade comparison remains pooled: re-reading halves from their eighths puts HL BELOW the measurement in five of six half-decades, by 2.5 to 3.4 percent at the top decade, at about two standard errors. The original within-one-standard-error and alternating-sign interpretation is superseded.' assert old.count(clause)==1 and old.count('0.954 to 0.995')==1 new = old.replace(clause,new_clause).replace('0.954 to 0.995','0.964 to 0.995') else: clause = 'the weighted run itself, computed exactly, sits at 0.83 of its allowance at s = 16 and (M8) is exactly where yesterday left it, OPEN.' new_clause = 'per the redteam-0830-doubling.md rider and section 1, both the truth and the maxsum certificate stay under the allowance on the original fourteen-step range, with sup w_true/a = 0.6591 and sup w/a = 0.8295 at s = 16; 0.83 is the certificate ratio, not the truth ratio. The all-s (M8) target remains OPEN.' assert old.count(clause)==1 new = old.replace(clause,new_clause) updated = text.replace('verdict: '+old+'\n','verdict: '+new+'\n') # A rider explains why this source ledger is stale. Keep its history and # all derivations unchanged; only the summary feeds the regenerated row. assert text.splitlines()[8:]==updated.splitlines()[8:] assert sum(a!=b for a,b in zip(text.splitlines(),updated.splitlines()))==1 revisions['research/'+source]=(text,updated) verdicts[identifier]=new new_lines=[] counts={key:0 for key in verdicts} for line in registry.splitlines(keepends=True): m=re.search(r'`(Q-[^`]+)`',line) if line.startswith('|') and m and m.group(1) in verdicts: c=cells(line);assert len(c) in [5,6] c[3]=verdicts[m.group(1)].replace('|','\\|') line='| '+' | '.join(c)+' |'+('\n' if line.endswith('\n') else '') counts[m.group(1)]+=1 new_lines.append(line) assert all(n==2 for n in counts.values()) updated_registry=''.join(new_lines) assert len(registry.splitlines())==len(updated_registry.splitlines()) assert sum(a!=b for a,b in zip(registry.splitlines(),updated_registry.splitlines()))==4 revisions['research/QUESTIONS.md']=(registry,updated_registry) patch='' files={} for source,(old,new) in revisions.items(): outname='registry-696-'+Path(source).name (dest/outname).write_text(new,encoding='utf-8') patch+=''.join(difflib.unified_diff(old.splitlines(True),new.splitlines(True),fromfile='a/'+source,tofile='b/'+source,n=1)) files[source]={'base_sha256':digest(old),'revised_sha256':digest(new),'output':outname} (dest/'registry-696.patch').write_text(patch,encoding='utf-8') manifest={'job_id':696,'window':[22,36],'ids_checked':15,'linked_owners':sum(len(r['owners']) for r in selected), 'all_ids_match_at_least_one_ledger':True,'rows':selected,'revised_files':files, 'source_ledger_lines_changed':2,'index_lines_changed':4,'statuses_changed':0, 'scope':'Documentary rider/summary consistency; no research producer or theorem verification.'} (dest/'registry-696-manifest.json').write_text(json.dumps(manifest,indent=2,sort_keys=True)+'\n',encoding='utf-8') print(json.dumps({'ids_checked':15,'linked_owners':manifest['linked_owners'],'changed_ids':list(targets),'source_lines':2,'index_lines':4,'statuses_changed':0,'files':files},indent=2))