# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-verifiable-credential-whose-revocation-status-is-cached-across-a-domain-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-trusts-a-verifiable-credential-whose-revocation-status-is-cached-across-a-domain-boundary.warrant.json
# Markdown SHA-256:   258e2fff068b3d26491941449a4d399481d395546d4e4fb62d71203769eeb799
# Code AST SHA-256:   3528b4d6a06d44a720fc1cb20eab7c5136d68d2e61501e1c7d6499ebbbf2f1ff
# Verified Classes:   StaleRevocationStatusError, StatusCacheEntry, Credential
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime, timedelta


class StaleRevocationStatusError(RuntimeError):
    """Raised when a verifier attempts to admit a holder on cached
    revocation status that exceeds the issuer's signed staleness bound."""


@dataclass(frozen=True)
class StatusCacheEntry:
    issuer: str
    snapshot_hash: str
    resolved_at: datetime
    # The issuer's signed promise about max propagation delay.
    # None means the issuer published no bound -> fail closed.
    delta_issuer: timedelta | None


@dataclass(frozen=True)
class Credential:
    issuer: str
    expiration: datetime
    capability: str
    sensitivity: str  # "low" | "high"


HIGH_SENSITIVITY = {"high"}


def admission_allowed(
    cred: Credential,
    cache: StatusCacheEntry,
    now: datetime,
    live_status: str | None = None,
) -> bool:
    """Decide whether to admit a holder based on cached revocation status.

    `live_status` is the freshly resolved status ("clear" | "revoked" | None).
    A None live_status means the caller did not resolve live and is relying
    on the cache.
    """
    if cache.issuer != cred.issuer:
        raise StaleRevocationStatusError(
            f"cache issuer {cache.issuer!r} does not match credential issuer "
            f"{cred.issuer!r}; refusing to use foreign cache"
        )

    # High-sensitivity capabilities must always resolve live; cache is
    # never admissible regardless of any published bound.
    if cred.sensitivity in HIGH_SENSITIVITY:
        if live_status is None:
            raise StaleRevocationStatusError(
                f"capability {cred.capability!r} is high-sensitivity; "
                "cached status is not admissible and no live resolution "
                "was supplied"
            )
        return live_status == "clear"

    # No issuer-published staleness bound -> caching is unsound. Fail closed
    # unless the caller resolved live.
    if cache.delta_issuer is None:
        if live_status is None:
            raise StaleRevocationStatusError(
                f"issuer {cred.issuer!r} published no staleness bound; "
                "cached status cannot be trusted, live resolution required"
            )
        return live_status == "clear"

    age = now - cache.resolved_at
    if age > cache.delta_issuer:
        raise StaleRevocationStatusError(
            f"cached status for issuer {cred.issuer!r} is {age} old, "
            f"exceeding issuer bound {cache.delta_issuer}; "
            f"snapshot {cache.snapshot_hash!r} must be re-resolved"
        )

    # Bound satisfied; still must not outlive the credential itself.
    if now > cred.expiration:
        return False
    return True
