# Copyright (c) 2026 CLSA contributors # SPDX-License-Identifier: MIT """Shared runner for experiment scripts: provenance, config, blocking.""" from __future__ import annotations import argparse import inspect import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from clsa.util import load_config, write_result # noqa: E402 def run(name: str, fn, config_name: str = "controller", notes: str = "") -> dict: ap = argparse.ArgumentParser() ap.add_argument("--model", default="primary") args = ap.parse_args() cfg = load_config(config_name) try: # Experiments take a backbone key; E8 takes an episode count, so only pass # the model where the signature asks for one. first = next(iter(inspect.signature(fn).parameters), None) payload = fn(args.model) if first in ("model", "backbone_key") else fn() payload.pop("raw_rows", None) Path("results").mkdir(exist_ok=True) path = write_result(name, payload, config=cfg, seed=int(cfg.get("seed", 0)), notes=notes) print(f"[{name}] wrote {path}") return payload except Exception as exc: # noqa: BLE001 - blocked experiments must be recorded import traceback payload = { "blocked": True, "error": f"{type(exc).__name__}: {exc}", "traceback": traceback.format_exc()[-2000:], } path = write_result( name, payload, config=cfg, seed=int(cfg.get("seed", 0)), notes=f"BLOCKED: {exc}" ) print(f"[{name}] BLOCKED -> {path}\n{exc}") return payload