# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-verify-attenuated-scope-at-every-delegation-hop-and-why-does-single-point-verification-at-the-root-silently-authorize-privil.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-verify-attenuated-scope-at-every-delegation-hop-and-why-does-single-point-verification-at-the-root-silently-authorize-privil.warrant.json
# Markdown SHA-256:   1d25efdfe2df90af57143919c7b1f46aedcf8b6e610f46308afa324297264747
# Code AST SHA-256:   7f37c5ea4dfe6148c45e65ba0812926387af91506c3a4e5126dfbb19ac542a72
# Verified Classes:   AttenuationViolation, Warrant
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class AttenuationViolation(RuntimeError):
    """Raised when a child warrant's scope is not a subset of its parent's."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    parent_id: str | None
    depth: int
    scope: FrozenSet[str]
    signature_valid: bool = True


def mint_child(parent: Warrant, requested_scope: FrozenSet[str], child_id: str) -> Warrant:
    # The mechanical intersection gate. This is the invariant.
    # A probabilistic minter may *propose* requested_scope, but it may not
    # bypass this check by reasoning that "the task needs it".
    if not requested_scope.issubset(parent.scope):
        illegal = requested_scope - parent.scope
        raise AttenuationViolation(
            f"child {child_id} requests {sorted(illegal)} not held by parent "
            f"{parent.warrant_id} (scope {sorted(parent.scope)}); "
            f"re-expansion across hop refused"
        )
    return Warrant(
        warrant_id=child_id,
        parent_id=parent.warrant_id,
        depth=parent.depth + 1,
        scope=requested_scope,
    )


def verify_chain(chain: list[Warrant], action: str) -> None:
    # Defense in depth: recompute the full intersection at execution time,
    # never trust root scope alone.
    if not chain:
        raise AttenuationViolation("empty delegation chain")
    for i in range(1, len(chain)):
        child, parent = chain[i], chain[i - 1]
        if child.parent_id != parent.warrant_id:
            raise AttenuationViolation(
                f"broken link: {child.warrant_id} claims parent {child.parent_id}, "
                f"chain has {parent.warrant_id}"
            )
        if not child.scope.issubset(parent.scope):
            raise AttenuationViolation(
                f"attenuation violated at hop {parent.warrant_id}->{child.warrant_id}: "
                f"{sorted(child.scope - parent.scope)} re-expanded"
            )
        if not child.signature_valid:
            raise AttenuationViolation(f"bad signature on {child.warrant_id}")
    effective = frozenset.intersection(*(w.scope for w in chain))
    if action not in effective:
        raise AttenuationViolation(
            f"action {action!r} not in effective scope {sorted(effective)}"
        )
