#!/usr/bin/env python3 """LLM agents on procedural text mazes: one-shot answer, then LLM-Modulo repair with exact validator feedback. GROQ_API_KEY=... CEREBRAS_API_KEY=... GEMINI_API_KEY=... python scripts/maze_llm_agents.py [agent ...] Resumable: results/maze_llm/.jsonl """ import json import re import sys import threading import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from robotics.benchmarks import maze_text as mt # noqa: E402 from robotics.llm.providers import LLMError, chat # noqa: E402 AGENTS = { "gpt-oss-120b@groq": ("groq", "openai/gpt-oss-120b", 3.0), "qwen3.8-27b@groq": ("groq", "qwen/qwen3.8-27b", 2.0), "qwen3.8-27b@cerebras": ("cerebras", "qwen-3.8-27b", 2.0), "gemini-3.1-flash-lite": ("gemini", "gemini-3.1-flash-lite", 4.5), } REPAIRS = 3 def run_agent(name, insts, outdir): prov, model, pace = AGENTS[name] path = Path(outdir) / (re.sub(r"[^A-Za-z0-9.@-]+", "_", name) + ".jsonl") done = set() if path.exists(): done = {json.loads(line)["id"] for line in open(path) if not json.loads(line).get("error_api")} for inst in insts: if inst["id"] in done: continue msgs = [{"role": "user", "content": mt.prompt(inst)}] attempts, err_api, t = [], None, time.perf_counter() for _ in range(REPAIRS + 1): try: text, _ = chat(msgs, providers=[prov], model=model, max_tokens=8000, retries_per_provider=6, backoff=8.0, timeout=240) except LLMError as e: if "empty content" in str(e): # the model spent its whole output budget without answering: text = "" # that is a failed attempt, not an infrastructure error else: err_api = str(e)[:150] break p = mt.parse(text) v = mt.validate(inst, p) attempts.append({"path": p, "solved": v["solved"], "error": v["error"], "n_moves": v["n_moves"]}) if v["solved"]: break msgs += [{"role": "assistant", "content": text[-1500:]}, {"role": "user", "content": f"That path is wrong: {v['error']}. Give a corrected complete path " "from S, ending with a line 'PATH: '."}] time.sleep(pace) rec = {"id": inst["id"], "agent": name, "cells": inst["cells"], "optimal": inst["optimal"], "attempts": attempts, "oneshot": bool(attempts and attempts[0]["solved"]), "modulo": bool(attempts and attempts[-1]["solved"]), "calls": len(attempts), "latency_s": round(time.perf_counter() - t, 1), "error_api": err_api} with path.open("a") as f: f.write(json.dumps(rec) + "\n") time.sleep(pace) print("done", name, flush=True) def main(): names = sys.argv[1:] or list(AGENTS) insts = mt.suite() Path("results/maze_llm").mkdir(parents=True, exist_ok=True) ths = [threading.Thread(target=run_agent, args=(n, insts, "results/maze_llm")) for n in names] for t in ths: t.start() for t in ths: t.join() if __name__ == "__main__": main()