# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-decay-weighted-read-as-a-write-and-refuse-to-forget-provenance-bearing-memory.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-decay-weighted-read-as-a-write-and-refuse-to-forget-provenance-bearing-memory.warrant.json
# Markdown SHA-256:   7a327a5e94824500403d6e00e9b9e5ed13a34af2ffb528b1dad6509b4c492ddc
# Code AST SHA-256:   94fbc085ca9da514a2291e75fe975667515342c6a37553a46180e763258c18ca
# Verified Classes:   ProvenanceErasureError, ReadIsWriteError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProvenanceErasureError(RuntimeError):
    """Raised when a forget would orphan a surviving provenance record."""
    pass


class ReadIsWriteError(RuntimeError):
    """Raised when a decision-purpose read would mutate decay weight."""
    pass


def safe_forget(memory_store, unit_id, purpose, pre_read_weight=None):
    """
    Enforce the invariant: no unit may be forgotten while it is the sole
    warrant of a surviving unit, and no inspection read may silently
    refresh decay weight.
    """
    unit = memory_store.get(unit_id)
    if unit is None:
        return  # already gone; idempotent

    # 1. Inspection reads must not mutate weight. If the store's read
    #    bumped last_access, restore it before deciding anything.
    if purpose == "inspect" and pre_read_weight is not None:
        if unit.last_access != pre_read_weight:
            memory_store.set_weight(unit_id, pre_read_weight)

    # 2. Dependency check: is this unit a sole warrant for any survivor?
    dependents = memory_store.inbound_provenance_edges(unit_id)
    for dep in dependents:
        if not dep.provenance.is_self_sufficient():
            raise ProvenanceErasureError(
                f"cannot forget {unit_id}: sole warrant for {dep.id}; "
                f"promote source into dependent provenance first"
            )

    # 3. Provenance-bearing units require explicit justification.
    if unit.provenance and not unit.forget_justification:
        raise ProvenanceErasureError(
            f"cannot forget provenance-bearing unit {unit_id} without "
            f"recorded justification"
        )

    memory_store.delete(unit_id)
    return True


def guarded_read(memory_store, unit_id, purpose):
    """Route inspection reads through a weight-neutral path."""
    if purpose in ("inspect", "audit", "pre_forget"):
        pre = memory_store.get_weight(unit_id)
        unit = memory_store.read_neutral(unit_id)
        if memory_store.get_weight(unit_id) != pre:
            # Store violated neutral-read contract; restore and flag.
            memory_store.set_weight(unit_id, pre)
            raise ReadIsWriteError(
                f"read of {unit_id} for {purpose} mutated weight; "
                f"restored to {pre}"
            )
        return unit
    return memory_store.read(unit_id)  # use-read may refresh
