# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-verifies-a-peer-s-agent-card-against-a-cached-revocation-list-that-has-silently-forked.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-verifies-a-peer-s-agent-card-against-a-cached-revocation-list-that-has-silently-forked.warrant.json
# Markdown SHA-256:   b51aa47d1454718523b4fa266d3af3e2da53be97a99deb749f52f84ff662da2a
# Code AST SHA-256:   20884bccb816eed36b08aefa3bec625e3bb439b677dd3147ec93e1d4dc50a4b5
# Verified Classes:   StaleRevocationViewError, RevocationSnapshot
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class StaleRevocationViewError(RuntimeError):
    """Raised when a verifier cannot bound the staleness of its revocation snapshot
    and therefore cannot treat absence-of-revocation as evidence of non-revocation."""
    pass


class RevocationSnapshot:
    def __init__(self, digest: str, sequence: int, issued_at: float, revoked_fps: set):
        self.digest = digest
        self.sequence = sequence
        self.issued_at = issued_at
        self.revoked_fps = revoked_fps


def verify_agent_card(card, snapshot: RevocationSnapshot, now: float,
                      max_staleness: float, propagation_sla: float) -> bool:
    # Cryptographic signature check is assumed to have passed upstream.
    if not card.signature_valid:
        return False

    snapshot_age = now - snapshot.issued_at

    # Guardrail 1: if the snapshot is older than the propagation SLA, absence
    # of revocation is not a valid inference. We must not go optimistic.
    if snapshot_age > propagation_sla:
        raise StaleRevocationViewError(
            f"revocation snapshot seq={snapshot.sequence} age={snapshot_age:.3f}s "
            f"exceeds propagation SLA {propagation_sla:.3f}s; "
            f"cannot treat absence-of-revocation as evidence"
        )

    # Guardrail 2: if the card was issued after our snapshot, a revocation
    # event for its key could have occurred that we have not seen.
    if card.issued_at > snapshot.issued_at:
        raise StaleRevocationViewError(
            f"card issued_at={card.issued_at:.3f} is newer than snapshot "
            f"issued_at={snapshot.issued_at:.3f}; optimistic verification invalid"
        )

    # Guardrail 3: hard staleness ceiling regardless of SLA.
    if snapshot_age > max_staleness:
        raise StaleRevocationViewError(
            f"snapshot age {snapshot_age:.3f}s exceeds hard ceiling {max_staleness:.3f}s"
        )

    if card.key_fingerprint in snapshot.revoked_fps:
        return False

    return True
