# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-delegated-aaif-credential-whose-delegation-depth-exceeds-the-verifier-s-locally-cached-trust-anchor-resolu.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-delegated-aaif-credential-whose-delegation-depth-exceeds-the-verifier-s-locally-cached-trust-anchor-resolu.warrant.json
# Markdown SHA-256:   8ca8bc7184180e76c4c70ad5469a32180b5fd6d16ec334a3eb378b9b23f58d4c
# Code AST SHA-256:   2401d2a2cca292118f3f1c911a098dbdef3935bfbd54fec243c80ed7e2dd5c8a
# Verified Classes:   DelegationHorizonExceeded
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class DelegationHorizonExceeded(RuntimeError):
    """Raised when a delegation chain extends beyond the verifier's resolved anchor horizon."""
    pass


def verify_delegation_chain(chain, resolved_anchor_ids, cache_epoch, current_epoch):
    presented_depth = len(chain)
    horizon = len(resolved_anchor_ids)

    # Condition 4: cache must not have rotated mid-validation.
    if cache_epoch != current_epoch:
        raise DelegationHorizonExceeded(
            f"anchor cache rotated during validation (epoch {cache_epoch} -> {current_epoch}); "
            "partial results discarded, live re-resolution required"
        )

    # Condition 2: depth must be positional, no reset or skip.
    for i, link in enumerate(chain, start=1):
        if link.depth != i:
            raise DelegationHorizonExceeded(
                f"delegation depth reset/skip at link {i}: expected {i}, got {link.depth}"
            )

    # Boundary: never fall through to transitive trust past the horizon.
    if presented_depth > horizon:
        raise DelegationHorizonExceeded(
            f"presented_depth={presented_depth} exceeds resolved horizon={horizon}; "
            "refusing transitive-trust shortcut"
        )

    # Condition 3: leaf delegator must chain to last resolved link.
    if chain[-1].delegator not in resolved_anchor_ids:
        raise DelegationHorizonExceeded(
            f"leaf delegator {chain[-1].delegator!r} not in resolved anchor set"
        )

    verified_depth = presented_depth
    assert verified_depth == presented_depth
    return {"status": "valid", "verified_depth": verified_depth}
