# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-distinguish-memory-forgotten-from-memory-never-written-when-scoped-memory-access-control-collapses-both-into-an-empty-read.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-distinguish-memory-forgotten-from-memory-never-written-when-scoped-memory-access-control-collapses-both-into-an-empty-read.warrant.json
# Markdown SHA-256:   f23488893efb1a0e0caec43640fc37e3720bc14db36d7a79e956d782f97879d8
# Code AST SHA-256:   761e79f165e62df604b5477e84e56ac1d906f70370ff653983625cfa46c9f0ea
# Verified Classes:   MemoryStateAmbiguityError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class MemoryStateAmbiguityError(RuntimeError):
    """Raised when an empty read cannot be distinguished from denied or forgotten."""
    pass


def safe_initialize(memory, scope: str, key: str, value) -> None:
    status = memory.probe(scope, key)  # must return exists/accessible/forgotten
    if status is None:
        raise MemoryStateAmbiguityError(
            f"scope={scope!r} key={key!r}: store returned no status; "
            "empty read is ambiguous between absent, forgotten, and forbidden"
        )
    if not status.accessible:
        raise MemoryStateAmbiguityError(
            f"scope={scope!r} key={key!r}: read denied; refusing to write as if virgin"
        )
    if status.forgotten:
        raise MemoryStateAmbiguityError(
            f"scope={scope!r} key={key!r}: previously forgotten; "
            "re-write would resurrect erased data"
        )
    if status.exists:
        return  # already present, no-op
    memory.write(scope, key, value, if_absent=True)
