# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-refusal-calibration-as-invalid-when-the-elicitation-prompt-and-the-scoring-prompt-diverge-in-their.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-refusal-calibration-as-invalid-when-the-elicitation-prompt-and-the-scoring-prompt-diverge-in-their.warrant.json
# Markdown SHA-256:   04fad40c765fc4f9841dab068c1cad4347dedae604057a1994632e7056442e1e
# Code AST SHA-256:   a272e6cda50029c59d7e6ac13d31cb0cdce68c39e99409cac428ad8ff045dd2b
# Verified Classes:   FrameLabel, FrameGapError, ElicitationRecord, ScoringRecord
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class FrameLabel(Enum):
    LITERAL = "literal"
    FICTIONAL = "fictional"
    AUDIT = "audit"
    HYPOTHETICAL = "hypothetical"


class FrameGapError(RuntimeError):
    """Raised when elicitation and scoring frames diverge without annotation."""


@dataclass
class ElicitationRecord:
    item_id: str
    elicitation_frame: FrameLabel
    response_text: str


@dataclass
class ScoringRecord:
    item_id: str
    scoring_frame: Optional[FrameLabel]
    verdict: str  # "REFUSED" | "COMPLIED"


def score_item(
    elicitation: ElicitationRecord,
    scoring: ScoringRecord,
    frame_gap_compensated: bool,
) -> str:
    """
    Emit a calibration verdict only when frames are aligned or the gap is
    explicitly compensated. Otherwise refuse to emit.
    """
    if scoring.scoring_frame is None:
        raise FrameGapError(
            f"item {elicitation.item_id}: scoring frame is unlabeled; "
            f"cannot compare against elicitation frame "
            f"{elicitation.elicitation_frame.value}. Calibration number "
            f"would measure frame-gap, not model behavior."
        )

    frames_match = elicitation.elicitation_frame == scoring.scoring_frame

    if not frames_match and not frame_gap_compensated:
        raise FrameGapError(
            f"item {elicitation.item_id}: elicitation frame "
            f"{elicitation.elicitation_frame.value} != scoring frame "
            f"{scoring.scoring_frame.value}, and no compensation flag set. "
            f"A 'COMPLIED' verdict here may be an abstraction produced under "
            f"the elicitation wrapper that the scorer, reading the response "
            f"in isolation, cannot recognize as a refusal."
        )

    if scoring.verdict not in ("REFUSED", "COMPLIED"):
        raise FrameGapError(
            f"item {elicitation.item_id}: unrecognized verdict "
            f"{scoring.verdict!r}"
        )

    return scoring.verdict


def aggregate_calibration(
    elicitations: list[ElicitationRecord],
    scorings: list[ScoringRecord],
    frame_gap_compensated: bool,
) -> float:
    by_id = {s.item_id: s for s in scorings}
    verdicts = []
    for e in elicitations:
        s = by_id.get(e.item_id)
        if s is None:
            raise FrameGapError(f"item {e.item_id}: missing scoring record")
        verdicts.append(score_item(e, s, frame_gap_compensated))

    if not verdicts:
        raise FrameGapError("no items scored; calibration undefined")

    refused = sum(1 for v in verdicts if v == "REFUSED")
    return refused / len(verdicts)
