# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-did-attestation-boundary-failure-when-an-autonomous-agent-s-key-rotation-event-invalidates-its-own-in-flight-verifi.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-did-attestation-boundary-failure-when-an-autonomous-agent-s-key-rotation-event-invalidates-its-own-in-flight-verifi.warrant.json
# Markdown SHA-256:   218f4c3bb9ba5846cb90cc80a2e022258b3a869a3a66eda3cde99514ed4a6074
# Code AST SHA-256:   2d54f0ac59a0af5a117108bc82b9cc85d8610c82d20bc5864d0da61585460a60
# Verified Classes:   AttestationIndeterminateError, VerificationMethod, Credential
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class AttestationIndeterminateError(RuntimeError):
    """Raised when a credential's signing key is absent from the current DID
    document but may have been valid at issuance time, and the historical
    document state or issuance timestamp cannot be resolved."""
    pass


@dataclass
class VerificationMethod:
    key_id: str
    valid_from: float
    valid_until: Optional[float]  # None = still active


@dataclass
class Credential:
    issuer_did: str
    signing_key_id: str
    issuance_date: Optional[float]  # None = agent did not provide verifiable time
    payload: dict


def resolve_document_at(did: str, when: float) -> Optional[list]:
    """Fetch the signed DID-document version whose effective interval contains
    `when`. Returns None if no versioned history is available."""
    # Placeholder: real impl fetches from a signed, versioned document log.
    return None


def current_document(did: str) -> list:
    # Placeholder: real impl fetches the latest DID document.
    return []


def verify_attestation(cred: Credential) -> str:
    """Returns 'VALID', 'INVALID', or raises AttestationIndeterminateError
    for the ambiguous rotation-window case."""
    current = current_document(cred.issuer_did)
    active = {vm.key_id for vm in current if vm.valid_until is None}

    if cred.signing_key_id in active:
        return "VALID"

    # Key not currently active. Could be never-existed OR rotated-out.
    if cred.issuance_date is None:
        raise AttestationIndeterminateError(
            f"credential from {cred.issuer_did} signed by {cred.signing_key_id} "
            f"is not currently active and carries no verifiable issuance timestamp; "
            f"cannot distinguish never-valid from rotated-out."
        )

    historical = resolve_document_at(cred.issuer_did, cred.issuance_date)
    if historical is None:
        raise AttestationIndeterminateError(
            f"no signed historical DID document for {cred.issuer_did} covering "
            f"issuance_date={cred.issuance_date}; rotation window unresolvable."
        )

    for vm in historical:
        if vm.key_id == cred.signing_key_id:
            if vm.valid_from <= cred.issuance_date and (
                vm.valid_until is None or cred.issuance_date < vm.valid_until
            ):
                return "VALID"
    return "INVALID"


def execute_delegated_action(cred: Credential, action) -> None:
    status = verify_attestation(cred)
    if status == "INDETERMINATE":
        raise AttestationIndeterminateError("unreachable: verify raises instead")
    if status != "VALID":
        raise AttestationIndeterminateError(
            f"refusing irreversible action: credential status={status}"
        )
    action()
