from pathlib import Path import sys,time ROOT=Path('/mnt/data/yuxu/datax-open');RUN=ROOT/'books-clean-full-20261205' sys.path[:0]=[str(ROOT/'books-clean10pct-20261205/vendor'),str(ROOT/'books-build-20261205/vendor'), str(ROOT/'books-build-20261205/scripts'),str(ROOT/'books-markdown-20261205/scripts')] import orjson as j,build_full as bf,strict_rules as sr,extra_gate import sqlite3,collections,gzip,concurrent.futures SEED='strict-full-clean-final-holdout-v1-20261205' def main(): while not (RUN/'reports/refilter-complete.json').exists(): if 'Traceback' in (RUN/'logs/refilter.log').read_text():raise RuntimeError('refilter failed') time.sleep(10) excluded=set() for p,key in [(RUN/'samples/audit.json','records'),(ROOT/'books-clean10pct-20261205/samples/review.json','samples'), (ROOT/'books-independent-audit-20261205/reports/audit.json','decisions'), (ROOT/'books-markdown-20261205/samples/final-sample-audit.v3-regression.json','samples'), (ROOT/'books-publish-20261205/reports/formula-preservation.json','samples')]: for d in j.loads(p.read_bytes())[key]:excluded.add(d['document_id']) con=sqlite3.connect(f'file:{RUN/"documents.sqlite"}?mode=ro',uri=True) buckets=collections.defaultdict(list) for raw, in con.execute('SELECT record FROM docs'): d=j.loads(raw) if d['disposition']!='kept_candidate_v2' or d['document_id'] in excluded:continue score=sr.sha(SEED+'\0'+d['document_id']);lst=buckets[d['repo']] lst.append((score,d));lst.sort(key=lambda x:x[0]) if len(lst)>10:lst.pop() selected=[x[1] for k,lst in sorted(buckets.items()) for x in lst] bf.save(RUN/'reports/final-audit-plan.json',{'seed':SEED,'frozen_at':bf.now(),'per_source':10,'count':len(selected), 'excluded_prior_records':len(excluded),'ids':[d['document_id'] for d in selected]}) groups=collections.defaultdict(dict) for d in selected:groups[(d['repo'],d['source_file'])][d['source_line']]=d jobs=[(s,f,groups[(s['repo'],f['path'])]) for _,s,f in bf.build.jobs() if (s['repo'],f['path']) in groups] def extract(job): s,f,want=job;rows=[] with gzip.open(bf.build.REC/f['local_path'],'rt',encoding='utf-8') as inp: for ln,line in enumerate(inp,1): if ln not in want:continue d=j.loads(line);row=want[ln];k=row['meta'];meta=dict(k) meta['cleaned_text_sha256']=k['input_markdown_sha256'];meta['cleaned_body_chars']=k['input_markdown_chars'] before,_=bf.current_body(d['text'],s['repo'],meta);after,rem=sr.clean(before,s['repo'],str(row['title'])) assert sr.sha(after)==row['cleaned_text_sha256'] assert not extra_gate.reasons(after,str(row['title']),s['repo']) points=sorted(set([0,max(0,len(after)//3-200),max(0,2*len(after)//3-200),max(0,len(after)-550)])) full=RUN/'samples/final-full';full.mkdir(exist_ok=True) (full/(row['document_id']+'.txt')).write_text(after) rows.append({'document_id':row['document_id'],'repo':row['repo'],'title':row['title'],'source_file':f['path'],'source_line':ln, 'hash':row['cleaned_text_sha256'],'chars':len(after),'tokens':row['tokens'],'flags':row['flags'], 'windows':[{'start':p,'text':after[p:p+550]} for p in points]}) return rows rows=[] with concurrent.futures.ThreadPoolExecutor(8) as pool: for xs in pool.map(extract,jobs):rows.extend(xs) assert len(rows)==30 bf.save(RUN/'samples/final-audit.json',{'created_at':bf.now(),'source_hashes_verified':True,'records':sorted(rows,key=lambda x:(x['repo'],x['document_id']))}) print('FINAL AUDIT READY',len(rows),flush=True) if __name__=='__main__':main()