Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): match a year-less table date against its evidence

Haozhe Wu5bb07d53aaac
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.py
index 1bbd8c2cb..3a4db9529 100644
--- a/agent/src/agent/grounding.py
+++ b/agent/src/agent/grounding.py
@@ -309,6 +309,53 @@ def _is_number(value: Any) -> bool:
)
+# "." is deliberately not a separator: a decimal price such as 8.5 would parse
+# as month 8 day 5 and match a real trading day.
+_YEARLESS_CLAIM_DATE_RE = re.compile(
+ r"^(0?[1-9]|1[0-2])\s*[-/月]\s*(0?[1-9]|[12]\d|3[01])\s*[日号]?$"
+)
+_ISO_TIMESTAMP_RE = re.compile(r"^(\d{4})-(\d{1,2})-(\d{1,2})")
+
+
+def _timestamp_matches_claim_date(timestamp: str, date_value: str) -> bool:
+ """Match an evidence timestamp against the date cell of a claim.
+
+ The comparison used to be ``timestamp.startswith(date_value)``, which can
+ only succeed when the answer repeats the year. A table whose date column
+ reads ``08-05`` — the ordinary way a report writes a trading day — matched
+ nothing, so every cell in the row was reported as having no supporting
+ evidence while that evidence sat right there (#983: 79 such rejections in
+ one run, every value inside the observed range).
+
+ A year-less date is matched on month and day. That is deliberately looser:
+ where the evidence spans more than one year, such a claim matches the same
+ calendar day in either. Matching the wrong year is a smaller failure than
+ matching nothing, but it is a real one, so the caller still compares the
+ value against every record that matched rather than trusting the date.
+
+ Args:
+ timestamp: Evidence timestamp, normally ISO ``YYYY-MM-DD``.
+ date_value: Date cell as written in the answer.
+
+ Returns:
+ True when the timestamp denotes the day the claim names.
+ """
+ stamp = (timestamp or "").strip()
+ claim = (date_value or "").strip()
+ if not stamp or not claim:
+ return False
+ if stamp.startswith(claim):
+ return True
+ yearless = _YEARLESS_CLAIM_DATE_RE.match(claim)
+ iso = _ISO_TIMESTAMP_RE.match(stamp)
+ if not yearless or not iso:
+ return False
+ return (int(iso.group(2)), int(iso.group(3))) == (
+ int(yearless.group(1)),
+ int(yearless.group(2)),
+ )
+
+
def _price_field_for_path(path: str) -> str | None:
"""Map a generic evidence JSON path to a canonical price field.
@@ -1637,7 +1684,8 @@ class GroundingLedger:
candidates = [
record
for record in candidates
- if record.timestamp and record.timestamp.startswith(date_value)
+ if record.timestamp
+ and _timestamp_matches_claim_date(record.timestamp, date_value)
]
if not candidates:
return {
diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.py
index aa0eb2f9c..c8307d562 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -993,3 +993,61 @@ def test_screening_run_reaches_a_final_answer_through_the_agent_loop(
"000543.SZ 买入价 8.20 CNY(100 股成本 820 CNY;source: tencent)"
)
assert validation.valid is True, validation.issues
+
+
+def test_price_table_with_a_year_less_date_matches_its_evidence(tmp_path: Path) -> None:
+ """A table dated ``08-01`` must find the evidence stamped ``2026-08-01`` (#983).
+
+ The date filter was ``timestamp.startswith(date_value)``, which can only
+ succeed when the answer repeats the year. A report writing the trading day
+ the ordinary way matched nothing, so every cell in the row came back as
+ having no supporting evidence — 79 such rejections in the trace attached to
+ #983, every value sitting inside the observed range.
+ """
+ ledger = _screened_ledger(tmp_path)
+
+ for date_cell in ("08-01", "8/1", "8月1日", "2026-08-01"):
+ draft = (
+ "000543.SZ 行情(source: tencent; currency: CNY)\n\n"
+ "| 日期 | 开盘 | 最高 | 最低 | 收盘 |\n"
+ "| --- | --- | --- | --- | --- |\n"
+ f"| {date_cell} | 7.90 | 8.50 | 7.90 | 8.20 |\n"
+ )
+ result = ledger.validate_final_answer(draft)
+ assert result.valid is True, (date_cell, result.issues)
+
+
+def test_year_less_date_still_rejects_a_wrong_quote(tmp_path: Path) -> None:
+ """Matching the day must not stop the value from being checked (#983).
+
+ Resolving the date is what lets the comparison happen at all; it must not
+ become a way to pass without one.
+ """
+ ledger = _screened_ledger(tmp_path)
+
+ draft = (
+ "000543.SZ 行情(source: tencent; currency: CNY)\n\n"
+ "| 日期 | 开盘 | 最高 | 最低 | 收盘 |\n"
+ "| --- | --- | --- | --- | --- |\n"
+ "| 08-01 | 7.90 | 8.50 | 7.90 | 42.00 |\n"
+ )
+ result = ledger.validate_final_answer(draft)
+
+ assert result.valid is False
+ assert "numeric_claim_conflict" in {issue["code"] for issue in result.issues}
+
+
+def test_a_date_that_names_a_different_day_is_still_unavailable(tmp_path: Path) -> None:
+ """Loosening the year must not collapse distinct trading days together."""
+ ledger = _screened_ledger(tmp_path)
+
+ draft = (
+ "000543.SZ 行情(source: tencent; currency: CNY)\n\n"
+ "| 日期 | 开盘 | 最高 | 最低 | 收盘 |\n"
+ "| --- | --- | --- | --- | --- |\n"
+ "| 07-15 | 7.90 | 8.50 | 7.90 | 8.20 |\n"
+ )
+ result = ledger.validate_final_answer(draft)
+
+ assert result.valid is False
+ assert "numeric_claim_unavailable" in {issue["code"] for issue in result.issues}