# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-attestation-chain-breakage-when-an-agent-crosses-a-federated-trust-domain-boundary-mid-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-attestation-chain-breakage-when-an-agent-crosses-a-federated-trust-domain-boundary-mid-execution.warrant.json
# Markdown SHA-256:   80ae6cb07dbbeab8f326ea33376be9f512a03c0335af51ca608651fea8eeada5
# Code AST SHA-256:   4c67ba01462f7f9fe325d9a50e50a1beb2e2798ad0573f95c87896983a831f63
# Verified Classes:   AttestationBoundaryError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class AttestationBoundaryError(RuntimeError):
    """Raised when a warrant chain crosses a federation boundary whose
    trust anchor covers authentication but not the capability being exercised."""


def resolve_crossing_authority(
    chain_depth: int,
    verb: str,
    federation_maps_capabilities: bool,
    receiver_default_allow: set[str],
    reattested: bool,
) -> str:
    """
    Decide the authority source for a verb at a federation boundary.
    Returns one of: 'explicit-grant', 'reattested', or raises.
    """
    if reattested:
        return "reattested"

    if federation_maps_capabilities:
        # Full per-link evaluation; depth is irrelevant to authority.
        return "explicit-grant"

    # Federation is silent on capability mapping: chain is authentication-only.
    # The depth-based inheritance fast path must never grant authority here.
    if chain_depth >= 2:
        raise AttestationBoundaryError(
            f"depth-{chain_depth} chain crossed a boundary with no capability "
            f"mapping; inheritance would grant '{verb}' without a local grant"
        )

    if verb in receiver_default_allow:
        # Permissive default still requires explicit acknowledgement, not inheritance.
        raise AttestationBoundaryError(
            f"verb '{verb}' hit a permissive default at the boundary; "
            f"require re-attestation rather than relying on default-allow"
        )

    raise AttestationBoundaryError(
        f"no local grant for '{verb}'; chain is authentication-bearing only"
    )


def guard_delegation(chain_depth: int, verb: str, receiver_policy: dict) -> str:
    try:
        return resolve_crossing_authority(
            chain_depth=chain_depth,
            verb=verb,
            federation_maps_capabilities=receiver_policy.get("maps_capabilities", False),
            receiver_default_allow=receiver_policy.get("default_allow", set()),
            reattested=receiver_policy.get("reattested", False),
        )
    except AttestationBoundaryError:
        # Halt further delegation: an inherited grant must not propagate.
        raise
