"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): stop the identity gate from refusing answers it has the evidence for
Haozhe Wu23aae78f8005
diff --git a/agent/src/agent/context.py b/agent/src/agent/context.pyindex 63e407387..6c687d3d6 100644--- a/agent/src/agent/context.py+++ b/agent/src/agent/context.py@@ -164,16 +164,21 @@ Decide which workflow to use based on the request:market/news/fundamentals/trading consumer MUST be in separate assistanttool-call turns;calls from one parallel batch share the identity state that existed before- the batch. Reuse the exact locked symbol and exchange suffix. Never silently- rewrite `.SS` to `.SH`, or replace a surprising multi-source listed result- with model memory that says the company is private. Ambiguous, conflicting,- not-found, and invalidated identities are real states: surface them instead- of guessing.+ the batch. Reuse the locked symbol; a provider's spelling of it (`600519.SS`,+ `sh600519`, `700.HK`, `BTC/USDT`) resolves to the same instrument, but never+ move a listing to a different exchange, and never replace a surprising+ multi-source listed result with model memory that says the company is+ private. Ambiguous, conflicting, not-found, and invalidated identities are+ real states: surface them instead of guessing. When the resolver answers with+ a shortlist — a dual A+H listing, a screening query — show the candidates and+ ask the user which one to use; re-querying will not collapse a genuine+ shortlist, and you may not pick one silently.- **Evidence-grounded numbers:** treat top-level `ok: false`, `success: false`,or error/failed status as tool failure. Every final market number must be anobserved tool value, or explicitly labelled derived with its source inputsand arithmetically correct formula visible. Price claims must surface the- locked canonical symbol+venue suffix, actual data source, and quote currency.+ locked canonical symbol+venue suffix, actual data source, and quote currency+ — all three may be written in the user's language (`雅虎`, `腾讯`, `元`).Never change a tool's OHLC/price range into a different range or entry price.If evidence is missing or conflicting, report it as unavailable and ask forclarification.diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.pyindex 13db61f80..ec9b6b96b 100644--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@ -42,17 +42,6 @@ _PRIVATE_COMPANY_SKILL_NAMES = {"private_company_analysis","private_company_research",}-_BARE_US_TICKER_TOOLS = {- "cancel_equity_order",- "get_equity_quotes",- "get_options_chain",- "get_sec_filings",- "get_stock_profile",- "place_equity_order",- "trading_cancel_order",- "trading_place_order",- "trading_quote",-}_SYMBOL_ARGUMENT_KEYS = {"code","codes",@@ -284,6 +273,27 @@ _PROSPECTIVE_LEVEL_RE = re.compile(# parentheses are deliberately not separators: an explicit derivation such as# "(8.5 - 7.9) / 2" must stay in one segment for the formula check._CLAUSE_SEPARATOR_RE = re.compile(r"[,,;;。、\n()【】]")+# The ASCII comma both separates clauses and groups thousands, and the clause+# split ran first: "收盘价 ¥1,309.22" became a clause ending in "¥1", whose 1 was+# compared against the observed 1300.01–1363.35 range and rejected as a+# conflict. That is every price above 999 written the ordinary way, and it is+# self-contradictory — ``_NUMBER_RE`` and the float conversion below it both+# already understand grouped numbers. Only a real group is removed: a comma+# needs a digit before it and exactly three digits after.+_THOUSANDS_SEPARATOR_RE = re.compile(r"(?<=\d),(?=\d{3}(?!\d))")+++def _split_clauses(text: str) -> list[str]:+ """Split prose into clauses without breaking a grouped number apart.++ Args:+ text: One line of candidate answer text.++ Returns:+ The clause segments, with thousands separators removed so a grouped+ price survives as one number.+ """+ return _CLAUSE_SEPARATOR_RE.split(_THOUSANDS_SEPARATOR_RE.sub("", text))_TABLE_SEPARATOR_RE = re.compile(r"^:?-{3,}:?$")_TABLE_FIELD_ALIASES = {@@ -307,6 +317,45 @@ _TABLE_FIELD_ALIASES = {"收盘价": "close",}_DATE_HEADERS = {"date", "datetime", "trade date", "timestamp", "日期", "交易日", "时间"}++# A loader's id is ASCII, but the answer follows the user's language, so a+# Chinese report names the same provider in Chinese. Demanding the ASCII id+# verbatim rejected correct prose: an answer reading "数据来源:腾讯财经" was+# reported as ``data_source_not_surfaced`` against evidence sourced from+# ``tencent``, and no rewrite short of writing the English word could pass.+_SOURCE_ALIASES = {+ "akshare": ("akshare", "ak share"),+ "baostock": ("baostock",),+ "binance": ("binance", "币安"),+ "ccxt": ("ccxt",),+ "eastmoney": ("eastmoney", "东方财富", "东财"),+ "futu": ("futu", "富途"),+ "mootdx": ("mootdx", "通达信"),+ "okx": ("okx", "欧易"),+ "pykrx": ("pykrx", "krx"),+ "sina": ("sina", "新浪"),+ "stooq": ("stooq",),+ "tencent": ("tencent", "腾讯"),+ "tushare": ("tushare",),+ "yahoo": ("yahoo", "雅虎"),+ "yfinance": ("yfinance", "yahoo", "雅虎"),+}+# "元" is how a Chinese answer writes a CNY quote, but it is also the tail of+# 港元/美元/日元, so accepting it unguarded would let an answer about a Hong Kong+# listing satisfy a CNY requirement. It counts only when no other currency's+# character owns it.+_BARE_YUAN_RE = re.compile(r"(?<![港美日欧韩台新加澳])元")+_CURRENCY_ALIASES = {+ "USD": ("usd", "us$", "美元", "美金"),+ # ¥ is how a model actually writes a CNY quote. It is the yen sign too, but+ # ``_infer_currency`` maps no venue to JPY, so nothing in this system can+ # mean yen by it; adding a JPY venue means revisiting this entry.+ "CNY": ("cny", "cnh", "rmb", "人民币", "¥", "¥"),+ "HKD": ("hkd", "hk$", "港元", "港币"),+ "KRW": ("krw", "韩元", "韩圜"),+ "INR": ("inr", "印度卢比", "卢比"),+ "CAD": ("cad", "加元", "加拿大元"),+}_SYMBOL_HEADERS = {"symbol", "ticker", "code", "标的", "代码", "证券代码"}@@ -315,9 +364,44 @@ def _utc_now() -> str:return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")+# Provider spellings that denote one instrument. Shanghai is quoted as ``.SH``+# by Eastmoney and ``.SS`` by Yahoo, A-share tools also accept an exchange+# prefix (``sh600519``), Hong Kong codes are zero-padded to five digits, and+# ccxt writes a crypto pair with a slash. Every one of these is a spelling, not+# an identity: ``_infer_venue`` and ``_infer_currency`` below already map ``.SS``+# and ``.SH`` to the same venue and the same currency. Treating them as+# different identities made ``search_symbol("600519")`` return two candidates+# for one listing, which no tie-break could resolve, so every Shanghai listing+# resolved ``ambiguous`` and no market tool could run for the rest of the run.+_EXCHANGE_PREFIXED_RE = re.compile(r"^(SH|SZ|BJ)(\d{6})$")++def _normalize_symbol(value: Any) -> str:- """Normalize a symbol for exact identity comparison."""- return str(value or "").strip().upper()+ """Normalize a symbol onto one canonical identity for exact comparison.++ Args:+ value: Any provider- or model-supplied symbol spelling.++ Returns:+ The canonical spelling — uppercased, with Shanghai's ``.SS`` alias+ folded onto ``.SH``, an exchange prefix rewritten as a suffix, a Hong+ Kong code zero-padded, and a crypto pair hyphenated. Text that is not a+ symbol is returned uppercased and otherwise untouched.+ """+ symbol = str(value or "").strip().upper().replace("/", "-")+ if not symbol:+ return ""+ prefixed = _EXCHANGE_PREFIXED_RE.match(symbol)+ if prefixed:+ return f"{prefixed.group(2)}.{prefixed.group(1)}"+ base, dot, suffix = symbol.rpartition(".")+ if not dot:+ return symbol+ if suffix == "SS":+ suffix = "SH"+ if suffix == "HK" and base.isdigit():+ base = base.zfill(5)+ return f"{base}.{suffix}"def _query_key(value: Any) -> str:@@ -423,7 +507,6 @@ def _infer_venue(symbol: str) -> str | None:suffixes = {".US": "us",".SH": "shanghai",- ".SS": "shanghai",".SZ": "shenzhen",".BJ": "beijing",".HK": "hong_kong",@@ -451,7 +534,6 @@ def _infer_currency(symbol: str) -> str | None:suffixes = {".US": "USD",".SH": "CNY",- ".SS": "CNY",".SZ": "CNY",".BJ": "CNY",".HK": "HKD",@@ -496,21 +578,6 @@ def _infer_instrument_type(symbol: str, candidate_type: Any = None) -> str:return "listed_security"-def _is_exchange_alias_conflict(left: str, right: str) -> bool:- """Return whether two symbols silently switch Shanghai suffix conventions."""- left_symbol = _normalize_symbol(left)- right_symbol = _normalize_symbol(right)- if left_symbol == right_symbol:- return False- left_base, _, left_suffix = left_symbol.rpartition(".")- right_base, _, right_suffix = right_symbol.rpartition(".")- return (- bool(left_base)- and left_base == right_base- and {left_suffix, right_suffix} == {"SH", "SS"}- )--@dataclass(frozen=True)class IdentityRecord:"""One versioned entity-to-instrument resolution result."""@@ -566,7 +633,10 @@ class ToolAuthorization:"identity": dict(identity),"required_action": ("Call search_symbol in a separate assistant tool turn, wait for "- "its result, then reuse the exact locked symbol and venue."+ "its result, then reuse the exact locked symbol and venue. If the "+ "resolver answers with a shortlist rather than one instrument, "+ "show the candidates and ask the user which one to use — narrowing "+ "the query again will not turn a genuine dual listing into one."),},ensure_ascii=False,@@ -631,17 +701,30 @@ class GroundingLedger:@propertydef identity_status(self) -> str:- """Return the aggregate first-class identity state."""+ """Return the aggregate first-class identity state.++ ``conflicting`` is the only state that outranks a successful lock: two+ sources contradicting each other about one query is a fact about the+ data, not a gap in it. Every other blocking state means "not known+ yet", and a side query that failed, went unanswered, or returned a+ shortlist must not retract an identity the run did lock — one flaky+ resolver call otherwise poisons every remaining answer in the session,+ with no path back. Per-symbol safety does not depend on this aggregate:+ a consumer still has to match a locked symbol in+ :meth:`_match_authorized_symbol` before it may run.+ """records = list(self._identities.values())if not records:return "unresolved" if self._identity_required else "not_required"statuses = {record.status for record in records}- for blocking in ("conflicting", "ambiguous", "invalidated", "unresolved"):- if blocking in statuses:- return blocking+ if "conflicting" in statuses:+ return "conflicting"if "locked" in statuses:return "locked"- if statuses == {"not_found"}:+ for blocking in ("ambiguous", "invalidated", "unresolved"):+ if blocking in statuses:+ return blocking+ if "not_found" in statuses:return "not_found"return "unresolved"@@ -738,7 +821,7 @@ class GroundingLedger:mismatched = tuple(symbolfor symbol in symbols- if self._match_authorized_symbol(tool_name, symbol, authorized) is None+ if self._match_authorized_symbol(symbol, authorized) is None)if mismatched:return ToolAuthorization(@@ -754,19 +837,24 @@ class GroundingLedger:@staticmethoddef _match_authorized_symbol(- tool_name: str,requested_symbol: str,authorized_symbols: Iterable[str],) -> str | None:"""Map a consumer argument to one unique locked canonical symbol.- Most repository tools accept the canonical symbol verbatim. A small- explicit set of U.S.-only APIs and broker transports require a bare- ticker; for those tools only, ``AAPL`` may consume ``AAPL.US``. Venue- aliases such as ``.SS`` and ``.SH`` are never treated as equivalent.+ Both sides are canonicalized first, so a provider alias (``600519.SS``),+ an exchange prefix (``sh600519``), an unpadded Hong Kong code+ (``700.HK``) or a slashed pair (``BTC/USDT``) addresses the instrument+ it names rather than being read as a silent venue rewrite.++ A bare code carries no venue, so it is accepted only when exactly one+ locked identity has it as its base. That uniqueness — not a list of+ which tools are allowed to use one — is what makes a bare ticker safe.+ The list this replaced named nine tools while eleven documented+ argument spellings across the registry were bare or prefixed, so the+ tools' own schema examples were being rejected.Args:- tool_name: Requested consumer tool.requested_symbol: Model-supplied symbol argument.authorized_symbols: Symbols locked before the tool batch.@@ -777,12 +865,12 @@ class GroundingLedger:authorized = {_normalize_symbol(item) for item in authorized_symbols}if requested in authorized:return requested- if tool_name not in _BARE_US_TICKER_TOOLS or "." in requested:+ if "." in requested:return Nonematches = [symbolfor symbol in authorized- if symbol.endswith(".US") and symbol.rsplit(".", 1)[0] == requested+ if "." in symbol and symbol.rsplit(".", 1)[0] == requested]return matches[0] if len(matches) == 1 else None@@ -1011,14 +1099,26 @@ class GroundingLedger:candidates = [dict(item) for item in raw_candidates if isinstance(item, dict)] if isinstance(raw_candidates, list) else []sources = data.get("sources") if isinstance(data.get("sources"), dict) else {}if not candidates:+ # "This entity does not exist" may only be concluded when every+ # source that could answer did answer. Counting two clean sources+ # instead was unreachable for a Chinese query — Yahoo cannot serve+ # one at all — so an entity that simply is not listed came back as+ # ``invalidated``, which blocks the run rather than answering it.+ # A source that skipped an unsupported query shape is not an outage.clean_sources = [str(name)for name, value in sources.items()if str(value).casefold() == "ok"]+ failed_sources = [+ str(name)+ for name, value in sources.items()+ if str(value).casefold() != "ok"+ and not str(value).casefold().startswith("skipped")+ ]self._identities[key] = IdentityRecord(query=query,- status="not_found" if len(clean_sources) >= 2 else "invalidated",+ status="not_found" if clean_sources and not failed_sources else "invalidated",source_tool_call_id=call_id,source=clean_sources,candidates=[],@@ -1048,19 +1148,16 @@ class GroundingLedger:)return- alias_conflicts = [- record- for record in self._identities.values()- if record.status == "locked"- and record.symbol- and _is_exchange_alias_conflict(record.symbol, symbol)- ]- if alias_conflicts:+ # A query that already spells a canonical symbol is asserting one, so a+ # resolver answering with a different instrument contradicts it rather+ # than refining it. This generalizes the ``.SS``/``.SH`` alias check it+ # replaces: that one fired on one exchange's two spellings and stayed+ # silent on an actual cross-exchange swap, which is the case that+ # matters.+ asserted = _scan_symbols(query)+ if asserted and symbol not in asserted:conflicting = list(candidates)- conflicting.extend(- {"symbol": record.symbol, "source": record.source}- for record in alias_conflicts- )+ conflicting.extend({"symbol": item, "source": ["query"]} for item in sorted(asserted))self._identities[key] = IdentityRecord(query=query,status="conflicting",@@ -1129,7 +1226,17 @@ class GroundingLedger:query: str,candidates: list[dict[str, Any]],) -> dict[str, Any] | None:- """Choose only a unique or strongly corroborated resolver candidate."""+ """Choose only a unique or strongly corroborated resolver candidate.++ Candidates are collapsed onto their canonical symbol first. Two rows+ that differ only by a provider's suffix convention describe one listing,+ and counting them as rival candidates is what left every Shanghai query+ with two "exact" matches and therefore no choice at all.+ """+ by_symbol: dict[str, dict[str, Any]] = {}+ for candidate in candidates:+ by_symbol.setdefault(_normalize_symbol(candidate.get("symbol")), candidate)+ candidates = list(by_symbol.values())if len(candidates) == 1:return candidates[0]normalized_query = re.sub(r"[^a-z0-9\u3400-\u9fff]", "", query.casefold())@@ -1229,12 +1336,12 @@ class GroundingLedger:exercised against nothing. Failed calls are deliberately excluded, so ablocked or erroring call never launders an invented ticker.- Bare symbol arguments are tracked separately as roots. Nine tools take a- bare US ticker by contract (``_BARE_US_TICKER_TOOLS``), so a run that- legitimately fetched ``AAPL`` never writes ``AAPL.US`` into any argument- or result. Without the root, the canonical spelling the rest of this- module demands — see ``canonical_symbol_not_surfaced`` — would be the one- spelling this gate rejects.+ Bare symbol arguments are tracked separately as roots. Many tools take a+ bare ticker by contract, so a run that legitimately fetched ``AAPL``+ never writes ``AAPL.US`` into any argument or result. Without the root,+ the canonical spelling the rest of this module demands — see+ ``canonical_symbol_not_surfaced`` — would be the one spelling this gate+ rejects.Args:arguments: Exact normalized tool arguments.@@ -1357,11 +1464,9 @@ class GroundingLedger:symbols = self._extract_symbol_arguments(arguments)symbol = symbols[0] if len(symbols) == 1 else Noneif symbol:- symbol = self._match_authorized_symbol(- tool_name,- symbol,- self.authorized_symbols,- ) or symbol+ symbol = (+ self._match_authorized_symbol(symbol, self.authorized_symbols) or symbol+ )source = str(payload.get("source") or tool_name)remaining = _MAX_GENERIC_EVIDENCE@@ -1399,12 +1504,27 @@ class GroundingLedger:"""Validate aggregate state and listed/private contradictions."""issues: list[dict[str, Any]] = []status = self.identity_status- if self._identity_required and status in {- "unresolved",- "ambiguous",- "conflicting",- "invalidated",- }:+ # Two conditions, both load-bearing.+ #+ # ``self._identities`` — a run that never named an instrument has no+ # identity to get wrong. The trigger phrase is matched against the user+ # message, so "什么是市盈率估值法?" set identity_required and then failed+ # every draft it could ever produce, including the honest answer. This+ # relaxation invents no licence to guess: a figure still has to survive+ # ``_validate_price_claims``, and a figure attached to a symbol no tool+ # handled still has to survive ``_validate_unsourced_symbols``.+ #+ # ``ambiguous`` is deliberately absent. A shortlist is an answer, which+ # is why ``_RESOLUTION_INCOMPLETE_STATUSES`` already lets workflow+ # selection proceed on it (#955) — but the final answer stayed blocked,+ # so a screening run loaded its skill and was then refused a conclusion.+ # Consumers remain blocked on ambiguous in ``authorize_tool_call``, so+ # such a run still cannot fetch a quote to misattribute.+ if (+ self._identity_required+ and self._identities+ and status in {"unresolved", "conflicting", "invalidated"}+ ):issues.append({"code": "identity_not_locked",@@ -1450,7 +1570,7 @@ class GroundingLedger:issues: list[dict[str, Any]] = []reported: set[str] = set()for line in content.splitlines():- for segment in _CLAUSE_SEPARATOR_RE.split(line):+ for segment in _split_clauses(line):unknown = sorted(symbolfor symbol in _scan_symbols(segment) - self._session_symbols - reported@@ -1485,6 +1605,10 @@ class GroundingLedger:"""issues, table_lines = self._validate_price_tables(content)records = self._comparable_price_records()+ # A report names its subject once and then writes prose about it. Both+ # narrower scopes are tried first; this is the last resort, and it only+ # resolves when the whole answer names exactly one evidence symbol.+ document_symbol = self._symbol_for_claim(content, records)has_price_claim = any(self._numbers_without_dates_or_percent(line)for index, line in enumerate(content.splitlines())@@ -1494,14 +1618,18 @@ class GroundingLedger:if index in table_lines or "|" in line:continueline_symbol = self._symbol_for_claim(line, records)- for segment in _CLAUSE_SEPARATOR_RE.split(line):+ for segment in _split_clauses(line):if not _PRICE_CONTEXT_RE.search(segment):continuevalues = self._numbers_without_dates_or_percent(segment)if not values:continuehas_price_claim = True- symbol = self._symbol_for_claim(segment, records) or line_symbol+ symbol = (+ self._symbol_for_claim(segment, records)+ or line_symbol+ or document_symbol+ )if self._is_explicit_derivation(segment, records, symbol):continuefor value in values:@@ -1543,7 +1671,14 @@ class GroundingLedger:issues: list[dict[str, Any]] = []folded = content.casefold()symbols = sorted({record.symbol for record in records if record.symbol})- mentioned = [symbol for symbol in symbols if symbol.casefold() in folded]+ # ``_scan_symbols`` canonicalizes, so an answer that writes Shanghai as+ # ``600519.SS`` still surfaces the ``600519.SH`` identity it names.+ written = _scan_symbols(content)+ mentioned = [+ symbol+ for symbol in symbols+ if symbol in written or symbol.casefold() in folded+ ]if not mentioned:issues.append({@@ -1568,7 +1703,16 @@ class GroundingLedger:if record.source and record.source.casefold() not in {"auto", "unknown"}})- missing_sources = [source for source in sources if source.casefold() not in folded]+ missing_sources = [+ source+ for source in sources+ if not any(+ alias in folded+ for alias in _SOURCE_ALIASES.get(+ source.casefold(), (source.casefold(),)+ )+ )+ ]if missing_sources:issues.append({@@ -1607,16 +1751,12 @@ class GroundingLedger:@staticmethoddef _currency_is_surfaced(currency: str, content: str) -> bool:"""Return whether a quote currency or an unambiguous alias is visible."""- aliases = {- "USD": ("usd", "us$", "美元"),- "CNY": ("cny", "rmb", "人民币"),- "HKD": ("hkd", "hk$", "港元"),- "KRW": ("krw", "韩元"),- "INR": ("inr", "印度卢比"),- }folded = content.casefold()- tokens = aliases.get(currency.upper(), (currency.casefold(),))- return any(token.casefold() in folded for token in tokens)+ code = currency.upper()+ tokens = _CURRENCY_ALIASES.get(code, (currency.casefold(),))+ if any(token.casefold() in folded for token in tokens):+ return True+ return code == "CNY" and bool(_BARE_YUAN_RE.search(content))def _validate_price_tables(self,@@ -1709,17 +1849,14 @@ class GroundingLedger:symbols = sorted({record.symbol for record in candidates if record.symbol})if not symbol and len(symbols) == 1:symbol = symbols[0]- elif not symbol and len(symbols) > 1:- return {- "code": "numeric_claim_ambiguous_symbol",- "claim": claim,- "value": value,- "symbols": symbols,- "message": (- f"Price claim {value:g} is ambiguous across multiple evidence symbols; "- "name the canonical symbol explicitly."- ),- }+ # An unattributed claim used to be rejected outright once the run held+ # evidence for more than one symbol. That is every comparison report:+ # "Apple's closing price was 313.33 USD. Microsoft closed higher." names+ # its subject by company name, and the clause was refused although the+ # value was exactly the observed close sitting in evidence. Such a claim+ # is now checked against the union of the observed quotes instead, so a+ # number the run never observed is still caught below — it simply has to+ # match nothing at all rather than nothing under one chosen symbol.if field_name:candidates = [record for record in candidates if record.field == field_name]if date_value:diff --git a/agent/src/tools/symbol_search_tool.py b/agent/src/tools/symbol_search_tool.pyindex 43e9ba293..e65e8e64c 100644--- a/agent/src/tools/symbol_search_tool.py+++ b/agent/src/tools/symbol_search_tool.py@@ -269,9 +269,18 @@ def _search_yahoo(query: str) -> tuple[List[Dict[str, Any]], str]:query: Free-text name or ticker fragment.Returns:- ``(candidates, status)`` where ``status`` is ``"ok"`` on success or a- short error string when the source failed (candidates is then empty).+ ``(candidates, status)`` where ``status`` is ``"ok"`` on success, a+ ``"skipped: ..."`` marker when the source cannot serve this query shape,+ or a short error string when the source failed (candidates is then+ empty)."""+ if not query.isascii():+ # Yahoo's search endpoint answers any non-ASCII query with HTTP 400+ # (verified against both query1 and query2 hosts), so calling it spends+ # a request to manufacture a source failure. That failure is not+ # cosmetic: a caller deciding whether an entity exists counts clean+ # sources, and an unsupported query shape must not read as an outage.+ return [], "skipped: non-ASCII query is not supported by this source"try:quotes = yahoo_client.search(query)except Exception as exc: # noqa: BLE001 - one source failing is non-fatal@@ -335,6 +344,15 @@ def _from_yahoo_symbol(raw_symbol: str, quote: Dict[str, Any]) -> tuple[str, strreturn f"{base.zfill(5)}.HK", "hk"if upper.endswith((".TO", ".V")):return upper, "ca"+ # Yahoo quotes Shanghai as ``.SS`` where this project (and Eastmoney) use+ # ``.SH``. Emitting both spellings published one listing as two rival+ # candidates, which the identity gate could not choose between, so every+ # Shanghai query dead-ended as ambiguous. Folding here also lets the two+ # sources merge and corroborate each other via ``also_from``.+ if upper.endswith(".SS"):+ return f"{upper[: -len('.SS')]}.SH", "cn"+ if upper.endswith((".SH", ".SZ", ".BJ")):+ return upper, "cn"quote_type = str(quote.get("quoteType") or "").strip().upper()if quote_type == "EQUITY" and "." not in raw_symbol and "-" not in raw_symbol:return f"{upper}.US", "us"diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.pyindex 8ce663af6..d122ad4bc 100644--- a/agent/tests/test_agent_grounding.py+++ b/agent/tests/test_agent_grounding.py@@ -217,7 +217,7 @@ def test_resolver_and_consumer_in_same_batch_cannot_race(assert resolver.calls == 1assert market.calls == 0- assert agent._grounding.authorized_symbols == {"562500.SS"}+ assert agent._grounding.authorized_symbols == {"562500.SH"}blocked = [json.loads(message["content"]) for message in messages]assert any(item.get("error_code") == "identity_required" for item in blocked)@@ -294,31 +294,91 @@ def test_market_sensitive_skill_waits_for_prior_identity_batch(assert skill.calls == 1-def test_bare_us_ticker_is_allowed_only_for_explicit_transport_contract(+ ("tool_name", "arguments"),+ [+ ("get_sec_filings", {"ticker": "AAPL"}),+ ("get_market_data", {"codes": ["AAPL"]}),+ ("get_fundamentals", {"symbols": ["AAPL"]}),+ ("technical_indicators", {"symbol": "AAPL"}),+ ("portfolio_risk_xray", {"symbols": ["AAPL"]}),+ ("trading_history", {"symbol": "AAPL"}),+ ],+)+def test_bare_ticker_is_allowed_whenever_it_names_one_locked_identity(tmp_path: Path,+ tool_name: str,+ arguments: dict[str, Any],) -> None:- """SEC's bare ticker consumes AAPL.US without permitting venue aliases."""+ """A bare ticker is safe by uniqueness, not by a hand-maintained tool list.++ Every spelling here is the first example in that tool's own parameter+ schema. The list this replaced named nine tools, so the other five were+ rejected for using their documented contract.+ """ledger = GroundingLedger(run_dir=tmp_path,user_message="请分析 AAPL.US 的价格",)- sec = ledger.authorize_tool_call(- "get_sec_filings",- {"ticker": "AAPL"},+ authorization = ledger.authorize_tool_call(+ tool_name,+ arguments,batch_authorized_symbols=ledger.authorized_symbols,- call_id="sec",+ call_id="consumer",+ )++ assert authorization.allowed is True+++def test_bare_ticker_stays_blocked_when_it_names_more_than_one_identity(+ tmp_path: Path,+) -> None:+ """Uniqueness is the whole guarantee, so a shared base must not resolve."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="对比 600519.SH 与 600519.SZ 的价格",)- market_alias = ledger.authorize_tool_call(++ authorization = ledger.authorize_tool_call(+ "get_market_data",+ {"codes": ["600519"]},+ batch_authorized_symbols=ledger.authorized_symbols,+ call_id="prices",+ )++ assert ledger.authorized_symbols == {"600519.SH", "600519.SZ"}+ assert authorization.allowed is False+ assert authorization.error_code == "identity_mismatch"+++ ("locked", "requested"),+ [+ ("600519.SH", "600519.SS"),+ ("600519.SH", "sh600519"),+ ("00700.HK", "700.HK"),+ ("00700.HK", "0700.HK"),+ ("BTC-USDT", "BTC/USDT"),+ ],+)+def test_provider_spellings_of_one_instrument_are_one_identity(+ tmp_path: Path,+ locked: str,+ requested: str,+) -> None:+ """A suffix convention, an exchange prefix, or a separator is not a venue."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message=f"{locked} 现价多少")++ authorization = ledger.authorize_tool_call("get_market_data",- {"codes": ["AAPL"]},+ {"codes": [requested]},batch_authorized_symbols=ledger.authorized_symbols,call_id="prices",)- assert sec.allowed is True- assert market_alias.allowed is False- assert market_alias.error_code == "identity_mismatch"+ assert ledger.authorized_symbols == {locked}+ assert authorization.allowed is Truedef test_stale_history_identity_does_not_unlock_new_subject(tmp_path: Path) -> None:@@ -387,10 +447,14 @@ def test_single_clean_not_found_source_is_not_enough_for_private_routing(assert json.loads(messages[-1]["content"])["error_code"] == "identity_required"-def test_explicit_symbol_and_resolver_suffix_alias_become_conflicting(+def test_explicit_symbol_and_resolver_suffix_alias_are_one_identity(tmp_path: Path,) -> None:- """A later .SH resolver result cannot silently replace explicit .SS identity."""+ """``.SS`` and ``.SH`` name the same Shanghai listing, not a contradiction.++ Reading them as rivals is what made every Shanghai query unusable: the two+ sources publish one listing under both spellings, so no tie-break existed.+ """ledger = GroundingLedger(run_dir=tmp_path,user_message="请分析 562500.SS 并给出买入价",@@ -411,15 +475,44 @@ def test_explicit_symbol_and_resolver_suffix_alias_become_conflicting(call_id="prices",)+ assert ledger.identity_status == "locked"+ assert ledger.authorized_symbols == {"562500.SH"}+ assert authorization.allowed is True+++def test_resolver_answering_a_different_venue_is_still_conflicting(+ tmp_path: Path,+) -> None:+ """Folding a suffix alias must not fold a genuinely different exchange."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="请分析 600519.SH 并给出买入价",+ )+ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "600519.SH"},+ result=_resolver_payload("600519.SZ", query="600519.SH"),+ call_id="resolver-venue",+ success=True,+ )++ authorization = ledger.authorize_tool_call(+ "get_market_data",+ {"codes": ["600519.SZ"]},+ batch_authorized_symbols=ledger.authorized_symbols,+ batch_identity_status=ledger.identity_status,+ call_id="prices",+ )+assert ledger.identity_status == "conflicting"assert authorization.allowed is Falseassert authorization.error_code == "identity_conflict"-def test_locked_symbol_rejects_silent_exchange_suffix_rewrite(+def test_locked_symbol_rejects_silent_exchange_rewrite(tmp_path: Path,) -> None:- """The consumer may not silently turn the resolver's .SS into .SH."""+ """The consumer may not move the locked listing to another exchange."""agent, _, market, _, trace = _build_direct_agent(tmp_path, _resolver_payload())messages: list[dict[str, Any]] = []react_trace: list[dict[str, Any]] = []@@ -433,7 +526,7 @@ def test_locked_symbol_rejects_silent_exchange_suffix_rewrite(1,)agent._process_tool_calls(- [_tool_call("wrong-suffix", "get_market_data", codes=["562500.SH"])],+ [_tool_call("wrong-venue", "get_market_data", codes=["562500.SZ"])],ContextBuilder,messages,trace,@@ -1144,3 +1237,393 @@ def test_plan_level_alone_reaches_a_valid_answer(tmp_path: Path) -> None:)assert result.valid is True, result.issues+++def _shanghai_shortlist() -> str:+ """One Shanghai listing as the two sources actually publish it."""+ return _resolver_payload(+ candidates=[+ {+ "symbol": "600519.SH",+ "name": "贵州茅台",+ "market": "cn",+ "type": "沪A",+ "source": "eastmoney",+ },+ {+ "symbol": "600519.SS",+ "name": "Kweichow Moutai Co Ltd",+ "market": "cn",+ "type": "EQUITY",+ "source": "yahoo",+ },+ ],+ query="600519",+ )+++def test_shanghai_ticker_resolves_to_one_locked_identity(tmp_path: Path) -> None:+ """Eastmoney's .SH and Yahoo's .SS describe one listing, so one lock."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="600519 现价多少")+ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "600519"},+ result=_shanghai_shortlist(),+ call_id="resolve",+ success=True,+ )++ assert ledger.identity_status == "locked"+ assert ledger.authorized_symbols == {"600519.SH"}+++def test_shanghai_lock_depends_on_symbol_canonicalization(+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Mutation guard: drop canonicalization and Shanghai dead-ends again."""+ monkeypatch.setattr(+ "src.agent.grounding._normalize_symbol",+ lambda value: str(value or "").strip().upper(),+ )+ ledger = GroundingLedger(run_dir=tmp_path, user_message="600519 现价多少")+ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "600519"},+ result=_shanghai_shortlist(),+ call_id="resolve",+ success=True,+ )++ assert ledger.identity_status == "ambiguous"+ assert ledger.authorized_symbols == set()+++def test_zero_candidates_with_a_skipped_source_are_not_found(tmp_path: Path) -> None:+ """A source that cannot serve the query shape does not block the answer.++ The counterpart — a source that actually failed — is covered by+ ``test_single_clean_not_found_source_is_not_enough_for_private_routing``,+ which must keep reporting ``invalidated``.+ """+ ledger = GroundingLedger(run_dir=tmp_path, user_message="这家公司现在股价多少")+ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "某某不存在公司"},+ result=json.dumps(+ {+ "ok": True,+ "source": "symbol_search",+ "data": {+ "query": "某某不存在公司",+ "count": 0,+ "candidates": [],+ "sources": {+ "eastmoney": "ok",+ "yahoo": "skipped: non-ASCII query is not supported",+ },+ },+ },+ ensure_ascii=False,+ ),+ call_id="resolve",+ success=True,+ )++ assert ledger.identity_status == "not_found"+ assert ledger.validate_final_answer("没有查到这家公司,无法给出结论。").valid is True+++def test_a_failed_side_query_does_not_retract_a_locked_identity(+ tmp_path: Path,+) -> None:+ """One flaky resolver call must not end the run's ability to answer."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="茅台现价多少")+ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "茅台"},+ result=json.dumps({"ok": False, "error": "timeout"}),+ call_id="flaky",+ success=False,+ )+ assert ledger.identity_status == "invalidated"++ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "贵州茅台"},+ result=_resolver_payload("600519.SH", query="贵州茅台"),+ call_id="retry",+ success=True,+ )++ assert ledger.identity_status == "locked"+ assert ledger.authorize_tool_call(+ "get_market_data",+ {"codes": ["600519.SH"]},+ batch_authorized_symbols=ledger.authorized_symbols,+ batch_identity_status=ledger.identity_status,+ call_id="prices",+ ).allowed is True+++def test_a_conflict_outranks_a_lock_from_another_query(tmp_path: Path) -> None:+ """A contradiction is a fact about the data, so a lock cannot mask it."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="对比 600519.SH 和 AAPL.US 的现价",+ )+ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "600519.SH"},+ result=_resolver_payload("600519.SZ", query="600519.SH"),+ call_id="venue-swap",+ success=True,+ )++ assert ledger.identity_status == "conflicting"+++def _cny_ledger(tmp_path: Path) -> GroundingLedger:+ """A ledger holding one Shanghai quote sourced from yahoo, priced in CNY."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="562500.SH 现价多少")+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["562500.SH"]},+ result=_market_payload(),+ call_id="prices",+ success=True,+ )+ return ledger+++def test_a_chinese_answer_may_name_its_source_and_currency_in_chinese(+ tmp_path: Path,+) -> None:+ """The answer follows the user's language; the gate must read that language."""+ result = _cny_ledger(tmp_path).validate_final_answer(+ "562500.SH 最新收盘价 1.171 元,数据来源:雅虎财经。"+ )++ assert result.valid is True, result.issues+++def test_another_currencys_yuan_does_not_satisfy_a_cny_requirement(+ tmp_path: Path,+) -> None:+ """A bare 元 counts for CNY only when no other currency's character owns it."""+ result = _cny_ledger(tmp_path).validate_final_answer(+ "562500.SH 最新收盘价 1.171 港元,数据来源:雅虎财经。"+ )++ assert result.valid is False+ assert "currency_not_surfaced" in {issue["code"] for issue in result.issues}+++def test_an_unnamed_source_is_still_reported(tmp_path: Path) -> None:+ """Accepting a localized provider name is not accepting no provider name."""+ result = _cny_ledger(tmp_path).validate_final_answer("562500.SH 最新收盘价 1.171 元。")++ assert result.valid is False+ assert "data_source_not_surfaced" in {issue["code"] for issue in result.issues}+++def _comparison_ledger(tmp_path: Path) -> GroundingLedger:+ """A ledger holding quotes for two instruments, as a comparison run does."""+ ledger = GroundingLedger(+ run_dir=tmp_path,+ user_message="对比 562500.SH 与 AAPL.US 现价",+ )+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["562500.SH"]},+ result=_market_payload(),+ call_id="cn-prices",+ success=True,+ )+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=_market_payload("AAPL.US"),+ call_id="us-prices",+ success=True,+ )+ return ledger+++def test_a_comparison_naming_its_subject_by_name_is_valid(tmp_path: Path) -> None:+ """Prose that identifies its subject in words must not be refused."""+ result = _comparison_ledger(tmp_path).validate_final_answer(+ "## 562500.SH vs AAPL.US(来源 yahoo)\n"+ "562500.SH 收盘价 1.171 元。\n"+ "苹果的收盘价是 1.171 美元,两者收在同一水平。"+ )++ assert result.valid is True, result.issues+++def test_a_comparison_with_an_invented_quote_is_still_rejected(+ tmp_path: Path,+) -> None:+ """Checking against the union of observed quotes is not checking nothing."""+ result = _comparison_ledger(tmp_path).validate_final_answer(+ "## 562500.SH vs AAPL.US(来源 yahoo)\n"+ "562500.SH 收盘价 1.171 元。\n"+ "苹果的收盘价是 999.99 美元。"+ )++ assert result.valid is False+ assert {"numeric_claim_conflict", "numeric_claim_unavailable"} & {+ issue["code"] for issue in result.issues+ }+++ ("question", "answer"),+ [+ (+ "什么是市盈率估值法?请解释一下原理",+ "市盈率是市值与净利润的比值,用于横向比较同行业公司的相对贵贱。",+ ),+ (+ "explain how to trade using RSI",+ "RSI above 70 is conventionally read as overbought, below 30 as oversold.",+ ),+ ],+)+def test_a_conceptual_question_reaches_its_answer(+ tmp_path: Path,+ question: str,+ answer: str,+) -> None:+ """A run that never named an instrument has no identity to get wrong.++ The trigger phrase is matched against the user message, so these questions+ demanded a locked identity that no correct answer could ever supply.+ """+ ledger = GroundingLedger(run_dir=tmp_path, user_message=question)++ assert ledger.validate_final_answer(answer).valid is True+++ "answer",+ [+ "贵州茅台现价约 1300 元,建议买入。",+ "600519.SH 收盘价 1300.00 元(source: tencent),建议买入。",+ ],+)+def test_an_unevidenced_price_is_still_rejected_without_any_tool_call(+ tmp_path: Path,+ answer: str,+) -> None:+ """Relaxing the identity check must not license a remembered quote."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="茅台适合买入吗")++ result = ledger.validate_final_answer(answer)++ assert result.valid is False+ assert {"numeric_claim_unavailable", "unsourced_symbol_figures"} & {+ issue["code"] for issue in result.issues+ }+++def test_a_shortlist_answers_the_user_but_still_cannot_fetch_a_quote(+ tmp_path: Path,+) -> None:+ """#955 closed half of this: the skill loaded, the answer stayed blocked."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="推荐几只低估值的高股息A股")+ ledger.ingest_tool_result(+ tool_name="search_symbol",+ arguments={"query": "高股息"},+ result=_resolver_payload(+ candidates=[+ {"symbol": "601398.SH", "name": "工商银行", "source": "eastmoney"},+ {"symbol": "600028.SH", "name": "中国石化", "source": "eastmoney"},+ ],+ query="高股息",+ ),+ call_id="screen",+ success=True,+ )++ assert ledger.identity_status == "ambiguous"+ assert ledger.validate_final_answer(+ "候选清单命中多个标的,请确认你要看哪一只,我再去取行情。"+ ).valid is True+ assert ledger.authorize_tool_call(+ "get_market_data",+ {"codes": ["601398.SH"]},+ batch_authorized_symbols=ledger.authorized_symbols,+ batch_identity_status=ledger.identity_status,+ call_id="prices",+ ).allowed is False+++def _large_cap_ledger(tmp_path: Path) -> GroundingLedger:+ """A ledger holding a quote large enough to be written with separators."""+ ledger = GroundingLedger(run_dir=tmp_path, user_message="600519.SH 收盘价多少")+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["600519.SH"]},+ result=json.dumps(+ {+ "600519.SH": [+ {+ "trade_date": "2026-08-07",+ "open": 1308.66,+ "high": 1315.28,+ "low": 1301.00,+ "close": 1309.22,+ "volume": 24976.0,+ }+ ],+ "_provenance": {+ "600519.SH": {+ "source": "tencent",+ "requested_source": "auto",+ "currency_conversion": "none",+ }+ },+ }+ ),+ call_id="prices",+ success=True,+ )+ return ledger+++def test_a_grouped_price_is_not_split_into_a_bogus_claim(tmp_path: Path) -> None:+ """"¥1,309.22" must stay one number when the clause is split.++ The comma is both a clause separator and a thousands separator, and the+ split ran first, leaving a clause ending in "¥1". That 1 was compared+ against the observed 1300.01–1363.35 range and rejected — which is every+ price above 999 written the ordinary way.+ """+ result = _large_cap_ledger(tmp_path).validate_final_answer(+ "贵州茅台 600519.SH 最近一个交易日的收盘价为 ¥1,309.22,数据来源:腾讯行情。"+ )++ assert result.valid is True, result.issues+++def test_a_grouped_price_that_contradicts_evidence_is_still_rejected(+ tmp_path: Path,+) -> None:+ """Keeping the group together is not the same as skipping the check."""+ result = _large_cap_ledger(tmp_path).validate_final_answer(+ "贵州茅台 600519.SH 最近一个交易日的收盘价为 ¥1,888.88,数据来源:腾讯行情。"+ )++ assert result.valid is False+ assert "numeric_claim_conflict" in {issue["code"] for issue in result.issues}+++def test_a_clause_comma_still_separates_clauses(tmp_path: Path) -> None:+ """Only a real thousands group is protected, not every comma."""+ result = _large_cap_ledger(tmp_path).validate_final_answer(+ "600519.SH 数据来源:腾讯行情, 收盘价 ¥1,888.88 元。"+ )++ assert result.valid is False+ assert "numeric_claim_conflict" in {issue["code"] for issue in result.issues}diff --git a/agent/tests/test_agent_output_discipline.py b/agent/tests/test_agent_output_discipline.pyindex aa2f8ba42..067083a83 100644--- a/agent/tests/test_agent_output_discipline.py+++ b/agent/tests/test_agent_output_discipline.py@@ -408,10 +408,10 @@ class TestTheGateDoesNotKillCorrectAnswers:result = ledger.validate_final_answer(f"{_GROUNDED_ANSWER}\n候选 600519.SH 权重 30.0。")- # A two-candidate shortlist is still ``ambiguous`` identity, which the- # pre-existing identity gate reports on its own; this gate must add- # nothing on top of it.- assert [issue["code"] for issue in result.issues] == ["identity_not_locked"]+ # A shortlist is an answer, not a gap, so it no longer blocks the final+ # answer either — consumers stay blocked on ``ambiguous`` instead. This+ # gate must add nothing of its own on top of that.+ assert [issue["code"] for issue in result.issues] == []def test_prose_with_no_symbols_and_no_evidence_is_untouched(self, tmp_path: Path) -> None:ledger = GroundingLedger(run_dir=tmp_path, user_message="什么是夏普比率?")diff --git a/agent/tests/test_symbol_search_tool.py b/agent/tests/test_symbol_search_tool.pyindex 8bc0b7d09..0780e5608 100644--- a/agent/tests/test_symbol_search_tool.py+++ b/agent/tests/test_symbol_search_tool.py@@ -225,3 +225,67 @@ class TestSymbolSearchErrors:# The US candidate still appears, just without a CIK.aapl = next(c for c in payload["data"]["candidates"] if c["symbol"] == "AAPL.US")assert "cik" not in aapl+++class TestShanghaiAliasAndUnsupportedQueries:+ """The two resolver defects that made Shanghai and Chinese queries unusable."""++ def test_yahoo_shanghai_suffix_folds_onto_the_project_convention(self):+ """Yahoo's ``.SS`` and Eastmoney's ``.SH`` must merge into one candidate.++ Emitted separately they became two rival candidates for one listing,+ which no downstream tie-break could resolve, so every Shanghai query+ dead-ended before any market tool could run.+ """+ with patch.object(+ ss.eastmoney_client, "get_json", return_value=_eastmoney_payload()+ ), patch.object(+ ss.yahoo_client,+ "search",+ return_value=[+ {+ "symbol": "600519.SS",+ "shortname": "Kweichow Moutai Co Ltd",+ "exchange": "SHH",+ "quoteType": "EQUITY",+ }+ ],+ ):+ data = json.loads(ss.SymbolSearchTool().execute(query="600519"))["data"]++ by_symbol = {c["symbol"]: c for c in data["candidates"]}+ assert "600519.SS" not in by_symbol+ assert by_symbol["600519.SH"]["market"] == "cn"+ assert "yahoo" in by_symbol["600519.SH"].get("also_from", [])++ def test_non_ascii_query_skips_yahoo_without_calling_it(self):+ """A source that cannot serve a query shape is skipped, not failed.++ Yahoo answers any non-ASCII query with HTTP 400. Recording that as a+ source failure made "this entity is not listed" indistinguishable from+ "a source is down" for every Chinese query.+ """+ with patch.object(+ ss.eastmoney_client, "get_json", return_value=_eastmoney_payload()+ ), patch.object(ss.yahoo_client, "search") as search, patch.object(+ ss.sec_edgar_client, "cik_for", return_value="0000320193"+ ):+ data = json.loads(ss.SymbolSearchTool().execute(query="贵州茅台"))["data"]++ search.assert_not_called()+ assert data["sources"]["yahoo"].startswith("skipped:")+ assert data["sources"]["eastmoney"] == "ok"++ def test_ascii_query_still_reaches_yahoo(self):+ """The skip is keyed on the query shape, not switched on permanently."""+ with patch.object(+ ss.eastmoney_client, "get_json", return_value=_eastmoney_payload()+ ), 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="apple"))["data"]++ search.assert_called_once()+ assert data["sources"]["yahoo"] == "ok"