"""Structural validation for SWE-DockerLess.jsonl. Checks that every row is SWE-bench shaped, that the two patches apply to the embedded snapshot, that they touch disjoint files, and that the gold patch stays inside a budget a 2048-token completion can produce. Run with: python tools/validate.py """ import json import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent DATASET = ROOT / "SWE-DockerLess.jsonl" SWE_BENCH_FIELDS = [ "instance_id", "repo", "base_commit", "patch", "test_patch", "problem_statement", "hints_text", "created_at", "version", "environment_setup_commit", "FAIL_TO_PASS", "PASS_TO_PASS", ] MAX_PROBLEM_CHARS = 4000 MAX_GOLD_ADDED_LINES = 120 failures = [] def check(condition, message): if not condition: failures.append(message) def write_tree(files, dest): for rel, content in files.items(): path = dest / rel path.parent.mkdir(parents=True, exist_ok=True) with open(path, "w", encoding="utf-8", newline="\n") as handle: handle.write(content) def git(args, cwd): return subprocess.run( [ "git", "-c", "core.autocrlf=false", "-c", "user.name=v", "-c", "user.email=v@example.invalid", ] + args, cwd=str(cwd), capture_output=True, text=True, encoding="utf-8", ) def touched_paths(patch): paths = set() for line in patch.splitlines(): if line.startswith("+++ b/") or line.startswith("--- a/"): paths.add(line[6:]) elif line.startswith("+++ ") or line.startswith("--- "): value = line[4:].strip() if value != "/dev/null": paths.add(value.split("/", 1)[-1]) return paths def added_lines(patch): return sum(1 for line in patch.splitlines() if line.startswith("+") and not line.startswith("+++")) def main(): rows = [json.loads(line) for line in open(DATASET, encoding="utf-8") if line.strip()] check(len(rows) == 4, "expected 4 rows, found %d" % len(rows)) seen = set() for row in rows: iid = row.get("instance_id", "") for field in SWE_BENCH_FIELDS: check(field in row, "%s: missing field %s" % (iid, field)) check(iid not in seen, "%s: duplicate instance_id" % iid) seen.add(iid) check(isinstance(row["FAIL_TO_PASS"], str), "%s: FAIL_TO_PASS must be a JSON string" % iid) check(isinstance(row["PASS_TO_PASS"], str), "%s: PASS_TO_PASS must be a JSON string" % iid) f2p = json.loads(row["FAIL_TO_PASS"]) p2p = json.loads(row["PASS_TO_PASS"]) check(len(f2p) >= 1, "%s: empty FAIL_TO_PASS" % iid) check(len(p2p) >= 1, "%s: empty PASS_TO_PASS" % iid) check(not (set(f2p) & set(p2p)), "%s: F2P/P2P overlap" % iid) check(len(row["base_commit"]) == 40, "%s: base_commit is not a 40-char sha" % iid) problem = row["problem_statement"] check(len(problem) <= MAX_PROBLEM_CHARS, "%s: problem statement too long" % iid) for test_id in f2p + p2p: check("::" not in problem, "%s: problem statement leaks a test id" % iid) gold_paths = touched_paths(row["patch"]) test_paths = touched_paths(row["test_patch"]) check(not (gold_paths & test_paths), "%s: patch and test_patch touch the same files" % iid) check( all(not path.startswith("tests/") for path in gold_paths), "%s: gold patch edits test files" % iid, ) check( all(path.startswith("tests/") for path in test_paths), "%s: test_patch edits non-test files" % iid, ) check( added_lines(row["patch"]) <= MAX_GOLD_ADDED_LINES, "%s: gold patch adds %d lines (budget %d)" % (iid, added_lines(row["patch"]), MAX_GOLD_ADDED_LINES), ) files = row["dockerless"]["files"] check(bool(files), "%s: empty snapshot" % iid) with tempfile.TemporaryDirectory(prefix="swedl-val-") as tmp: work = Path(tmp) / "w" work.mkdir() write_tree(files, work) git(["init", "-q", "-b", "main"], work) git(["add", "-A"], work) git(["commit", "-q", "-m", "base"], work) for label, patch_text in (("test_patch", row["test_patch"]), ("patch", row["patch"])): patch_file = Path(tmp) / (label + ".patch") patch_file.write_text(patch_text, encoding="utf-8", newline="\n") result = git(["apply", "--check", str(patch_file)], work) check( result.returncode == 0, "%s: %s does not apply (%s)" % (iid, label, result.stderr.strip()[:200]), ) if label == "test_patch": git(["apply", str(patch_file)], work) print( "%-22s fields=ok F2P=%-2d P2P=%-2d +%-3d lines problem=%d chars files=%d" % ( iid, len(f2p), len(p2p), added_lines(row["patch"]), len(problem), len(files), ) ) total_tests = sum(len(json.loads(row["FAIL_TO_PASS"])) for row in rows) print("\n%d instances, %d FAIL_TO_PASS tests total" % (len(rows), total_tests)) if failures: print("\nFAILED %d checks:" % len(failures)) for message in failures: print(" - %s" % message) return 1 print("all checks passed") return 0 if __name__ == "__main__": sys.exit(main())