Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Fix track_entity re-track silently overriding explicit parent_entity_id/derived_from (fixes #742)

Sameer630537c890bee267
diff --git a/semantica/provenance/manager.py b/semantica/provenance/manager.py
index 278a64d0..9bfea349 100644
--- a/semantica/provenance/manager.py
+++ b/semantica/provenance/manager.py
@@ -128,6 +128,13 @@ class ProvenanceManager:
except Exception:
pass
+ # Track whether the caller explicitly supplied a parent link (via
+ # parent_entity_id kwarg or source-as-known-entity-id resolution)
+ # BEFORE the history-preservation block below. If they did, that
+ # explicit value should not be silently overwritten by the auto-
+ # generated history pointer (#742).
+ explicit_parent_supplied = parent_id is not None
+
# If entity exists, preserve history by archiving the old state
if existing:
# Create a history entry for the previous state
@@ -145,8 +152,13 @@ class ProvenanceManager:
# Store the history entry
try:
self.storage.store(history_entry)
- # Link new entry to this history entry
- parent_id = history_id
+ # Link new entry to this history entry — but only when the
+ # caller didn't explicitly supply a new parent on this call.
+ # An explicit parent_entity_id (or derived_from on branches
+ # that support it) is an intentional override signal and must
+ # not be silently replaced by internal bookkeeping (#742).
+ if not explicit_parent_supplied:
+ parent_id = history_id
except Exception:
pass # If history archiving fails, proceed with update but lose history (graceful degradation)
diff --git a/tests/provenance/test_manager.py b/tests/provenance/test_manager.py
index 99040fad..a651bbab 100644
--- a/tests/provenance/test_manager.py
+++ b/tests/provenance/test_manager.py
@@ -154,3 +154,28 @@ class TestProvenanceManager:
lineage = prov_mgr.get_lineage("entity_1")
assert lineage == {}
+
+ def test_retrack_with_explicit_parent_overrides_history_link(self):
+ """#742 — re-tracking an entity with an explicit parent_entity_id must
+ honor the new value, not silently replace it with an auto-generated
+ history pointer."""
+ prov_mgr = ProvenanceManager()
+
+ e1 = prov_mgr.track_entity("X", source="doc_1", parent_entity_id="parent_v1")
+ e2 = prov_mgr.track_entity("X", source="doc_1", parent_entity_id="parent_v2")
+
+ assert e1.parent_entity_id == "parent_v1"
+ assert e2.parent_entity_id == "parent_v2"
+
+ def test_retrack_without_explicit_parent_still_uses_history_link(self):
+ """#742 — when NO explicit parent is given on a re-track call, the
+ auto-generated history link (Y:v:<timestamp>) should still be used,
+ preserving pre-existing behavior for callers that don't supply a parent."""
+ prov_mgr = ProvenanceManager()
+
+ y1 = prov_mgr.track_entity("Y", source="doc_1")
+ y2 = prov_mgr.track_entity("Y", source="doc_1")
+
+ assert y1.parent_entity_id is None
+ assert y2.parent_entity_id is not None
+ assert y2.parent_entity_id.startswith("Y:v:")