"""Shared deterministic I/O; never writes to the upstream snapshot.""" import hashlib, json, re, unicodedata from pathlib import Path import pyarrow as pa import pyarrow.parquet as pq ROOT = Path(__file__).resolve().parents[1] DATE = '2026-10-29' BASELINE = '0cd1e4f55e73c83d11eed1935ec32252eb9dac71' DOMAINS = ['social_sciences','health','economics_business','stem','agriculture_rural_development','environment_sustainability','education','humanities'] DOMAIN_MAP = dict(zip(['sociales','salud','economia','stem','agro_desarrollo','ambiente_sostenibilidad','educacion','humanidades'], DOMAINS)) FAMILIES = ['research_question','question_framework','search_strategy','boolean_query','source_appraisal','evidence_selection','evidence_synthesis','grounded_qa','unsupported_claim','reference_verification','study_comparison','contradiction_detection','literature_review','conceptual_framework','methodological_design','quantitative_methods','qualitative_methods','mixed_methods','experimental_design','impact_evaluation','causal_inference','indicator_design','operationalization','sampling','statistical_interpretation','table_analysis','results_interpretation','methodological_critique','peer_review','limitations','ethics_reproducibility','research_plan','research_workflow','tool_use','search_select_synthesize','evidence_based_decision'] def norm(s): s = ''.join(c for c in unicodedata.normalize('NFKD',s.lower()) if not unicodedata.combining(c)) return ' '.join(re.findall(r'\w+', s)) def digest(s): return hashlib.sha256(s.encode() if isinstance(s,str) else s).hexdigest() def dump(path, obj): p = Path(path); p.parent.mkdir(parents=True,exist_ok=True) p.write_text(json.dumps(obj,ensure_ascii=False,indent=2,allow_nan=False)+'\n') def load(path): return json.loads(Path(path).read_text()) def write_rows(path, rows): p=Path(path);p.parent.mkdir(parents=True,exist_ok=True) pq.write_table(pa.Table.from_pylist(rows),p,compression='zstd',write_page_index=True) def read_rows(path): return pq.read_table(path).to_pylist() def all_data(): return [(p.parent.name,p.stem,r) for p in sorted((ROOT/'data').glob('*/*.parquet')) for r in read_rows(p)] def inputs(r,c): if c=='sft': return '\n'.join(m['content'] for m in r['messages'] if m['role']=='user') return r[{'rag':'query','agentic':'objective','benchmark':'prompt'}[c]] def outputs(r,c): if c=='sft':return '\n'.join(m['content'] for m in r['messages'] if m['role']=='assistant') return r.get('reference_answer',r.get('expected_artifact','')) def text(r,c): return inputs(r,c)+'\n'+outputs(r,c) def pii_flags(s): patterns={'email':r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b', 'secret':r'\b(?:hf_[A-Za-z0-9]{20,}|sk-[A-Za-z0-9_-]{20,}|AKIA[A-Z0-9]{16})\b', 'phone':r'(?=24:signatures.add('prompt_prefix12:'+' '.join(words[:12])) answer=outputs(r,c).splitlines()[0] if c=='sft' and answer.isupper() and len(norm(answer).split())<=12: signatures.add('uppercase_heading:'+norm(answer)) return signatures