| from __future__ import annotations |
|
|
| import re |
| from pathlib import Path |
| from typing import Any |
|
|
|
|
| TEXT_SUFFIXES = {".md", ".py", ".json", ".jsonl", ".toml", ".yaml", ".yml", ".txt"} |
| FORBIDDEN = { |
| "Hugging Face access token": re.compile("h" + r"f_[A-Za-z0-9]{20,}"), |
| "macOS user path": re.compile("/" + r"Users/[^/\s]+/"), |
| "Linux user path": re.compile("/" + r"home/[^/\s]+/"), |
| "private key": re.compile(r"-----BEGIN (?:RSA |OPENSSH |EC )?PRIVATE KEY-----"), |
| "loopback URL in public result": re.compile(r"https?://(?:127\.0\.0\.1|localhost):\d+"), |
| } |
|
|
| PUBLIC_METADATA_KEYS = { |
| "target_prompt_tokens", |
| "requested_position", |
| "actual_position", |
| "query_placement", |
| "family", |
| "matrix", |
| "agentic_control_profile", |
| "agentic_set", |
| "agentic_family", |
| "real_repo", |
| "max_turns", |
| "max_tool_calls", |
| } |
| PUBLIC_TELEMETRY_KEYS = { |
| "wall_s", |
| "ttft_s", |
| "prompt_tokens", |
| "completion_tokens", |
| "cached_tokens", |
| "end_to_end_tokens_per_second", |
| "prefill_tokens_per_second", |
| "decode_tokens_per_second", |
| "active_memory_bytes", |
| "peak_memory_bytes", |
| "tool_call_count", |
| "dispatched_tool_call_count", |
| "blocked_duplicate_count", |
| "blocked_stall_count", |
| "terminal_correction_count", |
| "format_normalization_count", |
| "receipt_projection_count", |
| } |
| PUBLIC_REQUEST_OVERRIDE_KEYS = { |
| "temperature", |
| "top_p", |
| "top_k", |
| "max_tokens", |
| "reasoning_effort", |
| "thinking", |
| "mtp_depth", |
| "speculative_depth", |
| } |
|
|
|
|
| def scan_public_tree(root: Path) -> dict[str, Any]: |
| failures = [] |
| for path in sorted(root.rglob("*")): |
| if not path.is_file() or path.suffix.lower() not in TEXT_SUFFIXES: |
| continue |
| relative = path.relative_to(root).as_posix() |
| if any(part in {".git", ".pytest_cache", "__pycache__"} for part in path.parts): |
| continue |
| text = path.read_text(encoding="utf-8", errors="replace") |
| for label, pattern in FORBIDDEN.items(): |
| if pattern.search(text): |
| failures.append({"file": relative, "issue": label}) |
| return {"ok": not failures, "failures": failures, "files_scanned": sum(1 for p in root.rglob("*") if p.is_file())} |
|
|
|
|
| def _public_scalar(value: Any) -> bool: |
| return value is None or isinstance(value, (bool, int, float, str)) |
|
|
|
|
| def _error_category(error: Any) -> str | None: |
| if error in (None, ""): |
| return None |
| text = str(error).lower() |
| if "timeout" in text or "timed out" in text: |
| return "timeout" |
| if "budget" in text: |
| return "budget_exceeded" |
| if "missing required calls" in text: |
| return "missing_required_calls" |
| if "forbidden calls" in text: |
| return "forbidden_calls" |
| if "prompt token" in text or "token count" in text: |
| return "token_count_mismatch" |
| if "http" in text or "api" in text or "request" in text: |
| return "request_error" |
| return "evaluation_error" |
|
|
|
|
| def sanitize_result_row(row: dict[str, Any]) -> dict[str, Any]: |
| metadata = { |
| key: value |
| for key, value in (row.get("metadata") or {}).items() |
| if key in PUBLIC_METADATA_KEYS and _public_scalar(value) |
| } |
| telemetry = { |
| key: value |
| for key, value in (row.get("telemetry") or {}).items() |
| if key in PUBLIC_TELEMETRY_KEYS and _public_scalar(value) |
| } |
| return { |
| "schema_version": "1.0-public", |
| "suite_id": str(row.get("suite_id") or ""), |
| "case_id": str(row.get("case_id") or ""), |
| "lane": str(row.get("lane") or ""), |
| "variant": str(row.get("variant") or ""), |
| "passed": bool(row.get("passed")), |
| "score": float(row.get("score") or 0), |
| "score_max": float(row.get("score_max") or 0), |
| "error_category": _error_category(row.get("error")), |
| "telemetry": telemetry, |
| "metadata": metadata, |
| } |
|
|
|
|
| def _sanitize_request_overrides(value: Any) -> dict[str, Any]: |
| if not isinstance(value, dict): |
| return {} |
| clean: dict[str, Any] = {} |
| for key, item in value.items(): |
| if key not in PUBLIC_REQUEST_OVERRIDE_KEYS: |
| continue |
| if key == "thinking" and isinstance(item, dict): |
| clean[key] = {"enabled": bool(item.get("enabled"))} |
| elif _public_scalar(item): |
| clean[key] = item |
| return clean |
|
|
|
|
| def sanitize_manifest(manifest: dict[str, Any]) -> dict[str, Any]: |
| variants = [] |
| for variant in manifest.get("variants") or []: |
| if not isinstance(variant, dict): |
| continue |
| variants.append( |
| { |
| "label": str(variant.get("label") or ""), |
| "request_overrides": _sanitize_request_overrides( |
| variant.get("request_overrides") |
| ), |
| } |
| ) |
| clean: dict[str, Any] = { |
| "schema_version": "1.0-public", |
| "suite_id": str(manifest.get("suite_id") or ""), |
| "benchmark_version": str(manifest.get("benchmark_version") or ""), |
| "config_sha256": manifest.get("config_sha256"), |
| "tokenizer_fingerprint": manifest.get("tokenizer_fingerprint"), |
| "planned_cases": manifest.get("planned_cases"), |
| "requested_prompt_tokens": manifest.get("requested_prompt_tokens"), |
| "variants": variants, |
| "claim_policy": { |
| "raw_model_outputs_included": False, |
| "quality_and_performance_separate": True, |
| "single_aggregate_intelligence_score": False, |
| }, |
| } |
| for key in ( |
| "evaluated_artifact", |
| "benchmark_source", |
| "agentic_control_profile", |
| "agentic_set", |
| ): |
| value = manifest.get(key) |
| if key in {"evaluated_artifact", "benchmark_source"} and isinstance(value, dict): |
| clean[key] = { |
| "repo_id": str(value.get("repo_id") or ""), |
| "revision": str(value.get("revision") or ""), |
| } |
| elif _public_scalar(value) and value is not None: |
| clean[key] = value |
| return {key: value for key, value in clean.items() if value is not None} |
|
|