# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-capability-token-it-cannot-re-attenuate-the-monotonic-scope-invariant-and-the-sub-scope-resurrection-failure.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-capability-token-it-cannot-re-attenuate-the-monotonic-scope-invariant-and-the-sub-scope-resurrection-failure.warrant.json
# Markdown SHA-256:   659933ff56edca29bbc41d57d5d69dfe487216d55be43571358ce614b6293a58
# Code AST SHA-256:   df4073be155c22a29f727e607b5e5546103ad5905139492e8eadfddd52a7e16c
# Verified Classes:   NonAttenuatableDelegationError, Scope, DelegationToken
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class NonAttenuatableDelegationError(RuntimeError):
    """Raised when an instance attempts to sub-delegate a token whose scope
    it cannot prove is a superset of the intended child scope."""


@dataclass(frozen=True)
class Scope:
    vocabulary: str          # e.g. "glob", "arn", "structured"
    members: FrozenSet[str]


@dataclass(frozen=True)
class DelegationToken:
    signed_scope: Scope      # bound into the signature; authoritative
    declared_scope: Scope    # hop-local intent; advisory unless signed
    declared_is_signed: bool


def effective_scope(chain: list[DelegationToken]) -> Scope:
    """Effective scope is the MEET (intersection) of signed scopes only.
    Never the union. Non-signed declared scopes are ignored."""
    if not chain:
        raise NonAttenuatableDelegationError("empty delegation chain")
    vocabs = {t.signed_scope.vocabulary for t in chain}
    if len(vocabs) != 1:
        raise NonAttenuatableDelegationError(
            f"cannot intersect scopes across vocabularies: {vocabs}"
        )
    acc = chain[0].signed_scope.members
    for t in chain[1:]:
        acc = acc & t.signed_scope.members
    return Scope(next(iter(vocabs)), frozenset(acc))


def delegate(parent: DelegationToken, child_intent: Scope) -> DelegationToken:
    """Emit a sub-delegation ONLY if child_intent is provably a subset of the
    parent's signed scope in a common vocabulary. Otherwise refuse."""
    if parent.signed_scope.vocabulary != child_intent.vocabulary:
        raise NonAttenuatableDelegationError(
            "parent scope vocabulary "
            f"{parent.signed_scope.vocabulary!r} is not comparable to child "
            f"vocabulary {child_intent.vocabulary!r}; instance must downgrade "
            "to terminal-executor mode and refuse to delegate"
        )
    if not child_intent.members <= parent.signed_scope.members:
        raise NonAttenuatableDelegationError(
            "child scope is not a subset of parent signed scope; "
            "refusing to emit widening delegation"
        )
    # Bind the effective (meet) scope into the new signed envelope.
    return DelegationToken(
        signed_scope=child_intent,
        declared_scope=child_intent,
        declared_is_signed=True,
    )


def accept_for_execution(token: DelegationToken) -> Scope:
    """Terminal-executor intake: enforce the signed scope, ignore unsigned
    declared intent. This is always permitted for a valid token."""
    return token.signed_scope
