# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-trust-anchor-binding-in-cross-org-multi-agent-federation-with-capability-scoped-delegation-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-broken-trust-anchor-binding-in-cross-org-multi-agent-federation-with-capability-scoped-delegation-chains.warrant.json
# Markdown SHA-256:   3e712201c3eacade210f2f91291b97327b24e6d78ad5b7265e6ac4e77347b36f
# Code AST SHA-256:   265e507b8fbf127c6f5dfae84f4f612f9947317644ddbf9f370ff1b3164b0ab9
# Verified Classes:   AnchorBindingError, Link, TrustBundle
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class AnchorBindingError(RuntimeError):
    """Raised when a capability chain is structurally orphaned from its trust anchor."""


@dataclass
class Link:
    issuer: str
    audience: str
    scope: frozenset
    anchor_org: Optional[str]  # org whose bundle roots this link, if any


@dataclass
class TrustBundle:
    org: str
    next_update: int
    scoped_capabilities: frozenset


def verify_chain(chain: list[Link], bundle: TrustBundle, now: int) -> None:
    if not chain:
        raise AnchorBindingError("empty delegation chain")

    # Signal 1: every link must be rooted in a published anchor for this bundle's org.
    for i, link in enumerate(chain):
        if link.anchor_org is None:
            raise AnchorBindingError(
                f"link {i} has no anchor; unrooted interior segment (depth discontinuity)"
            )
        if link.anchor_org != bundle.org:
            raise AnchorBindingError(
                f"link {i} rooted at {link.anchor_org!r}, expected {bundle.org!r} (anchor drift)"
            )

    # Signal 3: issuer/audience must be consistent hop-to-hop.
    for i in range(1, len(chain)):
        if chain[i].issuer != chain[i - 1].audience:
            raise AnchorBindingError(
                f"link {i} issuer {chain[i].issuer!r} != prior audience {chain[i-1].audience!r}"
            )

    # Signal 4: attenuation must be monotonic (child scope subset of parent).
    for i in range(1, len(chain)):
        if not chain[i].scope <= chain[i - 1].scope:
            raise AnchorBindingError(
                f"link {i} broadens scope {chain[i].scope - chain[i-1].scope}; "
                "monotonicity violation — possible re-anchoring or escalation"
            )

    # Signal 2: anchor must not assert capabilities outside its scoped class.
    terminal = chain[-1]
    if not terminal.scope <= bundle.scoped_capabilities:
        raise AnchorBindingError(
            f"anchor {bundle.org!r} asserts out-of-scope capabilities "
            f"{terminal.scope - bundle.scoped_capabilities}"
        )

    # Signal 5: chain must not outlive the trust bundle's validity window.
    if now > bundle.next_update:
        raise AnchorBindingError(
            f"trust bundle for {bundle.org!r} stale (next_update={bundle.next_update}, now={now})"
        )
