# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-cross-org-aaif-attestation-chain-whose-signer-delegation-depth-exceeds-its-own-verification-horizon.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-cross-org-aaif-attestation-chain-whose-signer-delegation-depth-exceeds-its-own-verification-horizon.warrant.json
# Markdown SHA-256:   2dc3d047c01a02f42fc92d47f9703e9f26bf08a8836c9a97ec909efd238d0d32
# Code AST SHA-256:   7307d972f9fbf86a8e421ace7ffdf020f76384ec420f89402d4754f6b1c99b80
# Verified Classes:   HorizonViolationError, Hop
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class HorizonViolationError(RuntimeError):
    """Raised when an AAIF attestation chain's delegation depth or scope
    exceeds the receiving instance's independently anchorable trust horizon."""
    pass


class Hop:
    def __init__(self, issuer: str, scope: dict, reattests: bool = False):
        self.issuer = issuer
        self.scope = scope
        self.reattests = reattests


def verify_attestation_chain(chain: list[Hop], trust_horizon: int,
                             anchorable_issuers: set[str]) -> dict:
    """
    Enforce the two-dimensional horizon invariant:
    honored authority <= authority derivable from the anchorable sub-chain.
    Signature validity is assumed already checked upstream and is NOT
    sufficient. Depth and scope are both bounded here.
    """
    anchorable_depth = 0
    effective_scope = None

    for i, hop in enumerate(chain):
        # A hop is anchorable if its issuer is inside our horizon AND we have
        # not already exceeded the depth ceiling. Re-attestation by an
        # out-of-horizon party does NOT reset depth.
        if hop.issuer in anchorable_issuers and anchorable_depth < trust_horizon:
            anchorable_depth += 1
        else:
            raise HorizonViolationError(
                f"chain hop {i} (issuer={hop.issuer!r}) lies beyond trust "
                f"horizon depth={trust_horizon}; anchorable_depth="
                f"{anchorable_depth}. Refusing to honor leaf authority."
            )

        # Scope is intersected across every anchorable hop. A leaf cannot
        # claim more than the narrowest hop carried.
        if effective_scope is None:
            effective_scope = dict(hop.scope)
        else:
            for key, val in hop.scope.items():
                if key in effective_scope:
                    effective_scope[key] = min(effective_scope[key], val)
                else:
                    effective_scope[key] = val

    if effective_scope is None:
        raise HorizonViolationError("empty chain: no anchorable authority")

    return {"anchorable_depth": anchorable_depth, "effective_scope": effective_scope}
