# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-an-attested-sub-task-result-as-uncommitted-until-the-quorum-certificate-itself-is-re-verifiable-against-the-c.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-an-attested-sub-task-result-as-uncommitted-until-the-quorum-certificate-itself-is-re-verifiable-against-the-c.warrant.json
# Markdown SHA-256:   4ed431aa402f710030dfa69cff044d2a2c9ce8b317385534389b0a1e27562a49
# Code AST SHA-256:   fe58d27a6c5c1c87535b023c0b00571bf67ab3748464b54804aa978571014149
# Verified Classes:   UnverifiableQuorumError, QuorumCertificate
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class UnverifiableQuorumError(RuntimeError):
    """Raised when a result QC cannot be verified against the authoritative
    validator set for the committer's decision epoch."""


@dataclass(frozen=True)
class QuorumCertificate:
    result_digest: str
    epoch: int
    signers: frozenset[str]


def verify_commitment(
    qc: QuorumCertificate,
    decision_epoch: int,
    authoritative_set: dict[int, frozenset[str]],
    quorum_fn,
) -> None:
    """Verify a sub-task result QC against the epoch-bound validator set.

    Raises UnverifiableQuorumError if the QC's epoch does not match the
    decision epoch, if the authoritative set for that epoch is unavailable,
    or if the signer intersection falls below quorum for that epoch's set.
    """
    if qc.epoch != decision_epoch:
        # A QC from a prior epoch is not a degraded commitment; it is none.
        raise UnverifiableQuorumError(
            f"QC epoch {qc.epoch} != decision epoch {decision_epoch}; "
            "stale-authority window — must re-fetch and re-verify"
        )

    if decision_epoch not in authoritative_set:
        raise UnverifiableQuorumError(
            f"no authoritative validator set for epoch {decision_epoch}; "
            "refusing to fall back to cached set"
        )

    validators = authoritative_set[decision_epoch]
    threshold = quorum_fn(len(validators))
    effective = qc.signers & validators

    if len(effective) < threshold:
        raise UnverifiableQuorumError(
            f"QC signers {sorted(qc.signers)} yield {len(effective)} authorized "
            f"signatures against epoch {decision_epoch} set; need {threshold}"
        )
