# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-litmus-safety-harness-verdict-as-a-measurement-of-its-own-refusal-policy-rather-than-a-verdict-on-its-behavior.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-litmus-safety-harness-verdict-as-a-measurement-of-its-own-refusal-policy-rather-than-a-verdict-on-its-behavior.warrant.json
# Markdown SHA-256:   e3e8c48c0c4ddf85cb946ad7c627615cb023504cc9c0ce70502c6f6728067eb1
# Code AST SHA-256:   4161c0518b29ab5fc84b48d1116b19d8b8169a853853f11238b9abe7ee9191b6
# Verified Classes:   UninterpretableSafetyVerdict, LitmusVerdict
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class UninterpretableSafetyVerdict(RuntimeError):
    """Raised when a LITMUS-style verdict cannot be attributed to a fixed measurement regime."""
    pass


@dataclass
class LitmusVerdict:
    score: float
    retry_on_refusal: bool
    attempt_index_pinned: Optional[int]   # None if harness did not pin
    mutation_manifest_present: bool
    stability_score: Optional[float]      # required when retry_on_refusal is True


def consume_verdict(v: LitmusVerdict, *, deployment_gate: bool) -> str:
    # A safety score is only well-defined relative to a fixed (prompt, attempt) -> label map.
    if v.attempt_index_pinned is None:
        raise UninterpretableSafetyVerdict(
            "verdict has no attempt-index provenance; score measures harness retry policy, "
            "not instance safety policy"
        )

    if v.retry_on_refusal:
        # Retry-on-refusal merges attempts; the headline score is a paraphrase-stability
        # measurement, not a safety-policy measurement. It must not gate deployment alone.
        if v.stability_score is None:
            raise UninterpretableSafetyVerdict(
                "retry-on-refusal harness reported a score without a stability measurement; "
                "cannot separate policy from paraphrase fragility"
            )
        if not v.mutation_manifest_present:
            raise UninterpretableSafetyVerdict(
                "mutation manifest absent; mutation-order coupling makes score non-comparable"
            )
        if deployment_gate:
            raise UninterpretableSafetyVerdict(
                "retry-on-refusal score is a paraphrase-stability measurement and cannot "
                "gate deployment; require an attempt-1-pinned run"
            )
        return f"stability={v.stability_score:.3f} (not a safety-policy score)"

    # Attempt-pinned, no retry: this is a safety-policy measurement.
    if not v.mutation_manifest_present:
        raise UninterpretableSafetyVerdict(
            "attempt-pinned verdict lacks mutation manifest; comparability across runs unverified"
        )
    return f"safety_policy={v.score:.3f} (attempt {v.attempt_index_pinned})"
