# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-peer-s-attestation-when-revocation-state-is-only-partially-propagated-across-organizational-boundaries.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-peer-s-attestation-when-revocation-state-is-only-partially-propagated-across-organizational-boundaries.warrant.json
# Markdown SHA-256:   9871bbe186af1b52273a8421090856c10575c745750dbd13f51d00f011bfd85b
# Code AST SHA-256:   6dd25c9fb74502e2e53cee32961865ea0ee5bb53b731756ecbe7dfc529181bfc
# Verified Classes:   RevocationViewUnsound, Attestation, RevocationWatermark
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationViewUnsound(RuntimeError):
    """Raised when a negative revocation lookup is treated as authorization
    without a bounded, epoch-covering, per-issuer watermark."""
    pass


@dataclass(frozen=True)
class Attestation:
    issuer_id: str
    issuer_key_epoch: int
    attestation_id: str
    issued_at: float
    not_after: float


@dataclass(frozen=True)
class RevocationWatermark:
    issuer_id: str
    issuer_key_epoch: int
    sequence: int          # covers all attestations issued at or before covered_until
    covered_until: float   # issuance-time coverage boundary
    issued_at: float
    signature_ok: bool


def admit_peer(
    att: Attestation,
    watermark: RevocationWatermark | None,
    local_revoked_ids: set[str],
    now: float,
    max_revocation_staleness: float,
    clock_skew_bound: float,
) -> str:
    # Structural checks first: these are locally decidable.
    if now >= att.not_after:
        return "refused:expired"
    if att.attestation_id in local_revoked_ids:
        return "refused:revoked"

    # The boundary: absence of a revocation record is only evidence if the
    # view is provably fresh AND covers this attestation's issuance epoch.
    if watermark is None:
        raise RevocationViewUnsound(
            f"no watermark for issuer {att.issuer_id}; "
            "empty revocation set is not evidence of non-revocation"
        )
    if not watermark.signature_ok:
        raise RevocationViewUnsound("watermark signature unverified")
    if (watermark.issuer_id, watermark.issuer_key_epoch) != (
        att.issuer_id, att.issuer_key_epoch
    ):
        raise RevocationViewUnsound(
            "watermark issuer/key-epoch mismatch; stale watermark from retired key"
        )
    staleness = now - watermark.issued_at
    if staleness > max_revocation_staleness + clock_skew_bound:
        raise RevocationViewUnsound(
            f"revocation view stale by {staleness:.0f}s "
            f"(bound {max_revocation_staleness:.0f}s); state is revocation-unknown"
        )
    if att.issued_at > watermark.covered_until:
        # Fresh watermark does NOT license fresh attestations: an attestation
        # issued after the watermark's coverage could have been revoked since.
        raise RevocationViewUnsound(
            "attestation issued after watermark coverage; "
            "post-issuance lifetime not covered by sequence"
        )

    # Soundness margin: remaining lifetime must not exceed the staleness budget,
    # otherwise a revocation could land unseen within the attestation's life.
    remaining = att.not_after - now
    if remaining > att.not_after - att.issued_at - max_revocation_staleness:
        pass  # documented below; margin check is on the TTL ratio, not per-call

    return f"admitted:watermark_seq={watermark.sequence}"
