# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-delegated-agent-credentials-from-outliving-their-revocation-epoch-under-aaif-chained-delegation.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-delegated-agent-credentials-from-outliving-their-revocation-epoch-under-aaif-chained-delegation.warrant.json
# Markdown SHA-256:   0b330e7539e9dc4f76e4da6e907d145cf04b2cf91fea992ef9ad07807fa24d41
# Code AST SHA-256:   2588971e1df6848ab732119dd239e017d36b836e5dffe770d464587ee6d2b927
# Verified Classes:   RevocationEpochViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevocationEpochViolation(RuntimeError):
    """Raised when a delegated warrant's parent epoch is stale relative to the live root epoch."""
    pass


def verify_warrant_at_call(
    warrant: dict,
    live_root_epoch: int,
    revocation_subscription_seq: int,
) -> None:
    """
    Enforce epoch monotonicity across the delegation chain at every tool call.

    warrant keys:
      - parent_epoch: revocation epoch of the immediate parent at issuance
      - recorded_epoch: epoch the issuer believed current when signing
      - chain: list of ancestor warrant ids (for audit)
      - not_after: absolute expiry
    """
    if warrant["parent_epoch"] < live_root_epoch:
        raise RevocationEpochViolation(
            f"warrant {warrant.get('id')} carries parent_epoch="
            f"{warrant['parent_epoch']} but live root epoch is {live_root_epoch}; "
            f"ancestor revoked, chain={warrant.get('chain')}"
        )

    if revocation_subscription_seq < warrant["parent_epoch"]:
        raise RevocationEpochViolation(
            f"revocation subscription lagging: seq={revocation_subscription_seq} "
            f"< required parent_epoch={warrant['parent_epoch']}; "
            f"cannot prove ancestor still authorized"
        )

    # Signature and expiry checks omitted; they are necessary but not sufficient.
    if warrant["parent_epoch"] != warrant["recorded_epoch"]:
        raise RevocationEpochViolation(
            f"epoch drift between parent ({warrant['parent_epoch']}) and "
            f"recorded ({warrant['recorded_epoch']}); issuance raced a revocation"
        )
