# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-peer-s-capability-grant-despite-a-valid-trust-anchor-signature.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-peer-s-capability-grant-despite-a-valid-trust-anchor-signature.warrant.json
# Markdown SHA-256:   b0f68fb57382e170e78939b392317548b6c7a50114205211b730776ca6477ec1
# Code AST SHA-256:   ec2ba3832f32bde5dcbd216f1c72420e8ec8f42a25825f9dddbfb578b54a4b26
# Verified Classes:   ScopeWideningError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ScopeWideningError(RuntimeError):
    """Raised when a federated capability grant would be admitted under a
    translation that is broader than the remote scope and not locally authorized."""
    pass


def admit_federated_grant(grant, anchor_trusted, local_policy, local_authorizations):
    # Precondition: signature and anchor.
    if not anchor_trusted(grant.signature, grant.anchor):
        raise ScopeWideningError(
            f"anchor {grant.anchor} not trusted for grant {grant.id}"
        )

    translation = local_policy.translate(grant.scope)

    if translation.is_exact or translation.is_narrowing:
        return translation.local_token

    # Translation is widening. Admit only if locally authorized independently.
    if translation.local_scope in local_authorizations:
        local_policy.log_widening(grant.id, translation.dropped_qualifiers)
        return translation.local_token

    raise ScopeWideningError(
        f"grant {grant.id} from {grant.anchor}: remote scope {grant.scope!r} "
        f"translates to {translation.local_scope!r}, dropping qualifiers "
        f"{translation.dropped_qualifiers!r}; not authorized locally. "
        f"Valid signature is insufficient for admission."
    )
