# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-cached-aaif-trust-bundle-after-a-peer-revocation-propagates-through-an-offline-verification-window.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-cached-aaif-trust-bundle-after-a-peer-revocation-propagates-through-an-offline-verification-window.warrant.json
# Markdown SHA-256:   17b138d9dfcefd66154e45c268d6fd2e1023662fc5363802e234d37e2217e26a
# Code AST SHA-256:   8d353106c6890e24b188c99ad442967f536320b18473956966fb02df3a01b697
# Verified Classes:   RevocationAssertionError, Verdict, TrustBundle
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class RevocationAssertionError(RuntimeError):
    """Raised when an offline verifier attempts to assert non-revocation
    beyond the issuer-declared offline assertion window."""


class Verdict(Enum):
    VALID = "valid"
    INDETERMINATE = "indeterminate"
    REVOKED = "revoked"


@dataclass(frozen=True)
class TrustBundle:
    epoch: int
    fetched_at_monotonic: float
    not_after_monotonic: float
    offline_assertion_ttl: float
    revoked_warrant_ids: frozenset[str]


def verify_offline(
    bundle: TrustBundle,
    warrant_id: str,
    warrant_not_after_monotonic: float,
    now_monotonic: float,
) -> Verdict:
    if warrant_id in bundle.revoked_warrant_ids:
        return Verdict.REVOKED

    # Clamp: the bundle may not assert beyond the issuer's declared tolerance.
    assertion_deadline = min(
        bundle.not_after_monotonic,
        bundle.fetched_at_monotonic + bundle.offline_assertion_ttl,
    )

    if now_monotonic > assertion_deadline:
        # Outside the window: signature may still be cryptographically valid,
        # but we cannot assert absence of revocation. Must NOT return VALID.
        return Verdict.INDETERMINATE

    if warrant_not_after_monotonic <= assertion_deadline:
        # Peer warrant could outlive our assertion window; refuse to vouch.
        raise RevocationAssertionError(
            f"warrant {warrant_id} expires at {warrant_not_after_monotonic} "
            f"which is not strictly after assertion deadline {assertion_deadline}; "
            f"offline non-revocation cannot be soundly asserted"
        )

    return Verdict.VALID


def replay_on_heal(
    prior_verdicts: list[tuple[str, float, Verdict]],
    healed_bundle: TrustBundle,
) -> list[tuple[str, Verdict]]:
    """Re-verify handoffs made during partition against the healed bundle."""
    results = []
    for warrant_id, _ts, _old in prior_verdicts:
        if warrant_id in healed_bundle.revoked_warrant_ids:
            results.append((warrant_id, Verdict.REVOKED))
        else:
            results.append((warrant_id, Verdict.VALID))
    return results
