Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(context): deep-copy store results in ErasureReceipt.to_dict() (#1381)
Chetan Chittori754fe5fdd74d
diff --git a/semantica/context/erasure.py b/semantica/context/erasure.pyindex a676f61e..f1720177 100644--- a/semantica/context/erasure.py+++ b/semantica/context/erasure.py@@ -34,6 +34,7 @@ Example:'unsupported'"""+import copyimport inspectfrom dataclasses import dataclass, fieldfrom datetime import datetime, timezone@@ -117,13 +118,21 @@ class ErasureReceipt:]def to_dict(self) -> Dict[str, Any]:- """Serialize the receipt, deep-copying the per-store results."""+ """Serialize the receipt, deep-copying the per-store results.++ Each store result is copied recursively so the returned payload shares+ no mutable objects with the live receipt: mutating+ ``payload["stores"][name][...]`` (including nested dicts such as a+ vector backend's ``backend_result``) cannot corrupt the audit record.+ """return {"entity_id": self.entity_id,"reason": self.reason,"erased_at": self.erased_at,"complete": self.complete,- "stores": {name: dict(result) for name, result in self.stores.items()},+ "stores": {+ name: copy.deepcopy(result) for name, result in self.stores.items()+ },}diff --git a/tests/context/test_erasure_coordinator.py b/tests/context/test_erasure_coordinator.pyindex e4dcb21a..7bb6158a 100644--- a/tests/context/test_erasure_coordinator.py+++ b/tests/context/test_erasure_coordinator.py@@ -399,6 +399,121 @@ class TestReceipt(unittest.TestCase):self.assertEqual(receipt.stores["graph"]["status"], STATUS_ERASED)+ def test_to_dict_deep_copies_nested_store_results(self):+ """A nested dict in a store result is not shared with the live receipt.++ ``backend_result`` from a vector backend is a dict of its own, so a+ shallow per-store copy leaves it referenced by both the payload and the+ receipt -- sanitizing the payload for a user-facing response would+ silently corrupt the audit record.+ """+ receipt = ErasureReceipt(+ entity_id="customer-4471",+ stores={+ "vectors": {+ "status": STATUS_ERASED,+ "backend": "qdrant",+ "backend_result": {"status": "completed"},+ },+ },+ )++ payload = receipt.to_dict()+ payload["stores"]["vectors"]["backend_result"]["status"] = "redacted"++ self.assertEqual(+ receipt.stores["vectors"]["backend_result"]["status"], "completed"+ )++ def test_to_dict_store_results_are_distinct_objects(self):+ """The per-store dict and any nested dict in the payload must not be+ the same objects as the ones in the live receipt.++ A mutation test proves *isolation* only when the copy actually+ happened; identity checks prove *that* a copy was made.+ """+ receipt = ErasureReceipt(+ entity_id="customer-4471",+ stores={+ "vectors": {+ "status": STATUS_ERASED,+ "backend": "qdrant",+ # Realistic Qdrant-shaped backend_result with rendered enum+ "backend_result": {"status": "UpdateStatus.COMPLETED", "points": 1},+ "vector_ids": 2,+ "via": "delete_vectors",+ },+ "graph": {"status": STATUS_ERASED, "nodes": 1, "edges": 3},+ },+ )++ payload = receipt.to_dict()++ # The stores container itself is a new dict.+ self.assertIsNot(payload["stores"], receipt.stores)++ # Each per-store result dict is a new object.+ self.assertIsNot(+ payload["stores"]["vectors"], receipt.stores["vectors"]+ )+ self.assertIsNot(+ payload["stores"]["graph"], receipt.stores["graph"]+ )++ # The nested backend_result dict is also a new object.+ self.assertIsNot(+ payload["stores"]["vectors"]["backend_result"],+ receipt.stores["vectors"]["backend_result"],+ )++ # Values are equal (correct copy), not just distinct references.+ self.assertEqual(+ payload["stores"]["vectors"]["backend_result"],+ {"status": "UpdateStatus.COMPLETED", "points": 1},+ )+ self.assertEqual(payload["stores"]["graph"], {"status": STATUS_ERASED, "nodes": 1, "edges": 3})++ def test_to_dict_isolation_across_multiple_stores(self):+ """Mutations to any store in the payload must not affect any other+ store in either the payload or the live receipt.++ This catches a hypothetical implementation that shares a single deep+ copy across all stores rather than copying each independently.+ """+ receipt = ErasureReceipt(+ entity_id="e1",+ stores={+ "vectors": {+ "status": STATUS_UNSUPPORTED,+ "backend": "faiss",+ "vector_ids": 1,+ "detail": "backend exposes no delete()",+ },+ "memory": {"status": STATUS_ERASED, "items": 4},+ "graph": {+ "status": STATUS_ERASED,+ "nodes": 1,+ "edges": 2,+ },+ },+ )++ payload = receipt.to_dict()++ # Mutate every store in the payload.+ payload["stores"]["vectors"]["status"] = "tampered"+ payload["stores"]["memory"]["items"] = 0+ payload["stores"]["graph"]["nodes"] = 99++ # None of the live receipt's stores are affected.+ self.assertEqual(receipt.stores["vectors"]["status"], STATUS_UNSUPPORTED)+ self.assertEqual(receipt.stores["memory"]["items"], 4)+ self.assertEqual(receipt.stores["graph"]["nodes"], 1)++ # The other stores in the payload are also unaffected (no aliasing).+ self.assertEqual(payload["stores"]["memory"]["items"], 0) # our mutation+ self.assertEqual(receipt.stores["memory"]["items"], 4) # unchanged+def test_receipt_and_tombstone_agree_on_when_the_erasure_happened(self):graph = _graph()receipt = ErasureCoordinator(graph=graph).erase_entity(