#!/usr/bin/env python3 """End-to-end episode tests with scripted agents -- no model involved. Three controls: perfect fixes the real cause -> must resolve noop changes nothing -> must not resolve symptom-fix silences the error at the symptom site -> must NOT resolve The third is the one that matters. It is the fix a model reaches for when it reads the traceback and stops there, and a benchmark that scored it as correct would be rewarding exactly the behaviour it is meant to detect. """ import json, sys, tempfile, shutil from pathlib import Path EVAL = Path(__file__).resolve().parents[2] / "examples" / "llama-eval" sys.path.insert(0, str(EVAL)) sys.path.insert(0, str(Path(__file__).parent)) from agentic_eval import (AgenticTask, Workspace, ToolBox, Runner, LANGS, # noqa: E402 materialise, run_episode, run_tests_against, score) from build_corpus import sandbox_for # noqa: E402 fails = [] def check(name, cond, detail=""): print(f" {'PASS' if cond else 'FAIL'} {name}" + (f" [{detail}]" if not cond else "")) if not cond: fails.append(name) def scripted(*batches): """Turn lists of (tool, args) into an OpenAI-shaped chat callable.""" turns = list(batches) state = {"i": 0} def chat(messages, tools): i = state["i"] state["i"] += 1 if i >= len(turns): return {"choices": [{"message": {"content": "done", "role": "assistant"}}], "usage": {"prompt_tokens": 100, "completion_tokens": 10}} calls = [{"id": f"c{i}_{j}", "type": "function", "function": {"name": n, "arguments": json.dumps(a)}} for j, (n, a) in enumerate(turns[i])] return {"choices": [{"message": {"content": "", "role": "assistant", "tool_calls": calls}}], "usage": {"prompt_tokens": 500 * (i + 1), "completion_tokens": 60}} return chat corpus = [json.loads(l) for l in (Path(__file__).parent / "agentic-corpus.jsonl").read_text().splitlines()] by_id = {r["task_id"]: r for r in corpus} task = AgenticTask.from_record(by_id["ledger-since-inclusive"]) print(f"task: {task.task_id} f2p={len(task.fail_to_pass)} p2p={len(task.pass_to_pass)}") runner = Runner(sandbox_for("python"), LANGS["python"]) root = Path(tempfile.mkdtemp(prefix="episode-")) AGENTS = { "perfect": scripted( [("list_files", {})], [("read_file", {"path": "ledger/projections.py", "start_line": 1, "end_line": 60})], [("read_file", {"path": "ledger/store.py"})], [("edit_replace", {"path": "ledger/store.py", "old_text": "if e.seq >= seq", "new_text": "if e.seq > seq"})], [("lint", {"path": "ledger"})], [("finish", {"summary": "made since() exclusive again"})], ), "noop": scripted( [("list_files", {})], [("finish", {"summary": "nothing to do"})], ), "symptom-fix": scripted( [("read_file", {"path": "ledger/projections.py"})], # delete the ordering guard so the crash goes away [("edit_replace", { "path": "ledger/projections.py", "old_text": (" if event.seq <= self.last_seq:\n" " raise SequenceError(\n" " f\"event {event.seq} already applied " "(at {self.last_seq})\")\n"), "new_text": ""})], [("finish", {"summary": "removed the exception"})], ), } try: results = {} for name, chat in AGENTS.items(): ws_root = root / name materialise(task.files, ws_root) ws = Workspace(ws_root) tb = ToolBox(ws, linter=lambda t, w=ws_root: runner.lint(w, t)) ep, _ = run_episode(task, tb, chat, max_turns=20) final = ws.snapshot() outcomes = run_tests_against(final, task, runner, root, name) sc = score(task, outcomes) results[name] = (ep, sc) print(f" [{name}] stop={ep.stop_reason} calls={ep.tool_calls} " f"edits={ep.edits} errs={ep.tool_errors} -> " f"resolved={sc['resolved']} f2p={sc['f2p_passed']}/{sc['f2p_total']} " f"regressions={sc['n_regressions']}") ep, sc = results["perfect"] check("perfect agent resolves the task", sc["resolved"], json.dumps(sc)) check("perfect agent hit no tool errors", ep.tool_errors == 0, str(ep.tool_errors)) check("perfect agent terminated via finish", ep.stop_reason == "finished", ep.stop_reason) check("perfect agent caused no regressions", sc["n_regressions"] == 0) ep, sc = results["noop"] check("noop agent does not resolve", not sc["resolved"], json.dumps(sc)) check("noop agent fixes nothing", sc["f2p_passed"] == 0, str(sc["f2p_passed"])) ep, sc = results["symptom-fix"] check("symptom-only fix made an edit", ep.edits == 1, str(ep.edits)) check("symptom-only fix is NOT scored as resolved", not sc["resolved"], json.dumps(sc)) check("symptom-only fix is caught as a regression", sc["n_regressions"] > 0, json.dumps(sc)) # token accounting must be populated ep, _ = results["perfect"] check("token accounting recorded", ep.peak_context > 0 and ep.prompt_tokens > 0, f"peak={ep.peak_context} prompt={ep.prompt_tokens}") finally: shutil.rmtree(root, ignore_errors=True) print(f"\n{'ALL PASS' if not fails else 'FAILURES: ' + ', '.join(fails)}") sys.exit(1 if fails else 0)