"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): close the English price-phrase hole and pin language parity
Haozhe Wu19ff47d1ab2c
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.pyindex 23a35c366..96add7c82 100644--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@ -209,10 +209,25 @@ _PRIVATE_ASSERTION_RE = re.compile(r"(?:是|仍是|属于)(?:一家)?(?:私人|私营|非上市)公司|未上市|没有上市)",re.IGNORECASE,)+# The bare verbs below are present tense only, which is not how an answer+# actually states an observed price: "closed at 412.35" and "last traded at+# 412.35" are the ordinary spellings and neither matches \bclose\b or+# \btrade\b. Chinese 收盘 / 现价 match, so the gate was strictly leakier in+# English than in Chinese — a fabricated USD price in the most natural+# phrasing walked straight through while its Chinese translation was caught.+# The past-tense forms are required to be followed by "at" so that reporting+# volume ("traded 1.2M shares") or a corporate event ("the deal closed at a+# 30% premium" — a percentage, already masked) is not read as a quote.+_PRICE_VERB_PAST_RE = r"\b(?:closed|opened|traded|quoted|priced|settled|fixed)\s+at\b"_PRICE_CONTEXT_RE = re.compile(r"(?:\b(?:opening|open|high|low|closing|close|price|quote)\b|"+ + _PRICE_VERB_PAST_RE + r"|"r"\b(?:entry|buy|target|support|resistance)\s+(?:price|level)\b|"r"开盘价?|最高价?|最低价?|收盘价?|买入价|入场价|目标价|支撑位?|阻力位?|"+ # Chinese had the mirror-image gap: these four are as ordinary as 收盘价+ # and none of them matched, so a fabricated 成交价 / 股价 walked through+ # exactly the way "closed at" did in English.+ r"成交价|最新价|股价|收报|"r"现价|报价|价格|价位)",re.IGNORECASE,)diff --git a/agent/tests/test_grounding_language_parity.py b/agent/tests/test_grounding_language_parity.pynew file mode 100644index 000000000..848633898--- /dev/null+++ b/agent/tests/test_grounding_language_parity.py@@ -0,0 +1,93 @@+"""The price gate must reach the same verdict in every language it reads.++The gate keys on a phrase list, and a phrase list is written in whatever+language the bug report arrived in. Twice now that has left it strictly+leakier on one side than the other:++- ``\\bclose\\b`` does not match "closed at", which is how an answer actually+ states an observed price. "The stock closed at 412.35." with zero tool calls+ passed, while "该股收盘 412.35。" — the same fabricated claim — was caught.+ Same for "last traded at" and "quoted at".+- Chinese had the mirror-image hole: 成交价 / 最新价 / 股价 / 收报 are as+ ordinary as 收盘价 and none of them matched.++So the assertion here is deliberately NOT a literal per language. Each case+runs the same claim through both spellings and asserts the two verdicts are+EQUAL. A per-language literal is what let the two halves drift apart, and it+would let the next patch move the breakage to the other side while staying+green — which is exactly what #1249 does today (it inverts the asymmetry for+derived returns rather than removing it).++Both arms are pinned. A gate whose tests only assert rejection cannot see+itself closing on everything.+"""++from __future__ import annotations++import pathlib+import tempfile++import pytest++from src.agent.grounding import GroundingLedger+++def _has_issues(text: str) -> bool:+ """Validate a final answer with no tool evidence at all."""+ ledger = GroundingLedger(+ run_dir=pathlib.Path(tempfile.mkdtemp()),+ user_message="analyze",+ history=None,+ )+ return bool(ledger.validate_final_answer(text).issues)+++# Unsourced price claims: no tool ran, so both spellings must be rejected.+_MUST_REJECT = [+ ("The stock closed at 412.35.", "该股收盘 412.35。"),+ ("The stock last traded at 412.35.", "该股最新成交价 412.35。"),+ ("Shares were quoted at 412.35.", "该股报价 412.35。"),+ ("The share price is 412.35.", "该股股价 412.35。"),+ ("It opened at 400.10.", "开盘 400.10。"),+ ("The closing price was 412.35.", "收盘价为 412.35。"),+]++# Not price claims at all: both spellings must pass. Without this arm the+# suite is satisfied by a gate that rejects everything.+_MUST_ACCEPT = [+ ("The meeting closed at 5pm.", "会议 5 点结束。"),+ ("The deal closed at a 30% premium.", "交易以 30% 溢价成交。"),+ ("Volume traded 1200000 shares.", "成交量 1200000 股。"),+ ("I could not retrieve the price.", "我没能取到价格。"),+ ("Gross margin fell 3.6pp.", "毛利率下降 3.6 个百分点。"),+ # The past-tense verbs only count when followed by "at". Without that+ # constraint a bare "closed"/"traded" turns any nearby number into a+ # price claim, and these two are the cases that catch it — the numbers+ # are small enough that the aggregate-amount mask does not hide them.+ ("The position closed 3 days later.", "该仓位 3 天后平掉。"),+ ("They traded 8 times last week.", "他们上周交易了 8 次。"),+]++[email protected](("english", "chinese"), _MUST_REJECT)+def test_unsourced_price_claims_rejected_in_both_languages(+ english: str, chinese: str+) -> None:+ en, zh = _has_issues(english), _has_issues(chinese)+ assert en == zh, (+ f"verdicts disagree by language: EN={en} ZH={zh}\n"+ f" EN: {english}\n ZH: {chinese}"+ )+ assert en, "an unsourced price claim must be rejected"++[email protected](("english", "chinese"), _MUST_ACCEPT)+def test_non_price_statements_accepted_in_both_languages(+ english: str, chinese: str+) -> None:+ en, zh = _has_issues(english), _has_issues(chinese)+ assert en == zh, (+ f"verdicts disagree by language: EN={en} ZH={zh}\n"+ f" EN: {english}\n ZH: {chinese}"+ )+ assert not en, "a non-price statement must not be rejected"