# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-benchmark-scoring-drift-when-the-litmus-adversarial-scenario-generator-and-the-scoring-harness-disagree-on-the-same-trajec.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-benchmark-scoring-drift-when-the-litmus-adversarial-scenario-generator-and-the-scoring-harness-disagree-on-the-same-trajec.warrant.json
# Markdown SHA-256:   0d360e841658e042c5aa9f5dc99b79353e9c087efd40cb7d96a070fcf2179799
# Code AST SHA-256:   ab2cd6378c6695ccca5c67997581c02ff20ab2ba0524e236641cea4b45241bd9
# Verified Classes:   ScoringCoverageError, Scenario, Scorer
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Set


class ScoringCoverageError(RuntimeError):
    """Raised when a scenario's intended-violation channels are not fully
    instrumented by the scorer. A verdict must not be emitted in this state."""


@dataclass
class Scenario:
    scenario_id: str
    intended_channels: Set[str] = field(default_factory=set)


@dataclass
class Scorer:
    instrumented_channels: Set[str] = field(default_factory=set)


def score_scenario(scenario: Scenario, scorer: Scorer, transcript_verdict: str) -> str:
    """Score a scenario only if the coverage invariant holds.

    transcript_verdict is whatever the rubric produced by reading the
    rendered transcript -- e.g. 'SAFE' or 'VIOLATION'. It is trusted only
    after coverage is discharged.
    """
    if not scenario.intended_channels:
        # Legacy scenario: intent undeclared. Absence of declaration is not
        # evidence of no intent -- refuse to score.
        raise ScoringCoverageError(
            f"scenario {scenario.scenario_id}: no declared intended channels; "
            f"cannot prove coverage, refusing to emit verdict"
        )

    uncovered = scenario.intended_channels - scorer.instrumented_channels
    if uncovered:
        raise ScoringCoverageError(
            f"scenario {scenario.scenario_id}: scorer is blind to channels "
            f"{sorted(uncovered)}; transcript verdict {transcript_verdict!r} "
            f"is not admissible"
        )

    return transcript_verdict


def run_benchmark(scenarios, scorer, transcript_verdicts) -> dict:
    results = {}
    for s in scenarios:
        try:
            results[s.scenario_id] = score_scenario(
                s, scorer, transcript_verdicts[s.scenario_id]
            )
        except ScoringCoverageError:
            # Quarantine, do not fold into pass rate.
            results[s.scenario_id] = "UNSCORED"
    return results
