# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-a-gdpr-erasure-request-has-partially-failed-inside-an-agent-s-semantic-memory-store.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-a-gdpr-erasure-request-has-partially-failed-inside-an-agent-s-semantic-memory-store.warrant.json
# Markdown SHA-256:   b6ef20d729ce25815902dae753c0813e7e1a8704131125b6a7e3b9146ad193e0
# Code AST SHA-256:   748931d4daf9e90532025ce84a3770b942800e77b908d684a644947b3f487131
# Verified Classes:   ErasureUnverifiedError, ErasureReport
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class ErasureUnverifiedError(RuntimeError):
    """Raised when an erasure job reports success but structural signals
    indicate the closure over provenance did not complete."""
    pass


@dataclass
class ErasureReport:
    episodes_deleted: int
    facts_recomputed: int
    orphan_rate: float
    max_provenance_depth: int
    walk_was_single_pass: bool
    baseline_fanout: float
    fanout_sigma: float


def verify_erasure(report: ErasureReport, recall_on_replay: int) -> None:
    """Gate an erasure job's success claim on structural signals and a
    retrieval-replay recall test. Raises if erasure is unverified or failed."""
    if report.episodes_deleted == 0:
        # Nothing to erase is valid only if the subject truly had no presence;
        # caller must have already confirmed presence. Treat as unverified.
        raise ErasureUnverifiedError(
            "zero episodes deleted for a subject with confirmed presence"
        )

    fanout = report.facts_recomputed / report.episodes_deleted
    lower_bound = report.baseline_fanout - 2 * report.fanout_sigma
    if fanout < lower_bound:
        raise ErasureUnverifiedError(
            f"fanout_ratio={fanout:.3f} below baseline {report.baseline_fanout:.3f}"
            f" - 2sigma; closure walk likely missed a provenance layer"
        )

    if report.orphan_rate > 0.001:
        raise ErasureUnverifiedError(
            f"orphan_rate={report.orphan_rate:.4f} exceeds 0.1%; derived artifacts"
            f" without provenance are invisible to the closure walk"
        )

    if report.max_provenance_depth > 1 and report.walk_was_single_pass:
        raise ErasureUnverifiedError(
            f"provenance depth {report.max_provenance_depth} with single-pass walk;"
            f" must iterate to fixed point"
        )

    if recall_on_replay > 0:
        raise ErasureUnverifiedError(
            f"replay recall={recall_on_replay} on subject-seeded queries;"
            f" content remains retrievable despite reported success"
        )
