# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-s-delegated-capability-token-survive-re-delegation-but-silently-lose-its-scope-attenuation-at-depth-three.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-s-delegated-capability-token-survive-re-delegation-but-silently-lose-its-scope-attenuation-at-depth-three.warrant.json
# Markdown SHA-256:   503f9b97c1115d011efa7526d132f163b8087417947ec5b1f0003e3bf7f1511a
# Code AST SHA-256:   e8661715f54190edbb255a72b5cf36a045a24bb2ee7ba2048a3bb93dc7becba4
# Verified Classes:   AttenuationBypassError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class AttenuationBypassError(RuntimeError):
    """Raised when a delegated token's scope exceeds the recomputed
    intersection of all granted_scopes along its parent chain."""
    pass


def verify_delegated_scope(token, resolve_parent):
    # Walk the chain to the root, intersecting every granted_scope.
    chain = []
    cursor = token
    while cursor is not None:
        chain.append(cursor)
        cursor = resolve_parent(cursor.parent_hash)

    root = chain[-1]
    recomputed = set(root.granted_scope)
    for link in reversed(chain[:-1]):
        recomputed &= set(link.granted_scope)

    presented = set(token.attenuated_scope)
    if not presented.issubset(recomputed):
        over = presented - recomputed
        raise AttenuationBypassError(
            f"token {token.token_id} at depth {len(chain) - 1} claims "
            f"scope {sorted(presented)} but chain permits {sorted(recomputed)}; "
            f"escalated capabilities: {sorted(over)}"
        )
    return recomputed
