# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-tombstone-a-memory-write-before-its-retention-scope-closes.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-tombstone-a-memory-write-before-its-retention-scope-closes.warrant.json
# Markdown SHA-256:   c79fc5e1a03135187b3e4a54338de94806f8b4c5b3a3bc5270ef7c8848925003
# Code AST SHA-256:   304f0916ae8457b814e84ec6bbe4f262b7625f3619005189bda15c72f7089507
# Verified Classes:   RedactionClosureError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class RedactionClosureError(RuntimeError):
    """Raised when a forget cannot soundly tombstone its provenance closure."""
    pass


def rooted_forget(store, root_id, peer_registry, now):
    """
    Tombstone every live node reachable from root_id via derived_from edges.
    Refuse (raise) if any reachable node cannot be tombstoned locally or
    acknowledged by its holding peer.
    """
    closure = store.provenance_closure(root_id)  # set of node ids
    live = [n for n in closure if store.is_live(n, now)]

    if not live:
        # Nothing to do; but do NOT report success for a point delete that
        # merely coincided with expiry. Record the vacuous case explicitly.
        return {"status": "vacuous", "tombstoned": []}

    tombstoned = []
    pending_peers = set()
    for node in live:
        holder = store.holder(node)
        if holder == "local":
            store.tombstone(node, redaction_root=root_id)
            tombstoned.append(node)
        else:
            pending_peers.add(holder)

    unacked = [p for p in pending_peers if not peer_registry.acknowledge(p, root_id)]
    if unacked:
        # Local tombstones are necessary but not sufficient. A peer still
        # holding a derived node is an open leak for the node's full TTL.
        raise RedactionClosureError(
            f"redaction root {root_id}: peers {unacked} did not acknowledge "
            f"tombstone; local tombstoned={tombstoned}, closure_live={live}"
        )

    # Post-condition: closure must now contain zero live nodes.
    residual = [n for n in closure if store.is_live(n, now)]
    if residual:
        raise RedactionClosureError(
            f"redaction root {root_id}: residual live nodes after tombstone: {residual}"
        )

    return {"status": "complete", "tombstoned": tombstoned, "peers": list(pending_peers)}
