# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-peer-s-trust-score-outlives-its-credential-in-a-cross-org-a2a-mesh.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-peer-s-trust-score-outlives-its-credential-in-a-cross-org-a2a-mesh.warrant.json
# Markdown SHA-256:   594319a9cbc093a3420cdb16d1c055890711187820c8bfbdcb6303b89672d70b
# Code AST SHA-256:   8fa78dc1cc063cdcf9e46aea0aa43e6fe822891a62102ad4656b158b10adfddf
# Verified Classes:   TrustRevocationDivergence, TrustPosterior, RevocationRecord, Credential
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional

class TrustRevocationDivergence(RuntimeError):
    """Raised when a routing instance detects that its trust posterior for a
    peer is derived from evidence that pre-dates a lineage-scoped revocation
    it has already received, or when CRL epoch lag makes the peer
    revoked-by-default. The instance MUST halt routing for this peer."""
    pass

@dataclass(frozen=True)
class TrustPosterior:
    org_id: str
    logical_id: str
    key_id: str
    alpha: float
    beta: float
    last_evidence_ts: float
    attesting_orgs: frozenset

@dataclass(frozen=True)
class RevocationRecord:
    org_id: str
    logical_id: str
    revoked_key_id: str
    revoked_at_ts: float
    lineage_scope: bool  # True => invalidates all keys for logical_id

@dataclass(frozen=True)
class Credential:
    org_id: str
    logical_id: str
    key_id: str
    issued_at_ts: float

def route_or_halt(
    posterior: TrustPosterior,
    cred: Credential,
    revocations: list,
    local_crl_epoch: int,
    peer_crl_epoch: int,
    quorum_size: int,
    tau: float = 0.72,
) -> bool:
    # 1. CRL epoch lag: peer revoked-by-default until caught up.
    if peer_crl_epoch > local_crl_epoch:
        raise TrustRevocationDivergence(
            f"CRL epoch lag for {cred.org_id}: local={local_crl_epoch} "
            f"peer={peer_crl_epoch}; treating {cred.logical_id} as revoked-by-default"
        )

    # 2. Lineage-scoped revocation vs. stale trust over new key.
    for r in revocations:
        if r.org_id != cred.org_id or r.logical_id != cred.logical_id:
            continue
        if r.lineage_scope and posterior.last_evidence_ts < r.revoked_at_ts:
            raise TrustRevocationDivergence(
                f"Stale-trust-over-new-key: {cred.logical_id} key {cred.key_id} "
                f"issued at {cred.issued_at_ts} after lineage revocation at "
                f"{r.revoked_at_ts}, but posterior last evidence at "
                f"{posterior.last_evidence_ts}; halting routing"
            )
        if not r.lineage_scope and r.revoked_key_id == cred.key_id:
            raise TrustRevocationDivergence(
                f"Key {cred.key_id} for {cred.logical_id} is revoked"
            )

    # 3. Quorum re-attestation: single-org posteriors cannot authorize.
    if len(posterior.attesting_orgs) < quorum_size:
        raise TrustRevocationDivergence(
            f"Sybil-correlated trust: {cred.logical_id} posterior attested by "
            f"{len(posterior.attesting_orgs)} orgs, quorum requires {quorum_size}"
        )

    # 4. Trust threshold on the keyed posterior only.
    mean = posterior.alpha / (posterior.alpha + posterior.beta)
    return mean > tau
