File size: 5,048 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
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
from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from .runner import evaluate, find_task, load_tasks


def write_json(path, value):
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(
        json.dumps(value, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
    )


def main(argv=None):
    parser = argparse.ArgumentParser(
        prog="klein-bench",
        description="Klein-Code-Bench: 260 from-scratch Python projects. No Docker.",
    )
    commands = parser.add_subparsers(dest="command", required=True)
    listing = commands.add_parser("list", help="List task IDs and titles")
    listing.add_argument("--subset", choices=["easy", "intermediate", "hard", "max"])
    for name in ("prepare", "reference", "evaluate"):
        p = commands.add_parser(name)
        p.add_argument("task_id")
        if name == "evaluate":
            p.add_argument("--solution", type=Path, required=True)
            p.add_argument("--output", type=Path)
            p.add_argument("--fail-fast", action="store_true")
            p.add_argument(
                "--public-only",
                action="store_true",
                help="Smoke test only; never an official task pass",
            )
        else:
            p.add_argument("--output", type=Path, required=True)
    validation = commands.add_parser(
        "validate",
        help="Validate manifest, all goldens, references and empty submissions",
    )
    validation.add_argument("--subset", choices=["easy", "intermediate", "hard", "max"])
    validation.add_argument(
        "--output", type=Path, default=Path("results/validation.json")
    )
    commands.add_parser(
        "build", help="Rebuild dataset reproducibly from authored source"
    )
    score = commands.add_parser(
        "score", help="Aggregate one JSON evaluation report per submitted task"
    )
    score.add_argument("reports", nargs="+", type=Path)
    args = parser.parse_args(argv)
    try:
        if args.command == "list":
            for task in load_tasks(args.subset):
                print(task["task_id"] + "\t" + task["title"])
            return 0
        if args.command == "build":
            from .build import build

            print(json.dumps(build(), indent=2))
            return 0
        if args.command == "score":
            from .scoring import summarize

            print(
                json.dumps(
                    summarize([json.loads(p.read_text()) for p in args.reports]),
                    indent=2,
                )
            )
            return 0
        if args.command == "validate":
            from .validation import validate

            report = validate(args.subset)
            write_json(args.output, report)
            print(
                json.dumps(
                    {k: v for k, v in report.items() if k != "results"}, indent=2
                )
            )
            return 0 if report["passed"] else 1
        task = find_task(args.task_id)
        if args.command == "prepare":
            if args.output.exists() and any(args.output.iterdir()):
                raise ValueError("Prepare destination must be empty")
            args.output.mkdir(parents=True, exist_ok=True)
            (args.output / "project").mkdir()
            (args.output / "PROMPT.md").write_text(
                task["prompt"] + "\n", encoding="utf-8"
            )
            public = {k: v for k, v in task.items() if k != "acceptance_tests"}
            write_json(args.output / "task.json", public)
            print(f"Empty project: {args.output / 'project'}")
        elif args.command == "reference":
            from .reference.export import export_reference

            print(export_reference(task["family"], task["subset"], args.output))
        else:
            tests = None
            if args.public_only:
                examples = json.loads(task["public_tests"])
                tests = [
                    {
                        "name": "public-smoke-only",
                        "sessions": [
                            {
                                "requests": [e["request"] for e in examples],
                                "expected": [e["response"] for e in examples],
                            }
                        ],
                    }
                ]
            report = evaluate(
                task, args.solution, tests=tests, fail_fast=args.fail_fast
            )
            report["evaluation_scope"] = "public" if args.public_only else "acceptance"
            if args.public_only:
                report["resolved"] = False
            if args.output:
                write_json(args.output, report)
            print(json.dumps(report, indent=2))
            return 0 if report["passed"] else 1
        return 0
    except (OSError, ValueError) as error:
        print(f"klein-bench: {error}", file=sys.stderr)
        return 2


if __name__ == "__main__":
    raise SystemExit(main())