# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-agent-card-securityschemes-before-issuing-a-task-and-why-the-a2a-v1-0-push-notification-contract-silently-voids-the-t.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-agent-card-securityschemes-before-issuing-a-task-and-why-the-a2a-v1-0-push-notification-contract-silently-voids-the-t.warrant.json
# Markdown SHA-256:   5b048a88bc4203a9dc00856cc08bc296897a92e00b4b402ac3b4f6ce15e15117
# Code AST SHA-256:   6ce78e56c7e61eddf79b844ea6453017aa8a78b4332f36d26b6b170f52dffb1e
# Verified Classes:   PushAuthReconciliationError, TaskState, TaskResponse
# Defensive Guards:   2 fail-closed invariants (zero external deps)

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


class PushAuthReconciliationError(RuntimeError):
    """Raised when a task's push-notification auth scheme cannot be
    reconciled with the client receiver's provisioned schemes before the
    task leaves the `submitted` state. Proceeding past this point yields a
    silent task void: server-side completion with zero delivered webhooks."""


class TaskState(Enum):
    SUBMITTED = "submitted"
    WORKING = "working"
    COMPLETED = "completed"
    CANCELED = "canceled"


@dataclass
class TaskResponse:
    task_id: str
    state: TaskState
    push_scheme: Optional[str]      # task-scoped, authoritative
    push_overridable: bool          # does the remote honor auth overrides?


def accept_task(
    task: TaskResponse,
    receiver_provisioned_schemes: set[str],
    card_advertised_schemes: set[str],
) -> TaskState:
    # Card-scoped schemes are advisory only; never trust them as the binding.
    if task.push_scheme is None:
        # No webhook registered for this task; polling is the only channel.
        return TaskState.SUBMITTED

    if task.push_scheme in receiver_provisioned_schemes:
        # Reconciled: receiver can verify the scheme the remote actually selected.
        return TaskState.SUBMITTED

    # Mismatch: the card may have advertised a scheme we support, but the task
    # selected a different one. The card is not a warrant for the task.
    if task.push_overridable:
        # Caller must resubmit with an explicit pushNotification.auth override
        # pinning a scheme in receiver_provisioned_schemes.
        raise PushAuthReconciliationError(
            f"task {task.task_id}: remote selected push scheme "
            f"{task.push_scheme!r}, receiver provisioned for "
            f"{sorted(receiver_provisioned_schemes)}; card advertised "
            f"{sorted(card_advertised_schemes)} (advisory, not binding). "
            f"Resubmit with explicit auth override or downgrade to polling."
        )

    # Not overridable: the only safe path is to cancel and poll, never to
    # accept a webhook the receiver cannot verify.
    raise PushAuthReconciliationError(
        f"task {task.task_id}: remote selected push scheme "
        f"{task.push_scheme!r} with no override support; receiver cannot "
        f"verify. Cancel and resubmit with pushNotifications disabled, "
        f"then poll tasks/get for completion. Accepting this task would "
        f"void its notifications silently."
    )
