|
| 1 | +"""Fail-fast SWE-bench Verified ground-truth runner for nightly CI. |
| 2 | +
|
| 3 | +This is intentionally narrower than ``runner.py``: |
| 4 | +
|
| 5 | +* ground-truth patches only, no agent phase or OpenAI configuration |
| 6 | +* sequential execution inside each shard |
| 7 | +* exit immediately on the first unresolved instance, patch-apply failure, |
| 8 | + skip, or runner exception |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import argparse |
| 14 | +import asyncio |
| 15 | +import json |
| 16 | +import logging |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | +from typing import Any |
| 20 | + |
| 21 | +from datasets import load_dataset |
| 22 | +from runner import _selected_instances, _should_fail, evaluate_ground_truth_one |
| 23 | + |
| 24 | +logger = logging.getLogger("eval_cc_swe.ci_runner") |
| 25 | + |
| 26 | + |
| 27 | +def _write_run_summary(out_dir: Path, summaries: list[dict[str, Any]]) -> None: |
| 28 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 29 | + failures = [s for s in summaries if _should_fail(s)] |
| 30 | + (out_dir / "summary.json").write_text(json.dumps(summaries, indent=2)) |
| 31 | + if failures: |
| 32 | + (out_dir / "failures.json").write_text(json.dumps(failures, indent=2)) |
| 33 | + |
| 34 | + |
| 35 | +def _failure_reason(summary: dict[str, Any]) -> str: |
| 36 | + if summary.get("error"): |
| 37 | + return str(summary["error"]) |
| 38 | + if summary.get("skipped"): |
| 39 | + return f"skipped: {summary['skipped']}" |
| 40 | + if not summary.get("patch_applied"): |
| 41 | + return "patch did not apply" |
| 42 | + if not summary.get("resolved"): |
| 43 | + ftp = summary.get("fail_to_pass", {}) or {} |
| 44 | + ptp = summary.get("pass_to_pass", {}) or {} |
| 45 | + return ( |
| 46 | + "unresolved: " |
| 47 | + f"FAIL_TO_PASS failures={len(ftp.get('failure', []))}, " |
| 48 | + f"PASS_TO_PASS failures={len(ptp.get('failure', []))}" |
| 49 | + ) |
| 50 | + return "unknown failure" |
| 51 | + |
| 52 | + |
| 53 | +async def run_ci(args: argparse.Namespace) -> int: |
| 54 | + ds = load_dataset(args.dataset, split=args.split) |
| 55 | + instances = _selected_instances( |
| 56 | + ds, |
| 57 | + instance_ids=args.instance_id, |
| 58 | + limit=args.limit, |
| 59 | + ground_truth=True, |
| 60 | + num_shards=args.num_shards, |
| 61 | + shard_index=args.shard_index, |
| 62 | + ) |
| 63 | + if not instances: |
| 64 | + print("error: no instances selected", file=sys.stderr) |
| 65 | + return 2 |
| 66 | + |
| 67 | + out_dir = Path(args.out) |
| 68 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 69 | + print(f"selected {len(instances)} instance(s) (shard {args.shard_index + 1}/{args.num_shards}, fail_fast=True)") |
| 70 | + |
| 71 | + summaries: list[dict[str, Any]] = [] |
| 72 | + for offset, inst in enumerate(instances, start=1): |
| 73 | + iid = inst["instance_id"] |
| 74 | + print(f"[ci] {offset}/{len(instances)} {iid}") |
| 75 | + try: |
| 76 | + summary = await evaluate_ground_truth_one( |
| 77 | + inst, |
| 78 | + bundle_image=args.bundle_image, |
| 79 | + swebench_namespace=args.swebench_namespace, |
| 80 | + swebench_tag=args.swebench_tag, |
| 81 | + arch=args.arch, |
| 82 | + docker_platform=args.docker_platform, |
| 83 | + eval_timeout=args.eval_timeout, |
| 84 | + out_dir=out_dir, |
| 85 | + ) |
| 86 | + except Exception as exc: |
| 87 | + logger.exception("[%s] crashed", iid) |
| 88 | + summary = { |
| 89 | + "instance_id": iid, |
| 90 | + "mode": "ground_truth", |
| 91 | + "error": f"{type(exc).__name__}: {exc}", |
| 92 | + } |
| 93 | + (out_dir / f"{iid}.json").write_text(json.dumps(summary, indent=2)) |
| 94 | + |
| 95 | + summaries.append(summary) |
| 96 | + _write_run_summary(out_dir, summaries) |
| 97 | + |
| 98 | + if _should_fail(summary): |
| 99 | + print( |
| 100 | + f"[ci] fail-fast: {iid} failed: {_failure_reason(summary)}", |
| 101 | + file=sys.stderr, |
| 102 | + ) |
| 103 | + return 1 |
| 104 | + |
| 105 | + print(f"\n{len(summaries)}/{len(summaries)} resolved") |
| 106 | + return 0 |
| 107 | + |
| 108 | + |
| 109 | +def build_parser() -> argparse.ArgumentParser: |
| 110 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 111 | + parser.add_argument("--bundle-image", default="eval-cc-swe:0.2.0") |
| 112 | + parser.add_argument("--swebench-namespace", default="swebench") |
| 113 | + parser.add_argument("--swebench-tag", default="latest") |
| 114 | + parser.add_argument("--arch", default="x86_64", choices=["x86_64", "arm64"]) |
| 115 | + parser.add_argument( |
| 116 | + "--docker-platform", |
| 117 | + default=None, |
| 118 | + help="Docker platform for the runtime and task containers, e.g. linux/amd64.", |
| 119 | + ) |
| 120 | + parser.add_argument("--dataset", default="princeton-nlp/SWE-bench_Verified") |
| 121 | + parser.add_argument("--split", default="test") |
| 122 | + parser.add_argument( |
| 123 | + "--limit", |
| 124 | + type=int, |
| 125 | + default=None, |
| 126 | + help="Number of dataset rows to consider before sharding. Defaults to all rows.", |
| 127 | + ) |
| 128 | + parser.add_argument("--instance-id", action="append", default=None) |
| 129 | + parser.add_argument("--num-shards", type=int, default=1) |
| 130 | + parser.add_argument("--shard-index", type=int, default=0) |
| 131 | + parser.add_argument("--eval-timeout", type=float, default=1800) |
| 132 | + parser.add_argument("--out", default="runs/ci-ground-truth") |
| 133 | + return parser |
| 134 | + |
| 135 | + |
| 136 | +def main(argv: list[str] | None = None) -> int: |
| 137 | + logging.basicConfig( |
| 138 | + level=logging.INFO, |
| 139 | + format="%(asctime)s [%(name)s] %(message)s", |
| 140 | + ) |
| 141 | + args = build_parser().parse_args(argv) |
| 142 | + return asyncio.run(run_ci(args)) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + raise SystemExit(main(sys.argv[1:])) |
0 commit comments