Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): mask line references, count nouns, and since-references

William Liao55015a7d10ff
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.py
index c8b6d6c5d..8e4a2f4f3 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")",
@@ -291,6 +291,28 @@ _REFERENCE_LEVEL_RE = re.compile(
r"\s*" + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?",
re.IGNORECASE,
)
+# A date-anchored reference puts the historical extreme after the number:
+# "7/10(150.57)以来最高" and "highest since 7/10 (150.57)". The parenthesized
+# value is the earlier high/low, not a current quote, and was rejected against
+# the session's observed window (which does not reach back to July).
+_SINCE_REFERENCE_RE = re.compile(
+ r"[-+]?\d[\d,]*(?:\.\d+)?\s*\)?\s*以来(?:最高|最低|高|低)(?:点|位)?"
+ # Dates are masked before this runs, so "since 7/10 (150.57)" has lost its
+ # date digits by the time the connector is matched.
+ r"|(?:highest|lowest)\s+since[^0-9\n]{0,16}"
+ r"[-+]?\d[\d,]*(?:\.\d+)?",
+ re.IGNORECASE,
+)
+# A validation report cites a plan file by line number — "~line 206",
+# "第 206 行" — and the number is a document location, not a price. Before
+# this mask, "**文档 ~line 206「…」不成立" contributed 206.0 as a candidate
+# price claim and was rejected against the observed OHLC range.
+_LINE_REFERENCE_RE = re.compile(
+ r"(?:~\s*)?\blines?\b\s*[:#]?\s*\d{1,5}(?:\s*[-–—至~]\s*\d{1,5})?"
+ r"|第\s*\d{1,5}(?:\s*[-–—至~]\s*\d{1,5})?\s*行"
+ r"|行\s*[::]?\s*\d{1,5}(?:\s*[-–—至~]\s*\d{1,5})?",
+ 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
@@ -2196,6 +2218,8 @@ class GroundingLedger:
masked = _INDICATOR_VALUE_RE.sub(" ", masked)
masked = _PROSPECTIVE_LEVEL_RE.sub(" ", masked)
masked = _REFERENCE_LEVEL_RE.sub(" ", masked)
+ masked = _SINCE_REFERENCE_RE.sub(" ", masked)
+ masked = _LINE_REFERENCE_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 f53e98781..dcfb53836 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -1345,6 +1345,78 @@ def test_reference_levels_are_not_read_as_observed_price_claims(
assert GroundingLedger._numbers_without_dates_or_percent(segment) == expected
+# A validation report cites a plan file by line number ("~line 206",
+# "第 206 行"). The number is a document location, not a price, and was
+# compared against observed OHLC as a claim before this mask existed.
+_LINE_REFERENCE_SEGMENTS = [
+ "**文档 ~line 206「8/12 高 149.60 为 6/16 ATH 225.64 以来最高」不成立",
+ "line 206",
+ "lines 206-208",
+ "第 206 行",
+ "第 206–208 行",
+ "行 206",
+]
+
+
+ "segment", _LINE_REFERENCE_SEGMENTS, ids=range(len(_LINE_REFERENCE_SEGMENTS))
+)
+def test_line_number_references_are_not_read_as_price_claims(segment: str) -> None:
+ """A line citation is a document location, not an observed price."""
+ if segment.startswith("**文档"):
+ # The quoted 149.60 beside the line citation stays checked.
+ assert GroundingLedger._numbers_without_dates_or_percent(segment) == [149.6]
+ else:
+ assert GroundingLedger._numbers_without_dates_or_percent(segment) == []
+
+
+# Validation summaries count findings: "1 项事实错误", "2 项不一致", and
+# "16 个交易日" are count nouns, not prices. The count survived extraction
+# and was rejected against the observed OHLC range before 项/个交易日 joined
+# the quantity units.
+_COUNT_NOUN_SEGMENTS = [
+ ("- ❌ **1 项事实错误**:", []),
+ ("⚠️ **2 项不一致**", []),
+ ("3 项", []),
+ ("16 个交易日", []),
+ # 149.60 is masked here too: "高点高于 149.60" is a comparison level.
+ ("6/17–7/10 有 16 个交易日高点高于 149.60", []),
+]
+
+
+ "segment,expected",
+ _COUNT_NOUN_SEGMENTS,
+ ids=[c[0][:22] for c in _COUNT_NOUN_SEGMENTS],
+)
+def test_count_nouns_are_not_read_as_price_claims(
+ segment: str, expected: list[float],
+) -> None:
+ """项/个交易日 counts are quantities, not prices."""
+ assert GroundingLedger._numbers_without_dates_or_percent(segment) == expected
+
+
+_SINCE_REFERENCE_SEGMENTS = [
+ ("正确为 7/10(150.57)以来最高", []),
+ ("收 146.15 为 7/9(收 152.16)以来最高", [146.15]),
+ ("highest since 7/10 (150.57)", []),
+ # The current bar's own high is not a since-reference and stays checked.
+ ("8/12 高 149.60 为 6/16 ATH 225.64 以来最高", [149.6]),
+]
+
+
+ "segment,expected",
+ _SINCE_REFERENCE_SEGMENTS,
+ ids=[c[0][:24] for c in _SINCE_REFERENCE_SEGMENTS],
+)
+def test_since_references_are_not_read_as_observed_price_claims(
+ segment: str, expected: list[float],
+) -> None:
+ """A date-anchored "highest since" value is a reference, not a 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)
@@ -1460,6 +1532,21 @@ def test_reference_level_and_currency_thresholds_pass_end_to_end(tmp_path: Path)
assert result.valid is True, result.issues
+def test_validation_summary_with_counts_and_line_cites_passes_end_to_end(
+ tmp_path: Path,
+) -> None:
+ """A validation verdict naming counts and line citations passes the gate."""
+ ledger = _spcx_us_ledger(tmp_path)
+
+ result = ledger.validate_final_answer(
+ "SPCX.US 核验(USD, source: yahoo):❌ 1 项事实错误 — "
+ "文档 ~line 206「8/12 高 149.60 为 6/16 ATH 以来最高」不成立,"
+ "6/17–7/10 有 16 个交易日高点高于 149.60,正确为 7/10(150.57)以来最高。"
+ )
+
+ assert result.valid is True, result.issues
+
+
def _shanghai_shortlist() -> str:
"""One Shanghai listing as the two sources actually publish it."""
return _resolver_payload(