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(mcp): unwrap decision wrapper and avoid premature truncation in query_decisions
KaifAhmad13ea7b6406e35
diff --git a/semantica_mcp/mcp/tools/decisions.py b/semantica_mcp/mcp/tools/decisions.pyindex 0e2da2b1..61fc0383 100644--- a/semantica_mcp/mcp/tools/decisions.py+++ b/semantica_mcp/mcp/tools/decisions.py@@ -109,18 +109,27 @@ def handle_query_decisions(args: dict) -> dict:try:graph = get_graph()if query:+ # find_similar_decisions() sorts and truncates to max_results before+ # returning, so when an outcome filter is also applied we must+ # over-fetch first — otherwise a matching decision ranked just+ # below the requested limit is silently dropped.+ fetch_limit = limit * 5 if outcome_filter else limitresults = graph.find_similar_decisions(query,category=category or None,- max_results=limit,+ max_results=fetch_limit,)decisions = results if isinstance(results, list) else list(results)if outcome_filter:+ # Each result wraps the decision as {"decision": {...}, "similarity": ...},+ # so the outcome must be read from the nested decision, not the wrapper.decisions = [dfor d in decisions- if _get_decision_field(d, "outcome") == outcome_filter+ if _get_decision_field(d.get("decision", d), "outcome")+ == outcome_filter]+ decisions = decisions[:limit]else:nodes = graph.find_nodes(node_type="decision")decisions = list(nodes)diff --git a/tests/test_mcp_decisions_query.py b/tests/test_mcp_decisions_query.pyindex 41b1c662..411c7de1 100644--- a/tests/test_mcp_decisions_query.py+++ b/tests/test_mcp_decisions_query.py@@ -117,18 +117,22 @@ class TestMCPQueryDecisions(unittest.TestCase):@patch("semantica_mcp.mcp.tools.decisions.get_graph")def test_query_with_category_and_outcome(self, mock_get_graph):- """Verify natural language query passes category and filters outcome."""+ """Verify natural language query passes category and filters outcome.++ find_similar_decisions()/find_precedents_by_scenario() wrap each match as+ {"decision": {...}, "similarity": ...} — the mock must reflect that real+ shape, not the flat find_nodes() shape, or the test can't catch a+ regression in the wrapper-unwrapping logic.+ """mock_graph = MagicMock()mock_graph.find_similar_decisions.return_value = [{- "id": "dec_1",- "type": "decision",- "metadata": {"category": "infra", "outcome": "approved"},+ "decision": {"id": "dec_1", "category": "infra", "outcome": "approved"},+ "similarity": 0.9,},{- "id": "dec_2",- "type": "decision",- "metadata": {"category": "infra", "outcome": "rejected"},+ "decision": {"id": "dec_2", "category": "infra", "outcome": "rejected"},+ "similarity": 0.8,},]mock_get_graph.return_value = mock_graph@@ -141,13 +145,57 @@ class TestMCPQueryDecisions(unittest.TestCase):"limit": 5,})+ # An outcome filter is present, so the handler must over-fetch (limit * 5)+ # before filtering rather than asking the graph for only `limit` results.mock_graph.find_similar_decisions.assert_called_once_with("database migration",category="infra",- max_results=5,+ max_results=25,)self.assertEqual(res["count"], 1)- self.assertEqual(res["decisions"][0]["id"], "dec_1")+ self.assertEqual(res["decisions"][0]["decision"]["id"], "dec_1")++ @patch("semantica_mcp.mcp.tools.decisions.get_graph")+ def test_query_outcome_filter_does_not_drop_lower_ranked_matches(+ self, mock_get_graph+ ):+ """Regression: a matching decision ranked below the requested limit must+ not be dropped by truncating to `limit` before the outcome filter runs.+ """+ candidates = [+ {+ "decision": {+ "id": f"dec_{i}",+ "category": "infra",+ "outcome": "rejected",+ },+ "similarity": 1.0 - i * 0.01,+ }+ for i in range(5)+ ] + [+ {+ "decision": {+ "id": "dec_match",+ "category": "infra",+ "outcome": "approved",+ },+ "similarity": 0.5,+ }+ ]++ def fake_find_similar_decisions(query, category=None, max_results=10):+ # Mirrors the real graph: sort/slice happens inside the backend.+ return candidates[:max_results]++ mock_graph = MagicMock()+ mock_graph.find_similar_decisions.side_effect = fake_find_similar_decisions+ mock_get_graph.return_value = mock_graph++ res = handle_query_decisions(+ {"query": "db change", "outcome": "approved", "limit": 3}+ )+ self.assertEqual(res["count"], 1)+ self.assertEqual(res["decisions"][0]["decision"]["id"], "dec_match")@patch("semantica_mcp.mcp.tools.decisions.get_graph")def test_query_no_match_returns_empty_list(self, mock_get_graph):