"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): stop reading scores, indicators, and window lengths as prices
Haozhe Wuf8e888c7d408
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.pyindex d232867ce..b2d986057 100644--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@ -184,12 +184,49 @@ _AGGREGATE_AMOUNT_RE = re.compile(r"\s*(?:为|是|约)?\s*[::]?\s*[-+]?\d[\d,]*(?:\.\d+)?",re.IGNORECASE,)-# Quantities, horizons, and lot sizes are unit-bearing: "100 股", "1–4 周",-# "3 个月". None of them are prices.+# Quantities, horizons, lot sizes, and lookback windows are unit-bearing:+# "100 股", "1–4 周", "3 个月", "52-week", "20/50/200-day". None are prices.+# The hyphenated English compound needs its own branch: the range alternation+# consumes "-4" in "1-4 周" but stalls on "-week", which left 52 behind to be+# compared against an OHLC range (#1001). The slash enumeration shares a single+# trailing unit, so "20/50/200-day" has to be masked as one span or its first+# two window lengths survive. ASCII units carry a trailing word boundary so+# "120 more" is not read as a quantity; the CJK branch cannot, because 周 and+# 内 are both word characters and "1–4 周内" must still mask._QUANTITY_WITH_UNIT_RE = re.compile(- r"\d[\d,]*(?:\.\d+)?(?:\s*[-–—~至]\s*\d[\d,]*(?:\.\d+)?)?\s*"- r"(?:股|手|张|份|口|笔|倍|个月|周|天|日|年|次|"- r"shares?|contracts?|lots?|units?|weeks?|months?|days?|years?)",+ r"\d[\d,]*(?:\.\d+)?"+ r"(?:\s*/\s*\d[\d,]*(?:\.\d+)?)*"+ r"(?:\s*[-–—~至]\s*\d[\d,]*(?:\.\d+)?)?"+ r"\s*[-–—]?\s*"+ r"(?:"+ r"(?:股|手|张|份|口|笔|倍|个月|周|天|日|年|次)"+ r"|(?:shares?|contracts?|lots?|units?|sessions?|bars?|periods?|"+ r"wks?|weeks?|months?|days?|years?|yrs?)\b"+ r")",+ re.IGNORECASE,+)+# A conviction reading is on a labelled scale, not a price scale: the 6 in+# "CONFIDENCE: 6" is bounded by the label that introduces it. Only the value+# bound to the label is masked, so a genuine quote elsewhere in the same+# clause is still checked. The optional denominator covers "6/10" (#1001).+_LABELLED_SCORE_RE = re.compile(+ r"(?:confidence|conviction|score|rating|probability|odds|weighting|"+ r"置信度|信心|评分|得分|概率|胜率)"+ r"\s*(?:is|of|=|为|是)?\s*[::]?\s*"+ r"[-+]?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?",+ re.IGNORECASE,+)+# A named indicator reads on its own scale — "RSI of 46.7" is bounded at 100,+# not quoted in the instrument's currency. The name must be adjacent to the+# value, so a bare number elsewhere in the clause stays checked. Only+# unambiguous indicator names are listed: generic words such as "momentum" or+# "volatility" sit too close to price prose to mask safely.+_INDICATOR_VALUE_RE = re.compile(+ r"\b(?:rsi|macd|atr|adx|cci|obv|kdj|boll|dif|dea|vix|iv|"+ r"sharpe|sortino|beta)\b"+ r"(?:\s*\([^)]{0,20}\))?"+ r"\s*(?:is|at|of|reads?|=|为|是)?\s*[::]?\s*"+ r"[-+]?\d[\d,]*(?:\.\d+)?",re.IGNORECASE,)# Full-width brackets and enumeration commas delimit prose clauses. ASCII@@ -1658,9 +1695,12 @@ class GroundingLedger:"""Extract the numbers in a claim that could plausibly be prices.Digits that belong to a canonical symbol, a calendar date, an aggregate- amount, a unit-bearing quantity, or a percentage are masked first. Left- unmasked they are compared against observed OHLC ranges and reject a- correct draft: ``000543.SZ`` alone contributes 543.+ amount, a labelled score, a named indicator reading, a unit-bearing+ quantity, or a percentage are masked first. Left unmasked they are+ compared against observed OHLC ranges and reject a correct draft:+ ``000543.SZ`` alone contributes 543, and a well-formed verdict line+ contributes its confidence score and every moving-average window it+ names (#1001).Args:text: One claim segment or table cell.@@ -1672,6 +1712,8 @@ class GroundingLedger:masked = _LOCALIZED_DATE_RE.sub(" ", masked)masked = _DATE_RE.sub(" ", masked)masked = _AGGREGATE_AMOUNT_RE.sub(" ", masked)+ masked = _LABELLED_SCORE_RE.sub(" ", masked)+ masked = _INDICATOR_VALUE_RE.sub(" ", masked)without_dates = _QUANTITY_WITH_UNIT_RE.sub(" ", masked)values: list[float] = []for match in _NUMBER_RE.finditer(without_dates):diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.pyindex 67316e0e9..0d84abd01 100644--- a/agent/tests/test_agent_grounding.py+++ b/agent/tests/test_agent_grounding.py@@ -879,6 +879,53 @@ def test_price_validation_still_rejects_a_quote_outside_observed_range(assert [issue["code"] for issue in result.issues] == ["numeric_claim_conflict"]+def test_price_validation_ignores_score_indicator_and_window_digits(+ tmp_path: Path,+) -> None:+ """Confidence scores, indicator readings, and lookback windows are not prices (#1001).++ A well-formed verdict line carries a conviction score, the moving-average+ windows it cites, and an oscillator reading. Compared against an OHLC range+ every one of them contradicts it, which rejected drafts that were correct.+ """+ ledger = _screened_ledger(tmp_path)++ for draft in (+ # The reported shape: a labelled score on a 1-10 scale.+ "000543.SZ VERDICT: FLAT CONFIDENCE: 6 REASON: 收盘价 8.20 CNY(source: tencent)",+ "000543.SZ CONFIDENCE: 6/10,收盘价 8.20 CNY(source: tencent)",+ # The hyphenated English compound the quantity mask used to stall on.+ "000543.SZ 现价 8.20 CNY 距 52-week high 有距离(source: tencent)",+ "000543.SZ 收盘价 8.20 CNY 低于其 20/50/200-day 均线(source: tencent)",+ # An indicator reading sharing a clause with a genuine quote.+ "000543.SZ 现价 8.20 CNY 而 RSI 46.7(source: tencent)",+ ):+ result = ledger.validate_final_answer(draft)+ assert result.valid is True, (draft, result.issues)+++def test_masked_window_does_not_shield_a_wrong_quote_in_the_same_clause(+ tmp_path: Path,+) -> None:+ """The new masks are span-local: a bad quote beside one is still caught (#1001).++ This is the lower side of the guard. Masking ``52-week`` must remove only+ the window length; a contradicted price in the same clause has to survive+ the mask and reject the draft, or the fix would have bought precision by+ silencing the check it exists to run.+ """+ ledger = _screened_ledger(tmp_path)++ for draft in (+ "000543.SZ 52-week high 之下,收盘价 42.00 CNY(source: tencent)",+ "000543.SZ CONFIDENCE: 6 REASON: 收盘价 42.00 CNY(source: tencent)",+ "000543.SZ RSI 46.7,收盘价 42.00 CNY(source: tencent)",+ ):+ result = ledger.validate_final_answer(draft)+ assert result.valid is False, draft+ assert [issue["code"] for issue in result.issues] == ["numeric_claim_conflict"], draft++def test_screening_run_reaches_a_final_answer_through_the_agent_loop(tmp_path: Path,) -> None: