# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a2a-v1-0-agent-card-discovery-as-authoritative-only-for-the-capability-surface-and-never-for-task-lifecycle-s.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a2a-v1-0-agent-card-discovery-as-authoritative-only-for-the-capability-surface-and-never-for-task-lifecycle-s.warrant.json
# Markdown SHA-256:   dadeeb275b8af88ba326cfd55668acf610b3c5face68b605f860cd10703db048
# Code AST SHA-256:   4a334c7fbcc2a8eb06a14ce1aed82854f88a04381a8051022089d881f82266da
# Verified Classes:   CardTaskContractConflationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CardTaskContractConflationError(RuntimeError):
    """Raised when a client attempts to validate in-flight task state
    against a card version other than the one negotiated at tasks/send."""
    pass


def resume_task(task: dict, current_card: dict, negotiated_etag: str) -> dict:
    """
    Resume an A2A v1.0 task. The card is authoritative ONLY for new
    negotiations, never for validating artifacts already produced.
    """
    if not negotiated_etag:
        raise CardTaskContractConflationError(
            "No negotiation etag persisted for task "
            f"{task.get('id')!r}; refusing to re-negotiate mid-flight."
        )

    # Card changes affect transport, not task validity.
    if not current_card.get("capabilities", {}).get("streaming", False):
        task["_transport"] = "poll"  # fallback, do NOT discard artifacts
    else:
        task["_transport"] = "sse"

    # Skill removal from the card must not invalidate an accepted task.
    card_skills = {s.get("id") for s in current_card.get("skills", [])}
    if task.get("skill") and task["skill"] not in card_skills:
        # Block only NEW submissions; existing task proceeds.
        task["_block_new_submissions_for"] = task["skill"]

    # Artifacts were produced under negotiated outputModes; never re-validate.
    if task.get("artifacts") and task.get("_validated_against_etag") != negotiated_etag:
        task["_validated_against_etag"] = negotiated_etag

    state = task.get("status", {}).get("state")
    if state in ("completed", "failed", "canceled"):
        return task  # terminal: card is irrelevant

    if state in ("working", "input-required"):
        return task  # continue under original contract

    raise CardTaskContractConflationError(
        f"Task {task.get('id')!r} in unexpected state {state!r}; "
        "cannot resume without explicit peer TaskNotFound."
    )
