# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-hardware-attested-identity-as-insufficient-for-cross-domain-tool-invocation-after-a-trust-anchor-rotation.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-hardware-attested-identity-as-insufficient-for-cross-domain-tool-invocation-after-a-trust-anchor-rotation.warrant.json
# Markdown SHA-256:   260cb1f672217f3cca653462de6befcc79a279aa1e299cf5f842fb9d118043a1
# Code AST SHA-256:   85b568dfd2fc1a37ac1db003055421f7a619d63735a5f90a0d1c0778a414fcde
# Verified Classes:   IdentityBoundaryError, ErrorClass, Warrant, AnchorState
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class IdentityBoundaryError(RuntimeError):
    """Raised when attestation validity is mistaken for authorization validity."""
    pass


class ErrorClass(Enum):
    EPOCH_RETIRED = "epoch_retired"
    WARRANT_REVOKED = "warrant_revoked"
    UNKNOWN_ATTESTATION_KEY = "unknown_attestation_key"
    RATE_LIMITED = "rate_limited"
    UPSTREAM_TIMEOUT = "upstream_timeout"


IDENTITY_ERRORS = {
    ErrorClass.EPOCH_RETIRED,
    ErrorClass.WARRANT_REVOKED,
    ErrorClass.UNKNOWN_ATTESTATION_KEY,
}
TRANSIENT_ERRORS = {ErrorClass.RATE_LIMITED, ErrorClass.UPSTREAM_TIMEOUT}


@dataclass(frozen=True)
class Warrant:
    epoch: str
    revocation_status: str  # "active" | "revoked"
    ttl_seconds: int


@dataclass(frozen=True)
class AnchorState:
    current_epoch: str


def authorize_tool_call(
    warrant: Warrant,
    anchor: AnchorState,
    attestation_quote_fresh: bool,
    error: ErrorClass | None = None,
) -> str:
    # Attestation validity is necessary but NOT sufficient.
    if not attestation_quote_fresh:
        raise IdentityBoundaryError(
            "attestation quote is stale; cannot establish key possession this call"
        )

    if error is not None:
        if error in IDENTITY_ERRORS:
            # The correct action is re-attest + re-enroll, never retry or reroute.
            raise IdentityBoundaryError(
                f"authorization surface rejected identity ({error.value}); "
                f"re-enroll required — do not retry or reroute"
            )
        if error in TRANSIENT_ERRORS:
            return "backoff"  # genuinely transient; do not churn warrants
        raise IdentityBoundaryError(f"unclassified error taxonomy: {error}")

    # The invariant: warrant epoch must match the anchor's current epoch.
    if warrant.epoch != anchor.current_epoch:
        raise IdentityBoundaryError(
            f"warrant epoch {warrant.epoch} != anchor current epoch "
            f"{anchor.current_epoch}; attestation valid but authorization stale"
        )
    if warrant.revocation_status != "active":
        raise IdentityBoundaryError(
            f"warrant revoked (status={warrant.revocation_status}); "
            f"hardware attestation does not override revocation"
        )
    return "proceed"
