# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-keeps-a-revoked-peer-s-short-lived-credential-alive-across-a-rotation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-keeps-a-revoked-peer-s-short-lived-credential-alive-across-a-rotation-boundary.warrant.json
# Markdown SHA-256:   dc4df60bb791cb4f2d3460e837e71dfbcab083f185581e2f0d7b19f77d2e0c26
# Code AST SHA-256:   fedd245736d9de6c932a34054df29cb6daeba4fb226b4063b9285415d01d7b9f
# Verified Classes:   RevocationPropagationGap, PeerCredentialCache
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class RevocationPropagationGap(RuntimeError):
    """Raised when a relying party cannot prove a presented credential was
    minted after every revocation it is obligated to honor for that issuer."""
    pass


class PeerCredentialCache:
    def __init__(self, issuer_id: str, expected_seq: int):
        self.issuer_id = issuer_id
        self.expected_seq = expected_seq          # next revocation seq we must see
        self.last_known_epoch = 0                 # highest revocation_epoch applied
        self.snapshot_issued_at = 0               # wall-clock of last CRL we applied

    def apply_revocation_list(self, seq: int, epoch: int, issued_at: int) -> None:
        # Best-effort delivery: a gap means we may have missed a revocation.
        if seq > self.expected_seq:
            raise RevocationPropagationGap(
                f"issuer {self.issuer_id}: missed revocation seq "
                f"{self.expected_seq}..{seq - 1}; refusing to advance"
            )
        if seq == self.expected_seq:
            self.expected_seq += 1
            self.last_known_epoch = max(self.last_known_epoch, epoch)
            self.snapshot_issued_at = issued_at

    def validate(self, token: dict) -> bool:
        # token carries: sub, iat, exp, revocation_epoch, issuer
        if token["issuer"] != self.issuer_id:
            raise RevocationPropagationGap(
                f"token issuer {token['issuer']} != cache issuer {self.issuer_id}"
            )
        # Cannot prove the token post-dates revocations we haven't seen.
        if token["iat"] > self.snapshot_issued_at:
            raise RevocationPropagationGap(
                f"token iat={token['iat']} is newer than revocation snapshot "
                f"issued_at={self.snapshot_issued_at}; snapshot may be stale"
            )
        # A freshly minted token from a stale issuer replica carries a low epoch.
        if token["revocation_epoch"] < self.last_known_epoch:
            raise RevocationPropagationGap(
                f"token revocation_epoch={token['revocation_epoch']} predates "
                f"known epoch={self.last_known_epoch}; identity is revoked"
            )
        return True
