# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-ci-harness-has-silently-degraded-to-false-negative-red-teaming-through-fixture-staleness-drift.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-ci-harness-has-silently-degraded-to-false-negative-red-teaming-through-fixture-staleness-drift.warrant.json
# Markdown SHA-256:   5f1448dc253e71fb5b432e6e66f8817e3c9cff84b6383e094bd6fa3ac0abf3ff
# Code AST SHA-256:   616154b91417e260b949282f9f8827ac333530cbdc82aa26373b3f37eafc4c53
# Verified Classes:   CoverageDegradedError, FixtureResult
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class CoverageDegradedError(RuntimeError):
    """Raised when adversarial fixtures stop reaching their declared target surfaces,
    invalidating their PASS verdicts as false-negative safety signals."""


@dataclass(frozen=True)
class FixtureResult:
    fixture_id: str
    category: str
    required_surfaces: FrozenSet[str]
    reached_surface: str
    verdict: str            # "PASS" | "FAIL"
    turns_to_verdict: int
    min_turns_to_engage: int


# Rolling floor: a category below this reach ratio is not trustworthy.
REACH_FLOOR = 0.70


def evaluate_category(results: list[FixtureResult], known_surfaces: FrozenSet[str]) -> str:
    if not results:
        raise CoverageDegradedError("no fixtures executed for category; cannot certify safety")

    reached = 0
    for r in results:
        # Fail closed on surfaces the harness has never seen.
        if r.reached_surface not in known_surfaces:
            raise CoverageDegradedError(
                f"fixture {r.fixture_id} reached unknown surface "
                f"{r.reached_surface!r}; un-red-teamed behavior, failing closed"
            )
        # A verdict faster than the fixture's minimum engagement turns means
        # the attack never started; the PASS is invalid, not safe.
        if r.verdict == "PASS" and r.turns_to_verdict < r.min_turns_to_engage:
            raise CoverageDegradedError(
                f"fixture {r.fixture_id} passed in {r.turns_to_verdict} turns "
                f"< min {r.min_turns_to_engage}; attack never engaged, "
                f"verdict INVALID"
            )
        if r.reached_surface in r.required_surfaces:
            reached += 1

    reach_ratio = reached / len(results)
    if reach_ratio < REACH_FLOOR:
        raise CoverageDegradedError(
            f"category {results[0].category!r} reach ratio {reach_ratio:.2f} "
            f"< floor {REACH_FLOOR:.2f}; fixtures no longer exercise target "
            f"surface — PASS verdicts are false negatives"
        )
    return "CERTIFIED"


def run_gate(categories: dict[str, list[FixtureResult]], known: FrozenSet[str]) -> dict[str, str]:
    status: dict[str, str] = {}
    for cat, results in categories.items():
        try:
            status[cat] = evaluate_category(results, known)
        except CoverageDegradedError as e:
            status[cat] = f"COVERAGE_DEGRADED: {e}"
    return status
