# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-narrow-a-delegated-capability-scope-it-cannot-verify.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-narrow-a-delegated-capability-scope-it-cannot-verify.warrant.json
# Markdown SHA-256:   331cdb1f4a7ed2cc859379e500e88de6c90889d4626071e00c660612d9adc583
# Code AST SHA-256:   c3c4b2cac9f86fce5b894982f1106a9feec0da47dc2f309357565baa762d1b4c
# Verified Classes:   UnverifiableNarrowingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnverifiableNarrowingError(RuntimeError):
    """Raised when an agent cannot certify a child scope as a semantic subset of its parent."""
    pass


def delegate_narrowed_scope(parent_scope, child_scope, evaluator):
    """
    Mint a narrowed child scope only if the subset relation can be certified
    over expanded capability sets in a single shared evaluation context.
    """
    # Refuse if either scope contains unresolved symbolic references.
    if not evaluator.is_fully_expandable(parent_scope):
        raise UnverifiableNarrowingError(
            f"parent scope {parent_scope!r} contains unresolved references; "
            "cannot certify narrowing without full expansion context"
        )
    if not evaluator.is_fully_expandable(child_scope):
        raise UnverifiableNarrowingError(
            f"child scope {child_scope!r} contains deferred wildcards or aliases "
            "that this agent cannot resolve in the parent's evaluation context"
        )

    parent_set = evaluator.expand(parent_scope)
    child_set = evaluator.expand(child_scope)

    # Semantic subset check over expanded capability sets, not string prefixes.
    if not child_set.issubset(parent_set):
        leaked = child_set - parent_set
        raise UnverifiableNarrowingError(
            f"child scope is not a semantic subset of parent; "
            f"expansion escapes parent authority: {sorted(leaked)!r}"
        )

    return evaluator.mint(child_scope, proof=("subset", parent_set, child_set))
