Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): mask year-less dates and percentage ranges

Haozhe Wu97ae38fff4bc
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.py
index b2d986057..1bbd8c2cb 100644
--- a/agent/src/agent/grounding.py
+++ b/agent/src/agent/grounding.py
@@ -170,6 +170,19 @@ _NUMBER_RE = re.compile(
r"(?![A-Za-z0-9_])"
)
_DATE_RE = re.compile(r"\b(?:19|20)\d{2}[-/]\d{1,2}[-/]\d{1,2}\b")
+# A year-less "8/5" is how a trading day is written in running prose, and it
+# contributed 8 and 5 as candidate prices (#983). The month and day ranges are
+# bounded, and both sides are fenced off from a longer slash run, so the window
+# enumeration "20/50/200-day" cannot be mistaken for a date.
+_SHORT_DATE_RE = re.compile(
+ r"(?<![\d/])(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[12]\d|3[01])(?![\d/])"
+)
+# A percentage range masks only its upper bound through the "%" tail check
+# below, because the sign touches the second number: "1–2%" left 1 behind
+# (#983). Mask the span as a whole.
+_PERCENT_RANGE_RE = re.compile(
+ r"\d[\d,]*(?:\.\d+)?\s*[-–—~至]\s*\d[\d,]*(?:\.\d+)?\s*[%%]"
+)
# 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(
@@ -1711,6 +1724,8 @@ class GroundingLedger:
masked = _CANONICAL_SYMBOL_RE.sub(" ", text)
masked = _LOCALIZED_DATE_RE.sub(" ", masked)
masked = _DATE_RE.sub(" ", masked)
+ masked = _SHORT_DATE_RE.sub(" ", masked)
+ masked = _PERCENT_RANGE_RE.sub(" ", masked)
masked = _AGGREGATE_AMOUNT_RE.sub(" ", masked)
masked = _LABELLED_SCORE_RE.sub(" ", masked)
masked = _INDICATOR_VALUE_RE.sub(" ", masked)
diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.py
index 0d84abd01..aa0eb2f9c 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -904,6 +904,41 @@ def test_price_validation_ignores_score_indicator_and_window_digits(
assert result.valid is True, (draft, result.issues)
+def test_price_validation_ignores_short_dates_and_percent_ranges(
+ tmp_path: Path,
+) -> None:
+ """A year-less date and a percentage range are not prices (#983).
+
+ Taken from the trace attached to #983: "8/5 收盘 5.97" contributed 8 and 5,
+ and "1–2%" contributed its lower bound, because the percent tail check only
+ masks the number the sign touches.
+ """
+ ledger = _screened_ledger(tmp_path)
+
+ for draft in (
+ "000543.SZ 8/5 收盘价 8.20 CNY(source: tencent)",
+ "000543.SZ 现价 8.20 CNY,距阻力位仅 1–2%(source: tencent)",
+ "000543.SZ 现价 8.20 CNY,距阻力位仅 1-2%(source: tencent)",
+ ):
+ result = ledger.validate_final_answer(draft)
+ assert result.valid is True, (draft, result.issues)
+
+
+def test_short_date_mask_does_not_swallow_a_plain_ratio(tmp_path: Path) -> None:
+ """The month/day mask is bounded, so an ordinary ratio still reads (#983).
+
+ "P/E 15" and the window enumeration "20/50/200-day" both contain slashes.
+ Neither may be consumed as a date, or the mask would hide real figures.
+ """
+ ledger = _screened_ledger(tmp_path)
+
+ result = ledger.validate_final_answer("000543.SZ 收盘价 42.00 CNY,P/E 15(source: tencent)")
+
+ assert result.valid is False
+ assert [issue["code"] for issue in result.issues] == ["numeric_claim_conflict"]
+ assert [issue["value"] for issue in result.issues] == [42.0]
+
+
def test_masked_window_does_not_shield_a_wrong_quote_in_the_same_clause(
tmp_path: Path,
) -> None: