# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-stale-agent-card-s-advertised-capability-set-against-an-already-issued-a2a-task-warrant-when-the-remote-vendor-rota.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-stale-agent-card-s-advertised-capability-set-against-an-already-issued-a2a-task-warrant-when-the-remote-vendor-rota.warrant.json
# Markdown SHA-256:   102838feb1727cf5b576e08400256fa8e6a57b4559063d58fb94e70c18e71f5c
# Code AST SHA-256:   9557aca7df6009d95b165b161b78697d405fa1c079648f94382d1109d7e46753
# Verified Classes:   WarrantRevisionMismatch, AgentCard, TaskWarrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
import json
from dataclasses import dataclass, field


class WarrantRevisionMismatch(RuntimeError):
    """Raised when a live Agent Card revision no longer satisfies an in-flight warrant."""
    pass


@dataclass
class AgentCard:
    revision: str
    skills: dict  # skillId -> input schema fingerprint
    capabilities_hash: str


@dataclass
class TaskWarrant:
    task_id: str
    bound_capabilities_hash: str
    named_skills: list = field(default_factory=list)


def compute_capabilities_hash(skills: dict) -> str:
    canonical = json.dumps(skills, sort_keys=True, separators=(",", ":"))
    return hashlib.sha256(canonical.encode()).hexdigest()


def assert_warrant_binds_live_card(warrant: TaskWarrant, live_card: AgentCard) -> None:
    """
    Enforce the invariant: a warrant scoped to revision R is only executable
    by a peer that still attests R for every skill the warrant names.
    Signature validity alone is insufficient; revision binding is mandatory.
    """
    if live_card.capabilities_hash != warrant.bound_capabilities_hash:
        missing = [s for s in warrant.named_skills if s not in live_card.skills]
        if missing:
            # Skill-identity gap: a schema-superset sibling may silently accept
            # the payload. Abort rather than re-bind across the gap.
            raise WarrantRevisionMismatch(
                f"warrant {warrant.task_id} names skills {missing} absent from "
                f"live card revision {live_card.revision} "
                f"(bound={warrant.bound_capabilities_hash[:12]}, "
                f"live={live_card.capabilities_hash[:12]}); "
                f"refusing schema-fallthrough dispatch"
            )
        # All named skills still present under a new revision: re-bind is safe
        # only after explicit caller acknowledgement, not silently here.
        raise WarrantRevisionMismatch(
            f"warrant {warrant.task_id} bound to revision hash "
            f"{warrant.bound_capabilities_hash[:12]} but live card is "
            f"{live_card.capabilities_hash[:12]}; require re-issued warrant"
        )


def resume_task(warrant: TaskWarrant, live_card: AgentCard) -> str:
    assert_warrant_binds_live_card(warrant, live_card)
    return "dispatched"
