# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-credentials-from-resurrecting-across-federated-trust-graphs.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-revoked-agent-credentials-from-resurrecting-across-federated-trust-graphs.warrant.json
# Markdown SHA-256:   84c587f8e0175e1bffd968185520accdb50f5341aedd72a5e5e291bf9d006580
# Code AST SHA-256:   b324facfec13e8d5950b874229239aebbe0421d38c3d93e422026cae89e01640
# Verified Classes:   RevocationEpochViolation, VerifierDecisionCache
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class RevocationEpochViolation(RuntimeError):
    """Raised when a warrant's epoch is older than the verifier's known-highest
    epoch for its issuer, indicating a pre-revocation warrant replay or a
    stale decision-cache hit that must not be served."""
    pass


class VerifierDecisionCache:
    def __init__(self):
        # (agent_id, warrant_hash) -> (epoch, decision)
        self._cache = {}
        # issuer_id -> highest epoch observed (from revocations or warrants)
        self._highest_epoch = {}

    def observe_revocation(self, issuer_id: str, epoch: int, warrant_hash: str) -> None:
        # Revocation arrival MUST evict any cached decision for that warrant,
        # regardless of TTL. This is the cache-invalidation-coupling invariant.
        self._highest_epoch[issuer_id] = max(
            self._highest_epoch.get(issuer_id, 0), epoch
        )
        for key in [k for k in self._cache if k[1] == warrant_hash]:
            del self._cache[key]

    def authorize(self, issuer_id: str, agent_id: str, warrant_hash: str, epoch: int) -> bool:
        highest = self._highest_epoch.get(issuer_id, 0)
        if epoch < highest:
            # Pre-revocation warrant replay, or a cache entry that should have
            # been evicted. Hard reject; do not fall through to cache.
            raise RevocationEpochViolation(
                f"warrant {warrant_hash} epoch {epoch} < issuer {issuer_id} "
                f"highest epoch {highest}; refusing to serve stale acceptance"
            )
        key = (agent_id, warrant_hash)
        cached = self._cache.get(key)
        if cached is not None and cached[0] == epoch:
            return cached[1]
        decision = self._evaluate(issuer_id, agent_id, warrant_hash, epoch)
        self._cache[key] = (epoch, decision)
        return decision

    def _evaluate(self, issuer_id, agent_id, warrant_hash, epoch) -> bool:
        # Placeholder for signature + capability check against current feed.
        return True
