# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-signed-revocation-as-unverifiable-rather-than-valid-and-halt-dependent-tool-calls-across-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-signed-revocation-as-unverifiable-rather-than-valid-and-halt-dependent-tool-calls-across-trust-domains.warrant.json
# Markdown SHA-256:   7e07c71a25b67bd4aed8490192a3af5865002954dffe28c569e3cb22074408f8
# Code AST SHA-256:   834e0ad1c42c1c459ebdbdda7251f699581df11897e2253573fb114dbafe7d51
# Verified Classes:   RevocationIndeterminateError, RevocationVerifier
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class RevocationIndeterminateError(RuntimeError):
    """Raised when a revocation is well-formed but its signing key epoch
    cannot be anchored to the pinned root chain. Must fail CLOSED: the
    target warrant is treated as revoked until epoch resolution completes
    or the resolution timeout expires."""
    pass


class RevocationVerifier:
    def __init__(self, pinned_root, trust_bundle, epoch_resolver, timeout_s=30.0):
        self.pinned_root = pinned_root
        self.trust_bundle = trust_bundle      # {domain: {epoch: pubkey}}
        self.epoch_resolver = epoch_resolver  # fetches rotation attestations
        self.timeout_s = timeout_s

    def classify(self, revocation):
        domain = revocation.issuing_domain
        epoch = revocation.key_epoch
        known = self.trust_bundle.get(domain, {})

        if epoch in known:
            if self._verify(revocation, known[epoch]):
                return "VALID"
            # key present, signature bad -> affirmatively forged
            return "INVALID_DROP"

        if not known:
            # never seen this domain's key at all: cannot anchor provenance
            return "INDETERMINATE"

        # domain known, but this epoch not yet anchored -> fast path
        return "INDETERMINATE"

    def enforce(self, revocation, warrant_id):
        verdict = self.classify(revocation)

        if verdict == "VALID":
            return self._mark_revoked(warrant_id)

        if verdict == "INVALID_DROP":
            # forged revocation: do NOT halt, flag the peer
            self._flag_peer(revocation.origin_peer)
            return "ignored_forged"

        # INDETERMINATE: fail closed, attempt resolution, bound the wait
        self._halt_warrant(warrant_id)  # block dependent tool calls NOW
        try:
            attestation = self.epoch_resolver.fetch(
                revocation.issuing_domain, revocation.key_epoch,
                timeout=self.timeout_s,
            )
            if not self._chains_to_root(attestation):
                raise RevocationIndeterminateError(
                    f"rotation attestation for {revocation.issuing_domain}"
                    f" epoch {revocation.key_epoch} does not chain to pinned root"
                )
            self.trust_bundle.setdefault(revocation.issuing_domain, {})[
                revocation.key_epoch
            ] = attestation.public_key
            # re-verify against newly anchored key
            if self._verify(revocation, attestation.public_key):
                return self._mark_revoked(warrant_id)
            raise RevocationIndeterminateError(
                "revocation failed re-verification after epoch resolution"
            )
        except TimeoutError as e:
            raise RevocationIndeterminateError(
                f"epoch resolution for {revocation.issuing_domain} timed out;"
                f" warrant {warrant_id} remains halted pending auditor escalation"
            ) from e
