File size: 3,772 Bytes
072246b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reproducibly compile contracts and static acceptance suites into HF JSONL."""

from __future__ import annotations

import hashlib
import json
from pathlib import Path

from .catalog import FAMILIES
from .protocol import PROTOCOL_VERSION, TIERS, contract
from .reference.domains import solve
from .scenarios import expected_scenarios


def dumps(value):
    return json.dumps(
        value,
        ensure_ascii=False,
        sort_keys=True,
        separators=(",", ":"),
        allow_nan=False,
    )


def build(destination=None):
    root = Path(destination) if destination else Path(__file__).parent / "data"
    root.mkdir(parents=True, exist_ok=True)
    counts, hashes, assertions = {}, {}, {}
    for tier, models in TIERS.items():
        rows = []
        for index, f in enumerate(FAMILIES.values(), 1):
            # Hand-written goldens are independent of the oracle code.
            if solve(f.slug, f.inputs[0]) != f.golden:
                raise ValueError(f"Golden mismatch: {f.slug}")
            tests = expected_scenarios(f.slug, tier)
            examples = [
                {
                    "request": {"op": "solve", "input": x},
                    "response": {"ok": True, "result": solve(f.slug, x)},
                }
                for x in f.inputs[:2]
            ]
            task_id = f"kcb-{tier}-{index:03d}-{f.slug.replace('_', '-')}"
            title = (
                f"{f.slug.replace('_', ' ').title()} — "
                + {
                    "easy": "CLI project",
                    "intermediate": "workspace service",
                    "hard": "transactional service",
                    "max": "durable service",
                }[tier]
            )
            prompt = (
                f"# {title}\n\n"
                + contract(tier)
                + "\nDomain contract:\n"
                + f.specification
                + "\nRequired input fields: "
                + ", ".join(f.required)
                + ".\n\nExamples:\n"
                + json.dumps(examples, indent=2, ensure_ascii=False)
            )
            rows.append(
                {
                    "task_id": task_id,
                    "subset": tier,
                    "family": f.slug,
                    "title": title,
                    "category": f.category,
                    "target_model_parameters": models,
                    "language": "python",
                    "entrypoint": "main.py",
                    "protocol_version": PROTOCOL_VERSION,
                    "prompt": prompt,
                    "timeout_seconds": 10,
                    "public_tests": dumps(examples),
                    "acceptance_tests": dumps(tests),
                    "num_scenarios": len(tests),
                    "num_assertions": sum(
                        len(s["expected"]) for t in tests for s in t["sessions"]
                    ),
                }
            )
        file = root / f"{tier}.jsonl"
        file.write_text("".join(dumps(r) + "\n" for r in rows), encoding="utf-8")
        counts[tier] = len(rows)
        assertions[tier] = sum(r["num_assertions"] for r in rows)
        hashes[file.name] = hashlib.sha256(file.read_bytes()).hexdigest()
    manifest = {
        "benchmark": "Klein-Code-Bench",
        "version": "0.1.0",
        "protocol_version": PROTOCOL_VERSION,
        "tasks": 260,
        "families": 65,
        "counts": counts,
        "assertions": assertions,
        "sha256": hashes,
        "seed": 1729,
        "difficulty_calibrated": False,
    }
    (root / "manifest.json").write_text(
        json.dumps(manifest, indent=2) + "\n", encoding="utf-8"
    )
    return manifest


if __name__ == "__main__":
    print(json.dumps(build(), indent=2))