# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-delegated-aaif-credentials-from-silently-exceeding-their-scope-across-chained-agent-hops.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-delegated-aaif-credentials-from-silently-exceeding-their-scope-across-chained-agent-hops.warrant.json
# Markdown SHA-256:   d760eeda03b5c295ad2ca2cead5fccb8083103b9579e13da6e75c69fe7d8806c
# Code AST SHA-256:   d3ae1636bd203473c3035cd992b99cab43259369004c6189d9cce16755eedc2c
# Verified Classes:   ResourceEnumerationDrift, DelegatedCredential
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class ResourceEnumerationDrift(RuntimeError):
    """Raised when a delegated credential's resolved resource is not a member
    of the delegation-time resource enumeration, or when the enumeration
    was dropped across a re-delegation hop."""
    pass


class DelegatedCredential:
    def __init__(self, scope, audience, resource_enum, parent_enum=None):
        self.scope = scope
        self.audience = audience
        # resource_enum must be present; None means the field was dropped.
        self.resource_enum = resource_enum
        self.parent_enum = parent_enum


def redelegate(parent: DelegatedCredential, child_scope, child_audience) -> DelegatedCredential:
    """Re-delegate with monotonic resource enumeration. If the child cannot
    compute a subset, it MUST inherit the parent's enumeration verbatim."""
    if parent.resource_enum is None:
        raise ResourceEnumerationDrift(
            f"parent credential has no resource_enum; cannot re-delegate safely"
        )
    # Narrowing scope is fine; enumeration must be a subset (or inherited).
    child_enum = set(parent.resource_enum)  # conservative: inherit verbatim
    return DelegatedCredential(
        scope=child_scope,
        audience=child_audience,
        resource_enum=child_enum,
        parent_enum=set(parent.resource_enum),
    )


def verify_and_resolve(cred: DelegatedCredential, logical_name: str, registry: dict) -> str:
    """Verifier-side check: resolve the logical name through the trusted
    registry and assert membership in resource_enum BEFORE execution."""
    if cred.resource_enum is None:
        raise ResourceEnumerationDrift(
            f"credential for {logical_name} lacks resource_enum; refusing execution"
        )
    if logical_name not in registry:
        raise ResourceEnumerationDrift(
            f"logical name {logical_name!r} not resolvable in trusted registry"
        )
    concrete = registry[logical_name]
    if concrete not in cred.resource_enum:
        raise ResourceEnumerationDrift(
            f"resolved resource {concrete!r} for {logical_name!r} is not in "
            f"delegation-time enumeration {sorted(cred.resource_enum)}"
        )
    # Monotonicity check across hops, if parent enumeration is recorded.
    if cred.parent_enum is not None and not set(cred.resource_enum) <= cred.parent_enum:
        raise ResourceEnumerationDrift(
            f"child enumeration {sorted(cred.resource_enum)} is not a subset of "
            f"parent enumeration {sorted(cred.parent_enum)}"
        )
    return concrete
