File size: 3,412 Bytes
2983a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run the exported task directories the way a harness would.

  python3 tools/check_tasks.py              # every task, without Docker: oracle must score 1, no-op must score 0
  python3 tools/check_tasks.py --docker 2   # also build and run N tasks per environment in Docker with no network

The local mode copies environment/workdir to a temp folder, runs tests/verify.py for the no-op case,
then solution/oracle.py and tests/verify.py again. It uses only the files in the task directory.
"""
import json, os, shutil, subprocess, sys, tempfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

def tasks():
    with open(os.path.join(ROOT, "tasks.jsonl")) as f:
        return [json.loads(l) for l in f]

def reward(out):
    try: return float(open(os.path.join(out, "reward.txt")).read())
    except Exception: return None

def local(t):
    td = os.path.join(ROOT, t["path"])
    with tempfile.TemporaryDirectory() as tmp:
        app, out = os.path.join(tmp, "app"), os.path.join(tmp, "out")
        shutil.copytree(os.path.join(td, "environment", "workdir"), app)
        v = [sys.executable, "-I", os.path.join(td, "tests", "verify.py"), "--answer", os.path.join(app, "answer.json"), "--out", out]
        subprocess.run(v, capture_output=True, timeout=120); r0 = reward(out)
        subprocess.run([sys.executable, "-I", os.path.join(td, "solution", "oracle.py")], cwd=app, capture_output=True, timeout=120)
        subprocess.run(v, capture_output=True, timeout=120); r1 = reward(out)
    return r0, r1

def docker(t):
    td = os.path.join(ROOT, t["path"]); tag = "practice-check:" + t["id"].lower()
    b = subprocess.run(["docker", "build", "-q", "-t", tag, os.path.join(td, "environment")], capture_output=True, text=True, timeout=900)
    if b.returncode: return None, None, b.stderr[-300:]
    def run(cmd):
        r = subprocess.run(["docker", "run", "--rm", "--network", "none", "--memory", "1g", "--cpus", "1",
                            "-v", os.path.join(td, "tests") + ":/tests:ro", "-v", os.path.join(td, "solution") + ":/solution:ro", tag, "bash", "-c", cmd],
                           capture_output=True, text=True, timeout=300)
        try: return float(r.stdout.strip().splitlines()[-1])
        except Exception: return None
    r0 = run("bash /tests/test.sh >/dev/null 2>&1; cat /logs/verifier/reward.txt")
    r1 = run("bash /solution/solve.sh >/dev/null 2>&1; bash /tests/test.sh >/dev/null 2>&1; cat /logs/verifier/reward.txt")
    subprocess.run(["docker", "rmi", "-f", tag], capture_output=True)
    return r0, r1, ""

if __name__ == "__main__":
    ts = tasks(); bad = 0
    if "--docker" in sys.argv:
        n = int(sys.argv[sys.argv.index("--docker") + 1]); seen = {}
        for t in ts:
            k = (t["env"], t["family"])
            if seen.get(t["env"], 0) >= n or k in seen: continue
            seen[t["env"]] = seen.get(t["env"], 0) + 1; seen[k] = 1
            r0, r1, err = docker(t)
            ok = r0 == 0.0 and r1 == 1.0; bad += not ok
            print("%s docker  no-op=%s oracle=%s  %s %s" % ("ok  " if ok else "FAIL", r0, r1, t["id"], err))
    else:
        for t in ts:
            r0, r1 = local(t)
            if not (r0 == 0.0 and r1 == 1.0): bad += 1; print("FAIL no-op=%s oracle=%s %s" % (r0, r1, t["id"]))
        print("%d tasks checked, %d failures" % (len(ts), bad))
    sys.exit(1 if bad else 0)