# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-verify-a-delegated-agent-credential-when-the-federation-trust-anchor-rotates-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-verify-a-delegated-agent-credential-when-the-federation-trust-anchor-rotates-mid-handoff.warrant.json
# Markdown SHA-256:   01fd66ff8d5f7be8a60a603b82d9c8054015592a4d3081ed7112bb0cac09bf5c
# Code AST SHA-256:   2b40bd43980b01fb9d24f8e5187f2c52127ff88bd5d9ed957df2fb4835f82905
# Verified Classes:   AnchorEpochMismatch, RegistryView, CachedVerdict, DelegatedCredential
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class AnchorEpochMismatch(RuntimeError):
    """Raised when a delegated credential's anchor fingerprint does not match
    the relying party's current registry view for the issuer at re-delegation."""
    pass


@dataclass(frozen=True)
class RegistryView:
    issuer: str
    anchor_fingerprint: str
    epoch: int  # monotonic, incremented on every rotation/revocation


@dataclass(frozen=True)
class CachedVerdict:
    issuer: str
    subject: str
    anchor_fingerprint: str
    registry_epoch: int


@dataclass(frozen=True)
class DelegatedCredential:
    issuer: str
    subject: str
    embedded_anchor_fingerprint: str
    parent_epoch: int
    signature_valid: bool


def reverify_before_redelegation(
    cred: DelegatedCredential,
    registry: RegistryView,
    cache: Optional[CachedVerdict],
) -> CachedVerdict:
    # Signature validity is necessary but NOT sufficient.
    if not cred.signature_valid:
        raise AnchorEpochMismatch(
            f"credential from {cred.issuer} has invalid signature"
        )

    # The anchor the credential was minted under must still be the anchor
    # the registry currently endorses for that issuer.
    if cred.embedded_anchor_fingerprint != registry.anchor_fingerprint:
        raise AnchorEpochMismatch(
            f"issuer={cred.issuer} credential anchored at "
            f"{cred.embedded_anchor_fingerprint} but registry now endorses "
            f"{registry.anchor_fingerprint} (epoch {registry.epoch})"
        )

    # The parent's epoch must match the relying party's epoch; a stale
    # parent epoch means the chain crossed a rotation boundary.
    if cred.parent_epoch != registry.epoch:
        raise AnchorEpochMismatch(
            f"parent_epoch={cred.parent_epoch} != registry_epoch={registry.epoch} "
            f"for issuer={cred.issuer}; chain crossed a rotation boundary"
        )

    # A cached verdict is reusable only if it was computed in this epoch.
    if cache is not None and cache.registry_epoch != registry.epoch:
        raise AnchorEpochMismatch(
            f"cached verdict for {cache.issuer}/{cache.subject} is from epoch "
            f"{cache.registry_epoch}, current epoch is {registry.epoch}"
        )

    return CachedVerdict(
        issuer=cred.issuer,
        subject=cred.subject,
        anchor_fingerprint=registry.anchor_fingerprint,
        registry_epoch=registry.epoch,
    )
