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(explorer): map enrich dedup pairs to the Entity Resolution shape (#1586)
Nilay Mallikb4a922450268
diff --git a/semantica/explorer/routes/enrich.py b/semantica/explorer/routes/enrich.pyindex 8e935e75..491971a3 100644--- a/semantica/explorer/routes/enrich.py+++ b/semantica/explorer/routes/enrich.py@@ -52,6 +52,27 @@ def _safe_dict(obj) -> dict:return {"value": str(obj)}+def _dedup_pair(item: dict) -> dict:+ """Map a duplicate pair to the shape the Entity Resolution tab parses.++ ``DuplicateDetector`` yields ``DuplicateCandidate`` fields (``entity1`` /+ ``entity2`` / ``similarity_score``), but ``EntityResolutionTab.tsx`` reads+ ``entity_a`` / ``entity_b`` / ``similarity|score``. Emit both so the UI+ resolves ids/scores while any client on the old keys keeps working+ (see issue #1585).+ """+ pair = dict(item)+ if "entity_a" not in pair and "entity1" in pair:+ pair["entity_a"] = pair["entity1"]+ if "entity_b" not in pair and "entity2" in pair:+ pair["entity_b"] = pair["entity2"]+ if "similarity" not in pair and "similarity_score" in pair:+ pair["similarity"] = pair["similarity_score"]+ if "score" not in pair and "confidence" in pair:+ pair["score"] = pair["confidence"]+ return pair++def _parse_fact(fact: str) -> Optional[Tuple[str, List[str]]]:match = _FACT_RE.match((fact or "").strip())if not match:@@ -308,7 +329,7 @@ async def detect_duplicates(]duplicates = await asyncio.to_thread(detector.detect_duplicates, entities, threshold=body.threshold)duplicate_list = duplicates if isinstance(duplicates, list) else getattr(duplicates, "duplicates", [])- return DedupResponse(duplicates=[_safe_dict(item) for item in duplicate_list], total_flagged=len(duplicate_list))+ return DedupResponse(duplicates=[_dedup_pair(_safe_dict(item)) for item in duplicate_list], total_flagged=len(duplicate_list))except ImportError:raise HTTPException(status_code=503, detail="Deduplication module not available.")except Exception as exc:diff --git a/tests/explorer/test_enrich_dedup_wire_shape.py b/tests/explorer/test_enrich_dedup_wire_shape.pynew file mode 100644index 00000000..bd4e5b6c--- /dev/null+++ b/tests/explorer/test_enrich_dedup_wire_shape.py@@ -0,0 +1,109 @@+"""Regression tests for issue #1585.++``POST /api/enrich/dedup`` serialized ``DuplicateCandidate`` fields verbatim+(``entity1``/``entity2``/``similarity_score``), but the Entity Resolution tab+(``EntityResolutionTab.tsx::parseDuplicates``) reads only+``entity_a``/``entity_b``/``similarity|score``. The keys were disjoint, so+every flagged pair rendered with empty ids and a 0% score bar, and merge+POSTed empty ids.++Each test below fails while the backend omits the frontend-facing keys and+passes once the route maps candidates to the agreed pair shape.+"""++import pytest++# fastapi ships in the optional `explorer` extra, not in `dev`, so this module+# must skip rather than fail collection when it is absent.+pytest.importorskip("fastapi")++from fastapi.testclient import TestClient # noqa: E402++from semantica.context.context_graph import ContextGraph # noqa: E402+from semantica.explorer.app import create_app # noqa: E402+from semantica.explorer.session import GraphSession # noqa: E402+++def _client_with_duplicates() -> TestClient:+ graph = ContextGraph(advanced_analytics=False)+ graph.add_node("acme_inc", "Organization", content="Acme Corporation")+ graph.add_node("acme_corp", "Organization", content="Acme Corp")+ graph.add_node("globex", "Organization", content="Globex Industries")+ return TestClient(create_app(session=GraphSession(graph)))+++def _parse_score(item: dict) -> float:+ """Mirror EntityResolutionTab.tsx::parseDuplicates score logic."""+ return float(item.get("similarity") if item.get("similarity") is not None else item.get("score", 0))+++def _extract_id(entity) -> str:+ """Mirror EntityResolutionTab.tsx::extractId logic."""+ if not entity:+ return ""+ if isinstance(entity, str):+ return entity+ return str(entity.get("id", entity.get("text", entity)))+++def test_dedup_pairs_use_frontend_shape():+ """Flagged pairs must carry entity_a/entity_b with resolvable ids.++ Fails before the fix: the backend emits entity1/entity2, so both ids+ resolve to "" and the score to 0 — exactly what the UI renders.+ """+ with _client_with_duplicates() as client:+ response = client.post("/api/enrich/dedup", json={"threshold": 0.5})++ assert response.status_code == 200, response.text+ payload = response.json()+ assert payload["total_flagged"] >= 1+ assert len(payload["duplicates"]) >= 1++ for item in payload["duplicates"]:+ assert _extract_id(item.get("entity_a")), f"entity_a id missing in {item}"+ assert _extract_id(item.get("entity_b")), f"entity_b id missing in {item}"+ assert "similarity" in item, f"similarity key missing in {item}"+ assert item["similarity"] == item["similarity_score"], f"similarity diverged in {item}"+ assert _parse_score(item) > 0, f"score is 0 in {item}"+++def test_dedup_pair_ids_reference_graph_nodes():+ """The ids in each pair must be nodes that actually exist in the graph."""+ graph = ContextGraph(advanced_analytics=False)+ graph.add_node("acme_inc", "Organization", content="Acme Corporation")+ graph.add_node("acme_corp", "Organization", content="Acme Corp")++ with TestClient(create_app(session=GraphSession(graph))) as client:+ response = client.post("/api/enrich/dedup", json={"threshold": 0.5})++ assert response.status_code == 200, response.text+ node_ids = {node.get("id") for node in graph.to_kg_dict().get("entities", [])}+ for item in response.json()["duplicates"]:+ assert _extract_id(item.get("entity_a")) in node_ids+ assert _extract_id(item.get("entity_b")) in node_ids+++def test_dedup_merge_round_trip():+ """Ids from a dedup pair must drive a successful merge.++ Covers the reported failure end to end: before the fix the pairs+ carried no frontend-facing ids, so a merge built from them POSTed+ empty ids and failed with 404 ``Primary node '' not found``.+ """+ with _client_with_duplicates() as client:+ scan = client.post("/api/enrich/dedup", json={"threshold": 0.5})+ assert scan.status_code == 200, scan.text+ assert scan.json()["duplicates"], "expected at least one flagged pair"+ first = scan.json()["duplicates"][0]+ primary_id = _extract_id(first.get("entity_a"))+ duplicate_id = _extract_id(first.get("entity_b"))+ assert primary_id and duplicate_id, f"pair ids missing in {first}"++ merge = client.post(+ "/api/enrich/merge",+ json={"primary_id": primary_id, "duplicate_ids": [duplicate_id]},+ )++ assert merge.status_code == 200, merge.text+ assert merge.json()["removed_ids"] == [duplicate_id]