File size: 5,727 Bytes
cb0011e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""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", "<missing>")
        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())