# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-agent-credential-is-still-being-trusted-across-a-rotated-issuer-anchor.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-revoked-agent-credential-is-still-being-trusted-across-a-rotated-issuer-anchor.warrant.json
# Markdown SHA-256:   2c7d15b5ff50bd054e4291c5a9577c1fa3045fafba823528b1a6cf5d703da6e2
# Code AST SHA-256:   e5ca4e3038e133073082af0e190a5d594c2c8e50ea77a118e05b05eda3578ae6
# Verified Classes:   StaleDenySetAfterRotation, TrustSnapshot
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class StaleDenySetAfterRotation(RuntimeError):
    """Raised when a verifier would honor a cached warrant whose deny-set

    epoch is older than the anchor epoch it was re-verified against.

    This is the exact condition under which anchor rotation silently

    re-validates a revoked subject.
    """


@dataclass(frozen=True)
class TrustSnapshot:
    anchor_epoch: int
    deny_set_epoch: int
    last_verify: str


def verify_cached_warrant(snap: TrustSnapshot, subject_revoked: bool) -> bool:
    # Invariant: the deny-set consulted must be at least as new as the
    # anchor under which the warrant is being re-verified. Rotation raises
    # the floor; it must never reset it.
    if snap.deny_set_epoch < snap.anchor_epoch:
        raise StaleDenySetAfterRotation(
            f"anchor_epoch={snap.anchor_epoch} advanced past "
            f"deny_set_epoch={snap.deny_set_epoch}; cached chain for "
            f"revoked={subject_revoked} cannot be trusted until deny-set "
            f"is re-fetched to >= anchor_epoch"
        )
    if subject_revoked:
        # Only reachable when deny-set is current enough to know it.
        return False
    return snap.last_verify == "PASS"


def gateway_accepts(snap: TrustSnapshot, subject_revoked: bool) -> bool:
    try:
        return verify_cached_warrant(snap, subject_revoked)
    except StaleDenySetAfterRotation:
        # Fail closed: reject rather than serve stale-green.
        return False


# The observed failure trace:
#   anchor_epoch=8, deny_set_epoch=41, last_verify=PASS, subject_revoked=True
# gateway_accepts() returns False and the raise fires inside verify_cached_warrant,
# surfacing the divergence instead of logging a green.
