Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(agent): stop reading a ticker's last letter as an A-share or A/H constraint

Haozhe Wu7ff89e832ffc
diff --git a/agent/src/agent/resolution_context.py b/agent/src/agent/resolution_context.py
index b8b0c2fa4..b94e7f4c5 100644
--- a/agent/src/agent/resolution_context.py
+++ b/agent/src/agent/resolution_context.py
@@ -16,7 +16,7 @@ _MARKET_PATTERNS: tuple[tuple[str, re.Pattern[str]], ...] = (
(
"cn",
re.compile(
- r"A\s*股|沪深(?:市场)?|上交所|深交所|上海证券交易所|深圳证券交易所|"
+ r"(?<![A-Za-z])A\s*股|沪深(?:市场)?|上交所|深交所|上海证券交易所|深圳证券交易所|"
r"\bA[- ]?shares?\b|\bmainland China (?:stock|listing|market)s?\b",
re.IGNORECASE,
),
@@ -38,8 +38,11 @@ _MARKET_PATTERNS: tuple[tuple[str, re.Pattern[str]], ...] = (
),
),
)
+# Letter boundaries keep a ticker's last letter out: "NVDA股票" is not A股,
+# "NVDA和HOOD" / "NVDA/HOOD" are not an A/H cross-listing.
_CROSS_LISTING_RE = re.compile(
- r"(?:A\s*股?\s*[//和与、]\s*H\s*股?|H\s*股?\s*[//和与、]\s*A\s*股?|A\s*H\s*股)",
+ r"(?<![A-Za-z])(?:A\s*股?\s*[//和与、]\s*H\s*股?|H\s*股?\s*[//和与、]\s*A\s*股?|A\s*H\s*股)"
+ r"(?![A-Za-z])",
re.IGNORECASE,
)
_NEGATED_RE = re.compile(
diff --git a/agent/tests/test_grounding_resolution_context.py b/agent/tests/test_grounding_resolution_context.py
index e1bd288ec..4df0bf3bf 100644
--- a/agent/tests/test_grounding_resolution_context.py
+++ b/agent/tests/test_grounding_resolution_context.py
@@ -6,7 +6,10 @@ import json
from pathlib import Path
from typing import Any
+import pytest
+
from src.agent.grounding import GroundingLedger
+from src.agent.resolution_context import ResolutionContext
def _resolver_payload(query: str, candidates: list[dict[str, Any]]) -> str:
@@ -306,3 +309,28 @@ def test_feature_flag_can_restore_previous_resolution_behavior(tmp_path: Path) -
assert ledger.identity_status == "ambiguous"
assert ledger.authorized_symbols == set()
+
+
+ "message",
+ ["分析NVDA股票", "分析 NVDA 股票", "比较NVDA和HOOD的表现", "compare NVDA/HOOD performance"],
+)
+def test_a_ticker_ending_in_a_is_not_a_market_constraint(tmp_path: Path, message: str) -> None:
+ assert ResolutionContext.from_messages(message).constraints == ()
+ ledger = GroundingLedger(run_dir=tmp_path, user_message=message)
+ _ingest(ledger, "NVDA", [{"symbol": "NVDA.US", "name": "NVIDIA", "market": "us", "source": "yahoo"}])
+ assert ledger.authorized_symbols == {"NVDA.US"}
+
+
+ "message, expected",
+ [
+ ("请分析A股恒瑞医药", {"cn"}),
+ ("看 A 股", {"cn"}),
+ ("比较恒瑞医药 A/H 两地上市表现", {"cn", "hk"}),
+ ("恒瑞医药A股/H股", {"cn", "hk"}),
+ ("比较AH股溢价", {"cn", "hk"}),
+ ],
+)
+def test_written_market_words_still_constrain(message: str, expected: set[str]) -> None:
+ assert {c.value for c in ResolutionContext.from_messages(message).constraints} == expected