# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-cross-domain-revocation-epoch-as-authoritative-the-stale-warrant-resurrection-boundary-in-federated-agent-identity.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-cross-domain-revocation-epoch-as-authoritative-the-stale-warrant-resurrection-boundary-in-federated-agent-identity.warrant.json
# Markdown SHA-256:   9349901a64b0c5c5081117b4a2f2c56d33a842b0f99164c89d1e19c1094299dd
# Code AST SHA-256:   312c7f135bf99a1dc7205cb714ab663f218bc3df15beb39e555df1e3feb9417b
# Verified Classes:   StaleEpochAdmissionError, EpochLagExceededError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class StaleEpochAdmissionError(RuntimeError):
    """Raised when a verifier would admit a warrant whose mint epoch exceeds its local revocation epoch."""
    pass


class EpochLagExceededError(RuntimeError):
    """Raised when a verifier's epoch lag exceeds the federation's max_epoch_lag bound."""
    pass


def verify_warrant(
    warrant,
    local_epoch: int,
    revocations: dict,
    max_epoch_lag: int,
    presentation_time: float,
) -> str:
    """
    Returns one of: 'ADMIT', 'DEFER', 'REJECT'.
    Never returns 'ADMIT' when local_epoch < warrant.revocation_epoch_floor.
    """
    # Invariant 1: epoch ordering. A warrant minted after our view is unverifiable.
    if local_epoch < warrant.revocation_epoch_floor:
        lag = warrant.revocation_epoch_floor - local_epoch
        if lag > max_epoch_lag:
            # Fail closed: liveness bound exceeded, escalate to operator.
            raise EpochLagExceededError(
                f"warrant {warrant.id} floor={warrant.revocation_epoch_floor} "
                f"local_epoch={local_epoch} lag={lag} > max_epoch_lag={max_epoch_lag}"
            )
        # Within bound: defer, do not admit on a stale view.
        return "DEFER"

    # Invariant 2: revocation-effective-time binding.
    for rev in revocations.get(warrant.scope, []):
        if rev.effective_time <= presentation_time and rev.epoch <= local_epoch:
            return "REJECT"

    # Invariant 3: refuse to admit if the warrant's own epoch floor is unbound.
    if warrant.revocation_epoch_floor is None:
        raise StaleEpochAdmissionError(
            f"warrant {warrant.id} has no revocation_epoch_floor; "
            f"cannot establish epoch ordering against local_epoch={local_epoch}"
        )

    return "ADMIT"


def replay_handoff(handoff_log_entry, auditor_epoch: int) -> str:
    """
    Replay must use the verifier's attested execution epoch, not the auditor's.
    """
    attested = handoff_log_entry.epoch_attestation
    if attested is None:
        raise StaleEpochAdmissionError(
            f"handoff {handoff_log_entry.id} lacks epoch_attestation; "
            f"auditor cannot adjudicate without execution-time epoch"
        )
    if attested.epoch > auditor_epoch:
        # Auditor is staler than execution; its verdict would be unsound.
        raise EpochLagExceededError(
            f"auditor_epoch={auditor_epoch} < execution_epoch={attested.epoch}; "
            f"replay would produce a false Byzantine flag"
        )
    return verify_warrant(
        warrant=handoff_log_entry.warrant,
        local_epoch=attested.epoch,
        revocations=handoff_log_entry.revocations_at_execution,
        max_epoch_lag=handoff_log_entry.max_epoch_lag,
        presentation_time=handoff_log_entry.presentation_time,
    )
