Python
"""CLI entry point for the evaluation pipeline.
Scores agent output against rubric criteria defined in task.json using
an LLM judge. Each criterion is graded individually with only its
relevant deliverable files in context.
Usage:
uv run python -m evaluation.run_eval --run-id <id> --task real-estate/extract-psa-key-terms/scenario-01 --judge-model claude-sonnet-4-6
uv run python -m evaluation.run_eval --run-id <id> --task real-estate/extract-psa-key-terms/scenario-01 --dual
"""
import argparse
import json
import os
from datetime import datetime, timezone
from pathlib import Path
from evaluation.judge import Judge
from evaluation.report import generate_report
from evaluation.scoring import score_rubric
from utils.stdio import force_utf8_stdio
BENCH_ROOT = Path(__file__).resolve().parent.parent
RESULTS_DIR = BENCH_ROOT / "results"
REQUIRED_TASK_KEYS = {"title", "instructions", "criteria"}
REQUIRED_CRITERION_KEYS = {"id", "title", "match_criteria"}
JUDGE_MODELS = ("claude-sonnet-4-6", "gpt-5.5")
def validate_task_config(config: dict, task_path: Path) -> None:
"""Validate that task.json has all required fields for running and grading.
Raises ValueError with a specific message for any missing or malformed field.
"""
for key in REQUIRED_TASK_KEYS:
if key not in config:
raise ValueError(f"{task_path}: missing required key '{key}'")
criteria = config["criteria"]
if not isinstance(criteria, list) or not criteria:
raise ValueError(f"{task_path}: 'criteria' must be a non-empty list")
for i, criterion in enumerate(criteria):
for key in REQUIRED_CRITERION_KEYS:
if key not in criterion:
raise ValueError(
f"{task_path}: criterion {i} ('{criterion.get('id', '?')}') missing required key '{key}'"
)
# Validate deliverables is a list of strings when present
criterion_deliverables = criterion.get("deliverables", [])
if criterion_deliverables and not isinstance(criterion_deliverables, list):
raise ValueError(
f"{task_path}: criterion '{criterion['id']}' deliverables must be a list of filenames"
)
def _resolve_task_dir(task: str) -> Path:
"""Map a task name to its directory under tasks/."""
parts = task.split("/")
if len(parts) < 2:
raise ValueError(
f"Task name must have at least 2 parts (e.g., 'practice-area/task-slug'), got: {task}"
)
return BENCH_ROOT / "tasks" / Path(*parts)
def _load_env():
"""Auto-load .env if it exists and keys aren't already set."""
env_path = BENCH_ROOT / ".env"
if not env_path.exists():
return
with open(env_path) as f:
for line in f:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, _, value = line.partition("=")
key, value = key.strip(), value.strip().strip('"').strip("'")
if key and value:
os.environ.setdefault(key, value)
def evaluate_run(run_id: str, task: str, judge: Judge, parallel: int = 6) -> dict:
"""Score a run against the rubric defined in task.json.
Returns a scores dict with: run_id, task, score, max_score,
criteria_results, summary, cost, doc_coverage.
"""
task_dir = _resolve_task_dir(task)
run_dir = RESULTS_DIR / run_id
# Load task config
config_path = task_dir / "task.json"
if not config_path.exists():
raise FileNotFoundError(f"task.json not found: {config_path}")
config = json.loads(config_path.read_text(encoding="utf-8"))
# Validate and extract required fields
validate_task_config(config=config, task_path=config_path)
if not run_dir.exists():
raise FileNotFoundError(f"run directory not found: {run_dir}")
criteria = config["criteria"]
task_desc = config["title"]
result = score_rubric(
criteria=criteria,
run_dir=run_dir,
judge=judge,
task_desc=task_desc,
parallel=parallel,
)
n_criteria = len(result.criteria_results)
n_passed = sum(1 for c in result.criteria_results if c["verdict"] == "pass")
all_pass = n_criteria > 0 and n_passed == n_criteria
summary = (
f"{n_passed}/{n_criteria} criteria passed."
+ (" ALL-PASS." if all_pass else f" Missed {n_criteria - n_passed} — task FAIL.")
)
scores = {
"score": result.score,
"max_score": result.max_score,
"summary": summary,
"all_pass": all_pass,
"n_criteria": n_criteria,
"n_passed": n_passed,
"criteria_results": result.criteria_results,
"run_id": run_id,
"task": task,
"judge_model": judge.model,
"scored_at": datetime.now(timezone.utc).isoformat(),
}
# Load cost info and doc coverage from metrics.json
metrics_path = run_dir / "metrics.json"
if metrics_path.exists():
metrics = json.loads(metrics_path.read_text(encoding="utf-8"))
scores["cost"] = {
"input_tokens": metrics.get("input_tokens", 0),
"output_tokens": metrics.get("output_tokens", 0),
"wall_clock_seconds": metrics.get("wall_clock_seconds", 0),
}
scores["doc_coverage"] = {
"documents_read": metrics.get("documents_read", 0),
"total_documents": metrics.get("total_documents", 0),
"documents_skipped": metrics.get("documents_skipped", 0),
"documents_read_list": metrics.get("documents_read_list", []),
"documents_skipped_list": metrics.get("documents_skipped_list", []),
}
# Write scores.json
scores_path = run_dir / "scores.json"
scores_path.write_text(json.dumps(scores, indent=2))
return scores
def evaluate_run_dual(
run_id: str,
task: str,
parallel: int = 6,
) -> dict:
"""Score a run with both standard LAB judges and average the result.
Mirrors the dual-grading methodology used by the internal standard
evaluator, but for a single arbitrary task. Each judge grades every
criterion independently. Per-judge results are preserved alongside the
aggregate so single-judge artifacts are not overwritten.
"""
per_judge: dict[str, dict] = {}
run_dir = RESULTS_DIR / run_id
out_path = run_dir / "scores_dual.json"
# A failed re-grade must not leave an earlier complete aggregate in place.
out_path.unlink(missing_ok=True)
for judge_model in JUDGE_MODELS:
judge = Judge(model=judge_model)
scores = evaluate_run(
run_id=run_id,
task=task,
judge=judge,
parallel=parallel,
)
per_judge[judge_model] = scores
# Move the just-written scores.json to a per-judge filename so
# subsequent judges do not clobber it.
scores_path = run_dir / "scores.json"
if scores_path.exists():
scores_path.rename(run_dir / f"scores_{judge_model}.json")
def crit_frac(scores: dict) -> float:
return (
scores["n_passed"] / scores["n_criteria"]
if scores.get("n_criteria")
else 0.0
)
dual_crit = sum(crit_frac(scores) for scores in per_judge.values()) / len(
per_judge
)
dual_ap = sum(
1.0 if scores.get("all_pass") else 0.0
for scores in per_judge.values()
) / len(per_judge)
aggregate = {
"run_id": run_id,
"task": task,
"scored_at": datetime.now(timezone.utc).isoformat(),
"judges": list(JUDGE_MODELS),
"per_judge": per_judge,
"dual_criterion_pass": dual_crit,
"dual_all_pass_rate": dual_ap,
"all_pass": dual_ap == 1.0,
}
out_path.write_text(
json.dumps(aggregate, indent=2),
encoding="utf-8",
)
return aggregate
def _print_summary(scores: dict):
"""Print a concise score summary."""
print(f" {scores['summary']}")
print(f" Score: {scores['score']:.2f}")
cov = scores.get("doc_coverage", {})
if cov.get("total_documents"):
print(f" Doc coverage: {cov['documents_read']}/{cov['total_documents']} files read")
cost = scores.get("cost", {})
if cost.get("input_tokens"):
print(f" Tokens: {cost['input_tokens'] + cost['output_tokens']:,}")
print()
print(f" Scores written to results/{scores['run_id']}/scores.json")
def _print_dual_summary(aggregate: dict) -> None:
"""Print a concise summary of a complete dual-judge evaluation."""
print(f" Judges: {', '.join(aggregate['judges'])}")
print()
for judge_model, scores in aggregate["per_judge"].items():
print(f" {judge_model}:")
print(f" {scores['summary']}")
print()
print(f" Dual criterion-pass: {aggregate['dual_criterion_pass'] * 100:.1f}%")
print(f" Dual all-pass: {aggregate['dual_all_pass_rate'] * 100:.1f}%")
print()
print(
f" Per-judge scores: results/{aggregate['run_id']}/scores_<judge>.json"
)
print(f" Aggregate scores: results/{aggregate['run_id']}/scores_dual.json")
def main():
force_utf8_stdio()
parser = argparse.ArgumentParser(
description="Score a benchmark run against rubric criteria"
)
parser.add_argument("--run-id", required=True, help="Run ID to evaluate")
parser.add_argument(
"--task",
required=True,
help="Task ID (e.g., real-estate/extract-psa-key-terms/scenario-01)",
)
parser.add_argument(
"--judge-model",
default="claude-sonnet-4-6",
help="Model to use as LLM judge (single-judge mode). Ignored with --dual.",
)
parser.add_argument(
"--dual",
action="store_true",
help=(
"Grade with the standard LAB judge pair "
"(claude-sonnet-4-6 + gpt-5.5) and average their scores"
),
)
parser.add_argument(
"--parallel",
type=int,
default=6,
help="Number of judge calls to run concurrently.",
)
parser.add_argument("--verbose", action="store_true", help="Print detailed output")
args = parser.parse_args()
_load_env()
print(f"Evaluating run '{args.run_id}' on task '{args.task}'")
if args.dual:
print(f"Dual-judge mode: {', '.join(JUDGE_MODELS)}")
print()
scores = evaluate_run_dual(
run_id=args.run_id,
task=args.task,
parallel=args.parallel,
)
if args.verbose:
print(json.dumps(scores, indent=2))
else:
_print_dual_summary(scores)
else:
print(f"Judge model: {args.judge_model}")
print()
judge = Judge(model=args.judge_model)
scores = evaluate_run(
run_id=args.run_id,
task=args.task,
judge=judge,
parallel=args.parallel,
)
if args.verbose:
print(json.dumps(scores, indent=2))
else:
_print_summary(scores)
report_path = generate_report(run_id=args.run_id)
print(f" Report written to: {report_path}")
if __name__ == "__main__":
main()