# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-warrants-from-re-entering-a-trust-domain-through-cached-capability-manifests.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-warrants-from-re-entering-a-trust-domain-through-cached-capability-manifests.warrant.json
# Markdown SHA-256:   92b596080ab9dfcb1eaff215116fa42987d31b1fa3be4f4b526910545d0d765d
# Code AST SHA-256:   1264879b6e1713cd57f71472421778ef8c20dc31df3deae1492bfe9ea46bbb6a
# Verified Classes:   RevocationPropagationError, ManifestEntry, CrlState
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class RevocationPropagationError(RuntimeError):
    """Raised when a manifest entry cannot be proven causally after current revocation state."""


@dataclass(frozen=True)
class ManifestEntry:
    issuer_domain: str
    agent_id: str
    granted_capabilities: frozenset
    crl_epoch: int
    warrant_withdrawn: bool


@dataclass(frozen=True)
class CrlState:
    epoch: int
    revoked: frozenset  # set of (issuer_domain, agent_id)


def admit_handoff(entry: ManifestEntry, crl: CrlState) -> bool:
    """
    Admission gate enforcing the three-conjunct invariant:
      1. manifest.crl_epoch == crl.epoch  (causal ordering)
      2. agent not in current CRL          (revocation)
      3. warrant not withdrawn at issuer   (resurrection guard)
    """
    key = (entry.issuer_domain, entry.agent_id)

    if entry.crl_epoch < crl.epoch:
        # Manifest built before current revocation epoch: must re-validate,
        # not refresh. Refresh re-fetches the warrant and can resurrect it.
        if key in crl.revoked:
            raise RevocationPropagationError(
                f"stale manifest admits revoked agent {key}: "
                f"manifest_epoch={entry.crl_epoch} crl_epoch={crl.epoch}"
            )
        # Stale but not revoked: require synchronous re-check before proceeding.
        raise RevocationPropagationError(
            f"manifest for {key} predates crl epoch {crl.epoch}; "
            f"synchronous re-check required, refresh forbidden"
        )

    if key in crl.revoked:
        raise RevocationPropagationError(
            f"revoked agent {key} presented at crl_epoch={crl.epoch}"
        )

    if entry.warrant_withdrawn:
        raise RevocationPropagationError(
            f"warrant for {key} withdrawn at issuer; manifest grant is resurrected"
        )

    return True
