Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.5 KB
Raw Blame
"""Bounded error classifications for the disposable ZAP fixture server."""

from collections import Counter
import json
from pathlib import Path
import re
import sys
from threading import Lock


_LABEL = re.compile(r"[A-Za-z_][A-Za-z0-9_]{0,127}\Z")


class FixtureDiagnostics:
    """Retain code labels and counts, never exception values or requests."""

    def __init__(self, path: Path):
        self.path = path
        self.counts = Counter()
        self.dropped = 0
        self.lock = Lock()
        self._write()

    def _write(self):
        rows = [
            {
                "source": source,
                "function": function,
                "exception": error,
                "count": count,
            }
            for (source, function, error), count in sorted(self.counts.items())
        ]
        temporary = self.path.with_suffix(".tmp")
        temporary.write_text(
            json.dumps({"errors": rows, "dropped": self.dropped}),
            encoding="utf-8",
        )
        temporary.replace(self.path)

    def record(self, source, function, error_type):
        labels = (
            source,
            function,
            error_type.__name__ if error_type else "NoActiveException",
        )
        key = tuple(
            label if _LABEL.fullmatch(label) else "unknown" for label in labels
        )
        with self.lock:
            if key not in self.counts and len(self.counts) >= 128:
                self.dropped += 1
            else:
                self.counts[key] = min(self.counts[key] + 1, 999999)
            self._write()


class DiagnosticLogger:
    """Classify fixture errors and replace their messages with fixed text."""

    def __init__(self, logger, diagnostics: FixtureDiagnostics, source: str):
        self.logger = logger
        self.diagnostics = diagnostics
        self.source = source

    def __getattr__(self, name):
        return getattr(self.logger, name)

    def exception(self, *_args, **_kwargs):
        self.diagnostics.record(
            self.source, sys._getframe(1).f_code.co_name, sys.exc_info()[0]
        )
        return self.logger.opt(exception=False).error(
            "CI fixture error; see classified diagnostics"
        )

    def error(self, *_args, **_kwargs):
        self.diagnostics.record(
            self.source, sys._getframe(1).f_code.co_name, sys.exc_info()[0]
        )
        return self.logger.opt(exception=False).error(
            "CI fixture error; see classified diagnostics"
        )