# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-aaif-delegated-credential-whose-scope-chain-is-acyclic-but-whose-attenuation-lattice-is-not-a-sub-lat.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-aaif-delegated-credential-whose-scope-chain-is-acyclic-but-whose-attenuation-lattice-is-not-a-sub-lat.warrant.json
# Markdown SHA-256:   df9016d2e41f6ec8c7cca2aadb1c6e53fb85433ee1b52f705fe341b912b73024
# Code AST SHA-256:   241eba9e03fb54a208f511f9504e52f97d0311a02c17a8d9bc109a37e6f7d3c5
# Verified Classes:   SublatticeAttenuationError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class SublatticeAttenuationError(RuntimeError):
    """Raised when a delegated AAIF credential is acyclic and element-wise
    attenuated but its lattice closure is not embeddable in the parent's."""
    pass


def _closure(atoms, join, meet):
    # bounded closure over declared atoms under the token's own operators
    seen = set(atoms)
    frontier = list(atoms)
    while frontier:
        a = frontier.pop()
        for b in list(seen):
            for op in (join, meet):
                c = op(a, b)
                if c is not None and c not in seen:
                    seen.add(c)
                    frontier.append(c)
    return seen


def validate_delegation(parent, child):
    if not set(child.perms) <= set(parent.perms):
        raise SublatticeAttenuationError("element-wise permission escape")
    if not set(parent.obligations) <= set(child.obligations):
        raise SublatticeAttenuationError("obligation weakening")

    # operator-kind mismatch is the silent killer
    if child.obligation_algebra != parent.obligation_algebra:
        child_closure = _closure(child.obligations, child.join, child.meet)
        parent_closure = _closure(parent.obligations, parent.join, parent.meet)
        unreconstructible = child_closure - parent_closure
        if unreconstructible:
            raise SublatticeAttenuationError(
                f"child lattice closure not embeddable in parent: "
                f"{sorted(map(str, unreconstructible))} "
                f"(algebra {child.obligation_algebra} vs {parent.obligation_algebra})"
            )

    child_perm_closure = _closure(child.perms, child.join_perm, child.meet_perm)
    parent_perm_closure = _closure(parent.perms, parent.join_perm, parent.meet_perm)
    if child_perm_closure - parent_perm_closure:
        raise SublatticeAttenuationError(
            "child permission closure admits composite absent from parent"
        )
    return True


def exchange(parent, child):
    try:
        validate_delegation(parent, child)
    except SublatticeAttenuationError as e:
        # refuse at the delegating instance; do not propagate a convention-defaulted meet
        raise SublatticeAttenuationError(
            f"AAIF exchange refused at delegator: {e}"
        ) from e
    return child.mint()
