# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-abort-a-federated-handoff-when-a-peer-s-sovereignty-policy-is-satisfiable-only-under-a-locality-predicate-the-local.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-abort-a-federated-handoff-when-a-peer-s-sovereignty-policy-is-satisfiable-only-under-a-locality-predicate-the-local.warrant.json
# Markdown SHA-256:   00d0c1357640f0da77af02d5b8da587f84a3e4812b00333bac300f4f9a9523d4
# Code AST SHA-256:   d42effe3e5890689ca7f07341a6b44061329278fb3ea412c9c779f6f420c83cb
# Verified Classes:   HandoffAborted, ObligationScope, Obligation, NegotiationVerdict, LocalState
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum


class HandoffAborted(RuntimeError):
    """Raised when a federated handoff cannot be admitted under the peer's policy."""
    pass


class ObligationScope(Enum):
    SENDER_DISCHARGEABLE = "sender_dischargeable"
    RECEIVER_INTERNAL = "receiver_internal"
    THIRD_PARTY = "third_party"


@dataclass
class Obligation:
    predicate: str
    scope: ObligationScope
    discharged_by: str


@dataclass
class NegotiationVerdict:
    satisfiable: bool
    policy_snapshot_id: str
    lineage_snapshot_id: str
    obligations: list[Obligation] = field(default_factory=list)


@dataclass
class LocalState:
    current_policy_snapshot_id: str
    current_lineage_snapshot_id: str
    resolvable_stores: set[str]


def admit_handoff(verdict: NegotiationVerdict, local: LocalState) -> None:
    if not verdict.satisfiable:
        raise HandoffAborted(f"peer policy unsatisfiable: {verdict.policy_snapshot_id}")

    # Stale-verdict guard: lineage or policy mutated since negotiation.
    if verdict.policy_snapshot_id != local.current_policy_snapshot_id:
        raise HandoffAborted(
            f"stale policy snapshot: verdict={verdict.policy_snapshot_id} "
            f"local={local.current_policy_snapshot_id}"
        )
    if verdict.lineage_snapshot_id != local.current_lineage_snapshot_id:
        raise HandoffAborted(
            f"stale lineage snapshot: verdict={verdict.lineage_snapshot_id} "
            f"local={local.current_lineage_snapshot_id}; re-plan invalidated verdict"
        )

    # Empty obligations with a non-trivial policy implies negotiator defect.
    if not verdict.obligations:
        raise HandoffAborted(
            "peer returned SATISFIABLE with no enumerated obligations; "
            "treating as UNVERIFIABLE, not OK"
        )

    # Dischargeability guard: every obligation must be sender-resolvable.
    for ob in verdict.obligations:
        if ob.scope is not ObligationScope.SENDER_DISCHARGEABLE:
            raise HandoffAborted(
                f"obligation '{ob.predicate}' scoped {ob.scope.value}; "
                f"evidence at '{ob.discharged_by}' is not sender-readable"
            )
        store = ob.discharged_by.split(".", 1)[0]
        if store not in local.resolvable_stores:
            raise HandoffAborted(
                f"obligation '{ob.predicate}' requires store '{store}' "
                f"absent from sender's resolvable set {sorted(local.resolvable_stores)}"
            )
    # All obligations sender-dischargeable and snapshots current: transmit.
