File size: 3,541 Bytes
ae1ed83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""Aggregate Phase C closed-loop eval results for the Gate C verdict.

Reads results/eval_<arm>_<step>_<cond>.json (TRACE arms, written by
eval_trace.py), results/eval_rawmem_05000_long_hybrid.json and the
fs1 sweep RESULT lines in results/eval_c00_<step>_b.log /
results/eval_c00_long4_b.log. Prints the full success-rate table and
writes results/gate_c_summary.json. No numbers are invented: missing
files are reported as missing.
"""
import json
import re
from pathlib import Path

TV = Path.home() / "sera_v4_experiments" / "trace_validation"
RES = TV / "results"
STEPS = ["00500", "01000", "02000", "05000"]
TRACE_EVALS = [("base", "textonly"), ("rawmem", "hybrid"),
               ("rawmem", "correct"), ("rawmem", "nodemo"),
               ("rawmem", "swapped"), ("rawmem", "shuffled")]


def trace_sr(path):
    if not path.exists():
        return None
    d = json.loads(path.read_text())
    per_task = {}
    tot_s, tot_n = 0, 0
    for spec, t in d["tasks"].items():
        n = len(t["episodes"])
        per_task[spec] = {"successes": t["successes"], "n": n,
                          "shadow_successes": t.get("shadow_successes", 0)}
        tot_s += t["successes"]
        tot_n += n
    return {"successes": tot_s, "n": tot_n,
            "sr": round(100.0 * tot_s / tot_n, 1) if tot_n else None,
            "per_task": per_task}


def fs1_sr(path):
    if not path.exists():
        return None
    tot_s, tot_n = 0, 0
    per = {}
    for line in path.read_text().splitlines():
        if not line.startswith("RESULT "):
            continue
        r = json.loads(line[len("RESULT "):])
        key = f"{r['suite']}#{r['task_id']}"
        per.setdefault(key, [0, 0])
        per[key][1] += 1
        tot_n += 1
        if r["success"]:
            per[key][0] += 1
            tot_s += 1
    if tot_n == 0:
        return None
    return {"successes": tot_s, "n": tot_n,
            "sr": round(100.0 * tot_s / tot_n, 1),
            "per_task": {k: {"successes": v[0], "n": v[1]}
                         for k, v in sorted(per.items())}}


def main():
    out = {"trace": {}, "c00": {}, "long": {}}
    for arm, cond in TRACE_EVALS:
        for step in STEPS:
            r = trace_sr(RES / f"eval_{arm}_{step}_{cond}.json")
            if r is not None:
                out["trace"][f"{arm}_{cond}_{step}"] = r
    for step in STEPS:
        # chain3 logs use the 6-digit checkpoint dir names (000500...).
        r = fs1_sr(RES / f"eval_c00_{step.zfill(6)}_b.log")
        if r is not None:
            out["c00"][f"c00_b_{step}"] = r
    r = trace_sr(RES / "eval_rawmem_05000_long_hybrid.json")
    if r is not None:
        out["long"]["rawmem_hybrid_05000"] = r
    r = fs1_sr(RES / "eval_c00_long4_b.log")
    if r is not None:
        out["long"]["c00_b_long4"] = r

    def sr_of(d, key):
        v = d.get(key)
        return v["sr"] if v else None

    gains = {}
    for step in STEPS:
        c = sr_of(out["trace"], f"rawmem_correct_{step}")
        n = sr_of(out["trace"], f"rawmem_nodemo_{step}")
        if c is not None and n is not None:
            gains[step] = round(c - n, 1)
    out["demo_gain_rawmem_correct_minus_nodemo"] = gains
    out["h1_c01_textonly_05000_sr"] = sr_of(out["trace"],
                                            "base_textonly_05000")
    out["h1_c00_b_05000_sr"] = sr_of(out["c00"], "c00_b_05000")
    (RES / "gate_c_summary.json").write_text(json.dumps(out, indent=1))
    print(json.dumps(out, indent=1))


if __name__ == "__main__":
    main()