"""If we stop a FrontierCS run at the cut and use the predicted final as the run's reward, how much time do we save and how far off is the reward? -> results/v3/time_saving.json Forecasts: audited tasks (results/v3/audit/clean_tasks.txt) that every listed model forecast with a readable, non-empty agent log (matched set). Per forecast: runtime = when the ground-truth agent actually stopped: its last logged event, capped at the answering window saved = runtime - cut time (minutes); saved_pct = saved / runtime eval_min = official final evaluation time skipped as well (finished_at - final_submitted_at, formal results) err = predicted final - official final (points); abs_err = |err| Usage: python -I v3/della/time_saving.py opus55-high sonnet55-high [haiku55-high] """ import json, os, sys, statistics as st, collections, datetime R = '/scratch/gpfs/TRIDAO/ly3223/oraclebench/' M = R + 'data/v3/full/metadata/' tags = sys.argv[1:] or ['opus55-high', 'sonnet55-high'] idx = {json.loads(l)['task_id']: json.loads(l) for l in open(M + 'task_index.jsonl')} gt = json.load(open(M + 'ground_truth.json')) keep = set(open(R + 'results/v3/audit/clean_tasks.txt').read().split()) runs = {f"{r['task']}--{r['model']}".replace('_', '-').lower(): r for r in json.load(open(R + 'results/v3/curves_all.json'))} fr = {r['record_id']: r for r in json.load(open(R + 'data/hf_raw/metadata/formal-results.json'))['runs']} iso = lambda s: datetime.datetime.fromisoformat(s.replace('Z', '+00:00')) if s else None def load(tag): out = {} for l in open(R + f'results/v3/{tag}.preds.jsonl'): r = json.loads(l); log = r['trial'] + '/agent/claude-code.txt' if os.path.exists(log) and os.path.getsize(log) > 0: out[r['task_id']] = {p['target']: p['score'] for p in r['predictions'] if isinstance(p.get('score'), (int, float))} return out P = {t: load(t) for t in tags} common = sorted(t for t in keep if all(t in P[m] and P[m][t].get('final') is not None for m in tags)) rows = [] for tid in common: m = idx[tid]; run = runs[tid.rsplit('--', 1)[0]] lab = next(l for l in gt[tid] if l['target'] == 'final') if lab['status'] != 'ok': continue cut = m['elapsed_at_cut_s'] / 60; B = m['budget_s'] / 60 runtime = min(run['e'], B) f = fr[f"{m['fcs_task']}/{m['model']}"] a, b = iso(f.get('final_submitted_at')), iso(f.get('finished_at')) ev = (b - a).total_seconds() / 60 if a and b and b > a else None row = {'task_id': tid, 'p': int(round(m['cut_fraction'] * 100)), 'B': B, 'cut': round(cut, 1), 'runtime': round(runtime, 1), 'saved': round(max(0.0, runtime - cut), 1), 'saved_pct': round(max(0.0, runtime - cut) / runtime, 4) if runtime > 0 else 0.0, 'full_window': runtime >= B - 1, 'eval_min': None if ev is None else round(ev, 1), 'truth': lab['truth']} for t in tags: row[t] = round(max(0.0, min(100.0, P[t][tid]['final'])) - lab['truth'], 3) rows.append(row) def q(v, p): v = sorted(v); return v[min(len(v) - 1, int(p * (len(v) - 1) + .5))] summary = {} for p in (25, 50, 75): R_ = [r for r in rows if r['p'] == p] s = {'n': len(R_), 'runtime_mean': round(st.mean(r['runtime'] for r in R_), 1), 'cut_mean': round(st.mean(r['cut'] for r in R_), 1), 'saved_min_mean': round(st.mean(r['saved'] for r in R_), 1), 'saved_min_median': q([r['saved'] for r in R_], .5), 'saved_pct_mean': round(st.mean(r['saved_pct'] for r in R_), 4), 'saved_pct_median': q([r['saved_pct'] for r in R_], .5), 'full_window_share': round(sum(r['full_window'] for r in R_) / len(R_), 3), 'eval_min_mean': round(st.mean(r['eval_min'] for r in R_ if r['eval_min'] is not None), 1), 'truth_mean': round(st.mean(r['truth'] for r in R_), 2), 'models': {}} for t in tags: e = [r[t] for r in R_]; a = [abs(x) for x in e] s['models'][t] = {'mae': round(st.mean(a), 3), 'median_abs': round(q(a, .5), 3), 'bias': round(st.mean(e), 3), 'p90_abs': round(q(a, .9), 3), 'bands': {k: round(sum(lo <= x < hi for x in a) / len(a), 4) for k, (lo, hi) in {'le2': (0, 2), '2to5': (2, 5), '5to10': (5, 10), 'gt10': (10, 1e9)}.items()}} summary[p] = s json.dump({'tags': tags, 'n_forecasts': len(rows), 'summary': summary, 'rows': rows}, open(R + 'results/v3/time_saving.json', 'w')) print('matched forecasts', len(rows), 'models', tags) for p, s in summary.items(): print(f"== {p}% cut n={s['n']}: runtime {s['runtime_mean']} min, cut at {s['cut_mean']} min, saved {s['saved_min_mean']} min " f"(median {s['saved_min_median']}), {100*s['saved_pct_mean']:.0f}% of the run (median {100*s['saved_pct_median']:.0f}%), " f"ran full window {100*s['full_window_share']:.0f}%, + final eval {s['eval_min_mean']} min") for t, m in s['models'].items(): print(f" {t:14s} |final err| mean {m['mae']} median {m['median_abs']} p90 {m['p90_abs']} bias {m['bias']:+} bands {m['bands']}")