# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-a2a-v1-0-agent-card-drift-from-silently-desynchronizing-the-task-lifecycle-state-machine-during-long-lived-delegation.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-a2a-v1-0-agent-card-drift-from-silently-desynchronizing-the-task-lifecycle-state-machine-during-long-lived-delegation.warrant.json
# Markdown SHA-256:   89caad2d4e8501e1997ec9506ea3a54ba316d6eee6cbebfdc0adbf8cb3d9665f
# Code AST SHA-256:   cbb198b45cc6952f4a8aa57f68c68f988762b9b1d2fd0c2ef92470ddecabd3c7
# Verified Classes:   AgentCardDriftError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class AgentCardDriftError(RuntimeError):
    """Raised when a cached Agent Card no longer matches the live server card
    at a lifecycle transition that depends on card-declared schema."""
    pass


def respond_to_input_required(
    cached_card: dict,
    live_card: dict,
    task_id: str,
    payload: dict,
) -> dict:
    # Fields whose drift changes how we must interpret the payload.
    schema_fields = ("inputModes", "skills", "version")

    drifted = [
        f for f in schema_fields
        if cached_card.get(f) != live_card.get(f)
    ]
    if drifted:
        raise AgentCardDriftError(
            f"task {task_id}: cached Agent Card drifted on {drifted}; "
            f"refusing to interpret input-required payload against stale schema"
        )

    # Only now is it sound to parse the payload against the cached schema.
    clarification = payload.get("clarification")
    if clarification is None:
        raise AgentCardDriftError(
            f"task {task_id}: payload missing declared 'clarification' field "
            f"despite version match; treat as unrecoverable schema violation"
        )
    return {"clarification": clarification}
