# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-of-a2a-v1-0-resolve-an-agent-card-s-skills-inputmodes-before-or-after-task-creation-and-why-the-answer-changes-the-push-notific.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-of-a2a-v1-0-resolve-an-agent-card-s-skills-inputmodes-before-or-after-task-creation-and-why-the-answer-changes-the-push-notific.warrant.json
# Markdown SHA-256:   23de421f37f94c754581916bc4e83c9d21820e85de55976d4ff074426b0a6967
# Code AST SHA-256:   1bd7921f41ed87143ce07decc2d6f16b846d19f201db0785e06554f5b3ceab92
# Verified Classes:   UnresolvableTaskLifecycleError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class UnresolvableTaskLifecycleError(RuntimeError):
    """Raised when a task was accepted but can never reach a terminal state
    because skill-level inputModes were resolved after task.id allocation."""
    pass


def resolve_input_modes(card: dict, skill_id: str) -> frozenset[str]:
    defaults = set(card.get("defaultInputModes", []))
    for skill in card.get("skills", []):
        if skill.get("id") == skill_id:
            override = skill.get("inputModes")
            return frozenset(override) if override is not None else frozenset(defaults)
    raise KeyError(f"skill {skill_id!r} not present in agent card")


def accept_task(card: dict, skill_id: str, parts: list[dict], push_config: dict | None) -> str:
    # Acceptance-time resolution: this MUST run before task.id is minted.
    allowed = resolve_input_modes(card, skill_id)
    for part in parts:
        mode = part.get("mimeType") or part.get("mode")
        if mode not in allowed:
            # Loud rejection at the call site; no task.id, no webhook.
            raise UnresolvableTaskLifecycleError(
                f"skill {skill_id!r} accepts {sorted(allowed)}, got {mode!r}; "
                f"refusing to allocate task.id and bind push notification"
            )
    task_id = mint_task_id()
    if push_config is not None:
        bind_push_notification(task_id, push_config)
    return task_id


def bind_push_notification(task_id: str, push_config: dict) -> None:
    # Binding is only legal after acceptance has proven the mode invariant.
    if task_id is None:
        raise UnresolvableTaskLifecycleError("cannot bind push notification without an accepted task.id")
    _PUSH_STORE[task_id] = push_config


def emit_terminal(task_id: str, state: str) -> None:
    # Executor must NOT re-resolve modes as a gate here; doing so strands the webhook.
    if state not in ("completed", "failed", "canceled"):
        raise UnresolvableTaskLifecycleError(f"non-terminal state {state!r} passed to emit_terminal")
    cfg = _PUSH_STORE.pop(task_id, None)
    if cfg is not None:
        deliver(cfg["url"], {"taskId": task_id, "state": state})


_PUSH_STORE: dict[str, dict] = {}


def mint_task_id() -> str:
    import uuid
    return str(uuid.uuid4())


def deliver(url: str, payload: dict) -> None:
    pass
