# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-forget-tool-call-has-actually-propagated-through-an-agent-s-derived-memory-tier-rather-than-merely-de-indexed-the-s.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-forget-tool-call-has-actually-propagated-through-an-agent-s-derived-memory-tier-rather-than-merely-de-indexed-the-s.warrant.json
# Markdown SHA-256:   a7b31aeff04538b722dbef2a632980e363b5620363ee74e17466fa767ace872e
# Code AST SHA-256:   2689960f0395de76ccd7452b76e253b2e051369bd7a589778d650efd39f53456
# Verified Classes:   UnlearnVerificationError, ForgetAck
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class UnlearnVerificationError(RuntimeError):
    """Raised when a forget ack cannot be shown to cover all registered tiers."""


@dataclass
class ForgetAck:
    subject_id: str
    ok: bool
    tiers_covered: list[str]
    tiers_registered: list[str]
    records_deleted: int


def assert_unlearned(
    ack: ForgetAck,
    reconstruction_probe: callable,
    min_probe_similarity: float = 0.80,
) -> None:
    """
    Enforce the total-ack invariant: a forget is only 'unlearned' when the ack
    covers every registered tier AND a content-addressed reconstruction probe
    fails to recover subject-attributable facts.
    """
    registered = set(ack.tiers_registered)
    covered = set(ack.tiers_covered)

    if not registered:
        raise UnlearnVerificationError(
            f"store declared no tier registry for {ack.subject_id}; "
            "unenumerable store cannot produce a sound forget"
        )

    uncovered = registered - covered
    if uncovered:
        raise UnlearnVerificationError(
            f"forget for {ack.subject_id} left tiers uncovered: {sorted(uncovered)}; "
            f"ack.ok={ack.ok} is a partial operation, not completion"
        )

    # Content-addressed probe: ask by paraphrase, not by subject id.
    recovered = reconstruction_probe(ack.subject_id)
    if recovered is not None and recovered.get("similarity", 0.0) >= min_probe_similarity:
        raise UnlearnVerificationError(
            f"derived artifact still recoverable for {ack.subject_id} "
            f"(sim={recovered['similarity']:.2f}, key={recovered.get('key')}); "
            "de-index succeeded but unlearning did not propagate"
        )


def mark_consent(ack: ForgetAck, probe: callable) -> str:
    try:
        assert_unlearned(ack, probe)
    except UnlearnVerificationError as exc:
        # Hold the obligation open; do not emit consent_satisfied.
        return f"consent_provisional: {exc}"
    return "consent_satisfied"
