# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-warrants-from-resurrecting-through-cached-capability-handshakes-in-federated-meshes.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-warrants-from-resurrecting-through-cached-capability-handshakes-in-federated-meshes.warrant.json
# Markdown SHA-256:   77e832b0f717d3c5fb56881908bbf0cbfff8a19b01e1b975d78ad3b07628824c
# Code AST SHA-256:   23beb551adce81bcd2ea04a09714e59601dfdf18912e9b2f16ddb68e712399dd
# Verified Classes:   RevocationEpochUnobservable, Warrant, RevocationView
# Defensive Guards:   8 fail-closed invariants (zero external deps)

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


class RevocationEpochUnobservable(RuntimeError):
    """Raised when a hop cannot confirm the parent warrant's current revocation epoch.

    Minting a derived warrant from an unobservable epoch would launder a
    possibly-revoked identity into a fresh, unrevoked credential. We refuse
    rather than degrade to a cached positive decision.
    """
    pass


@dataclass
class Warrant:
    jti: str
    issuer: str
    subject: str
    parent_jti: Optional[str]
    minted_epoch: int
    expires_at: float
    signature_valid: bool = True


@dataclass
class RevocationView:
    """A verifier's local view of an issuer's revocation state."""
    issuer: str
    epoch: int
    revoked_jtis: set = field(default_factory=set)
    last_synced: float = 0.0


def mint_derived_warrant(
    parent: Warrant,
    derived_jti: str,
    derived_subject: str,
    ttl_seconds: float,
    view: RevocationView,
    issuer_epoch_now: Optional[int],
) -> Warrant:
    """Mint a child warrant only if the parent's revocation epoch is observable
    and current. Refuses otherwise. Enforces transitive revocability by
    recording parent_jti so downstream revocation can walk ancestry.
    """
    # 1. Signature and expiry are necessary but not sufficient.
    if not parent.signature_valid:
        raise RevocationEpochUnobservable(
            f"parent {parent.jti} signature invalid; refusing to mint"
        )
    if parent.expires_at <= time.time():
        raise RevocationEpochUnobservable(
            f"parent {parent.jti} expired; refusing to mint"
        )

    # 2. The epoch must be observable at all. No route to issuer => no mint.
    if issuer_epoch_now is None:
        raise RevocationEpochUnobservable(
            f"cannot observe revocation epoch for issuer {parent.issuer}; "
            f"refusing to mint derived warrant for {derived_subject}"
        )

    # 3. The epoch we hold must not lag the issuer's current epoch. A lagging
    #    view is a stale positive decision; minting from it is the laundering bug.
    if view.issuer != parent.issuer:
        raise RevocationEpochUnobservable(
            f"revocation view is for {view.issuer}, parent issued by {parent.issuer}"
        )
    if view.epoch < issuer_epoch_now:
        raise RevocationEpochUnobservable(
            f"revocation view epoch {view.epoch} lags issuer epoch "
            f"{issuer_epoch_now}; refusing to mint from stale authority"
        )

    # 4. The parent itself must not be revoked in the current epoch.
    if parent.jti in view.revoked_jtis:
        raise RevocationEpochUnobservable(
            f"parent {parent.jti} is revoked at epoch {view.epoch}; refusing to mint"
        )

    # 5. Mint, carrying parent_jti and the epoch under which authority was verified
    #    so the child is transitively revocable when the parent is revoked later.
    return Warrant(
        jti=derived_jti,
        issuer=view.issuer,
        subject=derived_subject,
        parent_jti=parent.jti,
        minted_epoch=issuer_epoch_now,
        expires_at=time.time() + ttl_seconds,
        signature_valid=True,
    )


def admit_call(
    warrant: Warrant,
    view: RevocationView,
    issuer_epoch_now: Optional[int],
) -> bool:
    """Admission check. A cached positive entry is only reusable if the epoch
    has not advanced since the entry was filled. Otherwise re-sync or refuse.
    """
    if issuer_epoch_now is None:
        raise RevocationEpochUnobservable(
            f"cannot observe epoch for {warrant.issuer}; refusing admission"
        )
    if view.epoch < issuer_epoch_now:
        raise RevocationEpochUnobservable(
            f"admission view epoch {view.epoch} < issuer epoch {issuer_epoch_now}"
        )
    # Transitive check: revoked parent poisons the child even if child's jti is clean.
    if warrant.jti in view.revoked_jtis:
        return False
    if warrant.parent_jti is not None and warrant.parent_jti in view.revoked_jtis:
        return False
    return warrant.expires_at > time.time() and warrant.signature_valid
