# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-federated-tool-warrant-is-still-being-honored-by-peer-agents.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-federated-tool-warrant-is-still-being-honored-by-peer-agents.warrant.json
# Markdown SHA-256:   bc67bf83370cf7aa361bdd01ffaa8d94aae01f7c36d49641cefbca04129f32c5
# Code AST SHA-256:   7371d84f09a293b5ca9665abde708c3373d2f3532ad05d23dfe4997bdd212d06
# Verified Classes:   StaleWarrantEpochError, UnprovableRevocationViewError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class StaleWarrantEpochError(RuntimeError):
    """Raised when a peer cannot prove its revocation view is at least as new
    as the epoch committed into the warrant it is being asked to honor."""
    pass


class UnprovableRevocationViewError(RuntimeError):
    """Raised when the warrant or the revocation list lacks a signed epoch,
    making staleness undetectable. Fail closed."""
    pass


def accept_warrant(warrant, revocation_list, now):
    # Provenance: signature must verify against issuer key.
    if not verify_sig(warrant.payload, warrant.signature, warrant.issuer_pubkey):
        raise StaleWarrantEpochError("warrant signature invalid")

    # Epoch commitment must exist on both sides.
    if warrant.issuer_revocation_epoch is None or revocation_list.signed_epoch is None:
        raise UnprovableRevocationViewError(
            f"cannot bound staleness: warrant_epoch={warrant.issuer_revocation_epoch} "
            f"list_epoch={revocation_list.signed_epoch}"
        )

    # Revocation list must itself be authentically signed.
    if not verify_sig(
        revocation_list.payload,
        revocation_list.signature,
        revocation_list.revocation_authority_pubkey,
    ):
        raise StaleWarrantEpochError("revocation list signature invalid")

    # The core invariant: observed epoch must be >= committed epoch.
    if revocation_list.signed_epoch < warrant.issuer_revocation_epoch:
        raise StaleWarrantEpochError(
            f"stale revocation view: observed={revocation_list.signed_epoch} "
            f"committed={warrant.issuer_revocation_epoch}; refuse and re-fetch"
        )

    # Membership must be keyed on warrant_id, not skill_id.
    entry = revocation_list.by_warrant_id.get(warrant.warrant_id)
    if entry is not None and entry.revoked_at <= now <= warrant.not_after:
        raise StaleWarrantEpochError(
            f"warrant {warrant.warrant_id} revoked at {entry.revoked_at}, "
            f"still inside live interval at {now}"
        )

    if not (warrant.issued_at <= now <= warrant.not_after):
        raise StaleWarrantEpochError("warrant outside validity interval")

    return True
