# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-re-verify-a-peer-s-delegated-capability-after-did-document-rotation-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-re-verify-a-peer-s-delegated-capability-after-did-document-rotation-mid-handoff.warrant.json
# Markdown SHA-256:   beda71faf2fa7608f4fb9379e1bcaf408fe9f8a5cdaba2471fb5c8cc3368abfa
# Code AST SHA-256:   9bb4da97acad76ced6185f07e0e3f85809c1f607ed24183811655ebf5997c33f
# Verified Classes:   BindingRotatedError, DidDocument, AdmissionRecord
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class BindingRotatedError(RuntimeError):
    """Raised when a capability's DID binding is no longer authoritative
    for the action being attempted, even though the credential itself
    has not expired and no capability-level revocation was published."""
    pass


@dataclass(frozen=True)
class DidDocument:
    did: str
    version_id: str
    verification_method_ids: frozenset
    deactivated: bool


@dataclass(frozen=True)
class AdmissionRecord:
    credential_id: str
    issuer_did: str
    version_id: str
    verification_method_id: str
    admitted_at: float


def resolve_did(did: str) -> DidDocument:
    # Stand-in for a universal resolver / trust-registry lookup.
    # In production this hits the authoritative source each call.
    raise NotImplementedError("wire to resolver")


def admit_capability(credential, doc: DidDocument) -> AdmissionRecord:
    if doc.deactivated:
        raise BindingRotatedError(
            f"issuer DID {doc.did} deactivated at admission"
        )
    vm = next(iter(doc.verification_method_ids))
    # ... verify credential proof against vm ...
    return AdmissionRecord(
        credential_id=credential["id"],
        issuer_did=doc.did,
        version_id=doc.version_id,
        verification_method_id=vm,
        admitted_at=time.time(),
    )


def authorize_consequential_action(record: AdmissionRecord, action: str) -> None:
    """Re-check that the admitted binding is still authoritative
    immediately before an externally-visible action."""
    current = resolve_did(record.issuer_did)

    if current.deactivated:
        raise BindingRotatedError(
            f"issuer DID {record.issuer_did} deactivated; "
            f"capability {record.credential_id} void for action {action!r}"
        )

    if current.version_id != record.version_id:
        # Document rotated since admission. The admitted verification
        # method may or may not survive the rotation.
        if record.verification_method_id not in current.verification_method_ids:
            raise BindingRotatedError(
                f"verification method {record.verification_method_id} "
                f"removed from {record.issuer_did} (was version "
                f"{record.version_id}, now {current.version_id}); "
                f"refusing action {action!r} under severed warrant"
            )
        # Method survived rotation; re-anchor the record to the new version.
        object.__setattr__(record, "version_id", current.version_id)

    # Binding is authoritative; proceed with the action.
