# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-cross-org-reputation-the-attestation-time-vs-score-time-divergence.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-cross-org-reputation-the-attestation-time-vs-score-time-divergence.warrant.json
# Markdown SHA-256:   06bad0b90176ebd4aaed93ab1205d70096fff2eab9c04b3502c834b855086721
# Code AST SHA-256:   cf218907eb6161d3ddfcb36d88d45a9ad78a35ca249ab55dc61de06ae4d6a8e0
# Verified Classes:   ReputationInheritanceError, Attestation, InheritedScore
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime


class ReputationInheritanceError(RuntimeError):
    """Raised when an instance attempts to inherit a cross-org score whose
    support set no longer justifies the authority being exercised."""


@dataclass(frozen=True)
class Attestation:
    edge_id: str
    issuer: str
    weight: float
    not_before: datetime
    not_after: datetime
    revoked_at: datetime | None
    scope: frozenset[str]


@dataclass(frozen=True)
class InheritedScore:
    value: float
    tier: str
    evaluated_at: datetime
    function_version: str
    support: tuple[Attestation, ...]


def assert_inheritance_valid(
    score: InheritedScore,
    now: datetime,
    required_scope: frozenset[str],
    verifier_function_version: str,
    min_distinct_issuers: int,
    staleness_horizon_seconds: float,
) -> None:
    if score.function_version != verifier_function_version:
        raise ReputationInheritanceError(
            f"score evaluated under function {score.function_version!r}, "
            f"verifier runs {verifier_function_version!r}; cross-version "
            f"inheritance is undefined"
        )

    age = (now - score.evaluated_at).total_seconds()
    if age > staleness_horizon_seconds:
        raise ReputationInheritanceError(
            f"score age {age:.0f}s exceeds staleness horizon "
            f"{staleness_horizon_seconds:.0f}s"
        )

    live = [
        a for a in score.support
        if a.not_before <= now <= a.not_after
        and (a.revoked_at is None or a.revoked_at > now)
    ]

    if not live:
        raise ReputationInheritanceError(
            "no live attestation edges remain in support set"
        )

    issuers = {a.issuer for a in live}
    if len(issuers) < min_distinct_issuers:
        raise ReputationInheritanceError(
            f"surviving issuer diversity {len(issuers)} < required "
            f"{min_distinct_issuers}; sybil-resistance constraint no longer "
            f"holds after revocation/expiry"
        )

    live_scope = frozenset().union(*(a.scope for a in live))
    if not required_scope.issubset(live_scope):
        missing = required_scope - live_scope
        raise ReputationInheritanceError(
            f"required authority scope {sorted(missing)} is not attested by "
            f"any surviving edge; aggregate score cannot witness scope"
        )
