# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-delegated-aaif-credential-scoping-has-silently-widened-past-its-warrant.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-delegated-aaif-credential-scoping-has-silently-widened-past-its-warrant.warrant.json
# Markdown SHA-256:   8a615cd396e825aa4afb28301cfa173095763fb949c8d992a5645a04161663af
# Code AST SHA-256:   9016cb92a156d0459e40ddcd534a9f9d6a04fe88639cdb802e8bd6acf07e2691
# Verified Classes:   DelegationScopeWideningError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class DelegationScopeWideningError(RuntimeError):
    """Raised when a delegated AAIF credential's declared scope is not a
    proper subset of its parent's reconstructible effective scope on a
    narrowing edge. Indicates intent-loss at mint time, not a signature
    failure."""
    pass


def mint_child_warrant(parent_warrant: dict, child_scope: set[str],
                       edge_type: str, delegator_key_sign) -> dict:
    parent_scope = set(parent_warrant["scope"])
    reconstructible = set(parent_warrant.get("reconstructible_eff", parent_scope))

    if edge_type == "narrowing":
        if child_scope == parent_scope:
            raise DelegationScopeWideningError(
                f"narrowing edge {parent_warrant['id']} -> child minted with "
                f"scope == parent scope ({sorted(child_scope)}); intent-loss "
                f"at mint time. Re-derive intent_scope before minting."
            )
        if not child_scope < parent_scope:
            raise DelegationScopeWideningError(
                f"child scope {sorted(child_scope)} is not a proper subset of "
                f"parent scope {sorted(parent_scope)} on narrowing edge."
            )

    if not child_scope <= reconstructible:
        unbacked = child_scope - reconstructible
        raise DelegationScopeWideningError(
            f"child claims {sorted(unbacked)} not derivable from credential "
            f"chain; session-held authority is not delegable."
        )

    attenuation = {
        "removed": sorted(parent_scope - child_scope),
        "edge_type": edge_type,
        "signed_by": delegator_key_sign(parent_warrant["id"]),
    }
    return {
        "parent": parent_warrant["id"],
        "scope": sorted(child_scope),
        "attenuation": attenuation,
    }
