"""Aggregate, code-free scoring counters for the Space. See space/TELEMETRY.md.""" from __future__ import annotations import asyncio from collections import OrderedDict from collections.abc import Iterable, Mapping from datetime import datetime, timezone import hashlib import hmac import json import logging from pathlib import Path import secrets import time from typing import Annotated, Any, Literal, Protocol from pydantic import BaseModel, ConfigDict, Field log = logging.getLogger("space.telemetry") DIGEST_HEX = 16 SEEN_LIMIT = 100_000 STATS_VERSION = 2 STATS_FILE = "stats.json" SQUIGGLE_KINDS = ( "deep_nesting", "poor_naming", "noisy_comment", "swallowed_error", "dense_line", "special_casing", "duplicate_block", "cramped_layout", ) LOC_BUCKETS = ("0-50", "50-100", "100-300", "300-1000", "1000+") CODE_EXTENSIONS = frozenset({ "py", "pyi", "js", "mjs", "cjs", "ts", "tsx", "jsx", "c", "h", "cc", "cpp", "cxx", "hpp", "rs", "go", "zig", "java", "cs", "lua", "rb", "php", "swift", "kt", "scala", "sh", }) OTHER_EXT = "other" SCORE_KEYS = frozenset(str(points) for points in range(101)) MS_STEPS_PER_DECADE = 8 MS_DECADES = 4 def _two_figures(value: float) -> int: """Round to two significant figures so bucket edges read as plain numbers.""" digits = len(str(int(value))) return int(round(value, 2 - digits)) MS_EDGES = tuple(sorted({ _two_figures(10 ** (step / MS_STEPS_PER_DECADE)) for step in range(MS_STEPS_PER_DECADE * MS_DECADES + 1) })) MS_OVERFLOW = f"{MS_EDGES[-1]}+" MS_KEYS = frozenset(str(edge) for edge in MS_EDGES) | {MS_OVERFLOW} SourceKind = Literal["examples", "repo", "folder", "snippet"] SquiggleKind = Literal[ "deep_nesting", "poor_naming", "noisy_comment", "swallowed_error", "dense_line", "special_casing", "duplicate_block", "cramped_layout", ] Count = Annotated[int, Field(ge=0, le=10_000)] COUNTERS = ("files_scored", "scan_batches", "score_scored_sum") TALLIES = ( "scored_by_ext", "score_sum_by_ext", "scored_by_loc_bucket", "squiggles_by_kind", "score_hist", "score_ms_hist", ) TALLY_KEYS: dict[str, frozenset[str]] = { "scored_by_ext": CODE_EXTENSIONS | {OTHER_EXT}, "score_sum_by_ext": CODE_EXTENSIONS | {OTHER_EXT}, "scored_by_loc_bucket": frozenset(LOC_BUCKETS), "squiggles_by_kind": frozenset(SQUIGGLE_KINDS), "score_hist": SCORE_KEYS, "score_ms_hist": MS_KEYS, } class ScanFile(BaseModel): model_config = ConfigDict(extra="forbid") digest16: str = Field(pattern=r"^[0-9a-f]{16}$") ext: str = Field("", max_length=8) loc_bucket: Literal["0-50", "50-100", "100-300", "300-1000", "1000+"] score: int = Field(ge=0, le=100) squiggles: dict[SquiggleKind, Count] = Field(default_factory=dict) score_ms: int | None = Field(None, ge=0, le=60_000) class ScanBody(BaseModel): model_config = ConfigDict(extra="forbid") source_kind: SourceKind files: list[ScanFile] = Field(max_length=200) def extension_of(filename: str) -> str: """Lower-case extension of the base name, matching the browser computation.""" base = filename.rsplit("/", 1)[-1] stem, dot, ext = base.rpartition(".") return ext.lower() if dot and stem else "" def known_ext(ext: str) -> str: """Bounded tally key: an extension from the fixed code set, or `other`.""" return ext if ext in CODE_EXTENSIONS else OTHER_EXT def content_digest(ext: str, content: str) -> str: """First 64 bits of SHA-256(ext + NUL + content), matching the browser digest.""" payload = f"{ext}\0{content}".encode("utf-8", errors="replace") return hashlib.sha256(payload).hexdigest()[:DIGEST_HEX] def loc_bucket_of(content: str) -> Literal["0-50", "50-100", "100-300", "300-1000", "1000+"]: lines = content.count("\n") + 1 if lines < 50: return "0-50" if lines < 100: return "50-100" if lines < 300: return "100-300" return "300-1000" if lines < 1000 else "1000+" def scan_file_from( filename: str, content: str, score: float, squiggles: Mapping[str, int], score_ms: int | None = None, ) -> ScanFile: raw_ext = extension_of(filename) ext = raw_ext if len(raw_ext) <= 8 else "" points = min(100, max(0, round(score * 100))) allowed = TALLY_KEYS["squiggles_by_kind"] clean_squiggles = { kind: min(10_000, count) for kind, count in squiggles.items() if kind in allowed and count > 0 } clamped_ms = min(60_000, max(0, score_ms)) if score_ms is not None else None return ScanFile.model_construct( digest16=content_digest(ext, content), ext=ext, loc_bucket=loc_bucket_of(content), score=points, squiggles=clean_squiggles, score_ms=clamped_ms, ) class DigestSet: """Rolling set of the most recent 64-bit digests, oldest dropped first.""" def __init__(self, limit: int = SEEN_LIMIT) -> None: self.limit = limit self._items: OrderedDict[int, None] = OrderedDict() def add(self, digest: str) -> bool: key = int(digest, 16) if key in self._items: self._items.move_to_end(key) return False self._items[key] = None if len(self._items) > self.limit: self._items.popitem(last=False) return True def __len__(self) -> int: return len(self._items) def empty_stats() -> dict[str, Any]: stats: dict[str, Any] = {"version": STATS_VERSION} stats.update({name: 0 for name in COUNTERS}) stats.update({name: {} for name in TALLIES}) return stats def _clean_count(value: Any) -> int: return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else 0 def sanitize_stats(raw: Any) -> dict[str, Any]: """Keep only known scoring counters and bounded tally keys from a stats file.""" stats = empty_stats() if not isinstance(raw, dict): return stats for name in COUNTERS: stats[name] = _clean_count(raw.get(name)) for name in TALLIES: tally = raw.get(name) if name == "squiggles_by_kind" and not isinstance(tally, dict): tally = raw.get("squiggles_before_by_kind") if isinstance(tally, dict): allowed = TALLY_KEYS[name] stats[name] = {key: _clean_count(val) for key, val in tally.items() if key in allowed} return stats def _bump(tally: dict[str, int], key: str, amount: int = 1) -> None: if amount > 0: tally[key] = tally.get(key, 0) + amount def _mean(total: int, count: int) -> float | None: return round(total / count, 1) if count else None def ms_bucket(ms: int) -> str: """Histogram key for a scoring time: the smallest edge at or above it.""" edge = next((edge for edge in MS_EDGES if ms <= edge), None) return str(edge) if edge is not None else MS_OVERFLOW def _median_key(tally: Mapping[str, int], keys: Iterable[str]) -> str | None: """Key holding the middle sample when keys are walked in order.""" total = sum(tally.values()) seen = 0 for key in keys: seen += tally.get(key, 0) if total and seen * 2 >= total: return key return None def median_score(tally: Mapping[str, int]) -> int | None: key = _median_key(tally, (str(points) for points in range(101))) return int(key) if key is not None else None def median_ms(tally: Mapping[str, int]) -> int | None: key = _median_key(tally, [*(str(edge) for edge in MS_EDGES), MS_OVERFLOW]) return int(key.rstrip("+")) if key is not None else None class Telemetry: """Aggregate scoring counters. Mutated only on the event loop; zero code, names or IPs.""" def __init__(self, excluded: Iterable[str] = ()) -> None: self.stats = empty_stats() self.dirty = False self.excluded = frozenset(excluded) self.seen_scored = DigestSet() def merge(self, raw: Any) -> None: loaded = sanitize_stats(raw) for name in COUNTERS: self.stats[name] += loaded[name] for name in TALLIES: for key, value in loaded[name].items(): _bump(self.stats[name], key, value) def record_files(self, files: Iterable[ScanFile]) -> int: counted = 0 for item in files: if item.digest16 in self.excluded or not self.seen_scored.add(item.digest16): continue counted += 1 ext = known_ext(item.ext) self.stats["score_scored_sum"] += item.score _bump(self.stats["scored_by_ext"], ext) _bump(self.stats["score_sum_by_ext"], ext, item.score) _bump(self.stats["scored_by_loc_bucket"], item.loc_bucket) _bump(self.stats["score_hist"], str(item.score)) if item.score_ms is not None: _bump(self.stats["score_ms_hist"], ms_bucket(item.score_ms)) for kind, amount in item.squiggles.items(): _bump(self.stats["squiggles_by_kind"], kind, amount) self.stats["files_scored"] += counted self.stats["scan_batches"] += 1 self.dirty = True return counted def record_scan(self, body: ScanBody) -> int: if body.source_kind == "examples": return 0 return self.record_files(body.files) def snapshot(self) -> str: stats = {**self.stats} stats["updated_at"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") return json.dumps(stats, indent=2, sort_keys=True) + "\n" def _by_ext(self) -> dict[str, dict[str, Any]]: stats = self.stats rows = {} for ext in sorted(stats["scored_by_ext"]): scored = stats["scored_by_ext"].get(ext, 0) rows[ext] = { "scored": scored, "avg_score": _mean(stats["score_sum_by_ext"].get(ext, 0), scored), } return rows def public(self) -> dict[str, Any]: stats = self.stats squiggles = stats["squiggles_by_kind"] return { "files_scored": stats["files_scored"], "issues_found": sum(squiggles.values()), "avg_score_scored": _mean(stats["score_scored_sum"], stats["files_scored"]), "median_score": median_score(stats["score_hist"]), "score_ms_median": median_ms(stats["score_ms_hist"]), "score_ms_samples": sum(stats["score_ms_hist"].values()), "by_ext": self._by_ext(), "by_loc_bucket": dict(stats["scored_by_loc_bucket"]), "squiggles_by_kind": dict(squiggles), } def preset_digests(presets_path: Path | None) -> set[str]: """Digests of the bundled examples, which never count.""" if presets_path is None or not presets_path.is_file(): return set() rows = json.loads(presets_path.read_text(encoding="utf-8")) return {content_digest(extension_of(row["filename"]), row["before"]) for row in rows} class RateLimiter: """Token bucket keyed by a salted hash of the caller; the address itself is never kept.""" def __init__(self, per_minute: int, idle_seconds: float = 120.0) -> None: self.per_minute = per_minute self.idle_seconds = idle_seconds self._salt = secrets.token_bytes(16) self._buckets: dict[bytes, tuple[float, float]] = {} def allow(self, caller: str) -> bool: key = hmac.new(self._salt, caller.encode(), hashlib.sha256).digest()[:8] now = time.monotonic() tokens, updated = self._buckets.get(key, (float(self.per_minute), now)) tokens = min(float(self.per_minute), tokens + (now - updated) * self.per_minute / 60.0) allowed = tokens >= 1.0 self._buckets[key] = (tokens - 1.0 if allowed else tokens, now) if len(self._buckets) > 10_000: self._prune(now) return allowed def _prune(self, now: float) -> None: stale = [key for key, (_, updated) in self._buckets.items() if now - updated > self.idle_seconds] for key in stale: del self._buckets[key] class StatsStore: """Reads and writes one JSON file locally and, when configured, in a Hugging Face Dataset repo.""" def __init__( self, local_path: Path, dataset_repo: str = "", token: str = "", filename: str = STATS_FILE, ) -> None: self.local_path = local_path self.dataset_repo = dataset_repo self.token = token self.filename = filename self.remote_loaded = not self.remote @property def remote(self) -> bool: return bool(self.dataset_repo and self.token) def load(self) -> dict[str, Any] | None: """Blocking. Returns the saved counters, or None when there is nothing to load yet.""" if self.remote: loaded = self._load_remote() self.remote_loaded = True return loaded if self.local_path.is_file(): return json.loads(self.local_path.read_text(encoding="utf-8")) return None def _load_remote(self) -> dict[str, Any] | None: from huggingface_hub import hf_hub_download from huggingface_hub.errors import EntryNotFoundError try: path = hf_hub_download( self.dataset_repo, self.filename, repo_type="dataset", token=self.token, force_download=True, ) except EntryNotFoundError: return None return json.loads(Path(path).read_text(encoding="utf-8")) def save(self, text: str) -> None: """Blocking. Never uploads before the remote copy was read, so it cannot be overwritten.""" self.local_path.parent.mkdir(parents=True, exist_ok=True) self.local_path.write_text(text, encoding="utf-8") if not self.remote or not self.remote_loaded: return from huggingface_hub import HfApi HfApi(token=self.token).upload_file( path_or_fileobj=text.encode("utf-8"), path_in_repo=self.filename, repo_id=self.dataset_repo, repo_type="dataset", commit_message=f"Update {self.filename}", ) class Persisted(Protocol): dirty: bool def merge(self, raw: Any) -> None: ... def snapshot(self) -> str: ... class SyncedFile: """Loads a model from its store once, then writes it back whenever it changed.""" def __init__(self, store: StatsStore, model: Persisted) -> None: self.store = store self.model = model def _warn(self, action: str, error: Exception) -> None: log.warning("%s %s failed: %s", self.store.filename, action, type(error).__name__) async def load(self) -> None: try: loaded = await asyncio.to_thread(self.store.load) except Exception as error: self._warn("load", error) return self.model.merge(loaded) async def flush(self) -> None: if not self.store.remote_loaded: await self.load() if not self.model.dirty: return text = self.model.snapshot() self.model.dirty = False try: await asyncio.to_thread(self.store.save, text) except Exception as error: self.model.dirty = True self._warn("sync", error)