| |
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import shutil |
| import subprocess |
| import time |
| from pathlib import Path |
|
|
|
|
| ROOT = Path(__file__).resolve().parents[1] |
| DEFAULT_EVALS = ROOT / "data" / "swival_tool_calling_eval.jsonl" |
| WORK_ROOT = ROOT / "tmp" / "swival-eval" |
|
|
|
|
| def iter_jsonl(path: Path): |
| with path.open() as f: |
| for line in f: |
| if line.strip(): |
| yield json.loads(line) |
|
|
|
|
| def seed_workspace(case: dict, workdir: Path) -> None: |
| if workdir.exists(): |
| shutil.rmtree(workdir) |
| workdir.mkdir(parents=True) |
| for rel, text in case["files"].items(): |
| dest = workdir / rel |
| dest.parent.mkdir(parents=True, exist_ok=True) |
| dest.write_text(text) |
|
|
|
|
| def verify_case(case: dict, workdir: Path) -> list[str]: |
| errors: list[str] = [] |
| for rel, expected in (case.get("verify") or {}).items(): |
| path = workdir / rel |
| if not path.exists(): |
| errors.append(f"{rel} is missing") |
| elif path.read_text() != expected: |
| errors.append(f"{rel} did not match expected content") |
| for rel, needles in (case.get("verify_contains") or {}).items(): |
| path = workdir / rel |
| if not path.exists(): |
| errors.append(f"{rel} is missing") |
| continue |
| text = path.read_text().lower() |
| for needle in needles: |
| if needle.lower() not in text: |
| errors.append(f"{rel} does not contain {needle!r}") |
| for rel in case.get("verify_missing") or []: |
| if (workdir / rel).exists(): |
| errors.append(f"{rel} still exists") |
| command = case.get("verify_command") |
| if command: |
| proc = subprocess.run(command, cwd=workdir, text=True, capture_output=True, timeout=120) |
| if proc.returncode != 0: |
| errors.append(f"verify_command failed: {proc.stdout}{proc.stderr}") |
| return errors |
|
|
|
|
| def apply_expected_outputs(case: dict, workdir: Path) -> None: |
| for rel, text in (case.get("verify") or {}).items(): |
| path = workdir / rel |
| path.parent.mkdir(parents=True, exist_ok=True) |
| path.write_text(text) |
| for rel, needles in (case.get("verify_contains") or {}).items(): |
| path = workdir / rel |
| path.parent.mkdir(parents=True, exist_ok=True) |
| path.write_text("\n".join(needles) + "\n") |
| for rel in case.get("verify_missing") or []: |
| path = workdir / rel |
| if path.exists(): |
| path.unlink() |
|
|
|
|
| def run_case(case: dict, profile: str, swival_bin: str, timeout: int) -> dict: |
| workdir = WORK_ROOT / case["id"] |
| seed_workspace(case, workdir) |
| cmd = [swival_bin, "--profile", profile, case["prompt"]] |
| start = time.time() |
| proc = subprocess.run(cmd, cwd=workdir, text=True, capture_output=True, timeout=timeout) |
| verify_errors = verify_case(case, workdir) if proc.returncode == 0 else ["swival exited nonzero"] |
| return { |
| "id": case["id"], |
| "returncode": proc.returncode, |
| "seconds": round(time.time() - start, 3), |
| "passed": proc.returncode == 0 and not verify_errors, |
| "verify_errors": verify_errors, |
| "stdout_tail": proc.stdout[-4000:], |
| "stderr_tail": proc.stderr[-4000:], |
| } |
|
|
|
|
| def self_check_case(case: dict) -> dict: |
| workdir = WORK_ROOT / f"self-check-{case['id']}" |
| seed_workspace(case, workdir) |
| apply_expected_outputs(case, workdir) |
| errors = verify_case(case, workdir) |
| return {"id": case["id"], "passed": not errors, "verify_errors": errors} |
|
|
|
|
| def main() -> int: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--evals", type=Path, default=DEFAULT_EVALS) |
| parser.add_argument("--profile", default="omlx") |
| parser.add_argument("--swival-bin", default="swival") |
| parser.add_argument("--timeout", type=int, default=300) |
| parser.add_argument("--limit", type=int) |
| parser.add_argument("--self-check", action="store_true", help="Verify eval seed/check logic without invoking Swival.") |
| args = parser.parse_args() |
|
|
| WORK_ROOT.mkdir(parents=True, exist_ok=True) |
| cases = list(iter_jsonl(args.evals)) |
| if args.limit is not None: |
| cases = cases[: args.limit] |
|
|
| results = [] |
| for case in cases: |
| if args.self_check: |
| result = self_check_case(case) |
| print(json.dumps(result, indent=2)) |
| results.append(result) |
| continue |
| print(f"running {case['id']}") |
| try: |
| result = run_case(case, args.profile, args.swival_bin, args.timeout) |
| except subprocess.TimeoutExpired as exc: |
| result = { |
| "id": case["id"], |
| "returncode": None, |
| "seconds": args.timeout, |
| "passed": False, |
| "verify_errors": ["swival timed out"], |
| "stdout_tail": (exc.stdout or "")[-4000:], |
| "stderr_tail": (exc.stderr or "")[-4000:], |
| } |
| print(json.dumps(result, indent=2)) |
| results.append(result) |
|
|
| out = ROOT / "tmp" / "swival-eval-results.json" |
| out.write_text(json.dumps(results, indent=2) + "\n") |
| failed = [r for r in results if not r["passed"]] |
| print(f"passed {len(results) - len(failed)}/{len(results)}") |
| return 1 if failed else 0 |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|