# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-aaif-credential-has-outlived-its-issuer-s-attenuation-chain-without-being-revoked.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-aaif-credential-has-outlived-its-issuer-s-attenuation-chain-without-being-revoked.warrant.json
# Markdown SHA-256:   fafb3331c9f98607827c2e8a305ba604d896766869786ea8954989d7703af61f
# Code AST SHA-256:   6cd23ed70e3752cc9205be04011b861641ad86cf63a7809fd8250c7e4068d3dc
# Verified Classes:   AncestryRevokedError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class AncestryRevokedError(RuntimeError):
    """Raised when a delegated credential's ancestry is revoked or unresolvable.

    Distinct from InvalidCredentialError (bad signature) and ExpiredCredentialError
    (past TTL): this fires when the presented token is individually valid but its
    attenuation chain is broken upstream. Silent admission in this case is the
    failure mode this guardrail exists to prevent.
    """


def admit_delegated_call(
    token: dict,
    deny_list: dict,          # keyed by token_id AND chain_root
    revocation_ttl_s: int,
    now: int,
) -> bool:
    # 1. Leaf must be individually valid.
    if not token.get("signature_ok"):
        raise AncestryRevokedError("leaf signature invalid")
    if token["expiry"] <= now:
        raise AncestryRevokedError("leaf expired")

    # 2. Ancestry must be reconstructible. If provenance was stripped during
    #    attenuation, we cannot enforce the invariant and must fail closed.
    chain_root = token.get("chain_root")
    ancestors = token.get("ancestors")  # list of token_ids, root first
    if not chain_root or ancestors is None:
        raise AncestryRevokedError(
            "ancestry unresolvable: token lacks chain_root/ancestors; "
            "fail-closed because revocation cannot be checked"
        )

    # 3. Invariant: every ancestor must be unrevoked at verification time.
    #    Check by chain_root (catches root revocation) and by each ancestor id
    #    (catches mid-chain revocation).
    if chain_root in deny_list.get("revoked_roots", set()):
        raise AncestryRevokedError(
            f"chain_root {chain_root!r} revoked; leaf {token['id']!r} "
            f"is a descendant and must not be admitted"
        )
    revoked_ids = deny_list.get("revoked_ids", set())
    for anc in ancestors:
        if anc in revoked_ids:
            raise AncestryRevokedError(
                f"ancestor {anc!r} revoked; leaf {token['id']!r} inherits revocation"
            )

    # 4. Propagation window: if the root was revoked within the propagation
    #    window, a verifier may not yet have seen it. Fail closed during window.
    root_revoked_at = deny_list.get("root_revoked_at", {}).get(chain_root)
    if root_revoked_at is not None and (now - root_revoked_at) < revocation_ttl_s:
        raise AncestryRevokedError(
            f"chain_root {chain_root!r} revoked at {root_revoked_at}; "
            f"inside propagation window ({revocation_ttl_s}s), fail-closed"
        )

    return True
