# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-silent-privilege-amplification-in-aaif-delegated-credential-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-silent-privilege-amplification-in-aaif-delegated-credential-chains.warrant.json
# Markdown SHA-256:   ed37a15c3d2b0024a5678264f154e50ee0251eb2f153cc9b50d0e804a146b34b
# Code AST SHA-256:   88edfa2b24ea4a2fe6ad1a3f598cb406b3bd17d8e1a546a6c68685cc16840aa7
# Verified Classes:   AAIFChainAmplificationError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AAIFChainAmplificationError(RuntimeError):
    """Raised when a delegated hop's scope exceeds its presented parent's scope."""
    pass


def validate_hop(presented_parent: dict, child_request: dict, act_depth: int, max_depth: int) -> None:
    parent_scope = set(presented_parent.get("scope", []))
    child_scope = set(child_request.get("scope", []))

    if act_depth > max_depth:
        raise AAIFChainAmplificationError(
            f"act-chain depth {act_depth} exceeds policy maximum {max_depth}"
        )

    # Narrowing is legal; widening at any non-root hop is not.
    widened = child_scope - parent_scope
    if widened:
        raise AAIFChainAmplificationError(
            f"hop requests scope {sorted(widened)} not held by presented parent; "
            f"possible refresh-path amplification"
        )

    # A non-root hop must never re-acquire the root's full authority.
    if act_depth > 1 and child_scope == set(presented_parent.get("root_scope", [])):
        raise AAIFChainAmplificationError(
            "non-root hop scope equals root authority; attenuation invariant violated"
        )
