# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-semantic-convention-exporter-is-collapsing-judge-verdicts-into-non-comparable-telemetry.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-semantic-convention-exporter-is-collapsing-judge-verdicts-into-non-comparable-telemetry.warrant.json
# Markdown SHA-256:   94f01a6521ff9685e35ce932cf76c40e7a70ef9a8ce7b099eebc0a4c7049ce8a
# Code AST SHA-256:   61437365fbe562e8994abf1a51c1785d76009e43064699e87bff08c9f2e2b9f3
# Verified Classes:   VerdictSchemaViolation, EvalMetricExporter
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class VerdictSchemaViolation(RuntimeError):
    """Raised when an exporter's attribute set cannot carry verdict identity."""
    pass

# Identity dimensions that MUST survive projection into a metric attribute set.
# score and sample_seed are deliberately excluded: score is the value, seed is a
# variance source that belongs on the span, not in the aggregation key.
VERDICT_IDENTITY: FrozenSet[str] = frozenset({
    "gen_ai.eval.rubric_id",
    "gen_ai.eval.rubric_version",
    "gen_ai.eval.label",
    "gen_ai.judge.model",
    "gen_ai.judge.model_digest",
})

@dataclass
class EvalMetricExporter:
    metric_name: str
    attribute_allowlist: FrozenSet[str]
    _started: bool = field(default=False, init=False)

    def start(self) -> None:
        missing = VERDICT_IDENTITY - self.attribute_allowlist
        if missing:
            # Fail closed: a lossy metric is worse than no metric.
            raise VerdictSchemaViolation(
                f"exporter {self.metric_name!r} cannot carry verdict identity; "
                f"missing identity dimensions: {sorted(missing)}. "
                f"Refusing to emit a non-comparable eval series."
            )
        self._started = True

    def emit(self, verdict: dict) -> None:
        if not self._started:
            raise VerdictSchemaViolation(
                f"exporter {self.metric_name!r} emitted before start(); "
                f"identity validation was skipped."
            )
        # Guard against per-emit drift: the harness may add identity fields
        # after startup, and the exporter must not silently drop them.
        dropped = (VERDICT_IDENTITY & verdict.keys()) - self.attribute_allowlist
        if dropped:
            raise VerdictSchemaViolation(
                f"verdict carries identity dimensions {sorted(dropped)} "
                f"not present in metric attribute set; aggregation would collide "
                f"distinct judgments into one series."
            )
        # ... emit point keyed on the full identity tuple ...

# Correct exporter: allowlist is a superset of identity.
good = EvalMetricExporter(
    metric_name="gen_ai.eval.score",
    attribute_allowlist=VERDICT_IDENTITY | {"gen_ai.eval.dataset"},
)
good.start()

# Lossy exporter: drops rubric_version and judge digest -> must refuse to start.
lossy = EvalMetricExporter(
    metric_name="gen_ai.eval.score",
    attribute_allowlist=frozenset({
        "gen_ai.eval.rubric_id",
        "gen_ai.eval.label",
        "gen_ai.judge.model",
    }),
)
try:
    lossy.start()
except VerdictSchemaViolation as exc:
    print(f"refused: {exc}")
