# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-trust-boundary-when-a-tool-manifest-declares-capabilities-it-does-not-own.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-trust-boundary-when-a-tool-manifest-declares-capabilities-it-does-not-own.warrant.json
# Markdown SHA-256:   93ac88395b54a7f6f7a181a6619758238a7d967e96a45d3ce7f7b3c323ebd414
# Code AST SHA-256:   6af912e4baa480b2600672134b152be2f1064dd7531a5da28db7d4038bce0108
# Verified Classes:   CapabilityShadowError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class CapabilityShadowError(RuntimeError):
    """Raised when a manifest claims authority its credential does not prove,
    or when a bare capability name is claimed by more than one identity."""


def bind_capability(
    manifest_name: str,
    declared_scopes: frozenset[str],
    credential_scopes: frozenset[str],
    claiming_identities: frozenset[str],
) -> frozenset[str]:
    # Ambiguity fails closed: never resolve by load order.
    if len(claiming_identities) > 1:
        raise CapabilityShadowError(
            f"capability {manifest_name!r} claimed by multiple identities "
            f"{sorted(claiming_identities)}; refusing to resolve by precedence"
        )

    # A manifest may narrow authority, never widen it.
    overreach = declared_scopes - credential_scopes
    if overreach:
        raise CapabilityShadowError(
            f"manifest {manifest_name!r} declares scopes {sorted(overreach)} "
            f"not covered by its credential; refusing server rather than "
            f"silently binding the intersection"
        )

    return declared_scopes & credential_scopes
