import re,json,hashlib from pathlib import Path B=Path('plans/extract-remaining-elements');batch=json.loads((B/'batch_2.json').read_text()); rows=[] idpat=r'R-PROV\d+-[45]-\d+' def definitions(t): return [(m[1],m.start()) for m in re.finditer(r'(?m)^#{3,6} [^\n]*\b('+idpat+r')\b[^\n]*',t)] def blocks(t): d=definitions(t);out={} for i,(rid,pos) in enumerate(d): seg=t[pos:d[i+1][1] if i+1om];oe=min(following) if following else len(o);ob=o[om:oe] if sm: authorities=re.findall(r'(?:LAW|PREC|BOOK):[^\s,;|`]+',sm[0]) if any(a.rstrip('.') not in ob for a in authorities):issues.append('authority mismatch '+rid) # Path headings are preserved. paths=set(re.findall(r'(?m)^### (5\.P\d+) ',s));opths=set(re.findall(r'(?m)^### (5\.P\d+) ',o)) if paths!=opths:issues.append('path set differs') # Markdown tables: logical pipe delimiter counts; escaped pipes ignored. tables=[] for m in re.finditer(r'(?m)^\|[^\n]*(?:\n\|[^\n]*)+',o): ls=m[0].splitlines() if len(ls)<2:continue counts=[len(re.split(r'(?1:tables.append({'line':o[:m.start()].count('\n')+1,'widths':sorted(set(counts))}) if tables:issues.append('table widths '+str(tables)) rows.append({'case':x['case'],'defined_elements':len(od),'paths':len(paths),'source_bytes':len(s.encode()),'output_bytes':len(o.encode()),'reduction_pct':round(100*(1-len(o.encode())/len(s.encode())),1),'issues':issues}) (B/'agent2_validation.json').write_text(json.dumps(rows,ensure_ascii=False,indent=2)+'\n') print(json.dumps({'files':len(rows),'elements':sum(r['defined_elements'] for r in rows),'paths':sum(r['paths'] for r in rows),'aggregate_reduction_pct':round(100*(1-sum(r['output_bytes'] for r in rows)/sum(r['source_bytes'] for r in rows)),1),'issues':[{r['case']:r['issues']} for r in rows if r['issues']]},ensure_ascii=False,indent=2))