# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-delegated-reputation-score-after-a-cross-org-quorum-split.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-delegated-reputation-score-after-a-cross-org-quorum-split.warrant.json
# Markdown SHA-256:   1f6edd56bb61c4a33293d301f2042f1309d39572b7e2e05134b10d5429abcf02
# Code AST SHA-256:   a7997618cf504cf1651996b6af11bd443ee0656dc2bc7ca6c4852cb901419ef5
# Verified Classes:   ReputationInheritanceError, QuorumCertificate
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet, Optional


class ReputationInheritanceError(RuntimeError):
    """Raised when a delegated reputation delta cannot be verified against pinned trust."""


@dataclass(frozen=True)
class QuorumCertificate:
    member_set_id: str          # hash of the roster used to validate
    signers: FrozenSet[str]     # identities that signed
    self_reported_threshold: int
    self_reported_signature_count: int
    score: float
    predecessor_cert_id: Optional[str]


def inherit_reputation(
    cert: QuorumCertificate,
    pinned_rosters: dict[str, FrozenSet[str]],   # member_set_id -> members
    active_member_set_id: str,
    fault_tolerance_f: int,
    predecessor_resolvable: bool,
) -> float:
    # Invariant 1: certificate must name a roster we have pinned.
    if cert.member_set_id not in pinned_rosters:
        raise ReputationInheritanceError(
            f"cert member_set_id {cert.member_set_id!r} not in pinned rosters; "
            f"cannot verify quorum epoch"
        )
    if cert.member_set_id != active_member_set_id:
        raise ReputationInheritanceError(
            f"cert epoch {cert.member_set_id!r} != active pin {active_member_set_id!r}; "
            f"cross-epoch delta requires governance acceptance, not silent inheritance"
        )

    roster = pinned_rosters[cert.member_set_id]
    required = 2 * fault_tolerance_f + 1

    # Invariant 2: threshold computed against intersection, never self-report.
    in_set = cert.signers & roster
    if len(in_set) < required:
        raise ReputationInheritanceError(
            f"quorum intersection {len(in_set)} < required {required} "
            f"(self-reported {cert.self_reported_signature_count}/"
            f"{cert.self_reported_threshold} rejected as untrusted)"
        )

    # Invariant 3: delta chain must be replayable to the last committed value.
    if cert.predecessor_cert_id is not None and not predecessor_resolvable:
        raise ReputationInheritanceError(
            f"predecessor cert {cert.predecessor_cert_id!r} unresolvable; "
            f"net delta cannot be replay-verified"
        )

    return cert.score
