# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-of-an-a2a-v1-0-client-silently-lose-task-authority-when-a-peer-rotates-its-agent-card-mid-task.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-of-an-a2a-v1-0-client-silently-lose-task-authority-when-a-peer-rotates-its-agent-card-mid-task.warrant.json
# Markdown SHA-256:   5e8438f077caed7f2b897f99c152d6c1ba32c0fe241e584689bdd2113a6b06d7
# Code AST SHA-256:   ddece167decf9397d7aaf80ecbcbef8c960e34618299a791ee69342c29587dac
# Verified Classes:   CardVersionMismatch
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CardVersionMismatch(RuntimeError):
    """Raised when a task continuation is validated against a card version
    the client did not present, or the server's echoed version diverges
    from the client's pinned version mid-task."""
    pass


def continue_task(client_card_version: str,
                  server_card_version: str,
                  task_id: str,
                  held_schema_version: str,
                  server_schema_version: str) -> dict:
    """Issue a task continuation only if card and schema versions agree.

    Guards the A2A v1.0 boundary where a peer rotates its Agent Card
    between tasks/send and tasks/get, leaving the client's pinned
    schema stale relative to the server's validation contract.
    """
    if client_card_version != server_card_version:
        raise CardVersionMismatch(
            f"task {task_id}: client pinned card {client_card_version!r} "
            f"but server is validating against {server_card_version!r}; "
            f"continuation would be checked against a schema the client "
            f"does not hold. Re-fetch card and reconcile before retry."
        )

    if held_schema_version != server_schema_version:
        raise CardVersionMismatch(
            f"task {task_id}: held schema {held_schema_version!r} != "
            f"server schema {server_schema_version!r} under matching card "
            f"{server_card_version!r}; artifact decode would silently "
            f"corrupt. Migrate held context to {server_schema_version!r}."
        )

    return {"task_id": task_id, "card_version": server_card_version,
            "schema_version": server_schema_version, "action": "proceed"}
