File size: 3,086 Bytes
fb5bf36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Export only new trajectories in each baseline's exact schema, preserving all old rows."""
from pathlib import Path
import sys,os,json,hashlib,copy
import pyarrow as pa
import pyarrow.parquet as pq
BASE=Path(__file__).resolve().parent;sys.path.insert(0,str(BASE/'runtime'))
os.environ.setdefault('CGD_ARTIFACTS',str(BASE.parents[2]/'artifacts'))
from code_search import sft
from generate import validate_messages
OUT=BASE/'release';OUT.mkdir(exist_ok=True)
prep=json.loads((BASE/'preparation.json').read_text());report={'target_unfiltered':14500,'arms':{}}
for arm in ['grep','colgrep']:
 complete=json.loads((BASE/arm/'COMPLETE.json').read_text());p=BASE/arm/'new-raw.jsonl'
 assert hashlib.sha256(p.read_bytes()).hexdigest()==complete['raw_sha256']
 raw=[json.loads(l) for l in p.open()];assert len(raw)==prep['baseline'][arm]['additional_needed']
 ids=[r['instance_id'] for r in raw];assert len(ids)==len(set(ids))
 variants={'unfiltered':[],'weighted':[],'f1':[],'filerecall':[]}
 for row in raw:
  validate_messages(row)
  weighted=row.pop('_weighted')
  variants['unfiltered'].append(row)
  if weighted:variants['weighted'].append(weighted)
  if sft.keep_record(row,min_file_recall=0.,allow_shell_search=arm=='grep') and row['file_f1']>=.5:variants['f1'].append(row)
  if sft.keep_record(row,min_file_recall=1.,allow_shell_search=arm=='grep'):variants['filerecall'].append(row)
 armreport={}
 for kind,rows in variants.items():
  paths=[p for f,p in sorted(prep['baseline'][arm]['files'].items()) if f.startswith(kind+'/')]
  old=pq.read_table(paths);schema=old.schema
  oldids=set(old['instance_id'].to_pylist());assert not oldids.intersection(ids)
  protocol=json.loads((BASE/f'{arm}-{kind}-protocol.json').read_text())
  cleaned=[]
  for r in rows:
   r=copy.deepcopy(r);r['messages'][0]['content']=protocol['system_prompt'];r['tools']=json.dumps(protocol['tools'],indent=2)
   if kind=='weighted':assert len(r['turn_weights'])==sum(m['role']=='assistant' for m in r['messages'])
   cleaned.append({k:r.get(k) for k in old.column_names})
  table=pa.Table.from_pylist(cleaned,schema=schema)
  dest=OUT/f'{arm}-python'/kind;dest.mkdir(parents=True,exist_ok=True)
  for i in range((len(table)+1999)//2000):pq.write_table(table.slice(i*2000,2000),dest/f'sft-{i:05d}.parquet',compression='zstd',row_group_size=100)
  # Round-trip ensures tool schemas and turn weights survive Arrow conversion.
  if len(table):assert pq.read_table(sorted(dest.glob('*.parquet'))).equals(table)
  armreport[kind]={'old_rows':len(old),'new_rows':len(table),'total_rows':len(old)+len(table),'source_repo':prep['baseline'][arm]['repo'],'source_revision':prep['baseline'][arm]['revision']}
 assert armreport['unfiltered']['total_rows']==14500
 report['arms'][arm]=armreport
 print(arm,armreport,flush=True)
report['new_files_sha256']={str(p.relative_to(OUT)):hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(OUT.rglob('*.parquet'))}
(OUT/'generation-report.json').write_text(json.dumps(report,indent=2)+'\n')
print('Export verified; no remote datasets modified by generation job',flush=True)