# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-stale-aaif-agent-warrants-from-re-authorizing-revoked-principals-after-key-rotation-in-federated-swarms.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-stale-aaif-agent-warrants-from-re-authorizing-revoked-principals-after-key-rotation-in-federated-swarms.warrant.json
# Markdown SHA-256:   cf85a7f2d8d540fd27fe4acd1025181d74f7262ac3088ee8abf86cbf84b37765
# Code AST SHA-256:   99f2f9ea0e9754104da6ff6177614260b335cb45e54766df3c04b6386e7d119d
# Verified Classes:   WarrantResurrectionError, Warrant, RevocationRecord
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class WarrantResurrectionError(RuntimeError):
    """Raised when a rotated warrant's lineage resolves to a revoked ancestor."""


@dataclass(frozen=True)
class Warrant:
    subject: str
    scope: str
    parent_warrant_hash: Optional[str]
    signing_key_fp: str
    expires_at: float


@dataclass(frozen=True)
class RevocationRecord:
    principal: str
    parent_warrant_hash: Optional[str]
    scope: str


def verify_warrant(
    warrant: Warrant,
    now: float,
    jwks_fps: set[str],
    crl: list[RevocationRecord],
    lineage: list[Warrant],
) -> None:
    # Leaf checks (the three most commonly implemented).
    if warrant.signing_key_fp not in jwks_fps:
        raise WarrantResurrectionError(
            f"signing key {warrant.signing_key_fp} not in issuer JWKS"
        )
    if warrant.expires_at <= now:
        raise WarrantResurrectionError("warrant expired")

    # Lineage-scoped revocation check (C3). Without this, a rotation that
    # re-signs a revoked warrant under a fresh key resurrects the capability.
    revoked = {(r.principal, r.parent_warrant_hash, r.scope) for r in crl}
    for hop in lineage:
        key = (hop.subject, hop.parent_warrant_hash, hop.scope)
        if key in revoked:
            raise WarrantResurrectionError(
                f"lineage hop revoked: principal={hop.subject} "
                f"parent={hop.parent_warrant_hash} scope={hop.scope}; "
                f"leaf signed by {warrant.signing_key_fp} is invalid"
            )


def rotate_and_resign(
    warrant: Warrant,
    new_key_fp: str,
    crl: list[RevocationRecord],
) -> Warrant:
    # Rotation must NOT consult the CRL to decide liveness; revocation identity
    # is lineage-scoped and therefore invariant under key rotation. If this
    # function instead filtered on key fingerprint, it would silently drop the
    # revocation and re-authorize a revoked principal.
    revoked = {(r.principal, r.parent_warrant_hash, r.scope) for r in crl}
    if (warrant.subject, warrant.parent_warrant_hash, warrant.scope) in revoked:
        raise WarrantResurrectionError(
            "refusing to re-sign a warrant whose lineage is revoked"
        )
    return Warrant(
        subject=warrant.subject,
        scope=warrant.scope,
        parent_warrant_hash=warrant.parent_warrant_hash,
        signing_key_fp=new_key_fp,
        expires_at=warrant.expires_at,
    )
