# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-enforce-data-residency-when-a-federated-peer-s-policy-engine-silently-widens-jurisdiction-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-enforce-data-residency-when-a-federated-peer-s-policy-engine-silently-widens-jurisdiction-mid-handoff.warrant.json
# Markdown SHA-256:   b044e5d1ff57fb9eefc3c79571579a60d520f9af32e4e306cc5c3372518e89cc
# Code AST SHA-256:   79b22bc1783e8a5108300aefa854e91587be7c14be78aa38ea453021841ef9a0
# Verified Classes:   ResidencyWarrantError, PolicyDecision, PlacementAttestation
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ResidencyWarrantError(RuntimeError):
    """Raised when a federated handoff cannot be bound to a residency guarantee."""


@dataclass(frozen=True)
class PolicyDecision:
    allow: bool
    effective_jurisdiction: frozenset[str]
    state_version: Optional[str]           # pinned state the decision was computed against
    placement_constraint: Optional[str]    # declared jurisdiction the scheduler must honor


@dataclass(frozen=True)
class PlacementAttestation:
    region: str
    state_version: str
    signed_by_scheduler: bool


def enforce_residency_handoff(
    declared_jurisdiction: str,
    decision: PolicyDecision,
    attestation: Optional[PlacementAttestation],
) -> None:
    if not decision.allow:
        raise ResidencyWarrantError(
            f"peer denied handoff for declared_jurisdiction={declared_jurisdiction}"
        )

    # A policy ALLOW without a pinned state version is a point-in-time assertion,
    # not a continuous residency invariant. Refuse to treat it as a guarantee.
    if decision.state_version is None:
        raise ResidencyWarrantError(
            "policy ALLOW lacks pinned state_version; cannot bind residency "
            "across evaluation-to-materialization gap"
        )

    if decision.placement_constraint != declared_jurisdiction:
        raise ResidencyWarrantError(
            f"placement_constraint={decision.placement_constraint!r} does not "
            f"match declared_jurisdiction={declared_jurisdiction!r}; scheduler "
            "is not obligated to honor sender's residency"
        )

    if not decision.effective_jurisdiction <= frozenset({declared_jurisdiction}):
        raise ResidencyWarrantError(
            f"effective_jurisdiction={set(decision.effective_jurisdiction)} "
            f"escapes declared_jurisdiction={declared_jurisdiction!r}"
        )

    # The policy engine is not the authority on where the payload ran.
    # Require a scheduler-signed placement attestation pinned to the same state.
    if attestation is None or not attestation.signed_by_scheduler:
        raise ResidencyWarrantError(
            "missing scheduler-signed placement attestation; policy ALLOW "
            "cannot be promoted to a placement guarantee"
        )

    if attestation.state_version != decision.state_version:
        raise ResidencyWarrantError(
            f"attestation state_version={attestation.state_version!r} diverged "
            f"from decision state_version={decision.state_version!r}; "
            "verification replay would evaluate a different world"
        )

    if attestation.region != declared_jurisdiction:
        raise ResidencyWarrantError(
            f"payload materialized in {attestation.region!r} under a warrant "
            f"declaring {declared_jurisdiction!r}; residency invariant violated"
        )
