# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-delegation-chain-in-aaif-autonomous-agent-credentials.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-delegation-chain-in-aaif-autonomous-agent-credentials.warrant.json
# Markdown SHA-256:   05edf7f5ce9a11fa3b220d3d25c674bebff829afad7a88defc6a9079b1d7049a
# Code AST SHA-256:   9e9c7a079323ec0deec733ca4e45f2b191c6717d5c41a34d1dc3061c014eec0c
# Verified Classes:   DelegationChainError, Credential
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Sequence


class DelegationChainError(RuntimeError):
    """Raised when an AAIF delegated credential chain is structurally incoherent."""
    pass


@dataclass(frozen=True)
class Credential:
    subject: str
    actor: str
    audience: str
    scopes: frozenset
    parent_hash: str | None
    not_before: float
    not_after: float


def verify_chain(chain: Sequence[Credential], skew_allowance: float = 2.0) -> None:
    """Walk a delegation chain root-to-leaf and enforce monotonic attenuation.

    Raises DelegationChainError on the first structural violation.
    """
    if not chain:
        raise DelegationChainError("empty chain: no root credential to anchor delegation")

    root = chain[0]
    if root.parent_hash is not None:
        raise DelegationChainError(
            f"root credential {root.subject!r} unexpectedly declares a parent"
        )

    allowed_scopes = set(root.scopes)
    prev = root

    for hop in chain[1:]:
        # 1. Linkage: simple path, no dangling or superseded edges.
        if hop.parent_hash is None:
            raise DelegationChainError(
                f"hop {hop.subject!r} is missing parent_hash linkage"
            )

        # 2. Attenuation monotonicity against the intersection of all ancestors.
        extra = set(hop.scopes) - allowed_scopes
        if extra:
            raise DelegationChainError(
                f"hop {hop.subject!r} introduces non-attenuated scopes: {sorted(extra)}"
            )

        # 3. Actor/audience coherence across the hop boundary.
        if hop.actor != prev.audience:
            raise DelegationChainError(
                f"actor/audience mismatch: hop {hop.subject!r} acts as {hop.actor!r} "
                f"but parent audience is {prev.audience!r}"
            )

        # 4. Timeline monotonicity with bounded skew.
        if hop.not_before < prev.not_before - skew_allowance:
            raise DelegationChainError(
                f"hop {hop.subject!r} not_before precedes its parent beyond skew allowance"
            )
        if hop.not_after > prev.not_after + skew_allowance:
            raise DelegationChainError(
                f"hop {hop.subject!r} extends validity beyond its parent"
            )

        allowed_scopes &= set(hop.scopes)
        prev = hop
