"""Check B over ALL tasks in out_v49: the last transcript unit is the tool result that delivered every received submission(s) (id + score/terminal state in one single-submission object, or unambiguous single-poll reply). Out: out_v49/work/check_cut_end.json """ import glob, json, os, re, sys, tarfile from collections import Counter from multiprocessing import Pool import zstandard sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import v49lib as L OUT = L.V + 'build_v2/out_v49/' index = {r['task_id']: r for r in json.load(open(OUT + 'metadata/task_index.json'))} def one(shard): files = {} with open(shard, 'rb') as f: with tarfile.open(fileobj=zstandard.ZstdDecompressor().stream_reader(f), mode='r|') as t: for m in t: if m.isfile() and (m.name.endswith('context/transcript.md') or m.name.endswith('context/submissions.json')): files[(m.name.split('/')[0], m.name.rsplit('/', 1)[1])] = t.extractfile(m).read().decode() rows = [] for (tid, fn), txt in files.items(): if fn != 'transcript.md': continue subs = {s['index']: s for s in json.loads(files[(tid, 'submissions.json')])} parts = re.split(r'\n### \[[^\]]*\] \[[^\]]*\] ', txt) last = parts[-1] kind = last.split('\n', 1)[0] last_body = last.split('\n', 1)[1] if '\n' in last else '' row = index[tid] fam = row['family'] segs = L.single_id_segments(last_body, fam) t_norm = last_body.replace('"', '"') if '"' in last_body else last_body last_clean = L.SHELL_WRAP_DONE.sub('', t_norm) id_rx = L.JOB_JSON_RX if fam == 'C' else L.VID_RX all_ids = set(id_rx.findall(t_norm)) ok = [] for j in row['received_indices']: s = subs[j] vals = [v for v in (s.get('score'), (s.get('metrics') or {}).get('score_unbounded') if isinstance(s.get('metrics'), dict) else None) if isinstance(v, (int, float))] if segs: good = any( not L.RUNNING.search(L.SHELL_WRAP_DONE.sub('', seg)) and not L.PLAIN_RUN.search(L.SHELL_WRAP_DONE.sub('', seg)) and (any(L.shows_value(L.SHELL_WRAP_DONE.sub('', seg), v) for v in vals) or bool(L.DONE.search(L.SHELL_WRAP_DONE.sub('', seg))) or (fam == 'C' and bool(L.C_RESULT.search(seg)))) for _, seg in segs ) if not good and len(all_ids) == 1 and not L.RUNNING.search(last_clean) and not L.PLAIN_RUN.search(last_clean): good = any(L.shows_value(last_clean, v) for v in vals) or bool(L.DONE.search(last_clean)) else: good = (not L.RUNNING.search(last_clean) and not L.PLAIN_RUN.search(last_clean)) and ( any(L.shows_value(last_clean, v) for v in vals) or bool(L.DONE.search(last_clean)) or bool(L.ZERO_LABEL.search(last_clean)) or (fam == 'C' and bool(L.C_RESULT.search(last_clean))) ) ok.append(good) rows.append({'task_id': tid, 'family': fam, 'last_kind': kind, 'all_shown': all(ok), 'tail': last[-400:] if not all(ok) else None}) return rows if __name__ == '__main__': allr = [] with Pool(6) as p: for r in p.imap_unordered(one, sorted(glob.glob(OUT + 'tasks/*.tar.zst'))): allr += r c = Counter((r['family'], r['last_kind'], r['all_shown']) for r in allr) print(len(allr), sorted(c.items())) json.dump({'counts': {str(k): v for k, v in c.items()}, 'fails': [r for r in allr if not r['all_shown']]}, open(OUT + 'work/check_cut_end.json', 'w'), indent=0) for r in [r for r in allr if not r['all_shown']][:12]: print('FAIL', r['task_id'], r['last_kind'], repr(r['tail'][-300:]))