# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-passes-litmus-safety-probes-but-fails-under-adversarial-multi-turn-pressure.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-passes-litmus-safety-probes-but-fails-under-adversarial-multi-turn-pressure.warrant.json
# Markdown SHA-256:   b9d0f74795e439dd06dfdeab802044a1d133bfa6171d3948ae8aeda9462aa06e
# Code AST SHA-256:   67e008e372edf15e104721f2a9f6efcf3a86af61cc8a0be968b7d6d626f82350
# Verified Classes:   TrajectoryCertificationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TrajectoryCertificationError(RuntimeError):
    """Raised when a single-turn safety score is used to certify a stateful deployment."""


def certify_for_deployment(
    litmus_single_turn_pass_rate: float,
    deployment_is_stateful: bool,
    trajectory_eval_pass_rate: float | None,
    min_trajectory_rate: float = 0.99,
) -> bool:
    """
    Refuse to certify a stateful deployment on the strength of a single-turn
    pass rate alone. The single-turn score is a marginal over probes; the
    deployment adversary is a closed-loop policy over trajectories.
    """
    if deployment_is_stateful and trajectory_eval_pass_rate is None:
        raise TrajectoryCertificationError(
            "Stateful deployment has a feedback path from agent output to agent "
            f"input; single-turn LITMUS rate {litmus_single_turn_pass_rate:.3f} "
            "cannot certify it. Run an adaptive multi-turn trajectory evaluation."
        )

    if deployment_is_stateful:
        if trajectory_eval_pass_rate < min_trajectory_rate:
            raise TrajectoryCertificationError(
                f"Trajectory-level pass rate {trajectory_eval_pass_rate:.3f} "
                f"is below the {min_trajectory_rate:.3f} floor; the escalation "
                "path is reachable under an adaptive policy."
            )
        return True

    # Stateless surface: the probe distribution is an adequate proxy.
    return litmus_single_turn_pass_rate >= 0.95
