"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
Merge PR #1282
Haozhe Wuf08ce62d0b06
diff --cc agent/src/agent/grounding.pyindex 55ecfbafe,4cf744983..f56a59ecb--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@@ -174,27 -155,6 +174,48 @@@ _GENERIC_PRICE_FIELD_ALIASES =# Project-style canonical symbols. A bare model-generated ticker is still# checked when it appears under a symbol argument key, but it is not accepted# as user-provided identity because it lacks venue information.+#+# A joined crypto pair (``BTCUSDT``, ``ETHUSDT`` …) is recognized alongside+# the dashed/slashed form so a user message like ``Get BTCUSDT spot price``+# seeds an asserted identity and the asserted-symbol conflict check at+# ``_ingest_resolution`` runs against it. The base is restricted to alpha+# so a numeric prefix cannot masquerade as a joined pair, and the suffix+# list is the unambiguous stablecoin set (``USDT`` / ``USDC`` / ``BUSD`` /+# ``TUSD``) — ``USD`` is excluded because too many non-crypto strings end+# in those three letters and over-matching would lock the wrong identity.+_JOINED_CRYPTO_QUOTE_SUFFIXES = ("USDT", "USDC", "BUSD", "TUSD")++# A dashed / slashed pair is crypto when its quote leg is unambiguously a++# crypto quote asset, or when a USD quote sits on one of these bases. Both++# sets MUST agree with ``_CRYPTO_QUOTE_ASSETS`` / ``_CRYPTO_USD_BASES`` in++# ``src.tools.symbol_search_tool`` — that module is the resolver, and a venue++# inferred here that disagrees with the identity it locks is a contradictory++# identity, which outranks every later lock and blocks all market tools. The++# tool imports this module, so the sets are duplicated rather than imported;++# ``test_crypto_pair_tables_match_the_resolver`` fails if they drift. ``USD``++# is the one quote the resolver accepts that is NOT unambiguous, so it is++# excluded here and decided by the base whitelist below instead.++_CRYPTO_QUOTE_ASSETS = frozenset(++ {"USDT", "USDC", "BUSD", "TUSD", "FDUSD", "BTC", "ETH", "BNB"}++)++_CRYPTO_USD_BASES = frozenset(++ {++ "BTC", "ETH", "BNB", "SOL", "ADA", "XRP", "DOGE", "TRX", "DOT",++ "MATIC", "AVAX", "LINK", "LTC", "BCH", "ETC", "XLM", "ATOM",++ "FIL", "APT", "NEAR", "ALGO", "SAND", "MANA", "AXS", "XAUT",++ "PAXG",++ }++)+# Spot precious metals quoted in USD collide with the TUSD suffix: XPTUSD is+# XPT + USD (platinum), but stripping "TUSD" leaves the alpha base "XP" and+# folds it to XP-TUSD — a crypto pair that does not exist, and the same class+# of misresolution the USD exclusion above exists to prevent. XAU/XAG/XPD do+# not collide today; they are listed together because they are the same kind+# of symbol and a future suffix would collide with them the same way.+_METAL_USD_PAIR_RE = re.compile(r"^(?:XAU|XAG|XPT|XPD)USD$", re.IGNORECASE)+_JOINED_CRYPTO_RE = re.compile(+ r"(?<![A-Za-z0-9_])[A-Z]{2,15}(?:" + "|".join(_JOINED_CRYPTO_QUOTE_SUFFIXES) + r")(?![A-Za-z0-9_])",+ re.IGNORECASE,+)_CANONICAL_SYMBOL_RE = re.compile(r"(?<![A-Za-z0-9_])(?:"r"\d{3,6}\.(?:SH|SZ|BJ|SS|HK|KS|KQ)|"@@@ -1130,10 -793,48 +1151,40 @@@ def _infer_venue(symbol: str) -> str |for suffix, venue in suffixes.items():if upper.endswith(suffix):return venue- if "-" in upper or "/" in upper:- return "crypto_or_fx"+ # Yahoo's continuous-front-month futures notation (GC=F, CL=F, SI=F, ...).+ # The exchange category is the venue class. The engine and the+ # correlation helper mirror this pattern; this is the third copy.if upper.endswith("=F"):return "futures"+ # Yahoo's forex notation (XAUUSD=X, EURUSD=X) is FX.+ if re.match(r"^[A-Z]{6}=X$", upper):+ return "forex"+ # Bare 6-character precious-metal / FX symbols. The whitelist is+ # ISO 4217 metals + G10 currencies; it intentionally does NOT include+ # any US-equity prefix. Mirroring the engine ``_MARKET_PATTERNS``.+ if re.match(+ r"^(?:XAU|XAG|XPT|XPD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|USD)[A-Z]{3}$",+ upper,+ ):+ return "forex"- # Dashed / slashed symbols are NOT categorically crypto. The crypto- # resolver (``_canonical_crypto_pair`` in ``src.tools.symbol_search_tool``)- # gates the ``USD`` quote leg on a 26-entry base whitelist of Binance /- # OKX spot bases (BTC / ETH / BNB / SOL / ADA / XRP / DOGE / TRX / DOT /- # MATIC / AVAX / LINK / LTC / BCH / ETC / XLM / ATOM / FIL / APT / NEAR /- # ALGO / SAND / MANA / AXS / XAUT / PAXG). ``XAU-USD``, ``EUR-USD``,- # ``GBP-USD`` are not in that whitelist, so they are NOT crypto and- # are NOT crypto_or_fx — they are forex. Stablecoin quotes (USDT /- # USDC / BUSD / TUSD / FDUSD) are unambiguous and accept any alphanumeric- # base. Without this guard a bare ``XAUUSD`` query would surface as a- # crypto-or-fx hybrid in the runtime registry, contradicting the engine- # classifier which already routes it to ``forex`` (see PR #1280).++ # Dashed / slashed symbols are NOT categorically crypto: a USD quote is++ # crypto only on a whitelisted base (``_CRYPTO_USD_BASES``), so++ # ``XAU-USD`` / ``EUR-USD`` / ``GBP-USD`` are forex. Without this guard a++ # spot-gold pair surfaced as a crypto-or-fx hybrid in the runtime++ # registry, contradicting the engine classifier that already routes it to++ # ``forex`` (#1280).+ if "-" in upper or "/" in upper:- base, sep, quote = upper.partition("-") if "-" in upper else upper.partition("/")- if quote in {"FDUSD", "USDT", "USDC", "BUSD", "TUSD"}:++ base, _, quote = (++ upper.partition("-") if "-" in upper else upper.partition("/")++ )++ if quote in _CRYPTO_QUOTE_ASSETS:+ return "crypto_or_fx"- if quote == "USD" and base in {- "BTC", "ETH", "BNB", "SOL", "ADA", "XRP", "DOGE", "TRX", "DOT",- "MATIC", "AVAX", "LINK", "LTC", "BCH", "ETC", "XLM", "ATOM",- "FIL", "APT", "NEAR", "ALGO", "SAND", "MANA", "AXS", "XAUT", "PAXG",- }:++ if quote == "USD" and base in _CRYPTO_USD_BASES:+ return "crypto_or_fx"+ # Any other dashed / slashed pair is forex-shaped (e.g. ``XAU-USD``,+ # ``EUR-USD``, ``GBP-USD``); the per-pair engine classifier decides+ # ``forex`` vs ``crypto`` vs ``futures`` downstream.+ return "forex"return None@@@ -1188,10 -883,36 +1239,36 @@@ def _infer_instrument_type(symbol: strreturn "future"if upper.endswith(".FX"):return "forex"+ # Yahoo's continuous-front-month futures notation (GC=F, CL=F, ...).+ # Mirrors the engine ``_MARKET_PATTERNS`` and the correlation helper.+ if re.match(r"^[A-Z]{2,5}=F$", upper):+ return "future"+ # Yahoo's forex notation (XAUUSD=X, EURUSD=X).+ if re.match(r"^[A-Z]{6}=X$", upper):+ return "forex"+ # Bare 6-character precious-metal / FX symbols (whitelist).+ if re.match(+ r"^(?:XAU|XAG|XPT|XPD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|USD)[A-Z]{3}$",+ upper,+ ):+ return "forex"+ # Dashed / slashed symbols: crypto only when the quote leg is a+ # stablecoin OR the base is in the USD-whitelist. The whitelist+ # mirrors ``_canonical_crypto_pair`` in+ # ``src.tools.symbol_search_tool``. ``XAU-USD`` / ``EUR-USD`` /+ # ``GBP-USD`` are NOT crypto and resolve as ``forex`` (the per-pair+ # engine classifier decides the final market downstream).if "-" in upper or "/" in upper:- return "crypto"- base, sep, quote = upper.partition("-") if "-" in upper else upper.partition("/")- if quote in {"FDUSD", "USDT", "USDC", "BUSD", "TUSD"}:++ base, _, quote = (++ upper.partition("-") if "-" in upper else upper.partition("/")++ )++ if quote in _CRYPTO_QUOTE_ASSETS:+ return "crypto"- if quote == "USD" and base in {- "BTC", "ETH", "BNB", "SOL", "ADA", "XRP", "DOGE", "TRX", "DOT",- "MATIC", "AVAX", "LINK", "LTC", "BCH", "ETC", "XLM", "ATOM",- "FIL", "APT", "NEAR", "ALGO", "SAND", "MANA", "AXS", "XAUT", "PAXG",- }:++ if quote == "USD" and base in _CRYPTO_USD_BASES:+ return "crypto"+ return "forex"+ if upper.startswith("^"):+ return "index"return "listed_security"diff --cc agent/src/tools/symbol_search_tool.pyindex a78102334,a461e973a..e409f24dd--- a/agent/src/tools/symbol_search_tool.py+++ b/agent/src/tools/symbol_search_tool.py@@@ -63,32 -58,18 +63,69 @@@ _CANADIAN_SYMBOL_RE = re.compile(r"^[A-# Explicit exchange-pair spellings are not equity/name searches. Restrict the# quote leg to assets used by the built-in crypto connectors so an equity such-# as ``BRK-B`` is never misclassified as a pair.-_CRYPTO_QUOTE_ASSETS = (- "FDUSD",- "USDT",- "USDC",- "BUSD",- "TUSD",- "BTC",- "ETH",- "BNB",- "USD",+# as ``BRK-B`` is never misclassified as a pair. The full set is split into+# two tiers:+#+# * Stablecoin quotes (FDUSD / USDT / USDC / BUSD / TUSD) are unambiguous -+# a ``BTC-USDT`` or ``XAUT-USDC`` cannot be confused with anything outside+# crypto. These are accepted on any alphanumeric base.+# * ``USD`` is ambiguous: a ``BTC-USD`` is a real Coinbase crypto pair, but+# ``XAU-USD`` is spot gold, ``EUR-USD`` is forex, and ``GBP-USD`` is+# currency. Restrict ``USD`` to a whitelist of well-known crypto bases+# (anything that's actually tradable on Binance/OKX spot, plus the+# stablecoin-gold tokens XAUT/PAXG that ARE crypto). Without this guard a+# bare ``XAUUSD`` query would lock onto a tokenized-gold row from+# Binance instead of the spot gold the user actually asked for.+_STABLECOIN_QUOTES = ("FDUSD", "USDT", "USDC", "BUSD", "TUSD")+_CRYPTO_QUOTE_ASSETS = _STABLECOIN_QUOTES + ("BTC", "ETH", "BNB", "USD")+# Bases that may pair with ``USD`` and still count as a crypto pair. Every+# entry here is listed on Binance spot or OKX spot; ``XAU``, ``EUR``, etc.+# are deliberately excluded.+_CRYPTO_USD_BASES = frozenset(+ {+ "BTC", "ETH", "BNB", "SOL", "ADA", "XRP", "DOGE", "TRX", "DOT",+ "MATIC", "AVAX", "LINK", "LTC", "BCH", "ETC", "XLM", "ATOM",+ "FIL", "APT", "NEAR", "ALGO", "SAND", "MANA", "AXS", "XAUT",+ "PAXG",+ })++#: Spot precious metals. Their pairs are shaped exactly like FX (three-letter++#: base, fiat quote) but the base is not a fiat code, so ``canonical_fx_pair``++#: rejects them and the crypto resolver must too (``XAU-USD`` is spot gold,++#: ``XAUT-USDT`` is the token).++_METAL_CODES = frozenset({"XAU", "XAG", "XPT", "XPD"})++++++def _metal_or_fx_legs(value: str) -> tuple[str, str] | None:++ """Return the ``(base, quote)`` legs of a spot metal / FX pair query.++++ Recognizes every spelling the tool has to reconcile — ``XAUUSD``,++ ``XAU/USD``, ``XAU-USD``, ``XAUUSD=X`` — so a candidate written in one++ spelling can be compared with a query written in another.++++ Args:++ value: A query string or a candidate symbol.++++ Returns:++ The uppercased legs when the base is a metal or fiat code and the++ quote is a fiat code, else ``None``.++ """++ clean = str(value or "").strip().upper()++ if clean.endswith("=X"):++ clean = clean[:-2]++ if "-" in clean or "/" in clean:++ base, _, quote = (++ clean.partition("-") if "-" in clean else clean.partition("/")++ )++ elif len(clean) == 6 and clean.isalpha():++ base, quote = clean[:3], clean[3:]++ else:++ return None++ if base in (_METAL_CODES | FIAT_CODES) and quote in FIAT_CODES:++ return base, quote++ return None++++_CRYPTO_PAIR_RE = re.compile(rf"^([A-Z0-9]{{2,15}})[-/]({'|'.join(_CRYPTO_QUOTE_ASSETS)})$",re.IGNORECASE,@@@ -252,6 -214,25 +289,32 @@@ class SymbolSearchTool(BaseTool)if _canonical_crypto_pair(str(candidate.get("symbol") or ""))== crypto_pair]- elif "-" in query or "/" in query or query.endswith("=F") or query.endswith("=X"):- # The query is a non-crypto, ticker-shaped symbol (e.g. ``XAUUSD``,- # ``EUR/USD``, ``GC=F``, ``XAUUSD=X``). Any candidate Yahoo- # returned whose canonical form IS a crypto pair (e.g.- # ``VALOUR-BTC-0-SEK.ST`` as a free-text near-string of a gold- # query, or ``AETHUSDT-USD`` for ``XAU-USD``) would silently lock- # a wrong crypto identity. Drop them. The gate was previously- # inverted (#1234 only checked the crypto-pair branch) — a bare- # ``XAUUSD`` query then propagated a near-string crypto- # candidate that the user's grounded ``_canonical_crypto_pair``- # already rejected, but the symbol-search tool never knew. A bare- # name query (``apple``, ``tesla``, ``Gold``) is NOT a ticker shape- # and falls through to the existing free-text behavior.++ elif _metal_or_fx_legs(query) is not None:++ # A spot metal / FX pair query (``XAUUSD``, ``XAU/USD``,++ # ``XAUUSD=X``) is an exact instrument assertion, exactly like the++ # crypto-pair branch above. Yahoo answers such a query by++ # free-text similarity, and its top hit for spot gold is a Swedish++ # Bitcoin ETP (``VALOUR-BTC-0-SEK.ST``) or an Aave token++ # (``AETHUSDT-USD``) — a wrong instrument that then locks the run's++ # identity. Keep only candidates naming the same two legs; an++ # empty candidate list leaves the identity unresolved, which is++ # the safe failure.++ query_legs = _metal_or_fx_legs(query)+ candidates = [+ candidate+ for candidate in candidates- if _canonical_crypto_pair(str(candidate.get("symbol") or ""))- is None++ if _metal_or_fx_legs(str(candidate.get("symbol") or "")) == query_legs++ ]++ elif query.strip().upper().endswith("=F"):++ # Yahoo's continuous-front-month futures notation is likewise an++ # exact assertion: ``GC=F`` is gold futures, and a near-string hit++ # is a different contract.++ candidates = [++ candidate++ for candidate in candidates++ if str(candidate.get("symbol") or "").strip().upper()++ == query.strip().upper()+ ]# Canada fail-fast: a Canadian ticker must resolve to the Canadian venue# only. Yahoo also returns the US OTC alias of the same company (e.g.diff --cc agent/tests/test_agent_grounding.pyindex cf923c069,5999c2d29..95520ec7a--- a/agent/tests/test_agent_grounding.py+++ b/agent/tests/test_agent_grounding.py@@@ -13,9 -13,8 +13,10 @@@ from src.agent.context import ContextBufrom src.agent.grounding import (GroundingLedger,_infer_currency,+ _infer_instrument_type,_infer_venue,+ _JOINED_CRYPTO_RE,+ _normalize_symbol,_scan_symbols,_symbol_from_csv_filename,_timestamp_matches_claim_date,@@@ -2777,903 -2522,3 +2828,922 @@@ def test_a_us_csv_stem_resolves_to_its_assert _symbol_from_csv_filename("GC_F") == "GC=F"# A bare name has no venue suffix and must stay unresolvable.assert _symbol_from_csv_filename("AAPL") is None+++class TestFiatPairAndIndexNormalization:+ """Search, fetch and grounding agree on one FX spelling; ^ is a symbol."""++ def test_fiat_pair_spellings_normalize_to_yahoo_form(self) -> None:+ from src.agent.grounding import _normalize_symbol++ assert _normalize_symbol("GBP/USD") == "GBPUSD=X"+ assert _normalize_symbol("GBPUSD") == "GBPUSD=X"+ assert _normalize_symbol("GBPUSD=X") == "GBPUSD=X"+ # Crypto/metals keep their pair form — not fiat/fiat FX.+ assert _normalize_symbol("ETH/USD") == "ETH-USD"+ assert _normalize_symbol("XAU/USD") == "XAU-USD"++ def test_scanned_slashed_pair_matches_resolver_answer(self) -> None:+ """The query-as-asserted scan must agree with the chosen candidate."""+ from src.agent.grounding import _scan_symbols++ assert _scan_symbols("use GBP/USD spot") == {"GBPUSD=X"}++ def test_index_symbols_are_scanned_and_typed(self) -> None:+ from src.agent.grounding import (+ _infer_currency,+ _infer_instrument_type,+ _scan_symbols,+ )++ assert _scan_symbols("quote ^SPX") == {"^SPX"}+ assert _infer_instrument_type("^SPX", "INDEX") == "index"+ assert _infer_instrument_type("^SPX") == "index"+ assert _infer_currency("GBPUSD=X") == "USD"++ def test_ingest_search_symbol_does_not_create_conflicting_identity(self) -> None:+ """The flagship regression: ingest('GBP/USD') must lock, never conflict."""+ from src.agent.grounding import _normalize_symbol++ # Chosen (from search_symbol) and asserted (the query text) must be+ # the same canonical identity — the comparison in _ingest_resolution.+ chosen = _normalize_symbol("GBPUSD=X")+ asserted = _scan_symbols("GBP/USD")+ assert chosen in asserted+++def test_fx_pair_resolution_authorizes_market_data_consumer(tmp_path: Path) -> None:+ """Issue: search_symbol('GBP/USD') must lock, and get_market_data('GBPUSD=X')+ must be authorized — the slashed query used to normalize to the crypto+ spelling (GBP-USD), disagreeing with the chosen GBPUSD=X candidate and+ creating a conflicting identity that outranked every later lock.+ """+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="Get me the GBP/USD spot rate.",+ )+ before_resolution = ledger.authorized_symbols+ resolver = ledger.authorize_tool_call(+ "search_symbol",+ {"query": "GBP/USD"},+ batch_authorized_symbols=before_resolution,+ call_id="resolve-fx",+ )+ assert resolver.allowed is True++ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "GBP/USD"},+ result=json.dumps(+ {+ "ok": True,+ "source": "symbol_search",+ "data": {+ "query": "GBP/USD",+ "count": 1,+ "sources": {"yahoo": "ok", "fx_normalizer": "ok"},+ "candidates": [+ {+ "symbol": "GBPUSD=X",+ "name": "GBP/USD",+ "market": "fx",+ "type": "currency",+ "exchange": "CCY",+ "source": "fx_normalizer",+ },+ ],+ },+ }+ ),+ call_id="resolve-fx",+ success=True,+ )++ authorization = ledger.authorize_tool_call(+ "get_market_data",+ {"codes": ["GBPUSD=X"]},+ batch_authorized_symbols=ledger.authorized_symbols,+ batch_identity_status=ledger.identity_status,+ call_id="fx-prices",+ )++ assert ledger.identity_status == "locked"+ assert ledger.authorized_symbols == {"GBPUSD=X"}+ assert authorization.allowed is True+++def test_backtest_metrics_rejected_when_analysis_tool_failed(+ tmp_path: Path,+) -> None:+ """#1336: failed analysis tools cannot ground backtest-style metrics."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="比较几支标的并回测不同市场状态",+ )+ ledger.ingest_tool_result(+ tool_name="backtest",+ arguments={"run_dir": "runs/compare"},+ result=(+ '{"status": "error", "error": "market data unavailable after dedup"}'+ ),+ call_id="bt-failed",+ success=False,+ )++ bad = ledger.validate_final_answer(+ "| 策略 | Return vol | MaxDD | Prob. of hitting target |\n"+ "|---|---:|---:|---:|\n"+ "| M1 | 12.4% | -8.1% | 55% |"+ )++ assert bad.valid is False, bad.issues+ assert any(+ issue["code"] == "analysis_claim_unavailable" for issue in bad.issues+ )+++def test_backtest_metrics_rejected_when_no_analysis_result(+ tmp_path: Path,+) -> None:+ """#1336: without any completed analysis, metric prose is unsupported."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="比较几个标的的历史行情",+ )++ bad = ledger.validate_final_answer(+ "历史回测显示策略年化波动率约 18.2%,最大回撤 -9.4%,"+ "夏普比率 1.21,命中目标概率 58%。"+ )++ assert bad.valid is False, bad.issues+ assert any(+ issue["code"] == "analysis_claim_unavailable" for issue in bad.issues+ )+++def test_backtest_metrics_accepted_after_successful_backtest(+ tmp_path: Path,+) -> None:+ """#1336: a genuinely completed backtest grounds metrics its artifact holds."""+ run_dir = tmp_path / "runs" / "compare"+ (run_dir / "artifacts").mkdir(parents=True)+ (run_dir / "artifacts" / "metrics.csv").write_text(+ "total_return,sharpe,max_drawdown\n0.124,1.21,-0.081\n",+ encoding="utf-8",+ )+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="比较几支标的并回测不同市场状态",+ )+ ledger.ingest_tool_result(+ tool_name="backtest",+ arguments={"run_dir": str(run_dir)},+ result=json.dumps(+ {+ "status": "ok",+ "exit_code": 0,+ "run_dir": str(run_dir),+ "artifacts": {+ "metrics.csv": str(run_dir / "artifacts" / "metrics.csv")+ },+ }+ ),+ call_id="bt-ok",+ success=True,+ )++ good = ledger.validate_final_answer(+ "| 策略 | 年化收益 | 夏普比率 | MaxDD |\n"+ "|---|---:|---:|---:|\n"+ "| M1 | 12.4% | 1.21 | -8.1% |"+ )++ assert good.valid is True, good.issues+++def test_analysis_mention_without_figures_is_allowed(tmp_path: Path) -> None:+ """#1336: refusal prose naming the gap is not a quantitative claim."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="回测这几个标的",+ )++ good = ledger.validate_final_answer(+ "回测未能完成(行情数据不可用),因此无法给出波动率或回撤数据。"+ )++ assert good.valid is True, good.issues+++def test_metrics_from_successful_numeric_tool_are_allowed(+ tmp_path: Path,+) -> None:+ """A successful generic analysis result grounds its returned metrics.++ The real tool returns fractions (annualized_vol 0.182, max_drawdown+ -0.094) while answers quote percents — the unit scaling must match.+ """+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="分析组合风险",+ )+ ledger.ingest_tool_result(+ tool_name="portfolio_risk_xray",+ arguments={"symbols": ["AAPL.US"]},+ result=json.dumps(+ {+ "status": "ok",+ "data": {+ "volatility": {"annualized_vol": 0.182},+ "drawdown": {"max_drawdown": -0.094},+ "sharpe": 1.21,+ },+ }+ ),+ call_id="risk-ok",+ success=True,+ )++ good = ledger.validate_final_answer(+ "组合年化波动率 18.2%,最大回撤 -9.4%,夏普比率 1.21。"+ )++ assert good.valid is True, good.issues+++def test_partially_unsupported_analysis_metrics_are_rejected(+ tmp_path: Path,+) -> None:+ """One observed metric cannot launder another invented metric."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="分析组合风险",+ )+ ledger.ingest_tool_result(+ tool_name="portfolio_risk_xray",+ arguments={"symbols": ["AAPL.US"]},+ result=json.dumps(+ {+ "status": "ok",+ "data": {"volatility": {"annualized_vol": 0.182}},+ }+ ),+ call_id="risk-partial",+ success=True,+ )++ bad = ledger.validate_final_answer(+ "组合年化波动率 18.2%,最大回撤 -9.4%。"+ )++ assert bad.valid is False, bad.issues+ assert any(+ issue["code"] == "analysis_claim_unavailable" for issue in bad.issues+ )+++def test_forecast_probability_is_not_a_measured_claim(tmp_path: Path) -> None:+ """#1336 gates measured facts, not forward-looking forecasts."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="明天市场会怎么样",+ )++ good = ledger.validate_final_answer(+ "预计明日上涨概率 70%,波动率可能放大。"+ )++ assert good.valid is True, good.issues+++def test_valid_price_does_not_launder_unsupported_analysis_metric(+ tmp_path: Path,+) -> None:+ """A valid quote must not make an invented backtest metric acceptable."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="AAPL.US 现价多少",+ )+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {"AAPL.US": [{"trade_date": "2026-09-02", "close": 18.2}]}+ ),+ call_id="quote",+ success=True,+ )++ result = ledger.validate_final_answer(+ "AAPL.US 收盘价 18.2 USD。历史回测年化波动率 18.2%。"+ )++ assert result.valid is False, result.issues+ assert any(issue["code"] == "analysis_claim_unavailable" for issue in result.issues)+++def test_successful_backtest_only_supports_metrics_in_its_artifact(+ tmp_path: Path,+) -> None:+ """One successful result must not authorize unrelated invented metrics."""+ metrics = tmp_path / "artifacts" / "metrics.csv"+ metrics.parent.mkdir()+ metrics.write_text("annual_return,sharpe\n0.182,1.21\n", encoding="utf-8")+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="回测策略",+ )+ ledger.ingest_tool_result(+ tool_name="backtest",+ arguments={"run_dir": str(tmp_path)},+ result=json.dumps(+ {+ "status": "ok",+ "run_dir": str(tmp_path),+ "artifacts": {"metrics": str(metrics)},+ }+ ),+ call_id="backtest",+ success=True,+ )++ result = ledger.validate_final_answer(+ "策略年化收益 18.2%,夏普比率 1.21,最大回撤 -9.4%。"+ )++ assert result.valid is False, result.issues+ assert any(issue.get("value") == "-9.4%" for issue in result.issues)+++def test_skipped_analysis_result_does_not_authorize_metrics(tmp_path: Path) -> None:+ """A skipped/deduplicated call is not a completed analysis."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="回测策略")+ ledger.ingest_tool_result(+ tool_name="backtest",+ arguments={"run_dir": str(tmp_path)},+ result=json.dumps({"skipped": True, "reason": "already completed"}),+ call_id="backtest-skipped",+ success=True,+ )++ result = ledger.validate_final_answer("回测年化收益 18.2%,最大回撤 -9.4%。")++ assert result.valid is False, result.issues+++def test_integer_historical_window_claim_is_rejected(tmp_path: Path) -> None:+ """Categorical claims about all historical windows need evidence too."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="分析策略")++ result = ledger.validate_final_answer("所有历史 12 个月窗口均实现正收益。")++ assert result.valid is False, result.issues+ assert any(issue["code"] == "analysis_claim_unavailable" for issue in result.issues)+++def test_analysis_definition_is_not_rejected(tmp_path: Path) -> None:+ """A threshold definition is not a measured result from this session."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="什么是夏普比率?")++ result = ledger.validate_final_answer("夏普比率大于 1.0 通常被认为较好。")++ assert result.valid is True, result.issues+++def test_forecast_table_cell_does_not_exempt_measured_cell(+ tmp_path: Path,+) -> None:+ """A forecast column must not hide an unsupported historical metric cell."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="比较两种策略")++ result = ledger.validate_final_answer(+ "| 策略 | 预计收益 | 历史最大回撤 |\n"+ "|---|---:|---:|\n"+ "| M1 | 预计 12.4% | -8.1% |"+ )++ assert result.valid is False, result.issues+ assert any(issue["code"] == "analysis_claim_unavailable" for issue in result.issues)+++def test_analysis_completion_is_persisted_with_metric_provenance(+ tmp_path: Path,+) -> None:+ """The grounding artifact records why an analysis figure was accepted."""+ metrics = tmp_path / "artifacts" / "metrics.csv"+ metrics.parent.mkdir()+ metrics.write_text("annual_return\n0.182\n", encoding="utf-8")+ ledger = GroundingLedger(run_dir=tmp_path, user_message="回测策略")+ ledger.ingest_tool_result(+ tool_name="backtest",+ arguments={"run_dir": str(tmp_path)},+ result=json.dumps(+ {+ "status": "ok",+ "run_dir": str(tmp_path),+ "artifacts": {"metrics": str(metrics)},+ }+ ),+ call_id="backtest",+ success=True,+ )++ artifact = json.loads(+ (tmp_path / "artifacts" / "grounding_evidence.json").read_text(encoding="utf-8")+ )++ assert artifact["analysis_evidence"]+ assert artifact["analysis_evidence"][0]["metric"] == "return"+++def test_derived_interval_return_from_observed_endpoints_is_allowed(+ tmp_path: Path,+) -> None:+ """#1338 review: a return figure derived from observed endpoints stays legal.++ Both endpoints are observed evidence and the answer states the growth+ inline — arithmetic on sourced inputs, not an invented backtest metric.+ """+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="AAPL.US 最近一个月走势如何",+ )+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {+ "AAPL.US": [+ {"trade_date": "2026-08-03", "close": 100.0},+ {"trade_date": "2026-09-02", "close": 112.4},+ ]+ }+ ),+ call_id="quote",+ success=True,+ )++ good = ledger.validate_final_answer(+ "AAPL.US 从 2026-08-03 的 100.0 涨到 2026-09-02 的 112.4,"+ "区间收益率为 12.4%。"+ )++ assert good.valid is True, good.issues+++def test_derived_cumulative_return_english_is_allowed(tmp_path: Path) -> None:+ """#1338 review: same derivation exemption for the English shape."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="AAPL.US price history",+ )+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {+ "AAPL.US": [+ {"trade_date": "2026-08-03", "close": 100.0},+ {"trade_date": "2026-09-02", "close": 112.4},+ ]+ }+ ),+ call_id="quote",+ success=True,+ )++ good = ledger.validate_final_answer(+ "AAPL.US rose from 100.0 to 112.4, a cumulative return of 12.4% "+ "over the window."+ )++ assert good.valid is True, good.issues+++def test_unanchored_return_claim_is_still_rejected(tmp_path: Path) -> None:+ """Without the from/to frame, a return figure stays gated."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="AAPL.US 价格",+ )+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {+ "AAPL.US": [+ {"trade_date": "2026-08-03", "close": 100.0},+ {"trade_date": "2026-09-02", "close": 112.4},+ ]+ }+ ),+ call_id="quote",+ success=True,+ )++ bad = ledger.validate_final_answer(+ "AAPL.US 区间收益率为 12.4%,历史回测年化收益 18.2%。"+ )++ assert bad.valid is False, bad.issues+ assert any(+ issue["code"] == "analysis_claim_unavailable" for issue in bad.issues+ )+++def test_wrong_derived_return_arithmetic_is_rejected(tmp_path: Path) -> None:+ """A from/to frame with arithmetic that no observed pair supports fails."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="AAPL.US 价格",+ )+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {+ "AAPL.US": [+ {"trade_date": "2026-08-03", "close": 100.0},+ {"trade_date": "2026-09-02", "close": 112.4},+ ]+ }+ ),+ call_id="quote",+ success=True,+ )++ bad = ledger.validate_final_answer(+ "AAPL.US 从 2026-08-03 的 100.0 涨到 2026-09-02 的 112.4,"+ "区间收益率为 30.5%。"+ )++ assert bad.valid is False, bad.issues+ assert any(+ issue.get("value") == "30.5%" for issue in bad.issues+ )+++def test_research_paper_reported_metrics_are_grounded(tmp_path: Path) -> None:+ """#1338 review: attributed paper figures must not be suppressed.++ research_papers reports `reported_annualized_return` / `reported_max_drawdown`+ — compound leaves whose kind must resolve by token, not verbatim alias.+ """+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="查一下动量策略论文的回测表现",+ )+ ledger.ingest_tool_result(+ tool_name="research_papers",+ arguments={"query": "momentum"},+ result=json.dumps(+ {+ "status": "ok",+ "data": {+ "results": [+ {+ "title": "Momentum crashes",+ "reported_annualized_return": 0.182,+ "reported_max_drawdown": -0.094,+ }+ ]+ },+ }+ ),+ call_id="rp-ok",+ success=True,+ )++ good = ledger.validate_final_answer(+ "该论文报告其策略年化收益 18.2%,最大回撤 -9.4%(论文自述,非本次回测)。"+ )++ assert good.valid is True, good.issues+++def test_compound_metric_leaf_kind_resolution() -> None:+ """Token-split kind resolution covers the compound-leaf family."""+ from src.agent.grounding import _metric_kind_for_path++ assert _metric_kind_for_path("results[0].reported_annualized_return") == "return"+ assert _metric_kind_for_path("strategy_max_drawdown") == "drawdown"+ assert _metric_kind_for_path("data.benchmark_return_vol") == "vol"+ assert _metric_kind_for_path("data.hit_rate_daily") == "win_rate"+ assert _metric_kind_for_path("data.risk_free_rate") is None+ assert _metric_kind_for_path("data.trade_count") is None+++def test_analysis_gates_accept_attributed_paper_restatement(tmp_path: Path) -> None:+ """An attributed figure is a citation, not an invented measurement (#1338+ review): 'The paper reports a Sharpe ratio of 1.8' must pass with zero tool+ results, in both languages, while unattributed phrasing stays blocked."""+ for answer in (+ "The paper reports a Sharpe ratio of 1.8 for the momentum factor.",+ "该论文报告其策略夏普比率为 1.8。",+ "研究机构指出该策略年化收益 18.2%。",+ "文献指出因子年化收益 18.2%。",+ "Analysts estimate an annualized volatility of 22%.",+ ):+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Research the factor.")+ issues = ledger.validate_final_answer(answer).issues+ assert not [i for i in issues if i.get("code") == "analysis_claim_unavailable"], (+ answer,+ issues,+ )+++def test_analysis_gate_still_rejects_unattributed_and_unsourced(+ tmp_path: Path,+) -> None:+ """The attribution exemption must not launder model-memory figures: the+ review's pinned reject pair and an invented metric with an ordinary prose+ subject keep failing."""+ for answer in (+ "TSLA.US last traded at 412.35 USD.",+ "特斯拉现价 412.35 美元。",+ "The strategy reports a Sharpe ratio of 1.8.",+ ):+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Analyze something.")+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {+ "AAPL.US": [+ {+ "trade_date": "2026-09-02T00:00:00",+ "close": 112.4,+ }+ ]+ }+ ),+ call_id="md-1",+ success=True,+ )+ issues = ledger.validate_final_answer(answer).issues+ assert issues, answer+ assert not any(+ issue.get("code") == "analysis_claim_unavailable" and "1.8" not in str(issue)+ for issue in issues+ ), answer+++def test_attribution_exemption_cannot_launders_self_claims(tmp_path: Path) -> None:+ """The #1336 attack shape must not escape via attribution vocabulary:+ "the backtest/data shows" is a self-claim, and a citation in one clause+ must not exempt an invented sibling metric in the next."""+ for answer in (+ "The backtest data shows an annualized return of 25%.",+ "回测数据显示策略年化收益 25%。",+ "数据显示策略夏普比率为 3.5。",+ "根据本次回测,年化波动率 18.2%。",+ "The strategy reports a Sharpe ratio of 1.8.",+ "研究显示策略年化收益 25%。",+ "研究报告显示策略年化收益 25%。",+ "回测研究显示策略年化收益 25%。",+ "投资者普遍认为其年化收益 25%。",+ # citation in clause 1, invented sibling in clause 2+ "The paper reports a Sharpe ratio of 1.8, and our strategy achieved "+ "an annualized return of 47.3%.",+ ):+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Research the factor.")+ issues = ledger.validate_final_answer(answer).issues+ assert [i for i in issues if i.get("code") == "analysis_claim_unavailable"], (+ answer,+ issues,+ )+++def test_attribution_never_exempts_a_price_claim(tmp_path: Path) -> None:+ """A citation subject must not launder a fabricated quote.++ The attribution exemption is legitimate in the ANALYSIS gate — a paper's+ Sharpe is a figure this run could never have observed. A price is the+ opposite: it is exactly what this run observes, so attributing it to a+ source is the laundering shape the price gate exists to catch. With the+ exemption applied to `_validate_price_claims`, every line below passed+ with zero tool evidence.+ """+ for answer in (+ "Analysts say TSLA.US last traded at 412.35 USD.",+ "分析师指出特斯拉现价 412.35 美元。",+ "The paper reports that AAPL.US closed at 189.20.",+ "据研究机构报告,AAPL.US 收盘价为 189.20。",+ "The filing reports the stock closed at 412.35.",+ ):+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Quote the price.")+ issues = ledger.validate_final_answer(answer).issues+ assert issues, f"attributed price accepted with zero evidence: {answer}"+++def test_derived_return_exemption_is_structural_not_phrasal(tmp_path: Path) -> None:+ """The same derivation must get the same verdict in both languages.++ Keying the exemption on a growth PHRASE ("从…到" / "from…to") made the+ gate stricter for every wording the list missed. The pairs below state the+ identical arithmetic on the identical observed endpoints; asserting the+ two verdicts are EQUAL is what stops the next patch moving the breakage to+ the other language, exactly as test_grounding_language_parity does.+ """++ def verdict(answer: str) -> bool:+ ledger = GroundingLedger(run_dir=tmp_path, user_message="AAPL.US 走势")+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {+ "AAPL.US": [+ {"trade_date": "2026-08-03", "close": 100.0},+ {"trade_date": "2026-09-02", "close": 112.4},+ ]+ }+ ),+ call_id="quote",+ success=True,+ )+ return bool(ledger.validate_final_answer(answer).issues)++ # Accepted in both: operands present in the clause and sourced.+ for english, chinese in (+ (+ "AAPL.US rose from 100.0 to 112.4, a cumulative return of 12.4%.",+ "AAPL.US 第一日收盘 100.0 美元,第二日收盘 112.4 美元,收益率 12.4%。",+ ),+ ):+ en, zh = verdict(english), verdict(chinese)+ assert en == zh, f"verdicts disagree by language: EN={en} ZH={zh}"+ assert en is False, f"sourced derivation rejected: {english}"++ # Still rejected in both: no operands in the clause, or wrong arithmetic.+ for answer in (+ "AAPL.US delivered a cumulative return of 12.4% over the window.",+ "AAPL.US 区间收益率为 12.4%。",+ "AAPL.US rose from 100.0 to 112.4, a cumulative return of 15.0%.",+ "AAPL.US 从 100.0 涨到 112.4,区间收益率为 15.0%。",+ ):+ assert verdict(answer) is True, f"unanchored/wrong return accepted: {answer}"+def test_generic_header_table_metric_rows_are_gated(tmp_path: Path) -> None:+ """#1336 must not be dodgeable by formatting the claim as a generic-header+ table (| 指标 | 数值 | / | Metric | Value |) with the metric kind in the+ row instead of prose or a metric-headed table."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="回测策略")++ result = ledger.validate_final_answer(+ "| 指标 | 数值 |\n|---|---:|\n| 年化收益 | 18.2% |\n| 最大回撤 | -9.4% |"+ )++ assert result.valid is False, result.issues+ assert [+ i for i in result.issues if i.get("code") == "analysis_claim_unavailable"+ ]++ # a comparison marker must not launder an unsupported figure through a+ # metric-headed table either: prose rejects "夏普比率 > 1.5。" without+ # evidence, so the table form must be rejected too+ result = ledger.validate_final_answer("| 夏普比率 |\n|---|---|\n| > 1.5 |")+ assert result.valid is False, result.issues++ # value-first layout (kind in a later cell) is gated too+ result = ledger.validate_final_answer(+ "| 数值 | 指标 |\n|---|---|\n| 18.2% | 年化收益率 |"+ )+ assert result.valid is False, result.issues+ # the bare fragment the prose detector treats as a metric word is gated+ result = ledger.validate_final_answer(+ "| 指标 | 数值 |\n|---|---|\n| 年化 | 18.2% |"+ )+ assert result.valid is False, result.issues+ # multiple (label, value) pairs in one row are each gated with their own kind+ result = ledger.validate_final_answer(+ "| 指标 | 数值 | 指标 | 数值 |\n|---|---|---|---|\n| 年化波动率 | 18.2% | 最大回撤 | -9.4% |"+ )+ assert result.valid is False, result.issues+ assert len([i for i in result.issues if i.get("code") == "analysis_claim_unavailable"]) == 2+++def test_generic_header_table_matches_prose_verdicts(tmp_path: Path) -> None:+ """The generic-header fallback must produce the SAME verdict as prose for+ the same claim — never looser (formatting dodge) and never stricter.++ Prose without evidence rejects comparison phrasing ("最大回撤小于 -5%+ 触发风控。"), rejects the definitional frame split across the number+ ("夏普比率通常大于1.0认为较好。"), and rejects a bare claim; with+ kind-matched evidence all of them pass. Tables must do exactly that.+ """+ bare = GroundingLedger(run_dir=tmp_path / "bare", user_message="回测策略")+ # comparison phrasing rejected without evidence, like prose+ assert bare.validate_final_answer(+ "| 指标 | 标准 |\n|---|---|\n| 最大回撤 | 小于 -5% 触发风控 |"+ ).valid is False+ # definitional frame split by the number: prose rejects it, tables must not+ # be looser than prose or the split becomes the new formatting dodge+ assert bare.validate_final_answer(+ "| 指标 | 备注 |\n|---|---|\n| 夏普比率 | 通常大于1.0认为较好 |"+ ).valid is False+ # a label cell smuggling its own measurement is prose-identical to+ # "年化收益率为 18.2%" — rejected+ assert bare.validate_final_answer(+ "| 指标 | 数值 |\n|---|---|\n| 年化收益率 18.2% | - |"+ ).valid is False++ backed = GroundingLedger(run_dir=tmp_path / "backed", user_message="回测策略")+ backed.ingest_tool_result(+ tool_name="portfolio_risk_xray",+ arguments={"symbols": ["AAPL"]},+ result=json.dumps({"annualized_vol": 0.182, "max_drawdown": -0.05}),+ call_id="x",+ success=True,+ )+ # value-backed row accepted+ assert backed.validate_final_answer(+ "| 指标 | 数值 |\n|---|---:|\n| 年化波动率 | 18.2% |"+ ).valid is True+ # the same comparison phrasing passes once the figure is kind-backed+ assert backed.validate_final_answer(+ "| 指标 | 标准 |\n|---|---|\n| 最大回撤 | 小于 -5% 触发风控 |"+ ).valid is True+ # contiguous definitional frame ("通常认为…") is exempt in prose, so in+ # tables too — the frame regex is the boundary, not the formatting+ assert backed.validate_final_answer(+ "| 指标 | 备注 |\n|---|---|\n| 夏普比率 | 通常认为大于 1.0 较好 |"+ ).valid is True+ # an annotation column past the value cell is not attributed to the label:+ # the prose verdict for "年化波动率 18.2%,较去年提升 2%。" with this evidence+ # is valid, so the table form must not be stricter (parity).+ assert backed.validate_final_answer(+ "| 指标 | 数值 | 备注 |\n|---|---|---|\n| 年化波动率 | 18.2% | 较去年提升 2% |"+ ).valid is True+ # a FORECAST frame in the label frames the claimed value clause-wide:+ # prose "预计夏普比率为 1.2。" and the metric-header path (header_forecast+ # column skip) both accept; the generic path must not be stricter.+ assert bare.validate_final_answer(+ "| 指标 | 数值 |\n|---|---|\n| 预计夏普比率 | 1.2 |"+ ).valid is True+++ ("raw", "expected"),+ [+ # Futu writes the venue as a prefix; every one of them must land on+ # the same identity the market-data chain uses.+ ("HK.06693", "06693.HK"),+ ("HK.00700", "00700.HK"),+ ("HK.700", "00700.HK"), # zero-padded like the suffix spelling+ ("US.AAPL", "AAPL.US"),+ ("US.BRK-B", "BRK-B.US"),+ ("SH.600519", "600519.SH"),+ ("SS.600519", "600519.SH"), # Yahoo's Shanghai alias folds onto .SH+ ("SZ.000001", "000001.SZ"),+ ("BJ.430047", "430047.BJ"),+ # Negatives: a non-numeric venue code is not a listing (HK.HSI is an+ # index feed), and the suffix spellings stay untouched.+ ("HK.HSI", "HK.HSI"),+ ("06693.HK", "06693.HK"),+ ("AAPL.US", "AAPL.US"),+ ("600519.SH", "600519.SH"),+ ],+)+def test_normalize_venue_prefixed_symbols(raw: str, expected: str) -> None:+ """A Futu-style venue prefix normalizes onto the canonical suffix form."""+ assert _normalize_symbol(raw) == expected+++ ("text", "expected"),+ [+ ("分析港股 HK.06693 的走势", {"06693.HK"}),+ ("持仓 US.AAPL 与 HK.00700", {"AAPL.US", "00700.HK"}),+ ("对比 SH.600519 和 SZ.000001", {"600519.SH", "000001.SZ"}),+ # Negatives: prose and URLs must not become symbols. The US branch is+ # case-sensitive precisely so a "…/us.reuters/…" host cannot.+ ("Revenue grew in the US. Apple led the pack.", set()),+ ("Listed in the U.S. AAPL is the largest.", set()),+ ("see https://example.com/us.quotes for details", set()),+ ],+)+def test_scan_symbols_detects_venue_prefixed_symbols(+ text: str, expected: set[str]+) -> None:+ """A pasted connector code is locked as an identity, prose is not."""+ assert _scan_symbols(text) == expected++++++def test_crypto_pair_tables_match_the_resolver() -> None:++ """The grounding copies of the crypto pair tables must not drift.++++ ``src.tools.symbol_search_tool`` is the resolver; it imports this module,++ so the tables are duplicated rather than shared. A venue inferred here++ that disagrees with the identity the resolver locks is a contradictory++ identity, which outranks every later lock and blocks all market tools —++ so the duplication needs a guard, not a comment.++ """++ from src.agent import grounding as g++ from src.tools import symbol_search_tool as ss++++ assert set(g._CRYPTO_USD_BASES) == set(ss._CRYPTO_USD_BASES)++ # ``USD`` is the one quote the resolver accepts that is ambiguous (spot++ # gold and forex are quoted in it too); grounding decides it by the base++ # whitelist instead, so it is the only permitted difference.++ assert set(g._CRYPTO_QUOTE_ASSETS) | {"USD"} == set(ss._CRYPTO_QUOTE_ASSETS)diff --cc agent/tests/test_symbol_search_tool.pyindex 95fa84fed,f66b17962..5e802e140--- a/agent/tests/test_symbol_search_tool.py+++ b/agent/tests/test_symbol_search_tool.py@@@ -766,152 -764,104 +766,279 @@@ class TestCryptoPairWithoutABrokerConneassert called == []+# --------------------------------------------------------------------------+# FX pairs + index symbols: search and fetch must agree on the symbol universe+# --------------------------------------------------------------------------+++class TestFxPairAlignment:+ """search_symbol must resolve FX pairs and return fetch-able symbols."""++ def test_canonical_fx_pair_spellings(self) -> None:+ assert ss._canonical_fx_pair("GBP/USD") == "GBPUSD=X"+ assert ss._canonical_fx_pair("gbp/usd") == "GBPUSD=X"+ assert ss._canonical_fx_pair("GBPUSD") == "GBPUSD=X"+ assert ss._canonical_fx_pair("GBPUSD=X") == "GBPUSD=X"+ assert ss._canonical_fx_pair("USD/JPY") == "USDJPY=X"+ assert ss._canonical_fx_pair("GBPCNY") == "GBPCNY=X"+ # Not pairs / not fiat-fiat+ assert ss._canonical_fx_pair("BRK-B") is None+ assert ss._canonical_fx_pair("AAPL") is None+ assert ss._canonical_fx_pair("ETH/USD") is None # crypto, not FX+ assert ss._canonical_fx_pair("XAU/USD") is None # metal, not fiat++ def test_fiat_pairs_are_not_misclassified_as_crypto(self) -> None:+ """GBP/USD must stop being treated as a crypto 'GBP-USD' pair."""+ assert ss._canonical_crypto_pair("GBP/USD") is None+ assert ss._canonical_crypto_pair("EURUSD") is None+ # Crypto classifications must remain untouched.+ assert ss._canonical_crypto_pair("ETH/USD") == "ETH-USD"+ assert ss._canonical_crypto_pair("BTC/USDT") == "BTC-USDT"+ assert ss._canonical_crypto_pair("BTCUSDT") == "BTC-USDT"++ def test_fx_query_returns_canonical_candidate_when_yahoo_unavailable(self) -> None:+ """A throttled/failed Yahoo must not turn a canonical pair into nothing."""+ with patch.object(+ ss.yahoo_client, "search", side_effect=Exception("Too Many Requests")+ ), patch.object(+ ss.eastmoney_client, "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(ss.sec_edgar_client, "cik_for", return_value=""):+ out = json.loads(ss.SymbolSearchTool().execute(query="GBP/USD", limit=5))++ by_symbol = {c["symbol"]: c for c in out["data"]["candidates"]}+ assert "GBPUSD=X" in by_symbol+ assert by_symbol["GBPUSD=X"]["market"] == "fx"+ assert by_symbol["GBPUSD=X"]["type"] == "currency"++ def test_from_yahoo_symbol_normalizes_currency_quotes(self) -> None:+ assert ss._from_yahoo_symbol("GBP/USD", {"quoteType": "CURRENCY"}) == (+ "GBPUSD=X",+ "fx",+ )+ assert ss._from_yahoo_symbol("GBPUSD=X", {"quoteType": "CURRENCY"}) == (+ "GBPUSD=X",+ "fx",+ )++ def test_from_yahoo_symbol_labels_indexes(self) -> None:+ assert ss._from_yahoo_symbol("^SPX", {"quoteType": "INDEX"}) == (+ "^SPX",+ "index",+ )+ assert ss._from_yahoo_symbol("^FTSE", {"quoteType": "INDEX"}) == (+ "^FTSE",+ "index",+ )+++class TestCryptoUsdBaseWhitelist:+ """``USD`` quote leg on the crypto resolver is gated on a base whitelist.++ Without this guard, ``XAUUSD`` / ``EURUSD`` / ``GBPUSD`` would all be+ classified as crypto pairs and the public-venue catalog (Binance/OKX)+ fallback would either lock onto a tokenized gold token (XAUT/PAXG) or+ find nothing — never on real spot gold. The fix: only accept ``USD`` on+ crypto when the base is in :data:`_CRYPTO_USD_BASES`.+ """++ @pytest.mark.parametrize(+ "code",+ [+ # Genuine crypto pairs - must keep working.+ "BTC-USD",+ "ETH-USD",+ "SOL-USD",+ "BNB-USD",+ "XLM-USD",+ "XRP-USD",+ # Stablecoin-quoted pairs (existing behaviour preserved).+ "BTC-USDT",+ "XAUT-USDT",+ "PAXG-USDT",+ "ETH-USDC",+ ],+ )+ def test_crypto_pairs_with_supported_base_accepted(self, code):+ assert ss._canonical_crypto_pair(code) is not None++ @pytest.mark.parametrize(+ "code",+ [+ # The reported bug: a spot gold query must NOT resolve to a+ # crypto pair, or the public-venue catalog fallback will lock a+ # tokenized-gold row (XAUT-USDT) as the answer.+ "XAUUSD",+ "XAU-USD",+ "XAU/USD",+ # Forex pairs in 6-letter, dashed, or slashed form.+ "EURUSD",+ "EUR-USD",+ "EUR/USD",+ "GBPUSD",+ "GBP-USD",+ "JPYUSD",+ ],+ )+ def test_non_crypto_bases_with_usd_quote_rejected(self, code):+ """The ``USD`` leg on non-crypto bases must NOT pass as a crypto pair."""+ assert ss._canonical_crypto_pair(code) is None++ def test_tokenized_gold_bases_still_resolve_as_crypto(self):+ # XAUT/PAXG ARE crypto (tokenized gold on Binance/OKX spot) and+ # must keep resolving as crypto. The whitelist includes them.+ assert ss._canonical_crypto_pair("XAUT-USDT") == "XAUT-USDT"+ assert ss._canonical_crypto_pair("PAXG-USDT") == "PAXG-USDT"+ assert ss._canonical_crypto_pair("XAUT-USD") == "XAUT-USD"+ assert ss._canonical_crypto_pair("PAXG-USD") == "PAXG-USD"++ def test_xauusd_and_xautusdt_are_not_equivalent_assets(self):+ # Identity correctness: the resolver must treat XAUUSD (spot gold)+ # and XAUT-USDT (tokenized gold) as different instruments. If both+ # return the same canonical string, downstream lock/identity+ # collision is possible.+ spot = ss._canonical_crypto_pair("XAUUSD")+ tokenized = ss._canonical_crypto_pair("XAUT-USDT")+ assert spot is None+ assert tokenized == "XAUT-USDT"+ assert spot != tokenized++ def test_usd_whitelist_is_a_strict_subset_of_crypto_bases(self):+ # Defensive: the whitelist is what stops non-crypto bases from+ # slipping through the ``USD`` branch. Every base in it must+ # therefore actually be tradable on Binance or OKX spot.+ # This is a coarse sanity check on the whitelist contents; if a+ # new entry is added in error, this test will still pass (it only+ # checks the union is non-empty and is a subset of the quote-asset+ # alphabet).+ assert ss._CRYPTO_USD_BASES+ for base in ss._CRYPTO_USD_BASES:+ assert base.isalpha()+ assert base.isupper()+++ class TestSpotGoldCandidateFilter:+ """Bare gold / FX / futures queries must not lock a wrong crypto identity.++ Yahoo's free-text search can return a near-string crypto pair for a+ non-crypto query (``XAUUSD`` -> ``VALOUR-BTC-0-SEK.ST`` is a real-world+ observation). The resolver-side ``_canonical_crypto_pair`` already+ rejects those candidates, but the symbol-search tool itself used+ to keep them in the candidate list, propagating the wrong instrument+ to the identity gate. The fix: when the query is a ticker shape+ (separator, ``=F``, or ``=X``) but NOT a crypto pair, drop any+ candidate whose canonical form IS a crypto pair. Free-text name+ queries (``apple``, ``tesla``) are unaffected.+ """++ def _run(self, query, yahoo_hits, eastmoney_rows=None):+ # Default to an empty Eastmoney payload so the test isolates the+ # Yahoo branch (the layer under test).+ tool = ss.SymbolSearchTool()+ em_payload = {"QuotationCodeTable": {"Data": eastmoney_rows or []}}+ with patch.object(+ ss.eastmoney_client, "get_json", return_value=em_payload+ ), patch.object(ss.yahoo_client, "search", return_value=yahoo_hits), \+ patch.object(ss, "_load_public_markets", return_value={}), \+ patch.object(ss, "_enrich_us_cik", side_effect=lambda c, s="ok": (c, s)), \+ patch.object(+ trading_profiles, "load_selected_profile_id",+ side_effect=OSError("no connector"),+ ):+ out = tool.execute(query=query, limit=10)+ return json.loads(out)++ def test_xauusdt_drops_near_string_btc_etp(self):+ """The bug repro: Yahoo returns a Swedish Bitcoin ETP for XAUUSD."""+ yahoo = [+ {"symbol": "VALOUR-BTC-0-SEK.ST",+ "shortname": "Valour Bitcoin Zero SEK",+ "exchange": "STO", "quoteType": "EQUITY", "index": "XETR"},+ ]+ data = self._run("XAUUSD", yahoo)["data"]+ assert data["count"] == 0+ assert data["candidates"] == []++ def test_xauusd_slash_drops_near_string_crypto_hits(self):+ yahoo = [+ {"symbol": "AETHUSDT-USD", "shortname": "Aave Ethereum USDT",+ "exchange": "CCC", "quoteType": "CRYPTOCURRENCY", "index": "AETH"},+ ]+ data = self._run("XAU/USD", yahoo)["data"]+ assert data["count"] == 0++ def test_xauusd_x_drops_near_string_crypto_hits(self):+ yahoo = [+ {"symbol": "AETHUSDT-USD", "shortname": "Aave Ethereum USDT",+ "exchange": "CCC", "quoteType": "CRYPTOCURRENCY", "index": "AETH"},+ ]+ data = self._run("XAUUSD=X", yahoo)["data"]+ assert data["count"] == 0+++ def test_gold_query_keeps_the_real_gold_candidate(self):++ """The filter must not be one-way: the right instrument survives.++++ Every other case here asserts a rejection, and a filter that dropped++ everything would pass all of them. These assert the opposite++ direction, across spellings — a candidate written ``XAUUSD=X`` or++ ``XAU/USD`` is the same instrument as the query and must be kept.++ """++ yahoo = [++ {"symbol": "XAUUSD=X", "shortname": "XAU/USD",++ "exchange": "CCY", "quoteType": "CURRENCY", "index": "XAUUSD"},++ ]++ for query in ("XAUUSD", "XAU/USD", "XAU-USD", "XAUUSD=X"):++ data = self._run(query, yahoo)["data"]++ assert [c["symbol"] for c in data["candidates"]] == ["XAUUSD=X"], query++++ def test_fx_query_keeps_its_pair_across_spellings(self):++ yahoo = [++ {"symbol": "EURUSD=X", "shortname": "EUR/USD",++ "exchange": "CCY", "quoteType": "CURRENCY", "index": "EURUSD"},++ ]++ for query in ("EURUSD", "EUR/USD", "EURUSD=X"):++ data = self._run(query, yahoo)["data"]++ assert [c["symbol"] for c in data["candidates"]] == ["EURUSD=X"], query+++ def test_gc_f_keeps_correct_futures_hit(self):+ yahoo = [+ {"symbol": "GC=F", "shortname": "Gold Continuous Front Month",+ "exchange": "NYM", "quoteType": "FUTURE", "index": "GC"},+ ]+ data = self._run("GC=F", yahoo)["data"]+ assert data["count"] == 1+ assert data["candidates"][0]["symbol"] == "GC=F"++ def test_xaut_usdt_keeps_tokenized_gold(self):+ yahoo = [+ {"symbol": "XAUT-USDT", "shortname": "Tether Gold",+ "exchange": "CCC", "quoteType": "CRYPTOCURRENCY", "index": "XAUT"},+ ]+ data = self._run("XAUT-USDT", yahoo)["data"]+ assert data["count"] == 1+ assert data["candidates"][0]["symbol"] == "XAUT-USDT"++ def test_paxg_usdt_keeps_tokenized_gold(self):+ yahoo = [+ {"symbol": "PAXG-USDT", "shortname": "Paxos Gold",+ "exchange": "CCC", "quoteType": "CRYPTOCURRENCY", "index": "PAXG"},+ ]+ data = self._run("PAXG-USDT", yahoo)["data"]+ assert data["count"] == 1+ assert data["candidates"][0]["symbol"] == "PAXG-USDT"++ def test_free_text_name_query_unaffected(self):+ """A free-text company name (``apple``) is NOT a ticker shape and+ must not have crypto candidates stripped. Yahoo's free-text+ answer may include near-string crypto hits (``BTC-USD``) which+ the user is allowed to see and choose from.+ """+ yahoo = [+ {"symbol": "BTC-USD", "shortname": "Bitcoin USD",+ "exchange": "CCC", "quoteType": "CRYPTOCURRENCY", "index": "BTC"},+ {"symbol": "AAPL.US", "shortname": "Apple Inc.",+ "exchange": "NMS", "quoteType": "EQUITY", "index": "AAPL"},+ ]+ data = self._run("apple", yahoo)["data"]+ symbols = [c["symbol"] for c in data["candidates"]]+ assert "BTC-USD" in symbols+ assert "AAPL.US" in symbols