# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-revoked-did-signer-when-its-manifest-cache-ttl-outlives-the-federation-revocation-window.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-revoked-did-signer-when-its-manifest-cache-ttl-outlives-the-federation-revocation-window.warrant.json
# Markdown SHA-256:   c685f2a0ca1f4db74ded485628def7b9685d8d7619813ddbd69d5c82603bf37d
# Code AST SHA-256:   29bc95599dd91653592bee1a467d79c481507dca19adee7187eed2ca110c0a35
# Verified Classes:   RevokedSignerAcceptedError, DIDDocument, RevocationList, SignedManifest, TrustAnchor
# Defensive Guards:   9 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field
from typing import Optional


class RevokedSignerAcceptedError(RuntimeError):
    """Raised when a manifest signature verifies against a key whose live
    status was never confirmed, or was confirmed only via a stale cache."""
    pass


@dataclass
class DIDDocument:
    did: str
    version: int
    verification_methods: set
    deactivated: bool
    fetched_at: float


@dataclass
class RevocationList:
    version: int
    revoked_keys: set
    issued_at: float


@dataclass
class SignedManifest:
    issuer_did: str
    signing_key_id: str
    scope: str
    signed_at: float


@dataclass
class TrustAnchor:
    did: str
    doc_cache_ttl: float          # C
    revocation_propagation_slo: float  # P
    max_privileged_scope_ttl: float    # E bound


def verify_manifest(
    manifest: SignedManifest,
    anchor: TrustAnchor,
    cached_doc: Optional[DIDDocument],
    cached_revlist: Optional[RevocationList],
    now: Optional[float] = None,
    privileged_scopes: frozenset = frozenset({"filesystem:write", "network:egress"}),
) -> bool:
    now = now if now is not None else time.time()

    if manifest.issuer_did != anchor.did:
        raise RevokedSignerAcceptedError(
            f"manifest issuer {manifest.issuer_did} does not match anchor {anchor.did}"
        )

    # Invariant: cache TTL must not exceed revocation propagation window
    # for any scope that can be privileged.
    if manifest.scope in privileged_scopes:
        if anchor.doc_cache_ttl > anchor.revocation_propagation_slo:
            raise RevokedSignerAcceptedError(
                f"cache TTL {anchor.doc_cache_ttl}s exceeds revocation SLO "
                f"{anchor.revocation_propagation_slo}s for privileged scope "
                f"{manifest.scope!r}; freshness cannot be guaranteed"
            )

    if cached_doc is None or cached_revlist is None:
        raise RevokedSignerAcceptedError("no DID document or revocation list available")

    doc_age = now - cached_doc.fetched_at
    if doc_age > anchor.doc_cache_ttl:
        raise RevokedSignerAcceptedError(
            f"DID document stale: age {doc_age:.1f}s > ttl {anchor.doc_cache_ttl}s"
        )

    revlist_age = now - cached_revlist.issued_at
    if revlist_age > anchor.revocation_propagation_slo:
        raise RevokedSignerAcceptedError(
            f"revocation list stale: age {revlist_age:.1f}s > slo "
            f"{anchor.revocation_propagation_slo}s; failing closed"
        )

    if cached_doc.deactivated:
        raise RevokedSignerAcceptedError(f"DID {anchor.did} deactivated")

    if manifest.signing_key_id not in cached_doc.verification_methods:
        raise RevokedSignerAcceptedError(
            f"signing key {manifest.signing_key_id} not in DID document"
        )

    if manifest.signing_key_id in cached_revlist.revoked_keys:
        raise RevokedSignerAcceptedError(
            f"signing key {manifest.signing_key_id} appears in revocation list "
            f"version {cached_revlist.version}"
        )

    if manifest.scope in privileged_scopes:
        attestation_age = now - manifest.signed_at
        if attestation_age > anchor.max_privileged_scope_ttl:
            raise RevokedSignerAcceptedError(
                f"privileged attestation expired: age {attestation_age:.1f}s > "
                f"max {anchor.max_privileged_scope_ttl}s"
            )

    return True
