Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): read an order line as an instruction, not an observed quote

Haozhe Wuf00e7a5adb1d
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.py
index 3f7dc89bd..0eff61f23 100644
--- a/agent/src/agent/grounding.py
+++ b/agent/src/agent/grounding.py
@@ -305,6 +305,32 @@ _INDICATOR_VALUE_RE = re.compile(
# it, the number survives masking and is compared against observed OHLC as a
# price claim even though it is a prospective level, not an observed quote.
_CURRENCY_TOKEN = r"(?:\$|US\$|C\$|HK\$|CAD|USD|CNY|HKD|¥|¥)?"
+
+# An order line is an instruction, not an observation. "100 @ $3.50" states
+# where a limit sits and "100" is a share count that was never a price at all,
+# yet both went to the OHLC check and rejected a weekly update whose quotes
+# were correct. This is the same category as the target/stop levels below -- a
+# level the report proposes, not one the data source reported.
+_ORDER_LEVEL_RE = re.compile(
+ r"(?:"
+ # (a) "<qty> [股|shares] @ <price>" -- the whole clause, quantity included
+ r"\d[\d,]*(?:\.\d+)?\s*(?:股|shares?)?\s*@\s*"
+ + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?"
+ r"|"
+ # (b) a bare "@ <price>" with no quantity in front
+ r"@\s*" + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?"
+ r"|"
+ # (c) an order label introducing the level
+ # 买入价/卖出价 are deliberately absent: in running prose they name the
+ # price a report OBSERVED entering at, not an instruction, and masking
+ # them stopped a genuinely ungrounded quote from being rejected.
+ r"(?:挂单|限价单|限价|委托价?|订单"
+ r"|limit\s+(?:order|price)|\bGTC\b|\bGTD\b|\bIOC\b|\bFOK\b)"
+ r"\s*(?:为|是|at|@|=)?\s*[::]?\s*"
+ + _CURRENCY_TOKEN + r"\s*[-+]?\d[\d,]*(?:\.\d+)?"
+ r")" + _RANGE_TAIL,
+ re.IGNORECASE,
+)
# A historical reference names a price the instrument once traded at — an
# all-time high, a 52-week extreme — and the answer is not claiming it as
# today's observed quote. "8/12 高 149.60 为 6/16 ATH 225.64 以来最高" was
@@ -2366,6 +2392,7 @@ class GroundingLedger:
masked = _SHORT_DATE_RE.sub(" ", masked)
masked = _DASH_DATE_RE.sub(" ", masked)
masked = _PERCENT_RANGE_RE.sub(" ", masked)
+ masked = _ORDER_LEVEL_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 21f6fe3ef..cf4de497a 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -2204,3 +2204,38 @@ def test_a_level_stated_as_a_range_masks_both_bounds(tmp_path: Path) -> None:
"000543.SZ 收盘价 8.20 CNY,目标价 10-20 元(source: tencent)"
)
assert result.valid is True, result.issues
+
+
+def test_an_order_line_is_an_instruction_not_an_observation(tmp_path: Path) -> None:
+ """"100 @ $3.50" states where a limit sits; 100 was never a price at all.
+
+ A GTC summary in a weekly update was read as two observed quotes and
+ rejected against the session's range, which is the same category error the
+ target/stop masks already prevent.
+ """
+ extract = GroundingLedger._numbers_without_dates_or_percent
+
+ for draft in (
+ "GTC 100 @ $3.50",
+ "buy 100 @ $3.50 GTC",
+ "挂单 100 股 @ 4.00",
+ "限价单 100 @ 3.50",
+ "委托价 3.50",
+ ):
+ assert extract(draft) == [], draft
+
+ # 买入价/卖出价 stay OUT of the label set: in running prose they name the
+ # price a report says it observed, and masking them let an ungrounded
+ # quote through the gate.
+ assert extract("买入价 0.881") == [0.881]
+
+ # An observed quote standing beside an order line is still checked, and a
+ # bare handle carries no price to mask.
+ assert extract("现价 8.20 CNY,挂单 100 @ 8.50") == [8.2]
+ assert extract("联系 @user 获取") == []
+
+ ledger = _screened_ledger(tmp_path)
+ result = ledger.validate_final_answer(
+ "000543.SZ 收盘价 8.20 CNY,挂单 100 股 @ 12.00(source: tencent)"
+ )
+ assert result.valid is True, result.issues