File size: 17,208 Bytes
5a46e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""Validate the frozen, no-results sub-100M benchmark scaffold."""

from __future__ import annotations

import argparse
import hashlib
import json
import re
import unicodedata
from collections.abc import Mapping, Sequence
from pathlib import Path, PurePosixPath
from typing import Any, NoReturn

BENCHMARK_ID = "mobile-actions-sub100m-v1"
CATALOG_SCHEMA = "barunaction-sub100m-catalog-template-v1"
LANE_SCHEMA = "barunaction-sub100m-lane-contracts-v1"
EXPECTED_BUNDLE_FILES = {"catalog.json", "lane-contracts.json"}
LANES = {"matched_adaptation", "off_the_shelf"}
MOVING_REVISIONS = {"latest", "main", "master"}
SHA256 = re.compile(r"[0-9a-f]{64}")
GIT_COMMIT = re.compile(r"[0-9a-f]{40}")


class ValidationError(ValueError):
    """A benchmark scaffold artifact violates its frozen public contract."""


def _reject_pairs(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
    result: dict[str, Any] = {}
    for key, value in pairs:
        if key in result:
            raise ValidationError(f"duplicate JSON key {key!r}")
        result[key] = value
    return result


def _reject_constant(value: str) -> NoReturn:
    raise ValidationError(f"non-finite JSON value {value!r}")


def _load_json(path: Path) -> Mapping[str, Any]:
    try:
        payload = json.loads(
            path.read_text(encoding="utf-8"),
            object_pairs_hook=_reject_pairs,
            parse_constant=_reject_constant,
        )
    except ValidationError:
        raise
    except (OSError, UnicodeError, json.JSONDecodeError) as error:
        raise ValidationError(f"cannot read strict JSON from {path}") from error
    if not isinstance(payload, Mapping):
        raise ValidationError(f"{path} must contain a JSON object")
    return payload


def _canonical_sha256(payload: Any) -> str:
    normalized = _normalize(payload)
    encoded = json.dumps(
        normalized,
        ensure_ascii=False,
        allow_nan=False,
        sort_keys=True,
        separators=(",", ":"),
    ).encode("utf-8")
    return hashlib.sha256(encoded).hexdigest()


def _normalize(value: Any) -> Any:
    if isinstance(value, str):
        normalized = unicodedata.normalize("NFC", value)
        if normalized != value:
            raise ValidationError("JSON strings must already be NFC-normalized")
        return normalized
    if isinstance(value, Mapping):
        return {str(key): _normalize(item) for key, item in value.items()}
    if isinstance(value, list):
        return [_normalize(item) for item in value]
    if isinstance(value, float):
        raise ValidationError("benchmark manifests must not contain floating-point values")
    return value


def _file_sha256(path: Path) -> str:
    digest = hashlib.sha256()
    try:
        with path.open("rb") as handle:
            for chunk in iter(lambda: handle.read(1024 * 1024), b""):
                digest.update(chunk)
    except OSError as error:
        raise ValidationError(f"cannot hash {path}") from error
    return digest.hexdigest()


def _require_sha256(value: Any, path: str) -> str:
    if not isinstance(value, str) or SHA256.fullmatch(value) is None:
        raise ValidationError(f"{path} must be a lowercase SHA-256")
    return value


def _require_mapping(value: Any, path: str) -> Mapping[str, Any]:
    if not isinstance(value, Mapping):
        raise ValidationError(f"{path} must be an object")
    return value


def _require_list(value: Any, path: str) -> list[Any]:
    if not isinstance(value, list):
        raise ValidationError(f"{path} must be an array")
    return value


def _require_relative_source(repo_root: Path, relative: Any, expected: Any, path: str) -> None:
    if not isinstance(relative, str):
        raise ValidationError(f"{path} source path must be a string")
    pure = PurePosixPath(relative)
    if pure.is_absolute() or ".." in pure.parts or str(pure) != relative:
        raise ValidationError(f"{path} source path must be normalized and repository-relative")
    expected_sha256 = _require_sha256(expected, f"{path} source SHA-256")
    source = repo_root / relative
    if not source.is_file() or source.is_symlink():
        raise ValidationError(f"{path} source file is missing or is a symlink")
    if _file_sha256(source) != expected_sha256:
        raise ValidationError(f"{path} source SHA-256 mismatch")


def _validate_contract(container: Mapping[str, Any], path: str) -> Mapping[str, Any]:
    contract = _require_mapping(container.get("contract"), f"{path}.contract")
    expected = _require_sha256(container.get("contract_sha256"), f"{path}.contract_sha256")
    if _canonical_sha256(contract) != expected:
        raise ValidationError(f"{path} contract SHA-256 mismatch")
    return contract


def _validate_lane_contracts(repo_root: Path, payload: Mapping[str, Any]) -> None:
    if payload.get("schema_version") != LANE_SCHEMA:
        raise ValidationError("unsupported lane-contract schema")
    if payload.get("benchmark_id") != BENCHMARK_ID:
        raise ValidationError("lane-contract benchmark_id mismatch")
    if payload.get("state") != "lane_contracts_frozen_catalog_unpopulated_no_runs":
        raise ValidationError("lane contracts must identify the unpopulated, no-run scaffold")

    scope = _require_mapping(payload.get("scope"), "$.scope")
    if scope.get("literal_exhaustive_coverage_claimed") is not False:
        raise ValidationError("the catalog must not claim literal exhaustive coverage")
    if scope.get("future_discoveries_require_a_new_catalog_version_before_scoring") is not True:
        raise ValidationError("future catalog additions must require a pre-score version change")

    eligibility = _require_mapping(payload.get("eligibility"), "$.eligibility")
    if eligibility.get("parameter_operator") != "<":
        raise ValidationError("parameter eligibility must use a strict less-than operator")
    if eligibility.get("parameter_limit_exclusive") != 100_000_000:
        raise ValidationError("parameter eligibility must be exactly <100,000,000")
    for key in (
        "immutable_revision_required",
        "license_required",
        "public_ungated_checkpoint_required",
        "standalone_local_checkpoint_required",
        "text_generation_required",
    ):
        if eligibility.get(key) is not True:
            raise ValidationError(f"eligibility.{key} must be true")
    for key in ("estimated_or_rounded_parameter_counts_allowed", "remote_api_only_allowed"):
        if eligibility.get(key) is not False:
            raise ValidationError(f"eligibility.{key} must be false")

    data = _require_mapping(payload.get("data"), "$.data")
    if data.get("official_evaluation_rows_allowed") is not False:
        raise ValidationError("official evaluation access must remain forbidden")
    if data.get("development_rows") != 756 or data.get("training_rows") != 7937:
        raise ValidationError("Mobile Actions population sizes changed")
    revision = data.get("dataset_revision")
    if not isinstance(revision, str) or GIT_COMMIT.fullmatch(revision) is None:
        raise ValidationError("$.data.dataset_revision must be a 40-character git commit")
    for key in (
        "development_manifest_sha256",
        "source_sha256",
        "training_manifest_sha256",
    ):
        _require_sha256(data.get(key), f"$.data.{key}")

    prompt = _require_mapping(payload.get("prompt"), "$.prompt")
    _validate_contract(prompt, "$.prompt")
    _require_relative_source(
        repo_root,
        prompt.get("renderer_source_path"),
        prompt.get("renderer_source_sha256"),
        "$.prompt",
    )

    decoding = _require_mapping(payload.get("decoding"), "$.decoding")
    decoding_contract = _validate_contract(decoding, "$.decoding")
    if decoding_contract.get("algorithm") != "unconstrained_deterministic_greedy":
        raise ValidationError("decoding must remain deterministic greedy")
    if decoding_contract.get("max_new_tokens") != 192:
        raise ValidationError("decoding max_new_tokens changed")
    _require_relative_source(
        repo_root,
        decoding.get("implementation_path"),
        decoding.get("implementation_sha256"),
        "$.decoding",
    )

    scoring = _require_mapping(payload.get("scoring"), "$.scoring")
    scoring_contract = _validate_contract(scoring, "$.scoring")
    if scoring_contract.get("callable") != "barunlm.evaluation.mobile_actions.write_scores":
        raise ValidationError("the protocol must reuse the existing Mobile Actions scorer")
    sources = _require_mapping(scoring.get("source_files"), "$.scoring.source_files")
    if set(sources) != {
        "src/barunlm/evaluation/action_ir.py",
        "src/barunlm/evaluation/evaluator.py",
        "src/barunlm/evaluation/mobile_actions.py",
    }:
        raise ValidationError("scorer source closure changed")
    for relative, digest in sources.items():
        _require_relative_source(repo_root, relative, digest, "$.scoring")

    lanes = _require_mapping(payload.get("lanes"), "$.lanes")
    if set(lanes) != LANES:
        raise ValidationError("the two benchmark lanes must remain separate")
    off_the_shelf = _require_mapping(lanes["off_the_shelf"], "$.lanes.off_the_shelf")
    off_contract = _validate_contract(off_the_shelf, "$.lanes.off_the_shelf")
    if (
        off_contract.get("parameter_updates") != 0
        or off_contract.get("adapters_added") is not False
    ):
        raise ValidationError("off-the-shelf lane may not adapt the model")
    matched = _require_mapping(lanes["matched_adaptation"], "$.lanes.matched_adaptation")
    matched_contract = _validate_contract(matched, "$.lanes.matched_adaptation")
    if matched.get("requires_no_prior_mobile_actions_adaptation") is not True:
        raise ValidationError("matched adaptation must exclude already task-adapted bases")
    if matched_contract.get("training_manifest_sha256") != data.get("training_manifest_sha256"):
        raise ValidationError("matched adaptation training population changed")
    if matched_contract.get("passes_over_training_manifest") != 1:
        raise ValidationError("matched adaptation must remain a one-pass treatment")


def _validate_entry(entry: Any, contract: Mapping[str, Any], limit: int) -> None:
    record = _require_mapping(entry, "$.entries[]")
    required = set(
        _require_list(contract.get("required_fields"), "$.entry_contract.required_fields")
    )
    if set(record) != required:
        raise ValidationError("catalog entry fields differ from the frozen entry contract")
    revision = record.get("revision")
    if not isinstance(revision, str) or not revision or revision.lower() in MOVING_REVISIONS:
        raise ValidationError("catalog entries require a non-moving revision")
    if record.get("generation_interface") not in {"causal_lm", "seq2seq_lm"}:
        raise ValidationError("catalog entry is not a supported text-generation model")

    access = _require_mapping(record.get("access"), "$.entries[].access")
    if access.get("public") is not True or access.get("gated") is not False:
        raise ValidationError("catalog checkpoints must be public and ungated")
    _require_sha256(access.get("verification_receipt_sha256"), "access receipt")

    parameters = _require_mapping(record.get("parameters"), "$.entries[].parameters")
    total = parameters.get("exact_unique_total")
    if isinstance(total, bool) or not isinstance(total, int) or not 0 < total < limit:
        raise ValidationError("catalog exact unique parameter count is not strictly below 100M")
    if not isinstance(parameters.get("unique_storages"), int):
        raise ValidationError("catalog parameter audit lacks an exact storage count")
    _require_sha256(parameters.get("count_receipt_sha256"), "parameter count receipt")

    artifacts = _require_mapping(record.get("artifacts"), "$.entries[].artifacts")
    for field in _require_list(
        contract.get("artifact_required_fields"), "$.entry_contract.artifact_required_fields"
    ):
        _require_sha256(artifacts.get(field), f"artifact {field}")

    lane_eligibility = _require_mapping(record.get("lane_eligibility"), "lane_eligibility")
    if set(lane_eligibility) != LANES:
        raise ValidationError("catalog entry must classify both lanes")
    eligible = 0
    for lane, decision in lane_eligibility.items():
        decision_map = _require_mapping(decision, f"lane_eligibility.{lane}")
        if decision_map.get("status") not in {"eligible", "ineligible"}:
            raise ValidationError("lane eligibility status must be eligible or ineligible")
        reasons = _require_list(decision_map.get("reason_codes"), "lane reason_codes")
        if reasons != sorted(set(reasons)):
            raise ValidationError("lane reason codes must be unique and sorted")
        eligible += decision_map.get("status") == "eligible"
    if eligible == 0:
        raise ValidationError("catalog entry is ineligible for both lanes")


def _validate_catalog(payload: Mapping[str, Any], lane_sha256: str) -> None:
    if payload.get("schema_version") != CATALOG_SCHEMA:
        raise ValidationError("unsupported catalog schema")
    if payload.get("benchmark_id") != BENCHMARK_ID:
        raise ValidationError("catalog benchmark_id mismatch")
    if payload.get("lane_contracts_sha256") != lane_sha256:
        raise ValidationError("catalog does not bind the exact lane-contract file")
    boundary = _require_mapping(payload.get("catalog_boundary"), "$.catalog_boundary")
    if boundary.get("literal_exhaustive_coverage_claimed") is not False:
        raise ValidationError("catalog must not claim literal exhaustive coverage")
    if boundary.get("scores_may_influence_membership") is not False:
        raise ValidationError("model scores may not influence catalog membership")

    entries = _require_list(payload.get("entries"), "$.entries")
    contract = _require_mapping(payload.get("entry_contract"), "$.entry_contract")
    if set(_require_list(contract.get("lane_names"), "$.entry_contract.lane_names")) != LANES:
        raise ValidationError("entry contract must classify both benchmark lanes")
    if boundary.get("roster_frozen") is False:
        if entries or payload.get("results_status") != "no_model_runs_or_results":
            raise ValidationError(
                "the unfrozen catalog template must contain no entries or results"
            )
    elif boundary.get("roster_frozen") is True:
        if not entries:
            raise ValidationError("a frozen roster may not be empty")
    else:
        raise ValidationError("catalog roster_frozen must be boolean")

    seen: set[str] = set()
    limit = 100_000_000
    for entry in entries:
        _validate_entry(entry, contract, limit)
        entry_id = _require_mapping(entry, "$.entries[]").get("entry_id")
        if not isinstance(entry_id, str) or not entry_id or entry_id in seen:
            raise ValidationError("catalog entry IDs must be unique non-empty strings")
        seen.add(entry_id)
    if [entry.get("entry_id") for entry in entries] != sorted(seen):
        raise ValidationError("catalog entries must be sorted by entry_id")

    exclusions = _require_list(payload.get("exclusions"), "$.exclusions")
    if exclusions and boundary.get("roster_frozen") is not True:
        raise ValidationError("exclusions belong only to a researched frozen roster")


def validate_sub100m_scaffold(repo_root: str | Path) -> dict[str, Any]:
    root = Path(repo_root).resolve()
    bundle = root / "benchmarks" / "sub100m"
    names = {path.name for path in bundle.iterdir() if path.is_file()}
    if names != EXPECTED_BUNDLE_FILES:
        raise ValidationError("sub100m must contain only catalog.json and lane-contracts.json")
    lanes_path = bundle / "lane-contracts.json"
    catalog_path = bundle / "catalog.json"
    lanes = _load_json(lanes_path)
    catalog = _load_json(catalog_path)
    _normalize(lanes)
    _normalize(catalog)
    _validate_lane_contracts(root, lanes)
    lanes_sha256 = _file_sha256(lanes_path)
    _validate_catalog(catalog, lanes_sha256)
    return {
        "benchmark_id": BENCHMARK_ID,
        "catalog_entries": len(_require_list(catalog.get("entries"), "$.entries")),
        "catalog_sha256": _file_sha256(catalog_path),
        "lane_contracts_sha256": lanes_sha256,
        "literal_exhaustive_coverage_claimed": False,
        "model_runs": 0,
        "ok": True,
        "scorer_callable": "barunlm.evaluation.mobile_actions.write_scores",
    }


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--repository",
        type=Path,
        default=Path(__file__).resolve().parents[1],
        help="repository root containing benchmarks/sub100m",
    )
    return parser


def main(argv: Sequence[str] | None = None) -> int:
    args = build_parser().parse_args(argv)
    try:
        result = validate_sub100m_scaffold(args.repository)
    except ValidationError as error:
        print(json.dumps({"error": str(error), "ok": False}, sort_keys=True))
        return 2
    print(json.dumps(result, sort_keys=True))
    return 0


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