"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(tools): mark unsupported ticker+name symbol queries as skipped (#1114)
lorenzozane12961ce14c9a
diff --git a/agent/src/tools/symbol_search_tool.py b/agent/src/tools/symbol_search_tool.pyindex 5b78b163f..34d1ec019 100644--- a/agent/src/tools/symbol_search_tool.py+++ b/agent/src/tools/symbol_search_tool.py@@ -219,6 +219,30 @@ def _is_canadian_symbol(text: str) -> bool:return bool(_CANADIAN_SYMBOL_RE.match((text or "").strip()))+def _is_ticker_name_query(query: str) -> bool:+ """Whether *query* is a bare all-caps ticker followed by a name hint.++ Yahoo's search endpoint answers this shape ("XOM ExxonMobil") with zero+ quotes, so the Yahoo path skips it rather than letting a caller deciding+ whether an entity exists read the empty result as "not listed". Unlike the+ Canadian fail-fast, Eastmoney is NOT skipped for this shape — it can serve+ multi-token queries — so this helper is only consulted on the Yahoo path.++ The first token must be bare all-caps (``[A-Z0-9&]{1,6}``); the absence of+ a dot/hyphen excludes suffixed tickers (``BTO.TO``, ``BRK.B``) and+ mixed-case name tokens.++ Args:+ query: Free-text name or ticker fragment.++ Returns:+ ``True`` when the query has at least two tokens and starts with a+ bare all-caps ticker-like token.+ """+ tokens = (query or "").strip().split()+ return len(tokens) >= 2 and bool(re.fullmatch(r"[A-Z0-9&]{1,6}", tokens[0]))++def _search_eastmoney(query: str) -> tuple[List[Dict[str, Any]], str]:"""Query Eastmoney's suggest endpoint and normalize the candidates.@@ -358,6 +382,13 @@ def _search_yahoo(query: str) -> tuple[List[Dict[str, Any]], str]:logger.warning("yahoo search failed for %r: %s", query, exc)return [], f"yahoo search failed: {exc}"+ if not quotes and _is_ticker_name_query(query):+ # Mirror the non-ASCII guard above: Yahoo answers a multi-token+ # ticker+name query ("XOM ExxonMobil") with zero quotes, which a+ # caller deciding whether an entity exists would otherwise read as+ # "not listed". An unsupported query shape must not read as an outage.+ return [], f"{_SKIPPED}ticker+name query is not supported by this source"+candidates: List[Dict[str, Any]] = []for quote in quotes[:_PER_SOURCE_CAP]:candidate = _yahoo_candidate(quote)diff --git a/agent/tests/test_symbol_search_tool.py b/agent/tests/test_symbol_search_tool.pyindex d51d50759..b7186b703 100644--- a/agent/tests/test_symbol_search_tool.py+++ b/agent/tests/test_symbol_search_tool.py@@ -434,3 +434,156 @@ class TestShanghaiAliasAndUnsupportedQueries:search.assert_called_once()assert data["sources"]["yahoo"] == "ok"+++class TestTickerNameQueryYahooSkip:+ """A ticker+name query Yahoo cannot serve must be skipped, not "ok".++ Yahoo's search endpoint answers a multi-token query whose first token is a+ bare all-caps ticker ("XOM ExxonMobil") with zero quotes. Recording that as+ "ok" counted a second clean source, so a caller deciding whether an entity+ exists read "not listed" as two corroborating "not found" answers; the+ unsupported shape must read as "skipped" instead, mirroring the non-ASCII+ guard. Eastmoney is NOT skipped for this shape — it can serve multi-token+ queries — only the Yahoo path relabels.+ """++ def test_ticker_name_query_skips_yahoo_without_ok_status(self):+ """Yahoo returns zero quotes for the shape and is relabeled "skipped"."""+ with patch.object(+ ss.eastmoney_client,+ "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(+ ss.yahoo_client, "search", return_value=[]+ ) as search, patch.object(+ ss.sec_edgar_client, "cik_for", return_value=None+ ):+ data = json.loads(+ ss.SymbolSearchTool().execute(query="XOM ExxonMobil")+ )["data"]++ # Post-response relabel, not a pre-call skip: Yahoo is still consulted.+ search.assert_called_once()+ assert data["sources"]["yahoo"].startswith("skipped:")+ assert data["sources"]["eastmoney"] == "ok"+ assert data["count"] == 0++ def test_ticker_name_query_with_matching_quotes_stays_ok(self):+ """The relabel must NOT fire when Yahoo can actually serve the shape."""+ quotes = [+ {+ "symbol": "XOM",+ "shortname": "Exxon Mobil Corp.",+ "exchange": "NYQ",+ "quoteType": "EQUITY",+ }+ ]+ with patch.object(+ ss.eastmoney_client,+ "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(+ ss.yahoo_client, "search", return_value=quotes+ ) as search, patch.object(+ ss.sec_edgar_client, "cik_for", return_value="0000034088"+ ):+ data = json.loads(+ ss.SymbolSearchTool().execute(query="XOM ExxonMobil")+ )["data"]++ search.assert_called_once()+ assert data["sources"]["yahoo"] == "ok"+ assert data["count"] == 1++ def test_multi_word_name_query_still_reaches_yahoo(self):+ """A multi-word NAME ("Exxon Mobil") is not a ticker+name shape."""+ with patch.object(+ ss.eastmoney_client,+ "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(+ ss.yahoo_client, "search", return_value=_yahoo_quotes()+ ) as search, patch.object(+ ss.sec_edgar_client, "cik_for", return_value="0000320193"+ ):+ data = json.loads(ss.SymbolSearchTool().execute(query="Exxon Mobil"))["data"]++ search.assert_called_once()+ assert data["sources"]["yahoo"] == "ok"++ def test_single_token_query_still_reaches_yahoo(self):+ """A bare single-token ticker ("XOM") is not a ticker+name shape."""+ with patch.object(+ ss.eastmoney_client,+ "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(+ ss.yahoo_client, "search", return_value=_yahoo_quotes()+ ) as search, patch.object(+ ss.sec_edgar_client, "cik_for", return_value="0000320193"+ ):+ data = json.loads(ss.SymbolSearchTool().execute(query="XOM"))["data"]++ search.assert_called_once()+ assert data["sources"]["yahoo"] == "ok"++ def test_suffixed_ticker_with_name_still_reaches_yahoo(self):+ """The bare-ticker clause must not fire on suffixed Canadian tickers."""+ with patch.object(+ ss.eastmoney_client,+ "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(+ ss.yahoo_client, "search", return_value=_yahoo_quotes()+ ) as search:+ data = json.loads(+ ss.SymbolSearchTool().execute(query="BTO.TO B2Gold")+ )["data"]++ search.assert_called_once()+ assert data["sources"]["yahoo"] == "ok"++ def test_single_token_ascii_empty_result_stays_ok(self):+ """A bare single token Yahoo cannot match is "not listed", not "skipped".++ The relabel is shape-specific: only a multi-token ticker+name query is+ unsupported. A single token (e.g. a bogus ticker) that returns zero+ quotes is an authoritative "not listed" and must stay "ok", otherwise+ every genuinely-absent entity would read as an unsupported shape.+ """+ with patch.object(+ ss.eastmoney_client,+ "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(+ ss.yahoo_client, "search", return_value=[]+ ) as search:+ data = json.loads(+ ss.SymbolSearchTool().execute(query="XOMZZZ")+ )["data"]++ search.assert_called_once()+ assert data["sources"]["yahoo"] == "ok"++ def test_multi_word_name_empty_result_stays_ok(self):+ """A multi-word NAME ("Exxon Mobil") with zero quotes is "not listed".++ The shape classifier keys on a bare all-caps FIRST token ("XOM+ ExxonMobil"). A name-led query ("Exxon Mobil") is a shape Yahoo can+ serve, so its empty answer is an authoritative "not listed" and must+ not be relabeled to "skipped".+ """+ with patch.object(+ ss.eastmoney_client,+ "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(+ ss.yahoo_client, "search", return_value=[]+ ) as search:+ data = json.loads(+ ss.SymbolSearchTool().execute(query="Exxon Mobil")+ )["data"]++ search.assert_called_once()+ assert data["sources"]["yahoo"] == "ok"