# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-differential-privacy-forget-when-federated-memory-tombstones-collide-across-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-differential-privacy-forget-when-federated-memory-tombstones-collide-across-trust-domains.warrant.json
# Markdown SHA-256:   942bf7cf99a61795d97a77d05deb0d07d5623f71ae294b361e7d862333b230ef
# Code AST SHA-256:   91ac99691027d4085b86fb5d9efdce43b463a20802a5b087e228ea665cfac3fe
# Verified Classes:   ComposingForgetError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ComposingForgetError(RuntimeError):
    """Raised when cross-domain DP forgets compose into a sharper estimate
    than any single domain held, violating the forget-reduces-information invariant."""
    pass


def apply_federated_forget(slot, incoming_tombstone, canary_probe, shift_delta, noise_scale):
    owners = slot.release_owners | {incoming_tombstone.budget_owner_id}

    # Invariant 1: multiple independent budget owners => composing forget.
    if len(owners) >= 2:
        raise ComposingForgetError(
            f"slot {slot.id}: {len(owners)} independent budget owners "
            f"{owners} for subject {incoming_tombstone.subject}; "
            f"averaging would halve noise variance and sharpen the estimate."
        )

    # Invariant 2: noise swamps the removal => cosmetic, not effective.
    if noise_scale > abs(shift_delta):
        slot.mark("forget_unverified")
        return slot  # tombstone applied for compliance only

    # Invariant 3: canary divergence must fall, not rise.
    pre, post = canary_probe.divergence_before, canary_probe.divergence_after
    if post >= pre:
        raise ComposingForgetError(
            f"slot {slot.id}: canary divergence rose {pre:.3f}->{post:.3f}; "
            f"forget increased recoverability of subject {incoming_tombstone.subject}."
        )

    slot.apply_tombstone(incoming_tombstone)
    slot.mark("forget_effective")
    return slot
