# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-handle-delegated-capability-tokens-whose-scope-has-silently-widened-after-a-parent-agent-s-revocation-cascade.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-handle-delegated-capability-tokens-whose-scope-has-silently-widened-after-a-parent-agent-s-revocation-cascade.warrant.json
# Markdown SHA-256:   4433468e45c1fcaf7b6f0c0ce579d892b363fa9e0eb5a3c90a7470e287730246
# Code AST SHA-256:   3a0ceb97f6b1da651890d8a203d25a032d68a9346a26f14e155da1cee0c35d91
# Verified Classes:   RevocationWideningError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class RevocationWideningError(RuntimeError):
    """Raised when a delegated token's scope would widen during revocation resolution."""


def resolve_delegated_scope(
    token: dict,
    ancestor_chain: list[dict],
    revocation_service,
) -> set[str]:
    """
    Resolve the effective scope of a delegated capability token.

    Invariant: a token's effective scope is its signed materialized_scope.
    Re-derivation from the ancestor chain is permitted only for narrowing,
    never for resolving a complement against a live-or-revoked ancestor.
    """
    materialized = token.get("materialized_scope")
    if materialized is None:
        # No signed materialized scope: this token is malformed for delegation.
        # Fail closed rather than attempt lazy resolution.
        raise RevocationWideningError(
            f"token {token.get('id')} has no materialized_scope; "
            "lazy complement resolution is unsafe under revocation"
        )

    materialized_set = set(materialized)

    # Walk the ancestor chain only to detect revocation, never to recompute scope.
    for ancestor in ancestor_chain:
        status = revocation_service.status(ancestor["id"])
        if status == "revoked":
            # Parent revoked: child halts under its materialized scope, does not widen.
            # Caller must treat the returned set as frozen: no further delegation.
            return materialized_set
        if status == "unresolvable":
            # Ancestor record absent or unqueryable: cannot verify chain integrity.
            # The materialized scope remains valid (it was signed at issue time),
            # but we must not permit re-derivation or further delegation.
            return materialized_set

    # All ancestors live: re-derivation is still forbidden for resolution.
    # Narrowing operations may proceed against materialized_set only.
    return materialized_set


def authorize_tool_call(
    token: dict,
    tool: str,
    ancestor_chain: list[dict],
    revocation_service,
) -> None:
    scope = resolve_delegated_scope(token, ancestor_chain, revocation_service)
    if tool not in scope:
        raise RevocationWideningError(
            f"tool {tool!r} not in materialized scope of token {token.get('id')}; "
            "refusing to resolve against ancestor chain (widening risk)"
        )
