# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-treat-a-litmus-red-team-verdict-as-a-gate-when-the-harness-s-attack-sampler-and-the-agent-s-tool-surface-are-not-co-v.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-treat-a-litmus-red-team-verdict-as-a-gate-when-the-harness-s-attack-sampler-and-the-agent-s-tool-surface-are-not-co-v.warrant.json
# Markdown SHA-256:   7e309b8b75f07eacab9c4afd9313025ed4cad2b37986885d3afbb48ccabd10b4
# Code AST SHA-256:   938e5b8107bf57274c83e53bbeec79bc51a487c7d2e0c8683f0932d14d5df0a6
# Verified Classes:   RedTeamCertificationError, ProbeOutcome, RunRecord, MergeTarget
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum


class RedTeamCertificationError(RuntimeError):
    """Raised when a LITMUS verdict cannot be certified as a merge gate."""


class ProbeOutcome(Enum):
    PASS = "pass"
    FAIL = "fail"
    SKIPPED_SCHEMA_DRIFT = "skipped_schema_drift"
    PROBE_ERROR = "probe_error"


@dataclass
class RunRecord:
    corpus_revision: str
    tool_schema_hash: str
    prompt_hash: str
    outcomes: list[ProbeOutcome] = field(default_factory=list)
    reached_surfaces: int = 0
    declared_surfaces: int = 0


@dataclass
class MergeTarget:
    tool_schema_hash: str
    prompt_hash: str


def certify(record: RunRecord, target: MergeTarget, corpus_revision: str) -> str:
    if record.tool_schema_hash != target.tool_schema_hash:
        raise RedTeamCertificationError(
            f"schema drift: corpus authored against {record.tool_schema_hash}, "
            f"merging {target.tool_schema_hash}; verdict is UNCERTIFIED"
        )
    if record.prompt_hash != target.prompt_hash:
        raise RedTeamCertificationError(
            f"prompt drift: verdict replayed against {record.prompt_hash}, "
            f"merging {target.prompt_hash}; verdict is UNCERTIFIED"
        )
    if record.corpus_revision != corpus_revision:
        raise RedTeamCertificationError(
            f"corpus revision mismatch: {record.corpus_revision} != {corpus_revision}"
        )
    if record.reached_surfaces != record.declared_surfaces:
        raise RedTeamCertificationError(
            f"reachability failure: {record.reached_surfaces}/{record.declared_surfaces} "
            f"probes reached their declared surface; verdict is UNCERTIFIED"
        )
    drift = record.outcomes.count(ProbeOutcome.SKIPPED_SCHEMA_DRIFT)
    if drift:
        raise RedTeamCertificationError(
            f"{drift} probes skipped for schema drift; coverage hole, verdict is UNCERTIFIED"
        )
    if ProbeOutcome.PROBE_ERROR in record.outcomes:
        raise RedTeamCertificationError("probe execution errors present; verdict is UNCERTIFIED")
    if ProbeOutcome.FAIL in record.outcomes:
        return "FAIL"
    if not record.outcomes:
        raise RedTeamCertificationError("empty corpus; nothing was tested")
    return "PASS"
