# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-re-scope-a-delegated-capability-token-after-a-trust-chain-hop-even-when-the-downstream-peer-presents.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-re-scope-a-delegated-capability-token-after-a-trust-chain-hop-even-when-the-downstream-peer-presents.warrant.json
# Markdown SHA-256:   988f73204486a1e817a2a2311f473798e5643490a0f8c5291f8d807ad3cc02f3
# Code AST SHA-256:   3195cbc1693aef9ed697027d8f7d94b0515618633e6d71364ec146f93c17d8a9
# Verified Classes:   ScopeWideningRefused
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ScopeWideningRefused(RuntimeError):
    """Raised when a re-scope request would broaden scope beyond the
    most-restricted ancestor on the delegation chain."""


def attenuate(parent_scope: frozenset, parent_depth: int,
              requested_scope: frozenset, requested_depth: int,
              effective_floor: frozenset, delegate_bit: bool) -> frozenset:
    # 1. subset of immediate parent
    if not requested_scope <= parent_scope:
        delta = requested_scope - parent_scope
        raise ScopeWideningRefused(
            f"child scope exceeds immediate parent; delta={sorted(delta)}")
    # 2. transitive floor: cannot recover anything an ancestor dropped
    if not requested_scope <= effective_floor:
        recovered = requested_scope - effective_floor
        raise ScopeWideningRefused(
            f"re-scope recovers ancestor-dropped caps: {sorted(recovered)}")
    # 3. depth must strictly decrement
    if requested_depth >= parent_depth:
        raise ScopeWideningRefused(
            f"depth not decremented: parent={parent_depth} child={requested_depth}")
    # 4. delegation authority for the specific resource
    if not delegate_bit:
        raise ScopeWideningRefused("issuer lacks delegate bit for requested resource")
    return requested_scope
