# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-litmus-safety-benchmark-score-as-non-transferable-to-deployment.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-litmus-safety-benchmark-score-as-non-transferable-to-deployment.warrant.json
# Markdown SHA-256:   fd24f556ca7921a872df8a5cc7295dc7e6855532b9d58e1d2cbc92af5559e2e5
# Code AST SHA-256:   330a84dc4bf78bca4c4c557725fb28c0c4554ccf1b431d429f5b37d2d5510c7f
# Verified Classes:   NonTransferableWarrantError, ScaffoldFingerprint, LitmusWarrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from typing import FrozenSet


class NonTransferableWarrantError(RuntimeError):
    """Raised when a LITMUS score is cited as a warrant outside its conditioning set."""


@dataclass(frozen=True)
class ScaffoldFingerprint:
    tool_schema_hash: str
    retry_budget: int
    detector_signature: str
    precedence_layout: str
    probe_families: FrozenSet[str]

    def dominates(self, deployment: "ScaffoldFingerprint") -> bool:
        # Benchmark scaffold must be equal-or-more-adversarial on every axis.
        if self.tool_schema_hash != deployment.tool_schema_hash:
            return False
        if self.retry_budget > deployment.retry_budget:
            # benchmark granted more retries than deployment -> lenient
            return False
        if self.detector_signature != deployment.detector_signature:
            return False
        if self.precedence_layout != deployment.precedence_layout:
            return False
        return deployment.probe_families.issubset(self.probe_families)


@dataclass
class LitmusWarrant:
    resistance: float
    scaffold: ScaffoldFingerprint
    trace_ids: tuple = field(default_factory=tuple)

    def cite_for(self, deployment: ScaffoldFingerprint, first_attempt_resistance: float) -> float:
        if not self.scaffold.dominates(deployment):
            raise NonTransferableWarrantError(
                "LITMUS warrant cited outside its conditioning set: "
                f"benchmark={self.scaffold} deployment={deployment}"
            )
        # Even when dominated, prefer the stricter first-attempt figure.
        return min(self.resistance, first_attempt_resistance)


def issue_warrant(
    resistance: float,
    scaffold: ScaffoldFingerprint,
    deployment: ScaffoldFingerprint,
    first_attempt_resistance: float,
) -> float:
    if not (0.0 <= resistance <= 1.0):
        raise NonTransferableWarrantError(f"resistance out of range: {resistance}")
    warrant = LitmusWarrant(resistance=resistance, scaffold=scaffold)
    return warrant.cite_for(deployment, first_attempt_resistance)
