from pathlib import Path import json,re,hashlib,runpy m=runpy.run_path(str(Path(__file__).with_name('agent3_extract.py'))) results=[] def keyblocks(t): return {h: b for h,b in m['pieces'](t) if re.match(r'#### (R-|6\.\d+\.\d+)',h)} for x in m['batch']: src=m['ROOT']/x['source'];dst=m['ROOT']/x['output'] s=src.read_text();d=dst.read_text();sb=keyblocks(s);db=keyblocks(d) checks={ 'source_hash_unchanged':hashlib.sha256(src.read_bytes()).hexdigest()==x['sha256'], 'case_in_h1':re.search(r'^# 요건사실 항목: '+re.escape(x['case'])+r'$',d,re.M) is not None, 'element_definitions':set(h for h in sb if h.startswith('#### R-'))==set(h for h in db if h.startswith('#### R-')), 'defense_definitions':set(h for h in sb if h.startswith('#### 6.'))==set(h for h in db if h.startswith('#### 6.')), 'path_headings':re.findall(r'^### 5\.P\d+ [^\n]+',s,re.M)==re.findall(r'^### 5\.P\d+ [^\n]+',d,re.M), 'no_external_urls':not re.search(r'https?://',d), 'even_code_fences':len(re.findall(r'^```',d,re.M))%2==0, 'element_authority_connections':True, 'element_burden_and_conditions':True, 'source_link_resolves':(dst.parent/'../raw'/src.name).resolve()==src.resolve(), 'source_status_preserved':re.search(r'^verification_status:.+$',s,re.M).group()==re.search(r'^verification_status:.+$',d,re.M).group(), 'all_core_ids_present':set(re.findall(r'R-PROV\d+-[45]-\d+',s[s.index('## §4.'):s.index('## §8.')])).issubset(set(re.findall(r'R-PROV\d+-[45]-\d+',d))), } for h,b in sb.items(): ob=db.get(h,'') am=re.search(r'^- \*\*권위 ID\*\*:(.+)$',b,re.M) if am and am.group(1).strip() not in ob:checks['element_authority_connections']=False if h.startswith('#### R-'): for field in ['적용 경로','요건분류','주장의 정도','주장책임 / 증명책임','추정·전환','다중 대응','흠결 빈발점']: match=re.search(r'^- \*\*'+re.escape(field)+r'\*\*:(.*?)(?=\n- \*\*|\Z)',b,re.M|re.S) if match and m['clean'](match.group()) not in ob:checks['element_burden_and_conditions']=False table_issues=[];lines=d.splitlines();i=0 while i1: counts=[len(re.split(r'(?1:table_issues.append({'line':start+1,'columns':list(set(counts))}) else:i+=1 checks['table_columns_consistent']=not table_issues results.append({'case':x['case'],'source':x['source'],'output':x['output'],'checks':checks,'element_count':sum(h.startswith('#### R-') for h in db),'defense_count':sum(h.startswith('#### 6.') for h in db),'source_bytes':x['bytes'],'output_bytes':dst.stat().st_size,'table_issues':table_issues,'reviewed_scope':m['case_notes'][x['case']]['scope'],'reviewed_limitations':m['case_notes'][x['case']]['limits']}) p=m['BASE']/'agent3_validation.json';p.write_text(json.dumps(results,ensure_ascii=False,indent=2)) print(json.dumps({'documents':len(results),'failures':[{r['case']:[k for k,v in r['checks'].items() if not v]} for r in results if not all(r['checks'].values())],'elements':sum(r['element_count'] for r in results),'defenses':sum(r['defense_count'] for r in results),'source_bytes':sum(r['source_bytes'] for r in results),'output_bytes':sum(r['output_bytes'] for r in results)},ensure_ascii=False,indent=2))