Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fixed qodo reviews

Sameer63054d1e5cf37cdb
diff --git a/semantica/provenance/manager.py b/semantica/provenance/manager.py
index 248545b8..2eafb937 100644
--- a/semantica/provenance/manager.py
+++ b/semantica/provenance/manager.py
@@ -278,8 +278,12 @@ class ProvenanceManager:
if _conn is not None:
raise
self.logger.error(
- f"Failed to track entity '{entity_id}' (transaction rolled back): {e}. "
- f"Returning pre-failure state ({'existing entry' if existing else 'None (no prior entry existed)'})."
+ "Failed to track entity '%s' (transaction rolled back): %s. "
+ "Returning pre-failure state (%s).",
+ entity_id,
+ e,
+ "existing entry" if existing else "None (no prior entry existed)",
+ exc_info=True,
)
if existing is not None:
return copy.deepcopy(existing)
@@ -477,7 +481,8 @@ class ProvenanceManager:
entity_metadata = {**metadata, **entity.get("metadata", {})}
try:
- self.track_entity(entity_id, source, entity_metadata, _conn=conn)
+ with self.storage.savepoint(conn):
+ self.track_entity(entity_id, source, entity_metadata, _conn=conn)
batch_count += 1
except Exception:
pass # Continue with other entities in this batch
@@ -522,16 +527,17 @@ class ProvenanceManager:
continue
try:
- self.track_chunk(
- chunk_id=chunk_id,
- source_document=source_document,
- source_path=source_path,
- start_index=chunk.get("start_index", 0),
- end_index=chunk.get("end_index", 0),
- parent_chunk_id=chunk.get("parent_chunk_id"),
- _conn=conn,
- **{**metadata, **chunk.get("metadata", {})}
- )
+ with self.storage.savepoint(conn):
+ self.track_chunk(
+ chunk_id=chunk_id,
+ source_document=source_document,
+ source_path=source_path,
+ start_index=chunk.get("start_index", 0),
+ end_index=chunk.get("end_index", 0),
+ parent_chunk_id=chunk.get("parent_chunk_id"),
+ _conn=conn,
+ **{**metadata, **chunk.get("metadata", {})}
+ )
batch_count += 1
except Exception:
pass
diff --git a/semantica/provenance/storage.py b/semantica/provenance/storage.py
index 42f6d251..7da91482 100644
--- a/semantica/provenance/storage.py
+++ b/semantica/provenance/storage.py
@@ -23,6 +23,7 @@ from abc import ABC, abstractmethod
from typing import List, Optional, Dict, Any
import sqlite3
import json
+import uuid
import threading
from collections import deque
from contextlib import contextmanager
@@ -109,6 +110,11 @@ class ProvenanceStorage(ABC):
"""Default transaction context manager for storage backends."""
yield None
+ @contextmanager
+ def savepoint(self, conn: Any = None):
+ """Default savepoint context manager for nested transactions / per-item isolation."""
+ yield conn
+
def _store_with_conn(self, conn: Any, entry: ProvenanceEntry) -> None:
"""Internal store method using an active connection/transaction."""
self.store(entry)
@@ -258,6 +264,12 @@ class InMemoryStorage(ProvenanceStorage):
stack.pop()
raise
+ @contextmanager
+ def savepoint(self, conn: Any = None):
+ """In-memory savepoint context manager with staging buffer rollback."""
+ with self.transaction() as tx:
+ yield tx
+
def _store_with_conn(self, conn: Any, entry: ProvenanceEntry) -> None:
"""Internal store method using an active connection/transaction."""
stack = self._get_pending_stack()
@@ -376,6 +388,32 @@ class SQLiteStorage(ProvenanceStorage):
finally:
conn.close()
+ @contextmanager
+ def savepoint(self, conn: Any = None):
+ """
+ Context manager providing a SAVEPOINT for per-item rollback isolation
+ within an existing transaction.
+
+ If no connection is provided, falls back to a full transaction.
+ """
+ if conn is None:
+ with self.transaction() as tx:
+ yield tx
+ return
+
+ sp_name = f"sp_{uuid.uuid4().hex}"
+ conn.execute(f"SAVEPOINT {sp_name}")
+ try:
+ yield conn
+ conn.execute(f"RELEASE {sp_name}")
+ except Exception:
+ try:
+ conn.execute(f"ROLLBACK TO {sp_name}")
+ conn.execute(f"RELEASE {sp_name}")
+ except Exception:
+ pass
+ raise
+
@contextmanager
def _read_connection(self):
"""
diff --git a/tests/provenance/test_manager.py b/tests/provenance/test_manager.py
index d818501a..87eab374 100644
--- a/tests/provenance/test_manager.py
+++ b/tests/provenance/test_manager.py
@@ -805,4 +805,72 @@ class TestProvenanceManager:
assert in_storage.metadata == {"key": "val"}
assert "tampered" not in in_storage.metadata
+ @pytest.mark.parametrize("backend_type", ["memory", "sqlite"])
+ def test_track_entities_batch_per_item_savepoint_rollback(self, backend_type, tmp_path):
+ """Test that in batch mode, an exception during primary write rolls back any staged history archive for that item."""
+ if backend_type == "memory":
+ class FlakyInMemory(InMemoryStorage):
+ def __init__(self):
+ super().__init__()
+ self.store_calls = 0
+ self.fail_on_call = None
+ def _store_with_conn(self, conn, entry):
+ self.store_calls += 1
+ if self.store_calls == self.fail_on_call:
+ raise RuntimeError(f"Simulated error on call {self.store_calls}")
+ super()._store_with_conn(conn, entry)
+ storage = FlakyInMemory()
+ else:
+ db_path = str(tmp_path / "test_batch_sp.db")
+ class FlakySQLite(SQLiteStorage):
+ def __init__(self, path):
+ super().__init__(path)
+ self.store_calls = 0
+ self.fail_on_call = None
+ def _store_with_conn(self, conn, entry):
+ self.store_calls += 1
+ if self.store_calls == self.fail_on_call:
+ raise RuntimeError(f"Simulated error on call {self.store_calls}")
+ super()._store_with_conn(conn, entry)
+ storage = FlakySQLite(db_path)
+
+ mgr = ProvenanceManager(storage=storage)
+ v1 = mgr.track_entity("e1", source="doc1")
+ assert v1.source_document == "doc1"
+
+ # Fail on call 3 (the primary update for e1, after its history entry was stored on call 2)
+ storage.fail_on_call = 3
+ count = mgr.track_entities_batch(
+ [{"id": "e1"}, {"id": "e2"}],
+ source="doc_batch"
+ )
+
+ assert count == 1 # Only e2 should succeed
+ all_entries = {e.entity_id: e for e in storage.retrieve_all()}
+
+ # Assert e2 is present
+ assert "e2" in all_entries
+ assert all_entries["e2"].source_document == "doc_batch"
+
+ # Assert e1 is untouched and NO history archive was left behind
+ assert all_entries["e1"].source_document == "doc1"
+ assert not any(":v:" in eid for eid in all_entries.keys()), f"Found leaked history entry: {list(all_entries.keys())}"
+
+ def test_track_entity_logs_exception_with_exc_info(self):
+ """Test that track_entity logs errors with exc_info=True when a storage write fails."""
+ class FlakyInMemory(InMemoryStorage):
+ def _store_with_conn(self, conn, entry):
+ raise RuntimeError("Simulated write failure")
+ storage = FlakyInMemory()
+ mgr = ProvenanceManager(storage=storage)
+
+ with patch.object(mgr.logger, "error") as mock_error:
+ res = mgr.track_entity("e1", source="doc1")
+ assert res is None
+ assert mock_error.called
+ _, kwargs = mock_error.call_args
+ assert kwargs.get("exc_info") is True
+
+
+