From 580c79a76e52c88f455cf93865a38c17021b60a1 Mon Sep 17 00:00:00 2001 From: Sarthak Agrawal Date: Fri, 12 Jun 2026 03:25:22 +0530 Subject: [PATCH] fix graph route ticker fallback --- src/kb/query/graph_route.py | 4 +++- tests/test_graph_route.py | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/test_graph_route.py diff --git a/src/kb/query/graph_route.py b/src/kb/query/graph_route.py index dbacbf6..b2c38c1 100644 --- a/src/kb/query/graph_route.py +++ b/src/kb/query/graph_route.py @@ -143,7 +143,9 @@ async def maybe_graph_answer( # Apply ticker filter if intent supplied one (e.g., "themes in NVIDIA filings"). ticker = (intent.filters or {}).get("ticker") if ticker: - rows = [r for r in rows if (r.get("fields") or {}).get("ticker") == ticker] or rows + rows = [r for r in rows if (r.get("fields") or {}).get("ticker") == ticker] + if not rows: + return None # Pick a grouping field: prefer `category` (RiskFactor has it on SEC), # fall back to `subject` / `type` / parent_id, otherwise no grouping. diff --git a/tests/test_graph_route.py b/tests/test_graph_route.py new file mode 100644 index 0000000..0c8940b --- /dev/null +++ b/tests/test_graph_route.py @@ -0,0 +1,39 @@ +"""Graph route behavior.""" + +from __future__ import annotations + +from kb.query.graph_route import maybe_graph_answer +from kb.query.intent import QueryIntent + + +async def test_graph_route_returns_none_when_ticker_filters_out_all(monkeypatch) -> None: + async def fake_get_active_schema(domain: str, project: str = "default"): + return { + "spec": { + "entities": [ + {"name": "RiskFactor", "fields": [{"name": "ticker"}, {"name": "category"}]} + ] + } + } + + async def fake_list_entities(*, domain: str, type: str, q, limit: int, project: str): + return [ + { + "id": "r1", + "display_name": "Risk", + "identity_key": "risk-1", + "fields": {"ticker": "MSFT", "category": "supply"}, + } + ] + + monkeypatch.setattr("kb.query.graph_route.repo.get_active_schema", fake_get_active_schema) + monkeypatch.setattr("kb.query.graph_route.repo.list_entities", fake_list_entities) + + out = await maybe_graph_answer( + intent=QueryIntent(kind="lookup", entity_type="RiskFactor", filters={"ticker": "AAPL"}), + domain="sec", + question="themes in NVIDIA filings", + project="default", + ) + + assert out is None