# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-delegation-chain-when-an-aaif-scoped-token-is-re-minted-across-an-agent-hop.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-delegation-chain-when-an-aaif-scoped-token-is-re-minted-across-an-agent-hop.warrant.json
# Markdown SHA-256:   acbd3f9163d9a6bfbc762a846eb9d0b6c99bcf80bdcf1f2a3c5708fa4dbbf4dd
# Code AST SHA-256:   7cc903014081e36179f1fac47f5d36e69b616827d97f2cc61a79b55137a89346
# Verified Classes:   DelegationChainDriftError, ScopedToken
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Callable, Optional

class DelegationChainDriftError(RuntimeError):
    """Raised when a re-minted delegated token is not a replayable, constructive
    narrowing of its parent scope."""
    pass

@dataclass(frozen=True)
class ScopedToken:
    subject: str
    scope: frozenset[str]
    parent_hash: Optional[str]
    narrowing_op: Optional[str]   # signed record of f; None means "root token"
    signature_valid: bool

def verify_delegation_hop(
    parent: ScopedToken,
    child: ScopedToken,
    replay: Callable[[str, frozenset[str]], frozenset[str]],
    ordering_comparable: Callable[[str, str], bool],
) -> None:
    if not child.signature_valid or not parent.signature_valid:
        raise DelegationChainDriftError("signature verification failed")

    if child.parent_hash is None:
        # A token with no parent is a root token; it must be verified against
        # the root authority, not against an assumed parent.
        raise DelegationChainDriftError(
            f"token for {child.subject} claims delegation but carries no parent_hash"
        )

    if child.narrowing_op is None:
        raise DelegationChainDriftError(
            f"token for {child.subject} claims parent {child.parent_hash} "
            f"but carries no replayable narrowing operation"
        )

    # Structural signal 1: identity is not narrowing.
    if child.scope == parent.scope:
        raise DelegationChainDriftError(
            f"token for {child.subject} claims narrowed_from parent "
            f"but scope equals parent scope verbatim (widening disguised as delegation)"
        )

    # Structural signal 2: empty intersection is a signal, not a fallback trigger.
    if not child.scope:
        raise DelegationChainDriftError(
            f"token for {child.subject} carries empty scope; "
            f"re-mint must reject, not fall back to parent scope"
        )

    # Structural signal 3: vocabularies must be comparable under a shared lattice.
    for child_atom in child.scope:
        if not any(ordering_comparable(child_atom, parent_atom) for parent_atom in parent.scope):
            raise DelegationChainDriftError(
                f"scope atom {child_atom!r} is not comparable to any parent atom; "
                f"narrowing is unverifiable, not empty"
            )

    # Structural signal 4: the narrowing must replay exactly.
    replayed = replay(child.narrowing_op, parent.scope)
    if replayed != child.scope:
        raise DelegationChainDriftError(
            f"replay of narrowing_op {child.narrowing_op!r} on parent scope "
            f"yielded {sorted(replayed)!r}, but child carries {sorted(child.scope)!r}"
        )
