"""Export the graded summary as one JSON file for visualisation.
Usage:
python3 -I study/export_grades_json.py <grades_dir> <overrides.tsv> <report.md> <rubric.md> <out.json>
Inputs are the same files used by finalize_grades.py:
<grades_dir>/*-A.md, *-B.md the two independent graders' tables
<overrides.tsv> adjudicated cells (study/grades_overrides.tsv)
<report.md> agent-runtime-comparison.md (for the evidence ledger)
<rubric.md> study/RUBRIC.md (for the criteria text)
The output schema is documented in study/VISUALIZE.md.
"""
import csv, json, pathlib, re, sys
grades_dir, ov_path, report_path, rubric_path, out_path = map(pathlib.Path, sys.argv[1:6])
ORDER = ["codex", "goose", "hermes-agent", "letta-code", "nullclaw",
"openclaw", "opencode", "pi", "software-agent-sdk", "spore"]
GROUPS = [
("Programmable capabilities", ["Capability interface", "Reusable computation", "Capability transfer"]),
("Evidence and memory", ["Durable evidence", "Context recovery", "Memory maintenance"]),
("Specialist collaboration", ["Specialist definition", "Continuation", "Coordination"]),
("Execution controls", ["Confinement", "Credential handling", "Budget enforcement"]),
("Product integration", ["Embedding interfaces", "Declarative configuration", "Durable execution"]),
("Operations and verification", ["Service dependencies", "Execution records", "Result checks"]),
]
CATEGORICAL = {"Service dependencies"}
def load_grader(path):
rows = {}
for line in path.read_text().splitlines():
c = [x.strip() for x in line.strip().strip("|").split("|")]
if len(c) >= 8 and c[0] in ORDER:
rows[(c[0], c[1])] = dict(D=c[2], B=c[3], c=c[4], cfg=c[5], refs=c[6], why=c[7])
return rows
A, B = {}, {}
for f in grades_dir.glob("*-A.md"):
A.update(load_grader(f))
for f in grades_dir.glob("*-B.md"):
B.update(load_grader(f))
overrides = {}
for r in csv.reader(open(ov_path), delimiter="\t"):
if r and not r[0].startswith("#"):
overrides[(r[0], r[1])] = dict(D=r[2], B=r[3], c=r[4], cfg=r[5], refs=r[6], why=r[7], basis=r[8])
cells = []
for rt in ORDER:
for group, cols in GROUPS:
for col in cols:
a, b = A[(rt, col)], B[(rt, col)]
final = overrides.get((rt, col), a)
cells.append({
"runtime": rt,
"group": group,
"column": col,
"kind": "categorical" if col in CATEGORICAL else "ordinal",
"default": final["D"],
"best": final["B"],
"best_config": None if final["cfg"] in ("-", "") else final["cfg"],
"confidence": final["c"],
"rationale": final["why"],
"refs": re.findall(r"[A-Z]\d+", final["refs"]),
"grader_a": {"default": a["D"], "best": a["B"]},
"grader_b": {"default": b["D"], "best": b["B"]},
"adjudication": overrides[(rt, col)]["basis"] if (rt, col) in overrides else None,
})
# Rubric criteria per column (text before the errata section).
rubric = rubric_path.read_text().split("## Errata")[0]
criteria, current = {}, None
for line in rubric.splitlines():
m = re.match(r"^\*\*(.+?):\*\*", line)
if m:
current = m.group(1)
criteria[current] = {}
continue
m = re.match(r"^\| *(\d|`[EORX]`) *\| *(.+?) *\|$", line)
if current and m:
criteria[current][m.group(1).strip("`")] = m.group(2)
# Evidence ledger entries actually cited by a grade.
used = {r for c in cells for r in c["refs"]}
ledger = {}
for line in report_path.read_text().splitlines():
m = re.match(r"^(?:- )?\[([A-Z]\d+)\] (\S+) — (https://\S+) — (.+)$", line.rstrip())
if m and m.group(1) in used:
what, _, ev = m.group(4).rpartition(" — ")
ledger[m.group(1)] = {"repo": m.group(2), "url": m.group(3),
"what": what or m.group(4), "evidence": ev.strip("[]")}
missing = sorted(used - set(ledger))
if missing:
sys.exit(f"refs without ledger entries: {missing}")
out = {
"schema": "agent-runtime-grades/1",
"runtimes": ORDER,
"groups": [{"name": g, "columns": cols} for g, cols in GROUPS],
"levels": {"min": 0, "max": 4, "evidence_cap": 2},
"categorical_classes": {"E": "embedded", "O": "on demand", "R": "resident service", "X": "external server required"},
"cells": cells,
"criteria": criteria,
"ledger": ledger,
}
out_path.write_text(json.dumps(out, ensure_ascii=False, indent=1))
print(f"{len(cells)} cells, {len(criteria)} columns of criteria, {len(ledger)} ledger entries -> {out_path}")