# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-tool-s-success-response-as-non-authoritative-for-provenance-graph-memory-poisoning-detection.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-tool-s-success-response-as-non-authoritative-for-provenance-graph-memory-poisoning-detection.warrant.json
# Markdown SHA-256:   a79f4dfd97f892a9f1ca54a8961d3427f9e9c8d1555592e6b69633bf43dca376
# Code AST SHA-256:   b5f25eaf951f9df4d37b2780a86ef92b1ee82ebd7dfcf8365ceee0413a462d0f
# Verified Classes:   NonAuthoritativeForgetError, MemNode, ProvenanceStore
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Protocol


class NonAuthoritativeForgetError(RuntimeError):
    """Raised when a forget tool reports success but the retraction did not
    cover the taint closure, leaving live derivations of the poisoned node."""


@dataclass
class MemNode:
    node_id: str
    parents: list[str] = field(default_factory=list)
    tainted: bool = False


class ProvenanceStore(Protocol):
    def get(self, node_id: str) -> MemNode | None: ...
    def descendants(self, node_id: str) -> set[str]: ...
    def rejects_dangling_parent_promotion(self) -> bool: ...
    def forget(self, node_id: str) -> dict: ...


def verified_forget(store: ProvenanceStore, target_id: str) -> set[str]:
    """Forget a poisoned node only if the retraction is closure-complete.

    Returns the set of nodes confirmed retracted. Raises if the tool's success
    response is non-authoritative for poisoning remediation.
    """
    target = store.get(target_id)
    if target is None:
        raise NonAuthoritativeForgetError(
            f"target {target_id} absent before forget; cannot verify closure"
        )

    closure = store.descendants(target_id) | {target_id}

    if not store.rejects_dangling_parent_promotion():
        raise NonAuthoritativeForgetError(
            "store promotes dangling children to roots; forget cannot be "
            "authoritative for poisoning remediation"
        )

    response = store.forget(target_id)
    if response.get("status") != "deleted":
        raise NonAuthoritativeForgetError(
            f"forget reported non-success: {response!r}"
        )

    affected = set(response.get("affected", []))
    if not closure.issubset(affected):
        missing = closure - affected
        raise NonAuthoritativeForgetError(
            f"partial retraction: poisoned derivatives {sorted(missing)} "
            f"survive as live memory; success response is non-authoritative"
        )

    for nid in affected:
        if store.get(nid) is not None:
            raise NonAuthoritativeForgetError(
                f"node {nid} reported retracted but still resolvable"
            )

    return affected
