# 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-task-whose-taskstate-transition-is-legal-per-the-sender-s-state-machine-but-whose-agentcard.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-an-a2a-v1-0-task-whose-taskstate-transition-is-legal-per-the-sender-s-state-machine-but-whose-agentcard.warrant.json
# Markdown SHA-256:   1d03b66b898a8d72d0ef11d57a325f836a1554b0f25caf8db0894c09d7ede987
# Code AST SHA-256:   22be4d64158c800c869985f68a73981fb97518948f0af75a5ac3ba177bfea9cb
# Verified Classes:   CardSnapshotDriftError, AgentCard, Task
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from typing import Optional


class CardSnapshotDriftError(RuntimeError):
    """Raised when a verifier evaluates a task against a card snapshot
    that differs from the one the task was created under, and cannot
    reconcile the two."""
    pass


@dataclass
class AgentCard:
    skills: dict            # skillId -> metadata
    security_schemes: dict
    raw: bytes

    @property
    def card_hash(self) -> str:
        return hashlib.sha256(self.raw).hexdigest()


@dataclass
class Task:
    task_id: str
    producing_skill: str
    created_card_hash: str
    metadata: dict = field(default_factory=dict)


def verify_artifact(
    task: Task,
    artifact_skill: str,
    current_card: AgentCard,
    pinned_card: Optional[AgentCard],
) -> bool:
    """Validate an artifact's producing skill against the correct snapshot.

    Returns True if valid. Raises CardSnapshotDriftError when the verifier's
    card has drifted and the pinned snapshot is unavailable — caller must
    escalate to input-required, never hard-reject the work task.
    """
    if current_card.card_hash == task.created_card_hash:
        # Same snapshot: absence of the skill is a genuine provenance failure.
        if artifact_skill not in current_card.skills:
            raise CardSnapshotDriftError(
                f"genuine provenance failure: skill {artifact_skill!r} "
                f"absent from pinned card {current_card.card_hash[:12]} "
                f"for task {task.task_id}"
            )
        return True

    # Snapshot drift: current card != creation card. Must reconcile.
    if pinned_card is None:
        raise CardSnapshotDriftError(
            f"cannot reconcile: task {task.task_id} created under "
            f"{task.created_card_hash[:12]} but verifier holds "
            f"{current_card.card_hash[:12]}; pinned snapshot unavailable — "
            f"escalate audit task to input-required"
        )
    if pinned_card.card_hash != task.created_card_hash:
        raise CardSnapshotDriftError(
            f"pinned card {pinned_card.card_hash[:12]} does not match "
            f"task creation hash {task.created_card_hash[:12]}"
        )
    if artifact_skill not in pinned_card.skills:
        raise CardSnapshotDriftError(
            f"genuine provenance failure under pinned snapshot: "
            f"skill {artifact_skill!r} absent for task {task.task_id}"
        )
    return True
