Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): stop reading a trading plan's levels as observed price claims

Haozhe Wu4aabf89c7000
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.py
index 3a4db9529..cf22d42da 100644
--- a/agent/src/agent/grounding.py
+++ b/agent/src/agent/grounding.py
@@ -242,6 +242,44 @@ _INDICATOR_VALUE_RE = re.compile(
r"[-+]?\d[\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
+# about what the instrument traded at. Compared against observed OHLC evidence
+# they were reported first as numeric_claim_unavailable (before the run fetched
+# prices) and then as numeric_claim_conflict (after it did) — the same false
+# positive under two codes.
+#
+# This is a real relaxation, so every branch is span-local and anchored to the
+# token that makes the number prospective, never to a word elsewhere in the
+# clause: "现价 5.97,目标位 6.45" masks 6.45 and still checks 5.97. An
+# assertion carries no such token and stays checked.
+#
+# Branch (d) accepts a conditional opener. A number inside "若收盘 5.36" is a
+# hypothesis, and a hypothesis does not misrepresent observed data the way a
+# bare quote does — but it is the widest branch here, so it requires the opener
+# to PRECEDE the number with no digits in between, which keeps it from reaching
+# back over an assertion that was already made.
+_PROSPECTIVE_LEVEL_RE = re.compile(
+ r"(?:"
+ # (a) comparison operator immediately before the number
+ r"(?:>=|<=|≥|≤|>|<|大于|小于|不低于|不高于|高于|低于)\s*[-+]?\d[\d,]*(?:\.\d+)?"
+ r"|"
+ # (b) a level marker introducing the number
+ r"(?:目标位|目标区|目标价|止损位?|止盈位?|触发价|触发位|触发点|上看|下看|"
+ r"target\s+(?:price|level|zone)|trigger|stop[-\s]?loss|take[-\s]?profit)"
+ r"\s*(?:为|是|至|到|on|at|of|=)?\s*[::]?\s*[-+]?\d[\d,]*(?:\.\d+)?"
+ r"|"
+ # (c) the number followed by a level marker
+ r"[-+]?\d[\d,]*(?:\.\d+)?\s*(?:一线|附近)?\s*(?:成为?|作为|是)?\s*"
+ r"(?:目标区|目标位|止损位|止盈位)"
+ r"|"
+ # (d) a conditional opener before the number, digits fencing the reach
+ r"(?:若|如果|一旦|倘若|假如|\bif\b|\bwhen\b|\bshould\b)[^0-9\n]{0,12}"
+ r"[-+]?\d[\d,]*(?:\.\d+)?"
+ r")",
+ re.IGNORECASE,
+)
# Full-width brackets and enumeration commas delimit prose clauses. ASCII
# parentheses are deliberately not separators: an explicit derivation such as
# "(8.5 - 7.9) / 2" must stay in one segment for the formula check.
@@ -1777,6 +1815,7 @@ class GroundingLedger:
masked = _AGGREGATE_AMOUNT_RE.sub(" ", masked)
masked = _LABELLED_SCORE_RE.sub(" ", masked)
masked = _INDICATOR_VALUE_RE.sub(" ", masked)
+ masked = _PROSPECTIVE_LEVEL_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 c8307d562..01c39d60d 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -7,6 +7,8 @@ from pathlib import Path
from types import SimpleNamespace
from typing import Any, Callable
+import pytest
+
from src.agent.context import ContextBuilder
from src.agent.grounding import GroundingLedger
from src.agent.loop import AgentLoop, _is_tool_success
@@ -1051,3 +1053,80 @@ def test_a_date_that_names_a_different_day_is_still_unavailable(tmp_path: Path)
assert result.valid is False
assert "numeric_claim_unavailable" in {issue["code"] for issue in result.issues}
+
+
+# ---------------------------------------------------------------------------
+# #983: a trading plan quotes levels it does not claim to have observed.
+#
+# The committee report attached to that issue was refused for its own entry
+# triggers and target zones. With no price evidence yet in the parent session
+# they came back as numeric_claim_unavailable; once the run fetched prices the
+# same numbers came back as numeric_claim_conflict. One false positive, two
+# codes.
+# ---------------------------------------------------------------------------
+
+_PLAN_LEVELS_FROM_983 = [
+ "- **转多信号:** 收盘 ≥6.45 且量 ≥35M手",
+ "- **转空强化:** 收盘 <5.36 且 3 日不收复 → 年线 4.63 成目标区",
+ "| B 破位空 | 任意收盘 <5.36 | 收盘 ≥5.75 且量 ≥20M手 |",
+ "目标位 6.80,止损 5.20",
+ "若收盘 5.36 则减仓",
+ "target price 6.80 with stop-loss 5.20",
+]
+
+
[email protected]("segment", _PLAN_LEVELS_FROM_983, ids=range(len(_PLAN_LEVELS_FROM_983)))
+def test_plan_levels_are_not_read_as_observed_price_claims(segment: str) -> None:
+ """A trigger, a target and a hypothesis assert nothing about observed data."""
+ assert GroundingLedger._numbers_without_dates_or_percent(segment) == []
+
+
+# The other side of the same guard. Every entry here is an assertion about what
+# the instrument did, and each must survive the mask above — otherwise the
+# relaxation is a way to state a price without being checked.
+_ASSERTIONS_THAT_MUST_STAY_CHECKED = [
+ ("8/5 收盘 5.97", [5.97]),
+ ("现价 5.97", [5.97]),
+ ("收盘价为 6.03", [6.03]),
+ ("the close was 6.03", [6.03]),
+ # Span-local: the target is masked, the quote beside it is not.
+ ("现价 5.97,目标位 6.45", [5.97]),
+ # A conditional opener must not reach back over a quote already made.
+ ("收盘 6.03,若跌破 5.36 减仓", [6.03]),
+ ("开盘 6.80 最高 7.18 最低 6.68", [6.80, 7.18, 6.68]),
+]
+
+
+ "segment,expected",
+ _ASSERTIONS_THAT_MUST_STAY_CHECKED,
+ ids=[c[0][:24] for c in _ASSERTIONS_THAT_MUST_STAY_CHECKED],
+)
+def test_plan_level_mask_leaves_observed_quotes_checked(
+ segment: str, expected: list[float],
+) -> None:
+ 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)
+
+ result = ledger.validate_final_answer(
+ "000543.SZ 收盘价 42.00 CNY,目标位 45.00(source: tencent)"
+ )
+
+ assert result.valid is False
+ assert "numeric_claim_conflict" in {issue["code"] for issue in result.issues}
+
+
+def test_plan_level_alone_reaches_a_valid_answer(tmp_path: Path) -> None:
+ """A plan stated without quoting an observed price passes the numeric gate."""
+ ledger = _screened_ledger(tmp_path)
+
+ result = ledger.validate_final_answer(
+ "000543.SZ 收盘价 8.20 CNY(source: tencent)。"
+ "转多信号:收盘 ≥9.10;转空强化:收盘 <7.40 且 3 日不收复 → 目标位 6.90。"
+ )
+
+ assert result.valid is True, result.issues