# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-attenuate-delegated-scope-collapse-in-aaif-capability-tokens-under-non-monotonic-tool-schemas.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-attenuate-delegated-scope-collapse-in-aaif-capability-tokens-under-non-monotonic-tool-schemas.warrant.json
# Markdown SHA-256:   f8ee4da99e7f544caef35000b2f15a2468701fb7f1cfd2205c77bea5a6dc97ab
# Code AST SHA-256:   180bc8eb5f7e7823421c591d74bca83eb7e3b3e84084b93300ae41fc06ce68f3
# Verified Classes:   UnprovableAttenuationError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class UnprovableAttenuationError(RuntimeError):
    """Raised when a child scope cannot be proven a subset of the parent scope
    under a closed, injective canonical algebra. Never attenuate on doubt."""
    pass


def attenuate(parent_scope: dict, child_scope: dict, canon, parser_subsumed: bool) -> dict:
    # canon: idempotent, injective canonicalizer shared by mint and enforcement.
    # parser_subsumed: True only if the enforcing tool's arg grammar is a strict
    # subset of the grammar the scope checker reasons about.
    if not parser_subsumed:
        raise UnprovableAttenuationError(
            f"tool parser not subsumed by scope grammar; child={child_scope!r}"
        )

    for key, cval in child_scope.items():
        if key not in parent_scope:
            raise UnprovableAttenuationError(
                f"child introduces scope dimension {key!r} absent from parent"
            )
        pval = parent_scope[key]

        # Opaque predicate components (where-clauses, globs, regexes) cannot be
        # proven closed by string comparison; treat as unprovable, not safe.
        if key in ("where", "glob", "regex", "query"):
            if cval != pval:
                raise UnprovableAttenuationError(
                    f"opaque predicate {key!r} altered: parent={pval!r} child={cval!r}"
                )
            continue

        pc, cc = canon(pval), canon(cval)
        if pc != cc and not _is_subset(cc, pc):
            raise UnprovableAttenuationError(
                f"canonical child {cc!r} not subset of canonical parent {pc!r} "
                f"(raw parent={pval!r}, raw child={cval!r})"
            )
    return dict(child_scope)


def _is_subset(child_canon, parent_canon) -> bool:
    # Placeholder for a closed, injective subset relation over the canonical form.
    # Must be total over the enforcement domain or return False.
    return False
