Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): mask numbered headings, ratios, and forex rates

William Liao0dee55057b4e
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.py
index 8e4a2f4f3..be5b1c431 100644
--- a/agent/src/agent/grounding.py
+++ b/agent/src/agent/grounding.py
@@ -235,7 +235,7 @@ _QUANTITY_WITH_UNIT_RE = re.compile(
r"(?:\s*[-–—~至]\s*\d[\d,]*(?:\.\d+)?)?"
r"\s*[-–—]?\s*"
r"(?:"
- r"(?:股|手|张|份|口|笔|倍|个月|周|天|日|年|次|个交易日|项)"
+ r"(?:股|手|张|份|口|笔|倍|个月|周|天|日|年|次|个交易日|项|行)"
r"|(?:shares?|contracts?|lots?|units?|sessions?|bars?|periods?|"
r"wks?|weeks?|months?|days?|years?|yrs?)\b"
r")",
@@ -313,6 +313,19 @@ _LINE_REFERENCE_RE = re.compile(
r"|行\s*[::]?\s*\d{1,5}(?:\s*[-–—至~]\s*\d{1,5})?",
re.IGNORECASE,
)
+# A numbered markdown heading ("### 6. 关键价位") names a section index, not
+# a price. The ordered-list mask only covers line-leading "1." and stops at
+# the "### " prefix, so the section number was extracted as a claim.
+_NUMBERED_HEADING_RE = re.compile(r"(?m)^\s*#{1,6}\s*\d+(?:[.、.])?\s*")
+# A ratio ("6:1 折算", "10:1") names a conversion basis, not a quote price.
+_RATIO_RE = re.compile(r"\d+(?:\.\d+)?\s*[::]\s*\d+(?:\.\d+)?")
+# A forex pair and its rate ("USD/CAD≈1.36") names a currency conversion, not
+# an instrument quote. The plain 汇率/“exchange rate” form is the same class.
+_FX_RATE_RE = re.compile(
+ r"[A-Z]{3}\s*/\s*[A-Z]{3}\s*(?:≈|~|=|约|为)?\s*\d+(?:\.\d+)?"
+ r"|(?:汇率|FX\s*rate|exchange\s+rate)\s*(?:≈|~|=|为|是)?\s*\d+(?:\.\d+)?",
+ re.IGNORECASE,
+)
# A trading plan quotes levels it does not claim to have observed. In the
# committee report attached to #983, "收盘 ≥6.45 且量 ≥35M手" is an entry
# trigger, "年线 4.63 成目标区" is a target zone, and neither asserts anything
@@ -2220,6 +2233,9 @@ class GroundingLedger:
masked = _REFERENCE_LEVEL_RE.sub(" ", masked)
masked = _SINCE_REFERENCE_RE.sub(" ", masked)
masked = _LINE_REFERENCE_RE.sub(" ", masked)
+ masked = _NUMBERED_HEADING_RE.sub(" ", masked)
+ masked = _RATIO_RE.sub(" ", masked)
+ masked = _FX_RATE_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.py
index dcfb53836..c7ea600e6 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -1417,6 +1417,49 @@ def test_since_references_are_not_read_as_observed_price_claims(
assert GroundingLedger._numbers_without_dates_or_percent(segment) == expected
+_NUMBERED_HEADING_SEGMENTS = [
+ "### 6. 关键价位/旁证核验",
+ "## 12. 结论",
+ "# 3. 身份与数据源",
+ "### 8/13 周中判定",
+ # The section number masks; the observed quote beside it stays checked.
+ ("### 6. 8/12 高 149.60 为 6/16 ATH 以来最高", [149.6]),
+]
+
+
+ "segment", _NUMBERED_HEADING_SEGMENTS, ids=range(len(_NUMBERED_HEADING_SEGMENTS))
+)
+def test_numbered_headings_are_not_read_as_price_claims(segment: str) -> None:
+ """A markdown heading number is a section index, not a price."""
+ if isinstance(segment, tuple):
+ segment, expected = segment
+ assert GroundingLedger._numbers_without_dates_or_percent(segment) == expected
+ else:
+ assert GroundingLedger._numbers_without_dates_or_percent(segment) == []
+
+
+_RATIO_AND_FX_SEGMENTS = [
+ ("TO 报价实际是按 6:1 平价锚定的", []),
+ ("25/25 行 OHLCV、25/25 项派生百分比、7/7 日 CDR 6:1 折算", []),
+ ("远小于当前 USD/CAD≈1.36 的量级", []),
+ ("usd/cad=1.36", []),
+ ("汇率 1.36", []),
+]
+
+
+ "segment,expected",
+ _RATIO_AND_FX_SEGMENTS,
+ ids=[c[0][:24] for c in _RATIO_AND_FX_SEGMENTS],
+)
+def test_ratios_and_fx_rates_are_not_read_as_price_claims(
+ segment: str, expected: list[float],
+) -> None:
+ """A conversion ratio or forex rate is not an instrument quote."""
+ assert GroundingLedger._numbers_without_dates_or_percent(segment) == expected
+
+
def test_plan_level_mask_does_not_shield_a_wrong_quote_end_to_end(tmp_path: Path) -> None:
"""The end-to-end gate still rejects a fabricated quote beside a plan level."""
ledger = _screened_ledger(tmp_path)