# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-warrant-the-liability-attribution-gap-at-the-policy-negotiation-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-warrant-the-liability-attribution-gap-at-the-policy-negotiation-boundary.warrant.json
# Markdown SHA-256:   a5b99eb76c1631703e7a38ed9e36be41a66fe8e79602d1660ee217dae56a45f8
# Code AST SHA-256:   635345839c8c638bf517765ec97b4cf8835b84e7f5eebb238175756ddd34e439
# Verified Classes:   LiabilityUnresolvedError, LiabilityClause, Warrant, ReceiverPolicy
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Mapping


class LiabilityUnresolvedError(RuntimeError):
    """Raised when a federated warrant is capability-admissible but
    liability-unbound for the concrete action about to be invoked.

    This is a hard refusal, never a default. The receiver must not
    substitute a zero-cap or 'origin-default' principal, because doing
    so silently assumes the foreign principal's liability.
    """


@dataclass(frozen=True)
class LiabilityClause:
    responsible_principal: str
    jurisdiction: str
    indemnity_cap: int


@dataclass(frozen=True)
class Warrant:
    scope: frozenset[str]
    delegation_chain: tuple[str, ...]
    liability: Mapping[str, LiabilityClause]


@dataclass(frozen=True)
class ReceiverPolicy:
    allowed_actions: frozenset[str]
    accepted_jurisdictions: frozenset[str]
    known_principals: frozenset[str]
    required_floor: Mapping[str, int] = field(default_factory=dict)


def admit_action(warrant: Warrant, action: str, policy: ReceiverPolicy) -> LiabilityClause:
    """Evaluate the liability-binding predicate for one concrete action.

    Called at handshake AND at every tool-call boundary, because the
    peer's reachable action set can grow under mid-execution re-planning
    while the warrant's liability map is fixed at mint time.
    """
    # Capability layer: scope intersection.
    if action not in warrant.scope or action not in policy.allowed_actions:
        raise LiabilityUnresolvedError(f"capability denied for {action!r}")

    # Liability layer: closure over the action set. A miss is an error,
    # not a default. This is the clause the naive handshake omits.
    if action not in warrant.liability:
        raise LiabilityUnresolvedError(
            f"liability_unresolved: {action!r} not in warrant liability map; "
            f"warrant must be amended (re-minted) before this action executes"
        )

    clause = warrant.liability[action]

    if clause.jurisdiction not in policy.accepted_jurisdictions:
        raise LiabilityUnresolvedError(
            f"jurisdiction {clause.jurisdiction!r} not accepted for {action!r}"
        )
    if clause.responsible_principal not in policy.known_principals:
        raise LiabilityUnresolvedError(
            f"responsible_principal {clause.responsible_principal!r} unknown to receiver"
        )
    floor = policy.required_floor.get(action, 0)
    if clause.indemnity_cap < floor:
        raise LiabilityUnresolvedError(
            f"indemnity_cap {clause.indemnity_cap} below required floor {floor} "
            f"for {action!r}"
        )
    return clause


def execute_federated_step(warrant: Warrant, action: str, policy: ReceiverPolicy) -> None:
    # Re-validate at the action boundary; never rely on handshake-time cache.
    admit_action(warrant, action, policy)
    # ... invoke tool only after liability is bound ...
