# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-scoped-memory-erasure-acknowledgement-as-non-transitive-across-derived-semantic-state.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-scoped-memory-erasure-acknowledgement-as-non-transitive-across-derived-semantic-state.warrant.json
# Markdown SHA-256:   3bf3070d5b5a6b7ff8e70b882e09cab2618d8bef39a1161edf8837b45dbaa269
# Code AST SHA-256:   1c6cd57640a2abc8425e6fff6c7c28bbb7a4ccad95ae87a67ace8f8d9a713a69
# Verified Classes:   NonTransitiveErasureError, ErasureAck
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class NonTransitiveErasureError(RuntimeError):
    """Raised when an agent tries to assert global erasure from a scoped ack."""
    pass


@dataclass
class ErasureAck:
    subject_id: str
    records_removed: int
    scope: str                      # e.g. "episodic"
    transitive: bool                # closure fully resolved?
    unresolved_derivations: list = field(default_factory=list)
    edge_dispositions: dict = field(default_factory=dict)  # edge_id -> "deleted"|"re-derived"|"quarantined"


def assert_subject_fully_erased(ack: ErasureAck) -> None:
    """
    Agent-layer guard: only permit a 'no data held' assertion when the ack
    covers the transitive derivation closure. A faithful-but-local ack
    (records_removed>0, status ok) is NOT sufficient.
    """
    if not ack.transitive:
        raise NonTransitiveErasureError(
            f"subject {ack.subject_id}: ack scope={ack.scope!r} is non-transitive; "
            f"unresolved derivations={ack.unresolved_derivations!r}. "
            f"Must enter taint-filter mode and emit reconciliation task; "
            f"may NOT assert 'no data held'."
        )
    bad = {e: d for e, d in ack.edge_dispositions.items()
           if d not in ("deleted", "re-derived", "quarantined")}
    if bad:
        raise NonTransitiveErasureError(
            f"subject {ack.subject_id}: derivation closure incomplete: {bad!r}"
        )
    # safe to propagate global erasure fact downstream


def handle_erasure_response(ack: ErasureAck) -> str:
    try:
        assert_subject_fully_erased(ack)
        return "ERASED_GLOBAL"
    except NonTransitiveErasureError:
        return "ERASED_EPISODIC_TAINTED_ELSEWHERE"
