# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-adversarial-skill-benchmark-verdicts-as-context-bound-rather-than-portable.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-adversarial-skill-benchmark-verdicts-as-context-bound-rather-than-portable.warrant.json
# Markdown SHA-256:   1e4edfef0e4d23936fcb335bdffabe4eca499fd970e31b11d22ffcced14bb9bb
# Code AST SHA-256:   ae6dcff19f3390c28f50fe22823ab3e4b2e49f304b581a7fec685b03894d2fea
# Verified Classes:   StaleVerdictError, Verdict, Surface
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class StaleVerdictError(RuntimeError):
    """Raised when a LITMUS verdict is consumed outside the tuple it was minted under."""


@dataclass(frozen=True)
class Verdict:
    probe_id: str
    outcome: str  # "PASS" | "FAIL"
    manifest_hash: str
    scoring_policy_version: str
    corpus_version: str
    model_fingerprint: str
    decoding_config: str


@dataclass(frozen=True)
class Surface:
    manifest_hash: str
    scoring_policy_version: str
    corpus_version: str
    model_fingerprint: str
    decoding_config: str


def gate_skill(verdict: Verdict, surface: Surface, *, reexecute=None) -> bool:
    """Return True only if the verdict is gating-eligible for this surface.

    A stale PASS is never gating-eligible without re-execution against the
    current surface. A stale FAIL may be surfaced as a diagnostic but must
    not be summed into a safety score.
    """
    tuple_match = (
        verdict.manifest_hash == surface.manifest_hash
        and verdict.scoring_policy_version == surface.scoring_policy_version
        and verdict.corpus_version == surface.corpus_version
        and verdict.model_fingerprint == surface.model_fingerprint
        and verdict.decoding_config == surface.decoding_config
    )
    if tuple_match:
        return verdict.outcome == "PASS"

    if verdict.outcome == "PASS":
        if reexecute is None:
            raise StaleVerdictError(
                f"probe {verdict.probe_id}: stale PASS with no re-execution path; "
                f"minted under manifest {verdict.manifest_hash}, "
                f"surface is {surface.manifest_hash}"
            )
        fresh = reexecute(verdict.probe_id, surface)
        if fresh.outcome != "PASS":
            raise StaleVerdictError(
                f"probe {verdict.probe_id}: stale PASS did not reproduce on current "
                f"surface (fresh outcome={fresh.outcome}); skill must not be exposed"
            )
        return True

    # Stale FAIL: diagnostic only, never gating-eligible.
    raise StaleVerdictError(
        f"probe {verdict.probe_id}: stale FAIL is diagnostic-only; "
        f"do not gate or aggregate across surfaces"
    )
