# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-delegation-chain-in-aaif-cross-domain-agent-handoffs.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-delegation-chain-in-aaif-cross-domain-agent-handoffs.warrant.json
# Markdown SHA-256:   0708fbcb69b42f52007847b94e6836864e2fe70f6b88b72336f76fec83e21819
# Code AST SHA-256:   490356edc376fdaeab1b50eaf52ae95d8c90ffbfedfa8e220f4257f7b2c1edef
# Verified Classes:   AttenuationViolation, Credential
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional

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

@dataclass
class Credential:
    jti: str
    scope: frozenset
    parent_jti: Optional[str] = None
    derived_from: Optional[str] = None  # prose-derived scope, unattested
    audience: str = ""

def verify_chain(chain: list) -> None:
    """Enforce monotonic attenuation across an AAIF delegated chain."""
    by_jti = {c.jti: c for c in chain}
    for child in chain:
        if child.derived_from and not child.scope:
            raise AttenuationViolation(
                f"credential {child.jti} has prose-derived scope with no "
                f"machine-readable claim; cannot prove attenuation"
            )
        if child.parent_jti is None:
            continue  # root; trust anchor checked elsewhere
        parent = by_jti.get(child.parent_jti)
        if parent is None:
            raise AttenuationViolation(
                f"credential {child.jti} references parent {child.parent_jti} "
                f"not present in chain; subset check impossible"
            )
        if not child.scope.issubset(parent.scope):
            widened = child.scope - parent.scope
            raise AttenuationViolation(
                f"scope widening at {child.jti}: {sorted(widened)} "
                f"not granted by parent {parent.jti}"
            )
