"""Acceptance grader for fruit-prep station FP-2. Runs /app/controller.py through the gauntlet on the hidden episodes, with this directory's own copies of the gauntlet, task.py, the scene and the robot model. The controller runs in a separate, unprivileged process (gauntlet/harness.py, the same module run_gauntlet.py uses): it receives each observation pickled by this side and answers with raw float64 bytes or a small JSON status object, so nothing from the controller is ever unpickled here. Writes /logs/verifier/reward.txt (mean episode score) and details.json. """ import hashlib import json import os import sys import time HERE = os.path.dirname(os.path.abspath(__file__)) WORLD = os.path.join(HERE, "world") sys.path.insert(0, WORLD) import task as task_mod # noqa: E402 (also puts WORLD/gauntlet on sys.path) import gauntlet as G # noqa: E402 import harness # noqa: E402 APP = os.environ.get("FP2_APP_DIR", "/app") LOG_DIR = os.environ.get("FP2_LOG_DIR", "/logs/verifier") CONTROLLER = os.path.join(APP, "controller.py") def sha256(path): with open(path, "rb") as f: return hashlib.sha256(f.read()).hexdigest() def write_result(reward, details): os.makedirs(LOG_DIR, exist_ok=True) with open(os.path.join(LOG_DIR, "reward.txt"), "w") as f: f.write(f"{reward:.6f}\n") with open(os.path.join(LOG_DIR, "details.json"), "w") as f: json.dump(details, f, indent=1, default=str) print(f"reward {reward:.6f}") def main(): with open(os.path.join(HERE, "hidden_seeds.json")) as f: spec = json.load(f) with open(os.path.join(HERE, "app_sha256.json")) as f: expected = json.load(f) details = {"episodes": []} for rel, h in expected.items(): p = os.path.join(APP, rel) if not os.path.isfile(p) or sha256(p) != h: details["error"] = f"/app/{rel} differs from the shipped file (it is read-only)" return write_result(0.0, details) if not os.path.isfile(CONTROLLER): details["error"] = "/app/controller.py not found" return write_result(0.0, details) if os.geteuid() == 0: try: os.chmod(HERE, 0o700) # the controller runs as nobody and cannot read the hidden seeds except Exception: pass os.makedirs(LOG_DIR, exist_ok=True) ws = harness.Workspace(CONTROLLER, APP, stderr_path=os.path.join(LOG_DIR, "controller_stderr.log")) task = task_mod.TASK scores, t_all, sim_s = [], time.monotonic(), 0.0 skills = {} try: for e in spec["episodes"]: tier, idx, band = e["tier"], int(e["idx"]), e["band"] task_mod.BAND = band r = harness.run_episode(G, task, ws, tier, idx, int(spec["base_seed"]), os.path.join(APP, "scene.xml")) rep = r["report"] s = float(rep["score"]) scores.append(s) m = r.get("metrics", {}) or {} sim_s += float(r.get("done_time_s") or 0.0) for st in rep["stages"]: k = st["name"].split(":")[0] a = skills.setdefault(k, [0, 0]) a[0] += 1 if st["done"] else 0 a[1] += 1 details["episodes"].append({ "tier": tier, "idx": idx, "band": band, "score": s, "fully_done": rep["fully_done"], "stages": rep["stages"], "cycle_time_s": rep["cycle_time_s"], "safe": r.get("safe"), "violations": r.get("violations"), "fault": (r.get("pspec", {}).get("fault") or "") if r.get("onset_tick") is not None else "", "fault_drawn": r.get("pspec", {}).get("fault") or "", "fault_trigger": r.get("pspec", {}).get("fault_trigger") or "", "fault_onset_s": r.get("onset_s"), "stages_done_before_fault": r.get("onset_done"), "n_stages": m.get("n_stages"), "aborted_ok": r.get("aborted_ok"), "disturbance": r.get("pspec", {}).get("disturbance") or "", "outcome": r.get("outcome"), "done_time_s": r.get("done_time_s"), "n_pieces": m.get("n_pieces"), "budget_s": m.get("budget_s"), "bowl_shift_mm": m.get("bowl_shift_mm"), "max_squeeze_n": m.get("max_squeeze_n"), "final": m.get("final"), "violation_times": m.get("violation_times"), "error": r.get("error", "")}) print(f"{tier} {idx:2d} {band:10s} score={s:.3f} fully_done={rep['fully_done']} violations={r.get('violations')}", flush=True) finally: ws.close() reward = sum(scores) / len(scores) if scores else 0.0 n_full = sum(1 for e in details["episodes"] if e["fully_done"]) details["reward"] = round(reward, 6) details["n_episodes"] = len(scores) details["fully_done_rate"] = round(n_full / max(1, len(scores)), 4) details["stage_done_share"] = {k: round(a / max(1, b), 4) for k, (a, b) in sorted(skills.items())} details["time_per_completed_s"] = round(sim_s / n_full, 2) if n_full else None details["simulated_s"] = round(sim_s, 2) print(f"grading wall time {time.monotonic() - t_all:.1f}s", file=sys.stderr) write_result(reward, details) if __name__ == "__main__": main()