File size: 6,146 Bytes
2983a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ba413f
2983a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ba413f
 
2983a9c
 
 
 
 
 
 
 
 
 
 
1ba413f
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
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
#!/usr/bin/env python3
"""Export every environment as Harbor task directories under tasks/<env>/<task_id>/.

  python3 tools/export.py            # all environments
  python3 tools/export.py ward_round # one

Each task directory is self-contained:
  instruction.md              what the agent is asked to do
  task.toml                   metadata, limits
  environment/Dockerfile      python:3.12-slim with the task files in /app; no network needed
  environment/workdir/...     the task files
  solution/solve.sh, oracle.py    reference solution (never shown to the agent)
  tests/test.sh, verify.py, checks.json[, validators.py]   hidden verifier; writes /logs/verifier/reward.txt
"""
import importlib, json, os, shutil, sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
TRAIN, EVAL, EVAL_BASE = 84, 24, 10000

def read_toml(path):
    """Tiny reader for the flat env.toml files (strings and string lists only)."""
    out = {}
    for line in open(path):
        line = line.strip()
        if not line or line.startswith("#") or "=" not in line: continue
        k, v = line.split("=", 1)
        out[k.strip()] = json.loads(v.strip())
    return out

def env_dirs():
    return sorted(d for d in os.listdir(os.path.join(ROOT, "envs")) if os.path.exists(os.path.join(ROOT, "envs", d, "env.toml")))

def plan(families):
    f = len(families)
    for split, n, base in (("train", TRAIN, 0), ("eval", EVAL, EVAL_BASE)):
        for i in range(n):
            yield split, base + i, sorted(families)[i % f], 1 + (i // f) % 3

DOCKERFILE = "FROM python:3.12-slim\nWORKDIR /app\nCOPY workdir/ /app/\n"
SOLVE = "#!/bin/bash\nset -e\ncd /app\npython3 /solution/oracle.py\n"
TEST = "#!/bin/bash\nmkdir -p /logs/verifier\npython3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt\n"

def write(path, text, mode=None):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w") as f: f.write(text)
    if mode: os.chmod(path, mode)

def export_env(d, manifest):
    meta = read_toml(os.path.join(ROOT, "envs", d, "env.toml"))
    languages = meta.get("languages", ["en"])
    gen = importlib.import_module("envs.%s.gen" % d)
    out = os.path.join(ROOT, "tasks", meta["name"])
    shutil.rmtree(out, ignore_errors=True)
    verify_src = open(os.path.join(ROOT, "practice", "verify.py")).read()
    oracle_src = open(os.path.join(ROOT, "envs", d, "oracle.py")).read()
    vpath = os.path.join(ROOT, "envs", d, "validators.py")
    validators_src = open(vpath).read() if os.path.exists(vpath) else None
    n = 0
    for split, seed, family, difficulty in plan(meta["families"]):
        t = gen.generate(seed, difficulty, family)
        tid = "%s-%s-d%d-%s%04d" % (meta["name"], family, difficulty, "e" if split == "eval" else "t", seed % EVAL_BASE)
        td = os.path.join(out, tid)
        write(os.path.join(td, "instruction.md"), t.instruction.strip() + "\n")
        write(os.path.join(td, "task.toml"), "\n".join([
            'version = "1.0"', "", "[metadata]", 'author_name = "Renaissance Labs"', 'author_email = "unknown"',
            'difficulty = "%s"' % {1: "easy", 2: "medium", 3: "hard"}[difficulty], 'category = "%s"' % meta["name"],
            "tags = %s" % json.dumps([meta["name"], family, "isced-" + meta["isced"], split] + ["language-" + lang for lang in languages]),
            'isced_f = "%s"' % meta["isced"], 'family = "%s"' % family, 'split = "%s"' % split, "seed = %d" % seed, "languages = " + json.dumps(languages), "",
            "[verifier]", "timeout_sec = 120.0", "", "[agent]", "timeout_sec = 900.0", "",
            "[environment]", "build_timeout_sec = 600.0", "cpus = 1", "memory_mb = 1024", "storage_mb = 2048", "allow_internet = false", ""]))
        write(os.path.join(td, "environment", "Dockerfile"), DOCKERFILE)
        for rel, text in t.files.items(): write(os.path.join(td, "environment", "workdir", rel), text)
        write(os.path.join(td, "solution", "solve.sh"), SOLVE, 0o755)
        write(os.path.join(td, "solution", "oracle.py"), oracle_src)
        write(os.path.join(td, "tests", "test.sh"), TEST, 0o755)
        write(os.path.join(td, "tests", "verify.py"), verify_src)
        write(os.path.join(td, "tests", "checks.json"), json.dumps(t.checks, indent=1) + "\n")
        if validators_src: write(os.path.join(td, "tests", "validators.py"), validators_src)
        manifest.append({"id": tid, "env": meta["name"], "isced_f": meta["isced"], "family": family, "difficulty": difficulty, "split": split,
                         "seed": seed, "languages": languages, "task_meta": t.meta, "path": "tasks/%s/%s" % (meta["name"], tid), "title": t.title, "checks": len(t.checks), "instruction": t.instruction.strip()})
        n += 1
    return meta, n

if __name__ == "__main__":
    only = sys.argv[1:]
    manifest, envs = [], []
    for d in env_dirs():
        if only and d not in only: continue
        meta, n = export_env(d, manifest); meta["tasks"] = n; meta["folder"] = d; envs.append(meta)
        print("%-22s %4d tasks" % (meta["name"], n))
    if only:
        # A partial rebuild must update the release indices as well as the folders.
        rebuilt = {e["name"] for e in envs}
        task_index = os.path.join(ROOT, "tasks.jsonl")
        env_index = os.path.join(ROOT, "environments.json")
        if os.path.exists(task_index):
            with open(task_index) as f:
                manifest += [m for m in map(json.loads, f) if m["env"] not in rebuilt]
        if os.path.exists(env_index):
            with open(env_index) as f:
                envs += [e for e in json.load(f) if e["name"] not in rebuilt]
        manifest.sort(key=lambda m: (m["env"], m["split"] != "train", m["seed"]))
        envs.sort(key=lambda e: e["name"])
    with open(os.path.join(ROOT, "tasks.jsonl"), "w") as f:
        for m in manifest: f.write(json.dumps(m, ensure_ascii=False) + "\n")
    with open(os.path.join(ROOT, "environments.json"), "w") as f:
        json.dump(envs, f, indent=1, ensure_ascii=False)
    print(len(manifest), "tasks")