# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-rotated-aaif-signing-key-as-an-identity-break-rather-than-a-credential-refresh.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-rotated-aaif-signing-key-as-an-identity-break-rather-than-a-credential-refresh.warrant.json
# Markdown SHA-256:   2d88a734c58df13fb93b2762d08b2e453c5d682b482855ca77968d8d2a61759d
# Code AST SHA-256:   3af06ba3fc826604c422ca08b66ef2334d148590709fea60207b9cb1eabd7d45
# Verified Classes:   IdentityBreakError, Credential, KeyTransition
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class IdentityBreakError(RuntimeError):
    """Raised when a credential refresh cannot be bound to the previously
    attested key via a valid issuer key-transition chain."""


@dataclass(frozen=True)
class Credential:
    did: str
    verification_method: str          # e.g. "did:aaif:exec-3#key-2"
    capabilities: frozenset[str]
    budget: int
    issuance_date: int
    proof_valid: bool


@dataclass(frozen=True)
class KeyTransition:
    old_key: str
    new_key: str
    signed_by: str                    # must equal old_key
    timestamp: int


def classify_refresh(
    cached: Credential,
    incoming: Credential,
    transitions: list[KeyTransition],
) -> str:
    if not incoming.proof_valid:
        # A proof failure on refresh is never a transient transport condition.
        raise IdentityBreakError(
            f"proof invalid for {incoming.verification_method}; "
            "cannot classify as refresh or rotation"
        )

    if incoming.verification_method == cached.verification_method:
        if not incoming.capabilities <= cached.capabilities:
            raise IdentityBreakError(
                "capability widening under same key requires re-attestation"
            )
        return "refresh"

    # Key changed: require an issuer-signed transition from the cached key.
    chain = [
        t for t in transitions
        if t.old_key == cached.verification_method
        and t.new_key == incoming.verification_method
        and t.signed_by == t.old_key          # old key must authorize new key
    ]
    if not chain:
        raise IdentityBreakError(
            f"no issuer transition {cached.verification_method} -> "
            f"{incoming.verification_method}; treating as identity substitution"
        )
    transition = chain[0]
    if incoming.issuance_date < transition.timestamp:
        raise IdentityBreakError("backdated credential relative to transition")
    if not incoming.capabilities <= cached.capabilities:
        raise IdentityBreakError(
            "capability widening on rotation requires local re-authorization"
        )
    return "rotation"


def apply_refresh(cached: Credential, incoming: Credential,
                  transitions: list[KeyTransition]) -> Credential:
    kind = classify_refresh(cached, incoming, transitions)
    if kind == "rotation":
        # Suspend, re-authorize, then resume with bounded new scope.
        pass
    return incoming
