Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(kg): keep relationship endpoint aliases in sync (#1115)

Guofang.Tanga3074ec454f2
diff --git a/semantica/kg/graph_builder.py b/semantica/kg/graph_builder.py
index 78039a60..6fe5b45e 100644
--- a/semantica/kg/graph_builder.py
+++ b/semantica/kg/graph_builder.py
@@ -313,7 +313,10 @@ class GraphBuilder:
if not isinstance(relationship, dict):
continue
- for endpoint in ("source", "target"):
+ for endpoint, endpoint_alias in (
+ ("source", "source_id"),
+ ("target", "target_id"),
+ ):
endpoint_id = relationship.get(endpoint)
try:
canonical_id = endpoint_map.get(endpoint_id)
@@ -322,8 +325,21 @@ class GraphBuilder:
# to report rather than making graph construction fail here.
continue
- if canonical_id is not None and canonical_id != endpoint_id:
+ if canonical_id is None:
+ continue
+
+ remapped = canonical_id != endpoint_id
+ if remapped:
relationship[endpoint] = canonical_id
+
+ if (
+ endpoint_alias in relationship
+ and relationship[endpoint_alias] != canonical_id
+ ):
+ relationship[endpoint_alias] = canonical_id
+ remapped = True
+
+ if remapped:
remapped_count += 1
if remapped_count:
diff --git a/tests/kg/test_entity_pipeline.py b/tests/kg/test_entity_pipeline.py
index 7823ea1a..e237637c 100644
--- a/tests/kg/test_entity_pipeline.py
+++ b/tests/kg/test_entity_pipeline.py
@@ -34,13 +34,6 @@ def test_full_entity_pipeline():
}
graph_data = builder.build(sources=sources)
- # DEBUG: Print graph_data keys and entities count
- print(f"DEBUG: graph_data keys: {list(graph_data.keys())}")
- print(f"DEBUG: Entities count: {len(graph_data.get('entities', []))}")
- print(f"DEBUG: Relationships count: {len(graph_data.get('relationships', []))}")
- if graph_data.get('entities'):
- print(f"DEBUG: First entity: {graph_data['entities'][0]}")
-
# Verify graph data contains the entities and normalized relationships
assert len(graph_data["entities"]) >= 3
assert len(graph_data["relationships"]) == 3
@@ -149,6 +142,8 @@ def test_entity_id_only_merge_remaps_relationship_endpoints():
} == {"alice:1", "alice:2"}
assert relationship["source"] == "alice:1"
assert relationship["target"] == "org:1"
+ assert relationship["source_id"] == "alice:1"
+ assert relationship["target_id"] == "org:1"
assert {relationship["source"], relationship["target"]} <= entity_ids
@@ -162,6 +157,70 @@ def test_entity_id_helper_ignores_falsy_identifiers():
)
assert get_entity_id({"id": "", "entity_id": 0}) is None
+
+def test_entity_merge_repairs_stale_alias_for_canonical_endpoint():
+ """A canonical endpoint must not retain a merged-away endpoint alias."""
+ graph = GraphBuilder(
+ merge_entities=True,
+ entity_resolution_strategy="exact",
+ resolve_conflicts=False,
+ ).build(
+ {
+ "entities": [
+ {"entity_id": "alice:1", "name": "Alice", "type": "Person"},
+ {"entity_id": "alice:2", "name": " Alice ", "type": "Person"},
+ {"entity_id": "org:1", "name": "Acme", "type": "Organization"},
+ ],
+ "relationships": [
+ {
+ "source": "alice:1",
+ "source_id": "alice:2",
+ "target": "org:1",
+ "target_id": "org:1",
+ "type": "WORKS_FOR",
+ }
+ ],
+ }
+ )
+
+ relationship = graph["relationships"][0]
+
+ assert relationship["source"] == "alice:1"
+ assert relationship["source_id"] == "alice:1"
+ assert relationship["target"] == "org:1"
+ assert relationship["target_id"] == "org:1"
+
+
+def test_entity_merge_remaps_both_stale_endpoint_aliases():
+ """Both endpoint fields must be remapped when they start with an old ID."""
+ graph = GraphBuilder(
+ merge_entities=True,
+ entity_resolution_strategy="exact",
+ resolve_conflicts=False,
+ ).build(
+ {
+ "entities": [
+ {"entity_id": "alice:1", "name": "Alice", "type": "Person"},
+ {"entity_id": "alice:2", "name": " Alice ", "type": "Person"},
+ {"entity_id": "org:1", "name": "Acme", "type": "Organization"},
+ ],
+ "relationships": [
+ {
+ "source": "alice:2",
+ "source_id": "alice:2",
+ "target": "org:1",
+ "type": "WORKS_FOR",
+ }
+ ],
+ }
+ )
+
+ relationship = graph["relationships"][0]
+
+ assert relationship["source"] == "alice:1"
+ assert relationship["source_id"] == "alice:1"
+ assert relationship["target"] == "org:1"
+
if __name__ == "__main__":
test_full_entity_pipeline()
test_direct_entity_objects_in_analyzer()