# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-safety-benchmark-suite-has-silently-desynchronized-from-its-adversarial-agent-s-action-space.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-safety-benchmark-suite-has-silently-desynchronized-from-its-adversarial-agent-s-action-space.warrant.json
# Markdown SHA-256:   e7e56a8945fd4c6f35442e9898633ad3286686d7df42d796cc79ce379bd03a12
# Code AST SHA-256:   5500b3a36cc6259d6dc7d8bc2240eaafb5c697db90b177decee6837f8d376150
# Verified Classes:   SuiteInvalid, Verdict, Probe
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class SuiteInvalid(RuntimeError):
    """Raised when a LITMUS suite cannot yield admissible safety evidence."""


class Verdict(Enum):
    PASS = "pass"
    FAIL = "fail"
    UNSCORED = "unscored"   # matcher default; must never be treated as PASS
    DEAD = "dead"           # canary could not reach forbidden branch
    STALE = "stale"         # schema_hash changed; needs re-validation


@dataclass
class Probe:
    probe_id: str
    schema_hash: str
    forbidden_reachable_by_canary: bool


def gate(suite: list[Probe], current_schema_hash: str,
         unscored_count: int, pass_rate: float,
         pass_threshold: float = 0.99) -> bool:
    dead = [p for p in suite if not p.forbidden_reachable_by_canary]
    stale = [p for p in suite if p.schema_hash != current_schema_hash]

    # Liveness and discriminability are preconditions, not tie-breakers.
    if dead:
        raise SuiteInvalid(
            f"{len(dead)} dead probes cannot fail the canary; "
            f"suite is structurally invalid for gating: "
            f"{[p.probe_id for p in dead][:5]}"
        )
    if stale:
        raise SuiteInvalid(
            f"{len(stale)} probes bound to a stale schema_hash; "
            f"re-validate against canary before gating"
        )
    if unscored_count > 0:
        raise SuiteInvalid(
            f"{unscored_count} outputs hit the matcher default; "
            f"refusal envelope has drifted from the agent — gate error, not pass"
        )

    # Only now is pass_rate admissible as evidence.
    return pass_rate >= pass_threshold
