# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-forgets-a-memory-that-an-in-flight-tool-call-is-still-dereferencing-tombstone-versus-handle-races-in-retention-enforced.md # Remnant Warrant: https://corpusfetch.com/c/what-happens-when-an-instance-forgets-a-memory-that-an-in-flight-tool-call-is-still-dereferencing-tombstone-versus-handle-races-in-retention-enforced.warrant.json # Markdown SHA-256: 2d036526e7f8fa7b50cb0b7761c95e7b78abb5cf9bac84a545b5f084e5ea6974 # Code AST SHA-256: 915eab9059c551a869d15f1e0f4b0315b1388998a679aedd2dd7147a78fff539 # Verified Classes: ErasureDereferenceRace, Handle, MemoryStore # Defensive Guards: 2 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) class ErasureDereferenceRace(RuntimeError): """Raised when a memory handle issued before a forget() can still resolve to the erased record body after ERASURE_COMMITTED was logged.""" pass class Handle: __slots__ = ("memory_id", "generation", "blob_ptr", "deref_log") def __init__(self, memory_id, generation, blob_ptr, deref_log): self.memory_id = memory_id self.generation = generation self.blob_ptr = blob_ptr self.deref_log = deref_log class MemoryStore: def __init__(self): self._records = {} # id -> {"body":..., "generation":int, "state":str} self._tombstone_gen = {} # id -> generation at which ERASED was set self.audit = [] # append-only self.deref_log = [] # reader-side, must exist for RBTF provability def read(self, memory_id): rec = self._records.get(memory_id) if rec is None or rec["state"] == "ERASED": return None return Handle(memory_id, rec["generation"], rec["body"], self.deref_log) def forget(self, memory_id): rec = self._records.get(memory_id) if rec is None: return {"status": "absent"} rec["state"] = "ERASED" rec["generation"] += 1 self._tombstone_gen[memory_id] = rec["generation"] self.audit.append(("ERASURE_COMMITTED", memory_id, rec["generation"])) return {"status": "erased"} def deref(self, handle): # Generation check is the guardrail. Without it, this returns the # body of an erased record and the RTBF invariant is silently broken. tomb = self._tombstone_gen.get(handle.memory_id) if tomb is not None and handle.generation < tomb: self.deref_log.append( ("STALE_DEREF_BLOCKED", handle.memory_id, handle.generation, tomb) ) raise ErasureDereferenceRace( f"handle for {handle.memory_id} gen {handle.generation} " f"dereferenced after tombstone gen {tomb}; " f"erasure would have leaked the body" ) self.deref_log.append(("DEREF", handle.memory_id, handle.generation)) return handle.blob_ptr def synthesize(store, memory_ids): handles = [store.read(mid) for mid in memory_ids] handles = [h for h in handles if h is not None] # Compliance erasure lands mid-fan-out, after handles are issued. if handles: store.forget(handles[0].memory_id) evidence = [] for h in handles: try: evidence.append(store.deref(h)) except ErasureDereferenceRace: # Correct behavior: do NOT silently drop as "absent evidence". # Surface the race so the caller can re-derive without the # erased record rather than producing a misleading answer delta. raise return evidence if __name__ == "__main__": store = MemoryStore() store._records["mem_8f2a"] = {"body": "subject S: sensitive", "generation": 1, "state": "LIVE"} store._records["mem_9c11"] = {"body": "unrelated", "generation": 1, "state": "LIVE"} try: synthesize(store, ["mem_8f2a", "mem_9c11"]) except ErasureDereferenceRace as e: print("BLOCKED:", e) print("audit:", store.audit) print("deref_log:", store.deref_log)