# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-litmus-safety-benchmark-pass-as-a-non-transferable-certificate-rather-than-a-ci-regression-gate.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-litmus-safety-benchmark-pass-as-a-non-transferable-certificate-rather-than-a-ci-regression-gate.warrant.json
# Markdown SHA-256:   0ae6e4813316625bf891b7c8f46790a866c3346765419343671fa93236694ec9
# Code AST SHA-256:   d59918ebef391934afcfd612860337f1a2fb9e5d2894a1d5985680d8ec6a40c2
# Verified Classes:   BenchmarkCertificateMismatch, RunConfig
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class BenchmarkCertificateMismatch(RuntimeError):
    """Raised when a LITMUS result is compared across incompatible config tuples."""
    pass


@dataclass(frozen=True)
class RunConfig:
    corpus_hash: str
    decode_config: dict
    prompt_hash: str
    tool_schema_hash: str
    classifier_threshold: float


def _tuple_hash(cfg: RunConfig) -> str:
    blob = repr((
        cfg.corpus_hash,
        sorted(cfg.decode_config.items()),
        cfg.prompt_hash,
        cfg.tool_schema_hash,
        cfg.classifier_threshold,
    )).encode()
    return hashlib.sha256(blob).hexdigest()


def gate_merge(baseline: RunConfig, candidate: RunConfig,
               candidate_aggregate: float, floor: float) -> bool:
    if _tuple_hash(baseline) != _tuple_hash(candidate):
        raise BenchmarkCertificateMismatch(
            "Aggregate comparison across differing config tuples is invalid; "
            "re-run baseline under candidate config and use a paired delta test."
        )
    if candidate_aggregate < floor:
        raise BenchmarkCertificateMismatch(
            f"Candidate aggregate {candidate_aggregate} below floor {floor}."
        )
    return True
