File size: 10,420 Bytes
0902868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import functools
import json
import logging
import math
import os
import sys
import threading
import time
import urllib.request
from collections.abc import Callable
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from datetime import UTC, date, datetime, timedelta
from pathlib import Path
from typing import Any

from jsonschema import Draft202012Validator, FormatChecker

ROOT = Path(__file__).parent
SCHEMAS = ROOT / "schemas" / "v1"
LEADERBOARDS = {"release": "v1/leaderboard", "all": "v1/leaderboard/all_samples"}
TTL_SECONDS = 300
HISTORY_TTL_SECONDS = 3600
SOCKET_TIMEOUT_SECONDS = 10
READ_DEADLINE_SECONDS = 30
MAX_BYTES = 25_000_000
HISTORY_START = date(2026, 7, 1)

log = logging.getLogger(__name__)


class InvalidData(ValueError):
    pass


_FORMATS = FormatChecker(formats=())


@_FORMATS.checks("date-time", raises=ValueError)
def _instant(value: object) -> bool:
    return not isinstance(value, str) or 2000 <= datetime.fromisoformat(value).year <= 2200


@functools.cache
def _validator() -> Draft202012Validator:
    doc = json.loads((SCHEMAS / "leaderboard.schema.json").read_text(encoding="utf-8"))
    Draft202012Validator.check_schema(doc)
    return Draft202012Validator(doc, format_checker=_FORMATS)


def _finite(text: str) -> str:
    if not math.isfinite(float(text)):
        raise InvalidData(f"number out of range: {text[:40]}")
    return text


def overall_board(doc: Any) -> Any:
    if isinstance(doc, dict) and isinstance(doc.get("boards"), dict) and "overall" in doc["boards"]:
        return {**doc, "boards": {"overall": doc["boards"]["overall"]}}
    return doc


def parse(data: bytes, select: Callable[[Any], Any] | None = None) -> dict[str, Any]:
    if len(data) > MAX_BYTES:
        raise InvalidData(f"larger than {MAX_BYTES} bytes")
    try:
        doc = json.loads(
            data.decode("utf-8"),
            parse_constant=_finite,
            parse_float=lambda text: float(_finite(text)),
            parse_int=lambda text: int(_finite(text)),
        )
    except InvalidData:
        raise
    except (ValueError, RecursionError) as exc:
        raise InvalidData(str(exc)[:200]) from exc
    if select:
        doc = select(doc)
    error = next(_validator().iter_errors(doc), None)
    if error is not None:
        raise InvalidData(f"{error.json_path}: {error.message[:200]}")
    return doc


class _NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, *args: Any, **kwargs: Any) -> None:
        return None


_OPENER = urllib.request.build_opener(_NoRedirect)


def read_url(url: str, max_bytes: int, deadline: float = READ_DEADLINE_SECONDS) -> bytes:
    request = urllib.request.Request(
        url, headers={"Accept": "application/json", "User-Agent": "cref-leaderboard"}
    )
    end = time.monotonic() + deadline
    chunks, size = [], 0
    with _OPENER.open(request, timeout=SOCKET_TIMEOUT_SECONDS) as response:
        while size <= max_bytes and (chunk := response.read1(65_536)):
            if time.monotonic() > end:
                raise TimeoutError(f"{url}: not read within {deadline} s")
            chunks.append(chunk)
            size += len(chunk)
    return b"".join(chunks)


def read(base: str, path: str, max_bytes: int = MAX_BYTES) -> bytes:
    if base.startswith("https://"):
        return read_url(f"{base.rstrip('/')}/{path}", max_bytes)
    with open(Path(base) / path, "rb") as file:
        return file.read(max_bytes + 1)


def check_base(base: str) -> str:
    if "://" in base and not base.startswith("https://"):
        raise ValueError(f"CREF_DATA_URL must start with https://, got {base[:80]!r}")
    return base


def base_from_env() -> str:
    return check_base(os.environ.get("CREF_DATA_URL", "").strip() or str(ROOT / "data"))


class Schedule:
    def __init__(self) -> None:
        self._lock = threading.Lock()
        self._wake = threading.Event()
        self._jobs: dict[object, list[Any]] = {}
        self._thread: threading.Thread | None = None

    def add(self, seconds: float, update: Callable[[], None]) -> Callable[[], None]:
        key = object()
        with self._lock:
            self._jobs[key] = [time.monotonic() + seconds, seconds, update]
        self._wake.set()
        self.ensure_running()

        def remove() -> None:
            with self._lock:
                self._jobs.pop(key, None)

        return remove

    # Called on every page view: cheap while the thread runs, and restarts it if it has died.
    def ensure_running(self) -> None:
        if self._thread is not None and self._thread.is_alive():
            return
        with self._lock:
            if self._thread is None or not self._thread.is_alive():
                self._thread = threading.Thread(target=self._run, name="cref-reads", daemon=True)
                self._thread.start()

    def _run(self) -> None:
        while True:
            with self._lock:
                jobs = list(self._jobs.values())
            for job in jobs:
                if job[0] <= time.monotonic():
                    job[0] = time.monotonic() + job[1]
                    try:
                        job[2]()
                    except Exception:
                        log.exception("scheduled read failed")
            due = min((job[0] for job in jobs), default=None)
            self._wake.wait(None if due is None else max(0, due - time.monotonic()))
            self._wake.clear()


SCHEDULE = Schedule()


@dataclass(frozen=True)
class Snapshot:
    board: dict[str, Any] | None
    stale: bool


class Source:
    def __init__(
        self, base: str, prefix: str = LEADERBOARDS["release"], ttl: float = TTL_SECONDS
    ) -> None:
        self.base = check_base(base)
        self._path = f"{prefix}/latest.json"
        self._ttl = ttl
        self._lock = threading.Lock()
        self._snapshot = Snapshot(None, stale=False)
        self._remove: Callable[[], None] | None = None

    @classmethod
    def from_env(cls, variant: str = "release") -> Source:
        return cls(base_from_env(), LEADERBOARDS[variant])

    def get(self) -> Snapshot:
        # Only the first read waits for a page view; later ones run on the schedule, never on demand.
        if self._remove is None:
            with self._lock:
                if self._remove is None:
                    self._refresh()
                    self._remove = SCHEDULE.add(self._ttl, self._refresh)
        SCHEDULE.ensure_running()
        return self._snapshot

    def close(self) -> None:
        if self._remove is not None:
            self._remove()

    def _refresh(self) -> None:
        try:
            self._snapshot = Snapshot(parse(read(self.base, self._path)), stale=False)
        except Exception as exc:
            # Logged once per outage, not on every scheduled read.
            report = log.debug if self._snapshot.stale else log.warning
            report("leaderboard read failed: %s: %s", type(exc).__name__, exc)
            self._snapshot = Snapshot(self._snapshot.board, stale=True)


class History:
    def __init__(
        self,
        base: str,
        start: date,
        prefix: str = LEADERBOARDS["release"],
        ttl: float = HISTORY_TTL_SECONDS,
        today: Callable[[], date] = lambda: datetime.now(UTC).date(),
    ) -> None:
        self.base = check_base(base)
        self._start = start
        self._prefix = prefix
        self._ttl = ttl
        self._today = today
        self._lock = threading.Lock()
        self._days: dict[str, list[dict[str, Any]]] = {}
        self._remove: Callable[[], None] | None = None

    @classmethod
    def from_env(cls, variant: str = "release") -> History:
        return cls(base_from_env(), HISTORY_START, LEADERBOARDS[variant])

    def _read(self, day: str) -> list[dict[str, Any]] | None:
        try:
            data = read(self.base, f"{self._prefix}/history/{day}.json")
            board = parse(data, select=overall_board)
        except InvalidData as exc:
            log.warning("history %s rejected: %s", day, exc)
            return None
        except Exception as exc:
            log.debug("history %s not available: %s", day, type(exc).__name__)
            return None
        overall = board["boards"].get("overall", {"sample_count": 0, "rankings": []})
        samples = overall["sample_count"]
        return [
            {
                "date": day,
                "model_id": row["model_id"],
                **{
                    f"{m}_{k}": row["metrics"][m][k]
                    for m in ("mase", "sql")
                    for k in ("skill_score", "win_rate")
                },
                "coverage_samples": 1 - row["missing_samples"] / samples if samples else None,
                "coverage_weight": None
                if row["missing_weight"] is None
                else 1 - row["missing_weight"],
            }
            for row in overall["rankings"]
        ]

    def rows(self) -> list[dict[str, Any]]:
        if self._remove is None:
            with self._lock:
                if self._remove is None:
                    self._update()
                    self._remove = SCHEDULE.add(self._ttl, self._update)
        SCHEDULE.ensure_running()
        days = self._days
        return [row for day in sorted(days) for row in days[day]]

    def close(self) -> None:
        if self._remove is not None:
            self._remove()

    def _update(self) -> None:
        days = range((self._today() - self._start).days + 1)
        due = [
            d
            for d in ((self._start + timedelta(n)).isoformat() for n in days)
            if d not in self._days
        ]
        if not due:
            return
        with ThreadPoolExecutor(16) as pool:
            read = zip(due, pool.map(self._read, due), strict=True)
            found = {day: rows for day, rows in read if rows is not None}
        self._days = {**self._days, **found}


def main(paths: list[str]) -> int:
    status = 0
    for path in paths:
        try:
            board = parse(Path(path).read_bytes())
        except (OSError, InvalidData) as exc:
            print(f"{path}: INVALID: {exc}")
            status = 1
        else:
            print(f"{path}: OK, {board['timestamp']}, {len(board['boards'])} boards")
    return status


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))