# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-s-delegated-capability-token-survive-revocation-across-a-scope-boundary-it-never-crossed.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-s-delegated-capability-token-survive-revocation-across-a-scope-boundary-it-never-crossed.warrant.json
# Markdown SHA-256:   7db5e3b4d8a6e0bd13a682c1cc51ee2851fc3d42411e167c2ce8214257e86d9c
# Code AST SHA-256:   deaf29bc4f55e676b81a7ca7aea4b5f4a40c6fe739150e909cd49c7b4db0938b
# Verified Classes:   ScopeAnchoredRevocationGap
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class ScopeAnchoredRevocationGap(RuntimeError):
    """Raised when a delegated token's verification path fails to consult
    an ancestor scope's revocation epoch, leaving a revocation shadow."""
    pass


def verify_delegated_token(token, epoch_store, max_depth=8):
    """
    Enforce epoch-chain closure: every scope in the delegation chain must
    report the parent's issue-epoch as still current.

    token: object with .scope, .delegation_chain (list of links), .expiry
    epoch_store: dict mapping scope -> current_epoch
    Each link: {parent_token_id, parent_scope, parent_epoch_at_issue}
    """
    if not token.delegation_chain:
        # Root token: leaf-anchored check is sound here.
        if epoch_store.get(token.scope) != token.issue_epoch:
            raise ScopeAnchoredRevocationGap(
                f"root token {token.id} issued at stale epoch"
            )
        return True

    if len(token.delegation_chain) > max_depth:
        raise ScopeAnchoredRevocationGap(
            f"delegation depth {len(token.delegation_chain)} exceeds {max_depth}"
        )

    for depth, link in enumerate(token.delegation_chain):
        parent_scope = link.get("parent_scope")
        parent_epoch = link.get("parent_epoch_at_issue")

        # The critical guard: a missing anchor is a failure, not a pass.
        if parent_scope is None or parent_epoch is None:
            raise ScopeAnchoredRevocationGap(
                f"delegation link {depth} for token {token.id} lacks "
                f"revocation anchor; cannot prove parent not revoked"
            )

        current = epoch_store.get(parent_scope)
        if current is None:
            raise ScopeAnchoredRevocationGap(
                f"ancestor scope {parent_scope} unknown to verifier at "
                f"depth {depth}; epoch-chain closure cannot be established"
            )

        if current != parent_epoch:
            raise ScopeAnchoredRevocationGap(
                f"ancestor scope {parent_scope} advanced from epoch "
                f"{parent_epoch} to {current}; token {token.id} is inside "
                f"a revocation shadow"
            )

    # Leaf check last, after the chain is proven intact.
    if epoch_store.get(token.scope) != token.issue_epoch:
        raise ScopeAnchoredRevocationGap(
            f"leaf scope {token.scope} epoch advanced; token {token.id} revoked"
        )

    return True
