# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-a-federated-agent-s-delegated-warrant-from-silently-outliving-its-home-domain-revocation-during-cross-org-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-a-federated-agent-s-delegated-warrant-from-silently-outliving-its-home-domain-revocation-during-cross-org-handoff.warrant.json
# Markdown SHA-256:   e1079802da469ee6f99b111d0f3d90bf5d8281a3afb856d30c2770aa8fe6e03f
# Code AST SHA-256:   d7a45402b20f46eb545648c2053c6b1a9b956e0b0046410ba1fb379cbadb5717
# Verified Classes:   RevocationShadowError
# Defensive Guards:   7 fail-closed invariants (zero external deps)

class RevocationShadowError(RuntimeError):
    """Raised when a delegation chain would admit a request whose root warrant
    is revoked but whose leaf is not, i.e. a revocation shadow."""
    pass


def verify_chain(chain, revocation_set, now, max_depth):
    # chain: list of warrants ordered root -> leaf.
    # Each warrant: {"id", "root_warrant_id", "issuer", "aud", "depth", "scope",
    #                "exp", "sig_ok"}
    if not chain:
        raise RevocationShadowError("empty delegation chain")

    root = chain[0]
    root_id = root["root_warrant_id"]

    # Invariant 1: every element must carry the same root identity.
    for w in chain:
        if w.get("root_warrant_id") != root_id:
            raise RevocationShadowError(
                f"warrant {w['id']} has root {w.get('root_warrant_id')!r}, "
                f"expected {root_id!r}; chain splicing or stripped root id"
            )

    # Invariant 2: revocation checked against EVERY hop, at use time.
    for w in chain:
        if w["id"] in revocation_set or root_id in revocation_set:
            raise RevocationShadowError(
                f"warrant {w['id']} (root {root_id}) is revoked; "
                f"refusing admission despite valid signature chain"
            )

    # Invariant 3: depth recomputed from structure, never read from claim.
    recomputed_depth = len(chain) - 1
    if recomputed_depth > max_depth:
        raise RevocationShadowError(
            f"recomputed depth {recomputed_depth} exceeds max {max_depth}; "
            f"claimed depth {chain[-1].get('depth')!r} is not authoritative"
        )

    # Invariant 4: freshness — every hop still within exp at use time.
    for w in chain:
        if now >= w["exp"]:
            raise RevocationShadowError(
                f"warrant {w['id']} expired at {w['exp']}, now {now}"
            )

    # Invariant 5: scope must narrow monotonically root -> leaf.
    for parent, child in zip(chain, chain[1:]):
        if not set(child["scope"]).issubset(parent["scope"]):
            raise RevocationShadowError(
                f"warrant {child['id']} widens scope beyond parent {parent['id']}"
            )

    # Invariant 6: audience reachability — leaf aud must be reachable from root aud.
    if chain[-1]["aud"] not in root.get("delegable_audiences", []):
        raise RevocationShadowError(
            f"leaf audience {chain[-1]['aud']!r} not reachable from root audience"
        )

    return True
