"""Rebuild cumulative TermiGen v3 tasks from pinned v1/v2 inputs and local selection metadata. Usage: python patch.py --out rebuilt.parquet Requires pyarrow and huggingface_hub. Does not rerun validation or teacher generation. """ import argparse,hashlib,json from pathlib import Path import pyarrow as pa,pyarrow.parquet as pq from huggingface_hub import hf_hub_download def build(selection, source, old): current={r['path']:r for r in source.to_pylist()};prior={r['path']:r for r in old.to_pylist()};rows=[] for tid,e in sorted(selection['tasks'].items()): if not e['kept']:continue if tid in prior:r=dict(prior[tid]) else: r=dict(current[tid]);r.update({k:e['v2_results'].get(k) for k in ['coder_solved','instruct_successes','instruct_missing_rewards']});r['attempts_per_model']=4 assert r['content_sha256']==e['content_sha256'] r.update({k:e[k] for k in ['qwen38_attempts','qwen38_successes','qwen38_missing_attempts','qwen38_saved_traces']});rows.append(r) schema=pa.schema([*old.schema,*(pa.field(k,pa.int64()) for k in ['qwen38_attempts','qwen38_successes','qwen38_missing_attempts','qwen38_saved_traces'])]);meta=dict(old.schema.metadata or {}) images=json.loads(source.schema.metadata[b'ot.images.v1']);kept={r['path'] for r in rows};images['tasks']={t:e for t,e in images['tasks'].items() if t in kept};used={e['bundle'] for t in images['tasks'].values() for e in t['environments']};images['bundles']={k:v for k,v in images['bundles'].items() if k in used};meta[b'ot.images.v1']=json.dumps(images).encode() meta[b'ot.release.v3']=json.dumps({'partial':True,'baseline_revision':selection['baseline_revision'],'source_revision':selection['source_revision'],'kept':len(rows),'new_model':'Qwen/Qwen3.8-27B','reward_policy':'terminal-errors-zero-v2'}).encode() return pa.Table.from_pylist(rows,schema=schema.with_metadata(meta)) if __name__=='__main__': ap=argparse.ArgumentParser();ap.add_argument('--out',type=Path,required=True);a=ap.parse_args();root=Path(__file__).parent;s=json.loads((root/'metadata/selection.json').read_text());inputs=json.loads((root/'metadata/inputs.json').read_text());tables={} for key,record in inputs.items(): path=Path(hf_hub_download(s['repo_id'],record['file'],repo_type='dataset',revision=record['revision']));assert hashlib.sha256(path.read_bytes()).hexdigest()==record['sha256'];tables[key]=pq.read_table(path) pq.write_table(build(s,tables['source'],tables['v2']),a.out,compression='zstd')