# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-and-survive-a-trust-anchor-revocation-race-during-cross-org-capability-negotiation.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-and-survive-a-trust-anchor-revocation-race-during-cross-org-capability-negotiation.warrant.json
# Markdown SHA-256:   5843b2831e8d3f6fdd4fc57ecb0be8a2894f445455a574086ceace0bc54819f7
# Code AST SHA-256:   9c2ae69a6c19b3c004dfafa378d9b301ed08ac21f9a55cbe5ad0d6c77622e508
# Verified Classes:   StaleAuthorityError, AnchorUnconfirmableError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class StaleAuthorityError(RuntimeError):
    """Raised when a warrant's legitimizing anchor is no longer confirmable."""
    pass


class AnchorUnconfirmableError(RuntimeError):
    """Raised when revocation state cannot be resolved within the safety window."""
    pass


def verify_warrant_for_execution(warrant, anchor_resolver, now, hot_ttl, last_anchor_check):
    # Condition 3: warrant must bind to a root fingerprint.
    if not getattr(warrant, "root_fingerprint", None):
        raise StaleAuthorityError(
            f"warrant {warrant.id} carries no anchor binding; cannot confirm authority"
        )

    remaining = warrant.exp - now
    anchor_age = now - last_anchor_check

    # Condition 1: if the warrant can outlive the anchor cache, force re-resolution.
    if remaining <= hot_ttl or anchor_age >= hot_ttl:
        try:
            current_chain = anchor_resolver.resolve(warrant.issuer_id)
        except Exception as exc:
            # Fail closed: cannot confirm revocation state.
            raise AnchorUnconfirmableError(
                f"revocation channel unreachable for {warrant.issuer_id}: {exc}"
            ) from exc

        if current_chain.root_fingerprint != warrant.root_fingerprint:
            # Re-anchoring event: warrant is void, peer is not.
            raise StaleAuthorityError(
                f"warrant {warrant.id} bound to {warrant.root_fingerprint}, "
                f"current root is {current_chain.root_fingerprint}; renegotiate"
            )

        if current_chain.revoked:
            raise StaleAuthorityError(
                f"anchor {current_chain.root_fingerprint} revoked at {current_chain.revoked_at}"
            )

    # Signature check is necessary but not sufficient; it runs last.
    if not warrant.verify_signature():
        raise StaleAuthorityError(f"warrant {warrant.id} signature invalid")

    return True
