Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): keep the attribution exemption out of the price gate, and make the derived-return exemption structural

Haozhe Wub6e4045bf0f4
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.py
index 6d51dbad8..b408e9254 100644
--- a/agent/src/agent/grounding.py
+++ b/agent/src/agent/grounding.py
@@ -369,10 +369,6 @@ _DERIVATION_RE = re.compile(
# arithmetic on sourced inputs, not an invented backtest metric (#1338
# review). The frame alone never grounds anything — the values must also
# match an observed endpoint pair exactly (see _return_derived_from_observed).
-_GROWTH_FRAME_RE = re.compile(
- r"从[^,。;;\n]{0,60}?(?:到|至)|\bfrom\b[^,;。\n]{0,80}?\bto\b",
- re.IGNORECASE,
-)
_NUMBER_RE = re.compile(
r"(?<![A-Za-z0-9_])[-+]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?"
r"(?![A-Za-z0-9_])"
@@ -2795,8 +2791,11 @@ class GroundingLedger:
line, price_records, line_symbol
):
continue
- if _GROWTH_FRAME_RE.search(line) and self._return_derived_from_observed(
- unsupported, price_records, line_symbol
+ operands = self._observed_operands_in_line(
+ line, price_records, line_symbol
+ )
+ if len(operands) >= 2 and self._return_derived_from_observed(
+ unsupported, price_records, line_symbol, operands=operands
):
continue
issues.append(
@@ -2890,11 +2889,48 @@ class GroundingLedger:
return any(close(candidate, item) for candidate in candidates for item in observed)
+ def _observed_operands_in_line(
+ self,
+ line: str,
+ records: Sequence[EvidenceRecord],
+ symbol: str | None,
+ ) -> list[float]:
+ """Observed values that literally appear as numbers in this clause.
+
+ This is the structural half of the derivation exemption. Keying it on
+ a growth PHRASE ("从…到" / "from…to") made the gate stricter for every
+ wording the list happened to miss, which is the same per-language
+ drift that ``test_grounding_language_parity`` exists to stop: the
+ Chinese "第一日收盘 100.0 美元,第二日收盘 112.4 美元,收益率 12.4%"
+ states the identical derivation and was rejected. Requiring the
+ operands themselves to be present and sourced is language-independent
+ and strictly narrower than a phrase list, because a bare
+ "cumulative return of 12.4%" carries no operands at all.
+ """
+ candidates = [record for record in records if record.value is not None]
+ if symbol:
+ candidates = [record for record in candidates if record.symbol == symbol]
+ observed = {float(record.value) for record in candidates}
+ if not observed:
+ return []
+ present: set[float] = set()
+ for raw in self._numbers_without_dates_or_percent(line):
+ try:
+ value = float(raw)
+ except (TypeError, ValueError):
+ continue
+ for candidate in observed:
+ if abs(value - candidate) <= max(abs(candidate) * 1e-9, 1e-9):
+ present.add(candidate)
+ return sorted(present)
+
def _return_derived_from_observed(
self,
claimed: Sequence[str],
records: Sequence[EvidenceRecord],
symbol: str | None,
+ *,
+ operands: Sequence[float] | None = None,
) -> bool:
"""True when a return figure equals growth between observed endpoints.
@@ -2904,10 +2940,15 @@ class GroundingLedger:
figure; the caller must already have verified the from/to frame, so a
bare unanchored return claim never reaches here.
"""
- candidates = [record for record in records if record.value is not None]
- if symbol:
- candidates = [record for record in candidates if record.symbol == symbol]
- observed = sorted({float(record.value) for record in candidates})
+ if operands is not None:
+ observed = sorted(set(operands))
+ else:
+ candidates = [record for record in records if record.value is not None]
+ if symbol:
+ candidates = [
+ record for record in candidates if record.symbol == symbol
+ ]
+ observed = sorted({float(record.value) for record in candidates})
if len(observed) < 2:
return False
values: list[float] = []
@@ -2968,16 +3009,13 @@ class GroundingLedger:
)
if self._is_explicit_derivation(segment, records, symbol):
continue
- # An attributed figure ("The paper reports TSLA.US traded at
- # 412.35") is a citation, not an invented price — skip the
- # price gate too, for the same reason as the analysis and
- # unsourced-symbol gates. Without this, an attributed price
- # is rejected or accepted purely on whether its verb happens
- # to match _PRICE_CONTEXT_RE ("reports" does, "estimate"
- # doesn't), which is the exact vocabulary asymmetry under
- # review.
- if _ATTRIBUTION_RE.search(segment):
- continue
+ # NO attribution exemption here, deliberately. A paper's
+ # Sharpe is a figure this run could never have observed, so
+ # citing it is legitimate; a PRICE is exactly what this run
+ # does observe, so "analysts say TSLA.US last traded at
+ # 412.35" is the laundering shape this gate exists to catch —
+ # adding a citation subject must not buy a fabricated quote a
+ # way through. The exemption stays in the analysis gate only.
for value in values:
issue = self._compare_price_claim(
value=value,
diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.py
index 805fe845d..2538573b9 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -3371,3 +3371,74 @@ def test_attribution_exemption_cannot_launders_self_claims(tmp_path: Path) -> No
answer,
issues,
)
+
+
+def test_attribution_never_exempts_a_price_claim(tmp_path: Path) -> None:
+ """A citation subject must not launder a fabricated quote.
+
+ The attribution exemption is legitimate in the ANALYSIS gate — a paper's
+ Sharpe is a figure this run could never have observed. A price is the
+ opposite: it is exactly what this run observes, so attributing it to a
+ source is the laundering shape the price gate exists to catch. With the
+ exemption applied to `_validate_price_claims`, every line below passed
+ with zero tool evidence.
+ """
+ for answer in (
+ "Analysts say TSLA.US last traded at 412.35 USD.",
+ "分析师指出特斯拉现价 412.35 美元。",
+ "The paper reports that AAPL.US closed at 189.20.",
+ "据研究机构报告,AAPL.US 收盘价为 189.20。",
+ "The filing reports the stock closed at 412.35.",
+ ):
+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Quote the price.")
+ issues = ledger.validate_final_answer(answer).issues
+ assert issues, f"attributed price accepted with zero evidence: {answer}"
+
+
+def test_derived_return_exemption_is_structural_not_phrasal(tmp_path: Path) -> None:
+ """The same derivation must get the same verdict in both languages.
+
+ Keying the exemption on a growth PHRASE ("从…到" / "from…to") made the
+ gate stricter for every wording the list missed. The pairs below state the
+ identical arithmetic on the identical observed endpoints; asserting the
+ two verdicts are EQUAL is what stops the next patch moving the breakage to
+ the other language, exactly as test_grounding_language_parity does.
+ """
+
+ def verdict(answer: str) -> bool:
+ ledger = GroundingLedger(run_dir=tmp_path, user_message="AAPL.US 走势")
+ ledger.ingest_tool_result(
+ tool_name="get_market_data",
+ arguments={"codes": ["AAPL.US"]},
+ result=json.dumps(
+ {
+ "AAPL.US": [
+ {"trade_date": "2026-08-03", "close": 100.0},
+ {"trade_date": "2026-09-02", "close": 112.4},
+ ]
+ }
+ ),
+ call_id="quote",
+ success=True,
+ )
+ return bool(ledger.validate_final_answer(answer).issues)
+
+ # Accepted in both: operands present in the clause and sourced.
+ for english, chinese in (
+ (
+ "AAPL.US rose from 100.0 to 112.4, a cumulative return of 12.4%.",
+ "AAPL.US 第一日收盘 100.0 美元,第二日收盘 112.4 美元,收益率 12.4%。",
+ ),
+ ):
+ en, zh = verdict(english), verdict(chinese)
+ assert en == zh, f"verdicts disagree by language: EN={en} ZH={zh}"
+ assert en is False, f"sourced derivation rejected: {english}"
+
+ # Still rejected in both: no operands in the clause, or wrong arithmetic.
+ for answer in (
+ "AAPL.US delivered a cumulative return of 12.4% over the window.",
+ "AAPL.US 区间收益率为 12.4%。",
+ "AAPL.US rose from 100.0 to 112.4, a cumulative return of 15.0%.",
+ "AAPL.US 从 100.0 涨到 112.4,区间收益率为 15.0%。",
+ ):
+ assert verdict(answer) is True, f"unanchored/wrong return accepted: {answer}"