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.

6.4 KB
Raw Blame
#!/usr/bin/env python3
"""Validate Cobertura coverage XML and emit PR-comment data as JSON."""

from __future__ import annotations

import json
import math
import re
import sys
from html import escape
from pathlib import Path

from defusedxml import ElementTree
from defusedxml.common import DefusedXmlException


_INTEGER = re.compile(r"[0-9]+")
_JS_MAX_SAFE_INTEGER = 2**53 - 1
_RATE_ROUNDING_TOLERANCE = 0.0000500000001
_MAX_DIAGNOSTIC_LENGTH = 1024
_ERROR_LOG_PREFIX = "Coverage report rejected: "
_MAX_ERROR_LENGTH = _MAX_DIAGNOSTIC_LENGTH - len(_ERROR_LOG_PREFIX)


def _local_name(tag: str) -> str:
    """Return an XML tag without its optional namespace."""
    return tag.rsplit("}", 1)[-1]


def _rate(attributes: dict[str, str], name: str) -> float:
    raw_value = attributes.get(name)
    if raw_value is None:
        raise ValueError(f"coverage.xml is missing {name!r}")
    value = float(raw_value)
    if not math.isfinite(value) or not 0 <= value <= 1:
        raise ValueError(f"coverage.xml has invalid {name!r}: {raw_value!r}")
    return value


def _count(attributes: dict[str, str], name: str) -> int:
    raw_value = attributes.get(name)
    if raw_value is None or _INTEGER.fullmatch(raw_value) is None:
        raise ValueError(f"coverage.xml has invalid {name!r}: {raw_value!r}")
    value = int(raw_value)
    if value > _JS_MAX_SAFE_INTEGER:
        raise ValueError(
            f"coverage.xml has {name!r} above the JavaScript safe integer limit"
        )
    return value


def _validate_rate(
    *,
    rate: float,
    covered: int,
    total: int,
    rate_name: str,
    covered_name: str,
) -> None:
    """Ensure a declared rate agrees with its coverage.py-rounded counts."""
    if covered > total:
        raise ValueError(
            f"coverage.xml {covered_name} exceeds its declared total"
        )
    if total == 0:
        # coverage.py emits 1 for an empty denominator. Other Cobertura
        # producers commonly emit 0, which is also an honest representation.
        if rate not in {0.0, 1.0}:
            raise ValueError(
                f"coverage.xml {rate_name!r} disagrees with its counts"
            )
        return

    expected_rate = covered / total
    if not math.isclose(
        rate,
        expected_rate,
        rel_tol=0.0,
        abs_tol=_RATE_ROUNDING_TOLERANCE,
    ):
        raise ValueError(
            f"coverage.xml {rate_name!r} disagrees with its counts"
        )


def _bounded_error(error: BaseException) -> str:
    """Return a single-line diagnostic safe to publish in JSON and logs."""
    raw_message = f"{type(error).__name__}: {error}"
    printable_message = "".join(
        character if character.isprintable() else " "
        for character in raw_message
    )
    message = " ".join(printable_message.split())
    if len(message) > _MAX_ERROR_LENGTH:
        return f"{message[: _MAX_ERROR_LENGTH - 1]}…"
    return message


def _display_filename(filename: str) -> str:
    """Return bounded HTML-safe text for the bot's ``<code>`` element."""
    printable = "".join(
        character if character.isprintable() else "�" for character in filename
    )
    if len(printable) > 300:
        printable = f"{printable[:299]}…"
    return (
        escape(printable, quote=True)
        .replace("@", "&#64;")
        .replace("`", "&#96;")
    )


def _parse_summary(coverage_path: Path) -> dict[str, object]:
    root = ElementTree.parse(coverage_path).getroot()
    if _local_name(root.tag) != "coverage":
        raise ValueError("coverage.xml root element is not <coverage>")

    line_rate = _rate(root.attrib, "line-rate")
    branch_rate = _rate(root.attrib, "branch-rate")
    lines_covered = _count(root.attrib, "lines-covered")
    lines_total = _count(root.attrib, "lines-valid")
    branches_covered = _count(root.attrib, "branches-covered")
    branches_total = _count(root.attrib, "branches-valid")
    _validate_rate(
        rate=line_rate,
        covered=lines_covered,
        total=lines_total,
        rate_name="line-rate",
        covered_name="lines-covered",
    )
    _validate_rate(
        rate=branch_rate,
        covered=branches_covered,
        total=branches_total,
        rate_name="branch-rate",
        covered_name="branches-covered",
    )

    file_coverages: list[tuple[str, float]] = []
    for element in root.iter():
        if _local_name(element.tag) != "class":
            continue
        filename = element.attrib.get("filename")
        if not filename:
            raise ValueError("coverage.xml contains a class without a filename")
        normalized_filename = filename.removeprefix("src/").removeprefix(
            "local_deep_research/"
        )
        file_coverages.append(
            (
                _display_filename(normalized_filename),
                _rate(element.attrib, "line-rate") * 100,
            )
        )

    lowest_coverage_files = [
        {"filename": filename, "rate": rate}
        for filename, rate in sorted(
            (item for item in file_coverages if item[1] < 50),
            key=lambda item: item[1],
        )[:5]
    ]
    return {
        "generated": True,
        "valid": True,
        "lineRate": line_rate,
        "branchRate": branch_rate,
        "linesCovered": lines_covered,
        "linesTotal": lines_total,
        "filesAnalyzed": len(file_coverages),
        "lowestCoverageFiles": lowest_coverage_files,
    }


def build_summary(coverage_path: Path) -> dict[str, object]:
    """Return validated coverage data or a non-valid diagnostic summary."""
    if not coverage_path.is_file():
        return {"generated": False, "valid": False}

    try:
        return _parse_summary(coverage_path)
    except (
        DefusedXmlException,
        ElementTree.ParseError,
        LookupError,
        OSError,
        ValueError,
    ) as error:
        return {
            "generated": True,
            "valid": False,
            "error": _bounded_error(error),
        }


def main() -> int:
    if len(sys.argv) != 3:
        print(
            "usage: coverage_summary.py COVERAGE_XML OUTPUT_JSON",
            file=sys.stderr,
        )
        return 2

    summary = build_summary(Path(sys.argv[1]))
    Path(sys.argv[2]).write_text(
        json.dumps(summary, allow_nan=False, sort_keys=True),
        encoding="utf-8",
    )
    if error := summary.get("error"):
        print(f"{_ERROR_LOG_PREFIX}{error}", file=sys.stderr)
    return 0 if summary.get("valid") is True else 1


if __name__ == "__main__":
    raise SystemExit(main())