# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-a-federated-task-graph-edge-has-silently-violated-data-residency-policy-when-the-peer-agent-s-capability-declaration.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-a-federated-task-graph-edge-has-silently-violated-data-residency-policy-when-the-peer-agent-s-capability-declaration.warrant.json
# Markdown SHA-256:   8c109437745c083bea58565fcaff617828d3fe576335cca0c4620ef8eb09440f
# Code AST SHA-256:   cec205436169b2f8799c51775942c281ac42cb6795a3f66f5307c11d262b4a67
# Verified Classes:   ResidencyWarrantError, RuntimeBinding, CapabilityCard
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Mapping


class ResidencyWarrantError(RuntimeError):
    """Raised when a federated edge cannot carry a residency warrant."""
    pass


@dataclass(frozen=True)
class RuntimeBinding:
    tool: str
    region: str
    is_fallback: bool = False


@dataclass(frozen=True)
class CapabilityCard:
    agent_id: str
    substrate_region: str
    outbound_bindings: tuple[RuntimeBinding, ...] = field(default_factory=tuple)


def validate_edge_warrant(
    peer: CapabilityCard,
    allowed_regions: frozenset[str],
    payload_classification_regions: frozenset[str],
) -> None:
    """
    Enforce closure-equality for residency warrant propagation.

    A warrant may be propagated across this edge only if every declared
    runtime binding (including fallbacks) resolves to a region inside the
    graph's allowed set, AND the payload is legal in the weakest leaf region.
    """
    if not peer.outbound_bindings:
        raise ResidencyWarrantError(
            f"peer {peer.agent_id!r} declares no outbound_bindings; "
            "closure is unenumerated, warrant cannot propagate"
        )

    leaf_regions = {b.region for b in peer.outbound_bindings}
    leaf_regions.add(peer.substrate_region)

    illegal = leaf_regions - allowed_regions
    if illegal:
        raise ResidencyWarrantError(
            f"peer {peer.agent_id!r} closure reaches disallowed regions "
            f"{sorted(illegal)}; declared node region was "
            f"{peer.substrate_region!r} but closure is not contained"
        )

    weakest_leaf = min(leaf_regions, key=lambda r: len(
        payload_classification_regions & allowed_regions
    ))
    if not payload_classification_regions <= allowed_regions:
        raise ResidencyWarrantError(
            f"payload classification {sorted(payload_classification_regions)} "
            f"exceeds allowed set at weakest leaf {weakest_leaf!r}"
        )


def dispatch(peer: CapabilityCard, allowed_regions: frozenset[str],
             payload_regions: frozenset[str]) -> str:
    validate_edge_warrant(peer, allowed_regions, payload_regions)
    return f"dispatched to {peer.agent_id} with residency_warranted"
