# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-delegation-the-attenuation-trap-in-transitive-agent-capability-scopes.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-delegation-the-attenuation-trap-in-transitive-agent-capability-scopes.warrant.json
# Markdown SHA-256:   b2887473db6e873a12fe6b4967248503755bc95d68533dca1558c8ec8ec24f1f
# Code AST SHA-256:   de20bc7644b6664828b518843f02d964a1775805553ce2f8ce5f5ae86b838dd7
# Verified Classes:   AttenuationViolationError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AttenuationViolationError(RuntimeError):
    """Raised when a delegation hop cannot guarantee effective-scope attenuation."""
    pass


def delegate_capability(parent_scope: frozenset, requested_scope: frozenset,
                        resolver_requires_parent_token: bool) -> frozenset:
    # Declared attenuation check: requested must be a subset of parent.
    if not requested_scope.issubset(parent_scope):
        raise AttenuationViolationError(
            f"requested scope {requested_scope} exceeds parent {parent_scope}"
        )

    # The non-obvious boundary: if the terminal resolver can derive
    # capabilities from resource metadata rather than the parent token,
    # declared attenuation is meaningless and the hop must be refused.
    if not resolver_requires_parent_token:
        raise AttenuationViolationError(
            "delegate resolver does not thread parent token; "
            "effective scope may re-widen downstream — delegation refused"
        )

    return frozenset(requested_scope)


def issue_delegation(parent_scope, requested_scope, resolver_requires_parent_token):
    effective = delegate_capability(
        parent_scope, requested_scope, resolver_requires_parent_token
    )
    # Invariant asserted at the boundary: effective ⊆ parent.
    assert effective.issubset(parent_scope), "attenuation invariant broken"
    return effective
