"""The judge endpoint's problem-id whitelist. ``POST /v1/judge`` must reject an unknown ``problem_id`` before anything is queued (ERA2_DESIGN.md: "rejects unknown problem_id — must be in public-200 ∪ canonical set"). public-200 is a subset of the canonical set — the eval Space enforces that invariant on its side (``evaluator._public_200_ids_ok``) — so the union IS the canonical 1,669 and this module is the whole answer. The set ships as data (``app/data/problem_ids.json``, regenerated by ``scripts/gen_problem_ids.py`` from the judge repo's four canonical JSONLs) rather than as a regex, because a regex is wrong: ``hard1_0070`` matches ``^(normal|hard[123])_\\d{4}$`` and does not exist. It carries IDS ONLY — no statements, no answers — since era 2 deliberately removes the oracle surface from the service path and this app is the public face of the collab. Loaded once per process (per path) behind ``lru_cache``: ~1,669 short strings, read at first use rather than at import, so a broken data file surfaces in the one route that needs it instead of as a Space that will not boot. The path is threaded in from ``Settings.problem_ids_path`` by the caller rather than read from the global settings here, so tests can point at a fixture file the same way they override every other dependency. """ from __future__ import annotations import json from functools import lru_cache from pathlib import Path DEFAULT_PATH = Path(__file__).resolve().parent / "data" / "problem_ids.json" @lru_cache def problem_ids(path: str | None = None) -> frozenset[str]: """Every judgeable problem id. Raises if the data file is missing or does not parse — an id set we cannot read must never degrade to "accept everything", and that is a deploy bug, not something the caller can fix.""" doc = json.loads((Path(path) if path else DEFAULT_PATH).read_text()) ids = frozenset(doc["ids"]) count = doc.get("count") if isinstance(count, int) and count != len(ids): raise ValueError( f"{path or DEFAULT_PATH}: declares count={count} " f"but carries {len(ids)} unique ids" ) if not ids: raise ValueError(f"{path or DEFAULT_PATH}: empty problem-id set") return ids def is_known_problem(problem_id: str, path: str | None = None) -> bool: return problem_id in problem_ids(path)