# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-re-advertised-agent-card-as-a-lifecycle-reset-rather-than-a-capability-refresh.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-re-advertised-agent-card-as-a-lifecycle-reset-rather-than-a-capability-refresh.warrant.json
# Markdown SHA-256:   dd98ad118a679acbb91dc82fb7f50a47d56507e09fa7b74f9b0e528752300d10
# Code AST SHA-256:   18fd990b7a0f792c5199a4ae4556190a3e409641a2359362410313f7114f88c1
# Verified Classes:   CardTransitionError, Transition, CardView
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class CardTransitionError(RuntimeError):
    """Raised when a card re-advertisement cannot be safely classified."""


class Transition(Enum):
    REFRESH = "refresh"
    RESET = "reset"
    AMBIGUOUS = "ambiguous"


@dataclass(frozen=True)
class CardView:
    push_auth_schemes: frozenset[str]
    skill_input_modes: dict[str, frozenset[str]]
    incarnation_nonce: Optional[str]


BINDING_GATING_FIELDS = ("push_auth_schemes", "skill_input_modes", "incarnation_nonce")


def classify_transition(old: CardView, new: CardView, live_bindings: bool) -> Transition:
    if not live_bindings:
        return Transition.REFRESH

    gating_changed = any(
        getattr(old, f) != getattr(new, f) for f in BINDING_GATING_FIELDS
    )
    if not gating_changed:
        return Transition.REFRESH

    # A strictly-changed incarnation nonce is positive proof of a new entity.
    if old.incarnation_nonce is not None and new.incarnation_nonce is not None:
        if old.incarnation_nonce != new.incarnation_nonce:
            return Transition.RESET
        # Same nonce but gating fields changed: the card is internally
        # inconsistent and must not be trusted in either direction.
        return Transition.AMBIGUOUS

    # No usable nonce: we cannot prove continuity, so we cannot claim it.
    return Transition.AMBIGUOUS


def resolve_ambiguous(probe_result: str) -> Transition:
    if probe_result == "task_found":
        return Transition.REFRESH
    if probe_result == "task_not_found_typed":
        return Transition.RESET
    raise CardTransitionError(
        f"incarnation probe returned '{probe_result}'; "
        "cannot classify card transition, blocking new dispatch"
    )


def apply_transition(
    transition: Transition,
    orphaned_task_ids: list[str],
    webhook_targets: list[str],
) -> dict:
    if transition is Transition.AMBIGUOUS:
        raise CardTransitionError(
            "refusing to apply AMBIGUOUS transition: "
            f"{len(orphaned_task_ids)} tasks and {len(webhook_targets)} "
            "webhooks remain bound to an unverified incarnation"
        )
    if transition is Transition.RESET:
        return {
            "orphaned": list(orphaned_task_ids),
            "reregister_webhooks": list(webhook_targets),
            "revalidate_input_modes": True,
        }
    return {"orphaned": [], "reregister_webhooks": [], "revalidate_input_modes": False}
