# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-forget-as-a-two-phase-tombstone-and-purge-and-why-does-single-phase-deletion-silently-break-retrieval-aud.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-forget-as-a-two-phase-tombstone-and-purge-and-why-does-single-phase-deletion-silently-break-retrieval-aud.warrant.json
# Markdown SHA-256:   c2c5778cb9fa698cf88c791be63d819017d657048526ae15f6a7cc2ce7c6ca21
# Code AST SHA-256:   9aa51f7849c2b17be389b110b74ed4cd5e4549f8dab153b31baee8adad76f6fa
# Verified Classes:   UnattributableAbsence, TombstoneStore
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class UnattributableAbsence(RuntimeError):
    """Raised when a record is absent but the store cannot prove why."""
    pass


class TombstoneStore:
    def __init__(self, log, substrates, lineage):
        self.log = log            # durable append-only tombstone log
        self.substrates = substrates  # list of content stores (sql, ann, cache, replicas)
        self.lineage = lineage    # derivation edge graph

    def forget(self, record_id, warrant, now):
        # Phase 1: tombstone first, fsync before touching content.
        tomb = {"id": record_id, "warrant": warrant, "deleted_at": now,
                "state": "TOMBSTONED"}
        self.log.append_and_fsync(tomb)
        # Rewrite lineage edges to the tombstone, never null them.
        for edge in self.lineage.edges_from(record_id):
            self.lineage.rewrite_target(edge, target=tomb["id"])

        # Phase 2: purge content across every substrate.
        pending = []
        for s in self.substrates:
            if not s.delete(record_id):
                pending.append(s.name)
        if pending:
            # Safe state: TOMBSTONED, auditable, retrieval-masked.
            return {"state": "TOMBSTONED", "pending_substrates": pending}

        tomb["state"] = "PURGED"
        self.log.append_and_fsync(tomb)
        return {"state": "PURGED"}

    def resolve_absence(self, record_id):
        tomb = self.log.lookup(record_id)
        if tomb is None:
            # No tombstone: absence is NOT attributable to a deletion.
            raise UnattributableAbsence(
                f"record {record_id} absent with no tombstone; "
                f"cannot distinguish never-written from evicted from lost"
            )
        return {"attributable": True, "warrant": tomb["warrant"],
                "deleted_at": tomb["deleted_at"], "state": tomb["state"]}

    def retrieve(self, record_id):
        # Query-time mask: TOMBSTONED is absent even if substrate lags.
        if self.log.lookup(record_id) is not None:
            return None
        return self._fetch_live(record_id)

    def _fetch_live(self, record_id):
        for s in self.substrates:
            hit = s.get(record_id)
            if hit is not None:
                return hit
        return None
