import re,json,hashlib from pathlib import Path B=Path('plans/extract-remaining-elements'); batch=json.loads((B/'batch_2.json').read_text());notes=json.loads((B/'agent2_review_notes.json').read_text()) def sections(t): ms=list(re.finditer(r'(?m)^(#{2,3}) (?:§)?(\d+(?:\.(?:P\d+|\d+))?)\.?[^\n]*',t));return {m[2]:t[m.end():ms[i+1].start() if i+1|)]+','',t);t=re.sub(r'(?','다음은 원문 §12.7에서 요건 선택·분류·적용에 영향을 주는 한계만 간추린 것이다.\n\n'+notes[str(i)]['limits']) # Remove duplicated statements only when text is exactly identical; retain tables and headings. paras=o.split('\n\n'); seen=set();out=[] for a in paras: if not a.startswith(('#','|','---','```')) and len(a)>100: if a in seen:continue seen.add(a) out.append(a) o='\n\n'.join(out) o=re.sub(r'\n{3,}','\n\n',o).strip()+'\n';p.write_text(o) ledger.append({'case':x['case'],'focus':notes[str(i)]['focus'],'source_sections_reviewed':['path and element headings','12.7 full text','4/5/6 full preserved core structure','mixed evidence recovery'],'mixed_fields_preserved':recovered,'source_unchanged':hashlib.sha256(Path(x['source']).read_bytes()).hexdigest()==x['sha256'],'output_bytes':len(o.encode())}) (B/'agent2_review_ledger.json').write_text(json.dumps(ledger,ensure_ascii=False,indent=2)+'\n') print('FINALIZED',len(ledger),'mixed_fields',sum(len(x['mixed_fields_preserved']) for x in ledger))