# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-s-revocation-epoch-advances-mid-handoff-across-aaif-registries.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-s-revocation-epoch-advances-mid-handoff-across-aaif-registries.warrant.json
# Markdown SHA-256:   36ec2710ebf1d3f32de9ece4815317933db478abfa297a366a91cf6ebafc5a0f
# Code AST SHA-256:   0546ec7f8d7747d819a1852696607d7f52b3ad963584a0d8d1022044f9d229fe
# Verified Classes:   EpochStaleWarrantError, TrustBelowFloorError, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Mapping


class EpochStaleWarrantError(RuntimeError):
    """Raised when a warrant's delegation-chain epoch disagrees with any
    upstream AAIF registry's latest known revocation epoch at commit time."""


class TrustBelowFloorError(RuntimeError):
    """Raised when event-aware trust decay has dropped the issuer below floor."""


@dataclass(frozen=True)
class Warrant:
    issuer_id: str
    epoch_at_issue: int
    trust_at_issue: float
    parent_epoch: int
    parent_registry: str


def validate_at_commit(
    warrant: Warrant,
    registry_epochs: Mapping[str, int],
    registry_sync_age_s: Mapping[str, float],
    replication_sla_s: float,
    current_trust: float,
    trust_floor: float,
) -> None:
    # 1. Every registry in the chain must be within its staleness budget.
    for reg, age in registry_sync_age_s.items():
        if age > replication_sla_s:
            raise EpochStaleWarrantError(
                f"registry {reg} epoch sync age {age:.2f}s exceeds SLA "
                f"{replication_sla_s:.2f}s; cannot certify epoch freshness"
            )

    # 2. Parent epoch must match the latest known epoch for the parent registry.
    latest_parent = registry_epochs.get(warrant.parent_registry)
    if latest_parent is None:
        raise EpochStaleWarrantError(
            f"no epoch observed for parent registry {warrant.parent_registry}"
        )
    if warrant.parent_epoch < latest_parent:
        raise EpochStaleWarrantError(
            f"warrant parent_epoch={warrant.parent_epoch} is behind "
            f"{warrant.parent_registry}@{latest_parent}; authority revoked"
        )

    # 3. Event-aware trust: revocation forces tau->0 immediately.
    if current_trust < trust_floor:
        raise TrustBelowFloorError(
            f"issuer {warrant.issuer_id} trust {current_trust:.3f} "
            f"below floor {trust_floor:.3f}"
        )


def handoff(warrant: Warrant, **kwargs) -> None:
    validate_at_commit(warrant, **kwargs)
    # Only reached if conjunctive freshness held atomically at commit.
    _execute_side_effects(warrant)


def _execute_side_effects(warrant: Warrant) -> None:
    ...
