# SPDX-License-Identifier: MIT
"""Minimal eval harness for an LLM gateway.

Replays recorded per-request latencies and error flags, then reports
tail-latency percentiles and the error rate against an SLO. No network
and no vendor SDK, so it is deterministic and free to run in CI.

Usage:
    python gateway_eval.py records.json
    cat records.json | python gateway_eval.py -
where records.json is a list of {"latency_ms": float, "error": bool}.
"""

from __future__ import annotations

import json
import sys
from dataclasses import dataclass

import numpy as np


@dataclass
class Slo:
    """Service-level objective the gateway is graded against."""

    p99_ms: float
    max_error_rate: float


def percentiles(latencies_ms: list[float]) -> dict[str, float]:
    arr = np.asarray(latencies_ms, dtype=float)
    return {
        "p50": float(np.percentile(arr, 50)),
        "p95": float(np.percentile(arr, 95)),
        "p99": float(np.percentile(arr, 99)),
    }


def evaluate(records: list[dict], slo: Slo) -> dict:
    latencies = [float(r["latency_ms"]) for r in records]
    errors = sum(1 for r in records if r.get("error"))
    pct = percentiles(latencies)
    error_rate = errors / max(len(records), 1)
    passed = pct["p99"] <= slo.p99_ms and error_rate <= slo.max_error_rate
    return {"percentiles": pct, "error_rate": error_rate, "passed": passed}


def read_source(path: str) -> str:
    if path == "-":
        return sys.stdin.read()
    with open(path, encoding="utf-8") as handle:
        return handle.read()


def main(argv: list[str]) -> int:
    path = argv[1] if len(argv) > 1 else "-"
    records = json.loads(read_source(path))
    slo = Slo(p99_ms=800.0, max_error_rate=0.01)
    report = evaluate(records, slo)
    print(json.dumps(report, indent=2))
    return 0 if report["passed"] else 1


if __name__ == "__main__":
    raise SystemExit(main(sys.argv))
