# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-re-delegated-a2a-warrant-whose-scope-is-a-strict-subset-of-its-own-and-how-does-attenuation-by-subset.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-re-delegated-a2a-warrant-whose-scope-is-a-strict-subset-of-its-own-and-how-does-attenuation-by-subset.warrant.json
# Markdown SHA-256:   0d282d645610917674dfbd3638e9cbdeb7e570a937b0a15f53d9e75a210a2c70
# Code AST SHA-256:   9c55feeea5f55301e093ba3cffe7c2851efe074d94ec2f280a34c6c06fe86db3
# Verified Classes:   AttenuationProofError, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AttenuationProofError(RuntimeError):
    """Raised when a broker cannot prove child scope ⊆ parent scope under the executor's resolver."""
    pass


class Warrant:
    def __init__(self, scope, verbs, resolver_id, resolver_version, parent=None):
        self.scope = scope
        self.verbs = verbs
        self.resolver_id = resolver_id
        self.resolver_version = resolver_version
        self.parent = parent


def re_delegate(parent: Warrant, child_scope, child_verbs,
                executor_resolver_id: str, executor_resolver_version: str) -> Warrant:
    # Invariant 1: resolver commensurability. If we cannot name the executor's
    # resolver, we cannot prove subset under its semantics.
    if (parent.resolver_id != executor_resolver_id
            or parent.resolver_version != executor_resolver_version):
        raise AttenuationProofError(
            f"resolver mismatch: parent attenuated under "
            f"{parent.resolver_id}@{parent.resolver_version}, "
            f"executor enforces {executor_resolver_id}@{executor_resolver_version}; "
            f"subset assertion is unprovable"
        )

    # Invariant 2: intensional scopes require an explicit, resolver-native
    # subset proof. Globs/prefixes/verb-hierarchies cannot be compared as strings.
    if _is_intensional(parent.scope) or _is_intensional(child_scope):
        if not _prove_subset_intensional(parent.scope, child_scope,
                                         executor_resolver_id,
                                         executor_resolver_version):
            raise AttenuationProofError(
                f"cannot prove {child_scope!r} ⊆ {parent.scope!r} under "
                f"{executor_resolver_id}@{executor_resolver_version}; "
                f"refusing to sign subset assertion"
            )

    # Invariant 3: verb lattice consistency.
    if not _verbs_subset(child_verbs, parent.verbs,
                         executor_resolver_id, executor_resolver_version):
        raise AttenuationProofError(
            f"verb set {child_verbs!r} widens parent {parent.verbs!r} "
            f"under executor verb lattice"
        )

    return Warrant(child_scope, child_verbs,
                   executor_resolver_id, executor_resolver_version,
                   parent=parent)


def _is_intensional(scope) -> bool:
    return any(c in scope for c in "*?[") or scope.endswith("/")


def _prove_subset_intensional(parent_scope, child_scope, rid, rver) -> bool:
    # Canonicalize BEFORE subset evaluation; reject on any post-expansion
    # normalization that could escape the parent prefix.
    if ".." in child_scope or ".." in parent_scope:
        return False
    return child_scope.startswith(parent_scope) and child_scope != parent_scope


def _verbs_subset(child_verbs, parent_verbs, rid, rver) -> bool:
    # Flat exact-match only; hierarchical lattices must be expanded first.
    return set(child_verbs).issubset(set(parent_verbs))
