File size: 12,066 Bytes
ea84b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""Prepare JevBench's three pinned public tiers without exposing answer metadata.

This is an independent format conversion. The public 231 questions are not the
534-question leaderboard, whose remaining questions are private or untracked.
"""

from __future__ import annotations

import copy
import json
import math
from dataclasses import dataclass
from pathlib import Path
from typing import Any

import httpx

from jev_adapter.protocol import SystemOneRequest

from .data import normalize_record, population_counts, require_sha256, sha256

JEVBENCH_COMMIT = "fd51755eb0c0b546ca206d764faf3302feca913e"
JEVBENCH_REPOSITORY = "https://github.com/fstandhartinger/jevbench"
JEVBENCH_RAW = (
    f"https://raw.githubusercontent.com/fstandhartinger/jevbench/{JEVBENCH_COMMIT}"
)
JEVBENCH_NOTICES = {
    "LICENSE": "3e5beed774bb0bcbfb2fcf24ba9554212c6ae112937d308040989465ab0c5784",
    "THIRD-PARTY.md": "396422e29055bba4073f4a9e5a7163cc724f13e970a34ef58e8ea1fb439b9b38",
}


@dataclass(frozen=True)
class JevBenchSpec:
    path: str
    sha256: str
    records: int
    description: str


JEVBENCH_SUITES = {
    "jevbench-original": JevBenchSpec(
        "datasets/public/original.jsonl",
        "5c2414edb3006b8bfcb70fda433f0f9ca015759433849f8d3104328a1f7c4180",
        72,
        "Public original tier: 72 decisions in 36 paraphrase pairs.",
    ),
    "jevbench-easy": JevBenchSpec(
        "datasets/public/easy.jsonl",
        "231df3c2c8e88a1a8c137ebe85de96ba70fabd330849098ac7b3c52c70b7172b",
        48,
        "Public easy tier: 48 explicit facts, intents and tool selections.",
    ),
    "jevbench-hard": JevBenchSpec(
        "datasets/public/hard.jsonl",
        "89e9e6becb33ed88c1de7d42dcc87531b2fb64cfaef4e1986faf7c37b3f80ebb",
        111,
        "Public hard tier: 111 decisions; ten supply probability references.",
    ),
}


def normalize_jevbench_record(raw: dict[str, Any], suite: str) -> dict[str, Any]:
    """Keep native question/option order and canonical scoring label order."""
    if raw.get("split") != "public":
        raise ValueError("JevBench converter accepts only the public subset")
    if not isinstance(raw.get("id"), str) or not raw["id"]:
        raise ValueError("JevBench record requires a nonempty id")
    labels = raw["labels"]
    if (
        not isinstance(labels, list)
        or len(labels) < 2
        or any(not isinstance(label, str) or not label for label in labels)
        or len(set(labels)) != len(labels)
    ):
        raise ValueError(f"{raw['id']}: invalid canonical labels")
    question = raw["question"]
    kind, gold = question["type"], raw["expected"]
    if kind == "choice":
        if not isinstance(question["criteria"], dict) or set(labels) != set(
            question["criteria"]
        ):
            raise ValueError(f"{raw['id']}: choice criteria/labels mismatch")
        if not isinstance(gold, str) or gold not in labels:
            raise ValueError(f"{raw['id']}: invalid choice expected label")
        canonical_labels, canonical_gold = list(labels), gold
    elif kind == "noul":
        if labels != ["no", "yes"] or gold not in ("no", "yes"):
            raise ValueError(f"{raw['id']}: noul requires no/yes canonical labels")
        canonical_labels = ["false", "true"]
        canonical_gold, gold = ("true" if gold == "yes" else "false"), gold == "yes"
    elif kind == "score":
        if not isinstance(question["criteria"], list) or labels != [
            str(i) for i in range(len(question["criteria"]))
        ]:
            raise ValueError(f"{raw['id']}: score criteria/labels mismatch")
        if type(gold) is not int or not 0 <= gold < len(labels):
            raise ValueError(f"{raw['id']}: invalid score expected label")
        canonical_labels, canonical_gold = list(labels), str(gold)
    else:
        raise ValueError(f"unsupported JevBench question type: {kind}")
    provenance = copy.deepcopy(raw["provenance"])
    reference = provenance.get("gold_probs")
    if provenance.get("exclude_reason"):
        raise ValueError(f"{raw['id']}: excluded upstream item is not evaluable")
    family = raw["family"]
    if not isinstance(family, str) or not family:
        raise ValueError(f"{raw['id']}: missing task family")
    # Only these three native question fields are allowed into the request.
    clean_question = {
        key: copy.deepcopy(question[key])
        for key in ("type", "instructions", "criteria")
        if key in question
    }
    clean_question["label"] = gold
    row = normalize_record(
        {
            "state": raw["state"],
            "questions": {"decision": clean_question},
            "_meta": {
                "id": raw["id"],
                "source": family,
                "variant": "clean",
                "group_id": raw.get("group") or raw["id"],
                "upstream_group": raw.get("group"),
                "canonical_labels": list(labels),
                "provenance": provenance,
                "gold_policy": {
                    "hard_label": "authored_reviewed_rubric",
                    "reference_probs": (
                        "countable_mathematical_probability"
                        if reference is not None
                        else None
                    ),
                    "argmax_tie_break": "lexicographic_label",
                },
            },
        },
        suite,
        "public",
    )
    expected = row["expected"]["decision"]
    expected["labels"] = canonical_labels
    expected["label"] = canonical_labels.index(canonical_gold)
    expected["target"] = [float(label == canonical_gold) for label in canonical_labels]
    if reference is not None:
        if not isinstance(reference, dict) or set(reference) != set(labels):
            raise ValueError(f"{raw['id']}: reference probability labels mismatch")
        probabilities = [reference[label] for label in labels]
        if any(
            isinstance(value, bool)
            or not isinstance(value, (float, int))
            or not math.isfinite(value)
            or not 0 <= value <= 1
            for value in probabilities
        ) or not math.isclose(sum(probabilities), 1.0, rel_tol=0, abs_tol=1e-9):
            raise ValueError(f"{raw['id']}: invalid reference probabilities")
        expected["reference_probs"] = [float(value) for value in probabilities]
    SystemOneRequest.model_validate({"model": "validation-only", **row["record"]})
    return row


def parse_jevbench(data: bytes, suite: str) -> list[dict[str, Any]]:
    records, seen = [], set()
    for number, line in enumerate(data.decode("utf-8").splitlines(), 1):
        if not line.strip():
            raise ValueError(f"{suite}/public:{number}: blank JSONL record")
        row = normalize_jevbench_record(json.loads(line), suite)
        if row["id"] in seen:
            raise ValueError(f"duplicate record id: {row['id']}")
        seen.add(row["id"])
        records.append(row)
    return records


def prepare_jevbench(
    suite: str,
    output: Path,
    *,
    client: httpx.Client | None = None,
    source_root: Path | None = None,
    limit: int | None = None,
) -> dict[str, Any]:
    """Verify all source bytes before writing an immutable prepared public tier."""
    if suite not in JEVBENCH_SUITES:
        raise ValueError(f"unknown JevBench suite: {suite}")
    if limit is not None and (type(limit) is not int or limit < 1):
        raise ValueError("--limit must be positive")

    def read_source(path: str) -> bytes:
        if source_root is not None:
            return (source_root / path).read_bytes()
        if client is None:
            raise ValueError("an HTTP client or --source-root is required")
        response = client.get(f"{JEVBENCH_RAW}/{path}")
        response.raise_for_status()
        return response.content

    spec = JEVBENCH_SUITES[suite]
    raw_bytes = read_source(spec.path)
    require_sha256(raw_bytes, spec.sha256, spec.path)
    notices = {}
    for path, digest in JEVBENCH_NOTICES.items():
        content = read_source(path)
        require_sha256(content, digest, path)
        notices[path] = content
    records = parse_jevbench(raw_bytes, suite)
    counts = population_counts(records)
    if counts["records"] != spec.records or counts["questions"] != spec.records:
        raise ValueError(f"upstream record/question count mismatch: {suite}/public")
    selected = records if limit is None else records[:limit]
    serialized = "".join(
        json.dumps(row, ensure_ascii=False, allow_nan=False) + "\n" for row in selected
    ).encode("utf-8")
    full = len(selected) == len(records)
    manifest = {
        "schema_version": 1,
        "suite": suite,
        "split": "public",
        "description": spec.description,
        "data_file": "public.jsonl",
        "data_sha256": sha256(serialized),
        "upstream": {
            "repository": JEVBENCH_REPOSITORY,
            "commit": JEVBENCH_COMMIT,
            "path": spec.path,
            "url": f"{JEVBENCH_RAW}/{spec.path}",
            "sha256": spec.sha256,
            "notice_sha256": dict(JEVBENCH_NOTICES),
        },
        "full_partition": counts,
        "selected": population_counts(selected),
        "selection": {
            "method": "full" if full else "prefix",
            "requested_limit": limit,
            "is_full_partition": full,
            "note": (
                "Full frozen public tier; not the complete published leaderboard."
                if full
                else "Smoke subset only; not a full-tier result. Pairs may be incomplete."
            ),
        },
        "protocol": {
            "calibration_applied": False,
            "training_data_downloaded": False,
            "gold_labels_sent_to_model": False,
            "headline_variant": "clean",
            "exclude_from_headline_sources": [],
            "locked_test": False,
            "notes": [
                "Community benchmark; not an official TypeSafe dataset release.",
                "Public original/easy/hard have 72/48/111 questions; report separately.",
                "The 534-item leaderboard contains private/untracked data not downloaded here.",
                "Choice request criteria retain their native order; scoring labels retain canonical order.",
                "Native accuracy uses argmax with lexicographic label tie-break, including Score.",
                "Score also supports expected-value MAE; original has 36 paraphrase groups.",
                "One-hot labels and ten explicit mathematical probability references are separate targets.",
                "Provenance, rationale and gold probabilities are never sent in requests.",
            ],
        },
        "provenance": {
            "dataset_license": "MIT",
            "license_url": f"{JEVBENCH_REPOSITORY}/blob/{JEVBENCH_COMMIT}/LICENSE",
            "notices": list(JEVBENCH_NOTICES),
            "reference_probability_questions": sum(
                "reference_probs" in row["expected"]["decision"] for row in records
            ),
            "gold_policy": (
                "Authored rubric labels reviewed before inference; hard items retain author "
                "and review metadata. Explicit mathematical distributions are not teacher "
                "model confidence or population frequency estimates."
            ),
        },
    }
    artifacts = {
        "public.jsonl": serialized,
        "public.manifest.json": (
            json.dumps(manifest, ensure_ascii=False, indent=2, allow_nan=False) + "\n"
        ).encode("utf-8"),
        **notices,
    }
    directory = output / suite
    for name, content in artifacts.items():
        path = directory / name
        if path.exists() and path.read_bytes() != content:
            raise FileExistsError(
                f"refusing to overwrite a different prepared artifact: {path}"
            )
    directory.mkdir(parents=True, exist_ok=True)
    for name, content in artifacts.items():
        (directory / name).write_bytes(content)
    return manifest