from pathlib import Path import json,re,hashlib W=Path(__file__).parent;I=json.loads((W/'identity_snapshot.json').read_text());V=json.loads((W/'verification_once.json').read_text());assert V['comprehensive_reviews']==1;S=Path(I['inputs']['source']['path']).read_text().splitlines();T=(W/'draft.md').read_text();changes=json.loads((W/'changes.json').read_text());fixlog=[] def replace(n,old,new,imp): global T marker=f'';st=T.index(marker);en=T.find('';st=T.index(marker);end=T.find('';st=T.index(marker);en=T.find('',T)} for p in P['units']: p['beta_line']=anchors[p['target'][1:]];p['result']='보존·의미대조완료';p['fix_scope_confirmation']=p['affected_findings'] or '관련개선없음' for r in R:r['result']='충족';r['beta_line']=T[:T.index(r['target'])].count('\n')+1 for sc in SC:sc['status']='대조완료·관련발견범위 개선반영';sc['fix_scope']=[f['id'] for f in V['findings'] if set(sc['source_lines'])&set(f['source_lines'])] for f in V['findings']:f['status']='개선반영·해당범위확인완료' V['status']='종합검증1회후16개개선반영·미해결형식실패0';V['subsequent_review']='추가종합검증없음·발견영향범위만확인';V['unresolved_legal']=unresolved for fx in fixlog:changes.append({'type':'종합검증발견범위개선','reason':fx['improvement'],'line':fx['source_line'],'old':fx.get('old',S[fx['source_line']-1]),'new':fx['new'],'target':fx['target'],'history':'부록 B 종합검증 개선'}) for fn,obj in [('preservation.json',P),('format_requirements.json',R),('changes.json',changes),('scenarios.json',SC),('verification_once.json',V),('targeted_fixes.json',{'comprehensive_reviews':1,'improvements':16,'fixes':fixlog,'scope_only':True})]:(W/fn).write_text(json.dumps(obj,ensure_ascii=False,indent=2)) (W/'final_reviewed.md').write_text(T);out=Path(I['output']);status='재작성·형식검증 완료' if out.exists() and out.read_bytes()!=T.encode():out=W/'final_reviewed.md';status='기존 파일 보존·검증본 작업디렉토리 저장' else:out.parent.mkdir(parents=True,exist_ok=True);out.write_text(T) B=out.read_bytes();assert B.decode().splitlines()[0]==I['title'];completion={'display_name':I['display'],'case_key':I['key'],'source':I['inputs']['source'],'output':str(out.resolve()),'requested_output':I['output'],'status':status,'units':len(P['units']),'requirements':len(R),'scenarios':len(SC),'improvements':16,'comprehensive_reviews':1,'unresolved_failures':0,'unresolved_legal':unresolved,'lines':len(B.decode().splitlines()),'bytes':len(B),'sha256':hashlib.sha256(B).hexdigest(),'ledgers':{fn:str((W/fn).resolve()) for fn in ['preservation.json','format_requirements.json','references.json','changes.json','scenarios.json','identity_snapshot.json','verification_once.json','targeted_fixes.json']},'external_url_access':False,'law_reverification':False,'llm_execution':False} (W/'completion.json').write_text(json.dumps(completion,ensure_ascii=False,indent=2));plan=Path('plans/batch5e-future.md');p=plan.read_text().replace('- [ ]','- [x]').replace('진행 중.','완료. 종합1회·16개 개선·원문1467단위/형식118/시나리오123 대조. 법률 원문 재검증·실제 LLM 실행 미실시. 미검증·적용미확정 경계는 부록B와 completion에 유지.');plan.write_text(p);print(json.dumps(completion,ensure_ascii=False,indent=2))