# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-an-a2a-v1-0-agent-card-whose-capabilities-statetransitionhistory-is-false-but-whose-advertised-taskstate.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-an-a2a-v1-0-agent-card-whose-capabilities-statetransitionhistory-is-false-but-whose-advertised-taskstate.warrant.json
# Markdown SHA-256:   5d7228a55a53ef2eafa2ff77fa728c61820b1bbe865adaa66cb109d858fd5692
# Code AST SHA-256:   fccf96c4b2b97a3b92aeb4bfe6d95b85f2e3b7bd285824421de6238e142d758f
# Verified Classes:   NonResumableParkError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class NonResumableParkError(RuntimeError):
    """Raised when a client attempts to checkpoint a resumable task handle
    against an A2A v1.0 Agent Card that does not guarantee transition history."""
    pass


def checkpoint_parked_task(card: dict, task: dict, local_store: dict) -> None:
    """
    Called when a remote A2A agent returns a Task in state 'input-required'.
    Enforces the continuity invariant: a bare taskId checkpoint is only valid
    when the card advertises stateTransitionHistory == true.
    """
    caps = card.get("capabilities", {})
    history = bool(caps.get("stateTransitionHistory", False))
    state = task.get("status", {}).get("state")
    task_id = task.get("id")

    if state != "input-required":
        # Not a park point; nothing to checkpoint here.
        return

    if not history:
        # Non-history card: the taskId is a live session token, not a durable
        # handle. We may only persist the FULL task payload and mark it
        # non-resumable, forcing a fresh message/send on any later resume.
        if "status" not in task or "artifacts" not in task:
            raise NonResumableParkError(
                f"Card for task {task_id!r} has stateTransitionHistory=false; "
                "cannot checkpoint a bare taskId, and the inline Task payload "
                "is incomplete. Refusing to write a resumable record."
            )
        local_store[task_id] = {
            "resumable": False,
            "full_task": task,          # serialize everything the remote gave us
            "resume_via": "message/send",  # fresh send, new taskId expected
        }
        return

    # History-enabled card: a durable handle is legitimate.
    local_store[task_id] = {
        "resumable": True,
        "full_task": task,
        "resume_via": "tasks/get",
    }


def resume_task(card: dict, task_id: str, local_store: dict) -> dict:
    """
    Resume path. Refuses to call tasks/get on a non-history card, because the
    remote has no durable record and the call would return TaskNotFoundError
    after the client has already advanced its local state machine.
    """
    record = local_store.get(task_id)
    if record is None:
        raise NonResumableParkError(f"No checkpoint for task {task_id!r}")

    if not record["resumable"]:
        raise NonResumableParkError(
            f"Task {task_id!r} was parked against a card with "
            "stateTransitionHistory=false. tasks/get is invalid; "
            "re-issue message/send with accumulated context instead."
        )

    # Only reached when the card promised durable transition history.
    return record["full_task"]
