"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): don't read percentage-point deltas as quoted prices
saju01270f1afb6a03
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.pyindex d13969f46..f16670444 100644--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@ -256,6 +256,17 @@ _RANGE_TAIL = r"(?:\s*[-–—~~至到]\s*[-+]?\d[\d,]*(?:\.\d+)?)?"_PERCENT_RANGE_RE = re.compile(r"\d[\d,]*(?:\.\d+)?\s*[-–—~至]\s*\d[\d,]*(?:\.\d+)?\s*[%%]")+# A percentage-POINT delta is not a quoted price. "~3.6pp below Penumbra"+# describes a margin gap; left unmasked the ".6" is consumed as a decimal+# and the number reaches the OHLC comparator, which then rejects an+# otherwise correct fundamentals answer and demands `get_market_data` to+# substantiate a claim that has nothing to do with price.+# The trailing "%" spellings are already handled above; this covers the+# percentage-point spellings, which the percent masks never matched.+_PERCENTAGE_POINT_RE = re.compile(+ r"[-+~≈]?\s*\d[\d,]*(?:\.\d+)?\s*(?:pp|ppt|ppts|bps|bp|百分点)\b",+ re.IGNORECASE,+)# Localized calendar text carries digits that the ISO pattern above leaves# behind: "8 月 3 日" otherwise contributes 8 and 3 as candidate prices._LOCALIZED_DATE_RE = re.compile(@@ -2548,6 +2559,7 @@ class GroundingLedger:masked = _SHORT_DATE_RE.sub(" ", masked)masked = _DASH_DATE_RE.sub(" ", masked)masked = _PERCENT_RANGE_RE.sub(" ", masked)+ masked = _PERCENTAGE_POINT_RE.sub(" ", masked)masked = _ORDER_LEVEL_RE.sub(" ", masked)masked = _AGGREGATE_AMOUNT_RE.sub(" ", masked)masked = _LABELLED_SCORE_RE.sub(" ", masked)diff --git a/agent/tests/test_grounding_percentage_points.py b/agent/tests/test_grounding_percentage_points.pynew file mode 100644index 000000000..adfded551--- /dev/null+++ b/agent/tests/test_grounding_percentage_points.py@@ -0,0 +1,67 @@+"""Percentage-point deltas must not be read as quoted prices.++Regression test for the false positive described in HKUDS/Vibe-Trading#1341:+`_numbers_without_dates_or_percent` masked "%" but not the percentage-point+spellings, so "~3.6pp below Penumbra" yielded 3.0 (the ".6" consumed as a+decimal). That number reached the OHLC comparator as an unsourced price claim,+which rejected a correct fundamentals answer and demanded `get_market_data` to+substantiate a statement that had nothing to do with price.++Both directions are pinned here: percentage-point deltas are masked, and a+genuine price is still extracted.+"""++import pytest++from src.agent.grounding import GroundingLedger++_extract = GroundingLedger._numbers_without_dates_or_percent+++ "text",+ [+ "gross margin ~3.6pp below Penumbra",+ "gross margin 3.6 pp below Penumbra",+ "operating margin improved 2.4ppt year over year",+ "operating margin improved 12.5 ppts sequentially",+ "spread widened 45bps after the print",+ "spread widened 45 bps after the print",+ "yield moved 7bp on the day",+ "\u6bdb\u5229\u7387\u4e0b\u964d 3.6 \u767e\u5206\u70b9",+ "margin fell -1.8pp sequentially",+ "margin rose +0.9pp sequentially",+ "roughly \u22483.6pp of dilution",+ ],+)+def test_percentage_point_deltas_are_not_prices(text):+ """No percentage-point delta should survive as a candidate price."""+ assert _extract(text) == [], f"leaked a price candidate from: {text!r}"+++ "text,expected",+ [+ ("closing price 48.20", 48.20),+ ("the stock closed at 48.20 on heavy volume", 48.20),+ ("last trade 152.75", 152.75),+ ("opened at 1,204.50 and faded", 1204.50),+ ],+)+def test_genuine_prices_are_still_extracted(text, expected):+ """The mask must not swallow real quoted prices -- that would blind the+ grounding check it exists to feed."""+ assert expected in _extract(text), f"lost a real price from: {text!r}"+++def test_mixed_sentence_keeps_price_drops_percentage_point():+ """A sentence carrying both must yield only the price."""+ values = _extract("closed at 48.20, with gross margin ~3.6pp below peers")+ assert 48.20 in values+ assert 3.0 not in values+ assert 3.6 not in values+++def test_bare_number_still_extracted():+ """Guard against the mask being over-broad: a plain number is untouched."""+ assert 48.20 in _extract("48.20")