# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-peer-agent-s-signed-manifest-when-revocation-state-is-only-partially-propagated-across-a-federation-bo.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-peer-agent-s-signed-manifest-when-revocation-state-is-only-partially-propagated-across-a-federation-bo.warrant.json
# Markdown SHA-256:   c99205a805ef30c0813f149651b371134b9f21d186453681a37d09c0f394ce1d
# Code AST SHA-256:   7b1f6cd803260dcc81a317bf26b57b86fc100a5a94f18bc9836f25e52eef92ba
# Verified Classes:   StaleRevocationView, RevocationView, Manifest
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class StaleRevocationView(RuntimeError):
    """Raised when a verifier cannot prove a manifest is unrevoked within
    the federation's declared propagation bound. Fail closed: do not admit."""
    pass


@dataclass(frozen=True)
class RevocationView:
    watermark: float          # last observed revocation-stream timestamp
    delta_max: float          # issuer-published max propagation latency (s)
    revoked_ids: frozenset    # manifest_ids known revoked as of watermark


@dataclass(frozen=True)
class Manifest:
    manifest_id: str
    issuer: str
    not_after: float
    signature_valid: bool


def admit_peer(
    manifest: Manifest,
    view: Optional[RevocationView],
    now: float,
    issuer_reachable: bool,
) -> bool:
    # Authenticity is necessary but NOT sufficient for authorization.
    if not manifest.signature_valid:
        return False
    if now >= manifest.not_after:
        return False

    # No revocation view at all -> cannot prove non-revocation.
    if view is None:
        if not issuer_reachable:
            raise StaleRevocationView(
                f"no revocation view and issuer {manifest.issuer} unreachable; "
                f"cannot prove {manifest.manifest_id} unrevoked"
            )
        # Caller is expected to synchronously fetch a fresh view and retry.
        raise StaleRevocationView(
            f"no revocation view for {manifest.manifest_id}; fetch watermark first"
        )

    staleness = now - view.watermark
    if staleness > view.delta_max:
        # The verifier's view is older than the federation guarantees.
        # Absence of evidence is not evidence of non-revocation: fail closed.
        raise StaleRevocationView(
            f"revocation view stale by {staleness:.1f}s > delta_max "
            f"{view.delta_max:.1f}s for manifest {manifest.manifest_id}; "
            f"cannot admit on stale view"
        )

    if manifest.manifest_id in view.revoked_ids:
        return False

    # Bounded-staleness safe: staleness <= delta_max, not in revocation set.
    return True
