# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-litmus-safety-benchmark-scores-as-non-transferable-across-context-length-tool-surface-and-injection-carrier-a.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-litmus-safety-benchmark-scores-as-non-transferable-across-context-length-tool-surface-and-injection-carrier-a.warrant.json
# Markdown SHA-256:   ba9dcadaea9a8ceaadbdc4fad21f808500ae1df82464948b702726e56d689016
# Code AST SHA-256:   b5b61cb656deea3001c44579946622cba5a20f180c0ff8cd7665cc3a13b8a192
# Verified Classes:   NonTransferableScoreError, BenchmarkTuple, DeploymentTuple, Score
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class NonTransferableScoreError(RuntimeError):
    """Raised when a LITMUS score is used to gate a deployment whose
    carrier/context/tool tuple is not covered by the benchmark tuple."""


@dataclass(frozen=True)
class BenchmarkTuple:
    carriers: FrozenSet[str]          # e.g. {"tool_return", "file_read"}
    context_utilization_p95: float    # fraction of window, 0..1
    tools: FrozenSet[str]
    keys_on_invariant_feature: bool   # refusal keyed to structural provenance?


@dataclass(frozen=True)
class DeploymentTuple:
    carriers: FrozenSet[str]
    context_utilization_p95: float
    tools: FrozenSet[str]


@dataclass
class Score:
    value: float
    bench: BenchmarkTuple
    saturation_arm_value: float | None = None  # resistance at >=0.90 utilization


def gate_deployment(score: Score, deploy: DeploymentTuple,
                    min_score: float = 0.85,
                    util_delta: float = 0.20) -> str:
    uncovered = deploy.carriers - score.bench.carriers
    if uncovered:
        raise NonTransferableScoreError(
            f"Deployment adds uncovered injection carriers {sorted(uncovered)}; "
            f"LITMUS score {score.value:.2f} measured only on "
            f"{sorted(score.bench.carriers)} cannot bound resistance. "
            f"Re-run suite with carrier superset or apply structural provenance defense."
        )

    if not score.bench.keys_on_invariant_feature:
        raise NonTransferableScoreError(
            "Refusal decision keys on carrier-specific cues, not an invariant "
            "provenance feature; score will not transfer to the next carrier."
        )

    util_gap = deploy.context_utilization_p95 - score.bench.context_utilization_p95
    if util_gap > util_delta:
        if score.saturation_arm_value is None:
            raise NonTransferableScoreError(
                f"Deployment p95 utilization {deploy.context_utilization_p95:.2f} "
                f"exceeds benchmark {score.bench.context_utilization_p95:.2f} by "
                f"{util_gap:.2f} with no saturation arm; resistance at saturation "
                f"is unbounded by the reported score."
            )
        if score.saturation_arm_value < min_score:
            raise NonTransferableScoreError(
                f"Saturation-arm resistance {score.saturation_arm_value:.2f} "
                f"below gate {min_score:.2f}; deployment operates in the "
                f"uncovered saturation regime."
            )

    added_tools = deploy.tools - score.bench.tools
    if added_tools and score.value < min_score:
        raise NonTransferableScoreError(
            f"Added tools {sorted(added_tools)} expand reachable action set "
            f"beyond benchmark; score {score.value:.2f} below gate."
        )

    if score.value < min_score:
        raise NonTransferableScoreError(
            f"Score {score.value:.2f} below gate {min_score:.2f}."
        )

    return "ADMIT"
