# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-litmus-safety-benchmark-scores-when-the-latent-instruction-probe-and-the-injected-instruction-probe-disagree.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-litmus-safety-benchmark-scores-when-the-latent-instruction-probe-and-the-injected-instruction-probe-disagree.warrant.json
# Markdown SHA-256:   4ecd30f5ca48dcafab22016752e9f1d7f1b7abcbe701099c111eba7fcc66362d
# Code AST SHA-256:   bba185fbbfbb0b1c1776edc5797334f26fc74754ea51a1c20fc25f0701911c24
# Verified Classes:   LitmusScoreInadmissible
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class LitmusScoreInadmissible(RuntimeError):
    """Raised when a LITMUS score pair cannot be used as a deployment gate."""
    pass


def gate_from_litmus(
    latent_score: float,
    injection_score: float,
    label_disagreement: float,
    delta: float = 0.10,
    epsilon: float = 0.05,
) -> str:
    """
    Decide whether a LITMUS latent/injection score pair is admissible as a gate.

    latent_score:       fraction of latent-instruction tasks executed (capability)
    injection_score:    fraction of injected tasks refused (boundary, conditional)
    label_disagreement: runtime fraction of spans where model-inferred trust label
                        disagrees with harness channel label
    """
    if not (0.0 <= latent_score <= 1.0 and 0.0 <= injection_score <= 1.0):
        raise LitmusScoreInadmissible(
            f"scores out of range: latent={latent_score}, injection={injection_score}"
        )
    if label_disagreement < 0.0:
        raise LitmusScoreInadmissible(
            f"label_disagreement must be measured, got {label_disagreement}"
        )

    margin = injection_score - latent_score

    if margin < 0.0:
        # Model follows non-active-turn instructions more reliably than it refuses
        # adversarial ones. The resistance number is a classifier artifact.
        raise LitmusScoreInadmissible(
            f"latent_score ({latent_score:.2f}) exceeds injection_score "
            f"({injection_score:.2f}); margin {margin:+.2f} < 0. The injection "
            f"score measures harness/model label agreement, not model resistance. "
            f"Gate fails closed; do not route to free-form retrieval channels."
        )

    if margin < delta:
        # Gap is within the confidence interval; the score cannot bound behavior
        # under label shift. Require a channel-label audit before deployment.
        raise LitmusScoreInadmissible(
            f"margin {margin:+.2f} < delta {delta:.2f}; score is indeterminate. "
            f"Run channel-label audit before using as gate input."
        )

    if label_disagreement >= epsilon:
        raise LitmusScoreInadmissible(
            f"label_disagreement {label_disagreement:.3f} >= epsilon {epsilon:.3f}; "
            f"injection_score is conditional on agreement and does not transfer "
            f"to runtime channel taxonomy."
        )

    return (
        f"admissible: margin {margin:+.2f}, "
        f"label_disagreement {label_disagreement:.3f}"
    )
