"""Closed-loop grounding rates and the parse-conditioning check (camera-ready Table "Grounding hit rate under three denominators", Section 7.7). python3 cu/closed_loop.py [--prop-run cu_prop] [--main-run cu_main] Own-proposal episodes exist only where the proposing model's greedy proposal parsed, so the own-proposal arm conditions on proposal success. This script reports, for the seven-model pool: oracle-rendered : hit rate on oracle configurations (conditions on nothing) own, parsed : hit rate on own-proposal episodes (conditional on parse) own, closed loop: per model, proposal parse rate x conditional hit rate, i.e. unparsed proposals counted as failures and two selection checks on the oracle-rendered arm: within-model hit rates on instances whose own proposal parsed versus failed (pooled over families and for layouts alone), and own-proposal versus oracle-rendered hit rates on the same parsed instances. Renderer crashes are infrastructure and are excluded, as in analyze_cu.py. """ from __future__ import annotations import argparse import json import os import numpy as np EXP = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) POOL = ["gemma-4-31b", "gptoss-120b", "gptoss-20b", "leanstral-2603", "qwen3-30b-a3b-instruct-fp8", "qwen3.5-9b", "qwen3.6-27b-fp8"] VISION = ["gemma-4-31b", "qwen3.5-9b", "qwen3.6-27b-fp8"] FAMILIES = ["menu", "layout"] N_INSTANCES = 16 EPISODES_PER_PROPOSAL = 9 # 3 usage-sampled targets x 3 densities def load(run_id: str) -> list[dict]: rows, seen = [], set() path = os.path.join(EXP, "results", run_id, "records.jsonl") for line in open(path): try: r = json.loads(line) except Exception: continue if "error" in r or r["key"] in seen: continue seen.add(r["key"]) if r.get("model") in POOL: rows.append(r) return rows def hit_rate(rows: list[dict]) -> float: return float(np.mean([bool(r["hit"]) for r in rows])) if rows else float("nan") def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--prop-run", default="cu_prop") ap.add_argument("--main-run", default="cu_main") args = ap.parse_args() prop, main_rows = load(args.prop_run), load(args.main_run) parsed = {(r["model"], r["family"], r["instance_idx"]) for r in prop} total = len(POOL) * len(FAMILIES) * N_INSTANCES failed = [(m, f, i) for m in POOL for f in FAMILIES for i in range(N_INSTANCES) if (m, f, i) not in parsed] print(f"proposals parsed: {len(parsed)} of {total}; failed by family: " f"{ {f: sum(1 for _, ff, _ in failed if ff == f) for f in FAMILIES} }") print("\ninterface oracle-rendered own,parsed own,closed-loop") for mode, models in [("a11y", POOL), ("som", VISION), ("pixel", VISION)]: num = 0.0 for m in models: own = [r for r in prop if r["mode"] == mode and r["model"] == m] n_parsed = sum(1 for f in FAMILIES for i in range(N_INSTANCES) if (m, f, i) in parsed) num += hit_rate(own) * n_parsed * EPISODES_PER_PROPOSAL closed = num / (len(models) * len(FAMILIES) * N_INSTANCES * EPISODES_PER_PROPOSAL) orac = hit_rate([r for r in main_rows if r["mode"] == mode]) ownp = hit_rate([r for r in prop if r["mode"] == mode]) print(f"{mode:<22} {100 * orac:15.1f} {100 * ownp:11.1f} {100 * closed:16.1f}") # Pooling families mixes a selection effect with family composition (most # failed proposals are layouts), so the layout-only gap is reported too. for scope, fams in [("both families", FAMILIES), ("layout only", ["layout"])]: print(f"\noracle-rendered arm, within model, {scope}: " "parsed vs failed own proposal") for mode in ["a11y", "som", "pixel"]: diffs = [] for m in POOL: rows = [r for r in main_rows if r["mode"] == mode and r["model"] == m and r["family"] in fams] a = [r for r in rows if (m, r["family"], r["instance_idx"]) in parsed] b = [r for r in rows if (m, r["family"], r["instance_idx"]) not in parsed] if a and b: diffs.append((hit_rate(a) - hit_rate(b), len(b))) if diffs: d = np.average([x for x, _ in diffs], weights=[w for _, w in diffs]) print(f" {mode}: parsed minus failed = {100 * d:+.1f} points " f"(weighted by failed-instance episodes)") print("\nsame parsed instances: own-proposal vs oracle-rendered") for mode in ["som", "pixel"]: for m in VISION: own = [r for r in prop if r["mode"] == mode and r["model"] == m] orac = [r for r in main_rows if r["mode"] == mode and r["model"] == m and (m, r["family"], r["instance_idx"]) in parsed] print(f" {mode:<6} {m:<18} own {100 * hit_rate(own):5.1f} " f"oracle-rendered {100 * hit_rate(orac):5.1f}") if __name__ == "__main__": main()