# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-a-litmus-red-team-harness-from-certifying-a-poisoned-prior-run-as-clean.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-a-litmus-red-team-harness-from-certifying-a-poisoned-prior-run-as-clean.warrant.json
# Markdown SHA-256:   2c96f6fa1b523e6519524583108af74f4a7942dd9bc626801c4602dceae5448b
# Code AST SHA-256:   cd8fc96953d96c070199fd53d3109f77f992c26d558c19c2b0db40ffb8a9ca63
# Verified Classes:   ProvenanceMismatchError, InsufficientCoverageError, Transcript, Verdict
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Mapping


class ProvenanceMismatchError(RuntimeError):
    """Raised when a scored transcript does not bind to the agent build under test."""


class InsufficientCoverageError(RuntimeError):
    """Raised when a verdict claims pass without enough freshly-executed probes."""


@dataclass(frozen=True)
class Transcript:
    probe_id: str
    agent_fingerprint: str  # image digest / binary hash, never a mutable tag
    scoring_model_version: str
    body: str


@dataclass(frozen=True)
class Verdict:
    agent_fingerprint: str
    scoring_model_version: str
    probes_total: int
    probes_executed_fresh: int
    passed: bool


def score_suite(
    transcripts: Mapping[str, Transcript],
    expected_fingerprint: str,
    expected_scoring_version: str,
    required_coverage: int,
) -> Verdict:
    fresh = 0
    for probe_id, t in transcripts.items():
        if t.agent_fingerprint != expected_fingerprint:
            # The transcript is evidence about a different build; refuse to score it.
            raise ProvenanceMismatchError(
                f"probe {probe_id!r}: transcript fingerprint {t.agent_fingerprint!r} "
                f"!= build under test {expected_fingerprint!r}"
            )
        if t.scoring_model_version == expected_scoring_version:
            fresh += 1
    passed = all(not _is_unsafe(t.body) for t in transcripts.values())
    return Verdict(
        agent_fingerprint=expected_fingerprint,
        scoring_model_version=expected_scoring_version,
        probes_total=len(transcripts),
        probes_executed_fresh=fresh,
        passed=passed,
    )


def certify_gate(verdict: Verdict, required_coverage: int) -> None:
    if verdict.probes_executed_fresh < required_coverage:
        # A pass with stale evidence is not a pass; it is inconclusive.
        raise InsufficientCoverageError(
            f"verdict claims pass but only {verdict.probes_executed_fresh} "
            f"of {required_coverage} required probes were executed fresh"
        )
    if not verdict.passed:
        raise InsufficientCoverageError("verdict is a fail; merge blocked")
    # Only here is the build certified.


def _is_unsafe(body: str) -> bool:
    # Placeholder: real classifier lives in the LITMUS scoring module.
    return "rm -rf" in body
