# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-memory-tool-forget-as-a-tombstone-write-rather-than-a-deletion-and-why-scoped-forget-permissions-break.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-memory-tool-forget-as-a-tombstone-write-rather-than-a-deletion-and-why-scoped-forget-permissions-break.warrant.json
# Markdown SHA-256:   50c2d98ab57befbae33ff70ec471cfd58a3ee9866fb5eb3122056132183d3547
# Code AST SHA-256:   f322062b56173c2cbf06031464c00bf6b9ed806059b351db9d8572329b67fe14
# Verified Classes:   UnverifiedForgetError, ScopedMemoryClient
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnverifiedForgetError(RuntimeError):
    """Raised when an instance acts on a forget success it cannot verify as erasure."""
    pass


class ScopedMemoryClient:
    def __init__(self, scopes: set[str], store: dict):
        self.scopes = scopes
        self.store = store  # key -> {"value": ..., "tombstone": bool, "supersedes": str|None}

    def forget(self, key: str) -> dict:
        if "forget" not in self.scopes:
            raise PermissionError(f"no forget scope for {key}")
        # Soft-delete: write a tombstone, do not remove the record.
        rec = self.store.setdefault(key, {"value": None, "tombstone": False, "supersedes": None})
        rec["tombstone"] = True
        return {"status": "ok", "retention": "tombstone", "key": key}

    def can_inspect_audit(self) -> bool:
        return "read:audit" in self.scopes

    def safe_rewrite(self, key: str, new_value, justification: str) -> None:
        existing = self.store.get(key)
        if existing and existing.get("tombstone"):
            if not self.can_inspect_audit():
                raise UnverifiedForgetError(
                    f"key {key!r} may hold a tombstone but instance lacks read:audit; "
                    f"cannot confirm erasure before rewrite"
                )
            if not justification:
                raise UnverifiedForgetError(
                    f"rewrite of tombstoned key {key!r} requires a justification record"
                )
            self.store[key] = {
                "value": new_value,
                "tombstone": False,
                "supersedes": key,
            }
            return
        self.store[key] = {"value": new_value, "tombstone": False, "supersedes": None}


def repair_deadline(client: ScopedMemoryClient) -> None:
    client.forget("C1")
    # Instance must NOT assume C1 is erased; route through safe_rewrite.
    client.safe_rewrite("C1", "deadline=2025-06-16", justification="C1 based on hallucinated tool output")
