# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-capability-attenuation-laundering-in-multi-hop-agent-delegation-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-capability-attenuation-laundering-in-multi-hop-agent-delegation-chains.warrant.json
# Markdown SHA-256:   defd95cc2a7564c6b5f57e05361498a8f1ba7d42010247ee540877aba2cf2d4b
# Code AST SHA-256:   b11b20b5a5272ca6a250ec6a4e8e5ca9405f69de7625413e3bff98ada4edc32d
# Verified Classes:   CapabilityLaunderingError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class CapabilityLaunderingError(RuntimeError):
    """Raised when a delegated action's authority resolves to a data handle
    rather than to a warrant in the caller's chain."""
    pass


def authorize_action(caller_warrant, presented_handle, required_capability, tool_policy):
    # INVARIANT: the warrant authorizes; the handle only selects a resource.
    # A handle's scope must never substitute for the caller's capability set.
    if required_capability not in caller_warrant.capabilities:
        raise CapabilityLaunderingError(
            f"caller warrant {caller_warrant.id!r} lacks {required_capability!r}; "
            f"presented handle scope {getattr(presented_handle, 'scope', None)!r} "
            f"cannot authorize (handle selects, warrant authorizes)"
        )

    # Second check: the handle must actually reference a resource the tool
    # policy permits for this capability. A read-scoped handle cannot select
    # a write target even when the caller holds write:docs.
    if presented_handle is not None:
        if required_capability not in tool_policy.scopes_for(presented_handle):
            raise CapabilityLaunderingError(
                f"handle {presented_handle.id!r} scope "
                f"{presented_handle.scope!r} does not cover {required_capability!r}"
            )

    return tool_policy.execute(presented_handle, required_capability)


def delegate(parent_warrant, child_caps, context_items):
    # Attenuation-by-closure: strip or re-scope every authority-bearing item
    # before forwarding. Inert data passes unchanged.
    safe_context = []
    for item in context_items:
        if item.is_authority_bearing:
            if not item.scope.issubset(child_caps):
                # Do not forward a handle wider than the child's grant.
                rescoped = item.rescope(item.scope & child_caps)
                if rescoped is None:
                    raise CapabilityLaunderingError(
                        f"cannot forward authority-bearing {item.id!r}: "
                        f"scope {item.scope!r} exceeds child caps {child_caps!r} "
                        f"and is not re-scopable"
                    )
                safe_context.append(rescoped)
            else:
                safe_context.append(item)
        else:
            safe_context.append(item)

    if not child_caps.issubset(parent_warrant.capabilities):
        raise CapabilityLaunderingError(
            f"child caps {child_caps!r} widen parent grant "
            f"{parent_warrant.capabilities!r}"
        )

    return ChildWarrant(parent=parent_warrant, capabilities=child_caps,
                        context=safe_context)
