"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): exempt externally attributed figures from the analysis gate (#1336)
cgycorey37a20272ffa9
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.pyindex e48b98cb5..7375ae7fd 100644--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@ -252,6 +252,28 @@ _ANALYSIS_METRIC_RE = re.compile(r"夏普|回撤|波动率|胜率|命中率|概率|年化|回测|窗口|收益(?:率)?|回报(?:率)?)",re.IGNORECASE,)+# Phrases that indicate a figure is attributed to an external source rather+# than model memory (paper restatement, #1338 review). Attributing a number+# to a named source is the opposite of an unsourced claim. The subject list+# is deliberately restricted to sources that cannot be this run's own output+# (papers, studies, analysts, filings): "the backtest reports" / "the data+# shows" is exactly how a model dresses up its own numbers (#1336), so+# data/backtest/strategy/report subjects do NOT count.+_ATTRIBUTION_RE = re.compile(+ r"(?:"+ r"\b(?:the\s+(?:papers?|studies?|researchers?|analysts?|surveys?"+ r"|regulators?|authorities|literature|authors?)"+ r"|analysts?|researchers?|literature|sec\s+filings?\b|filings?"+ r"|annual\s+filings?|quarterly\s+filings?|rating\s+agencies?)"+ r"\s+(?:reports?|estimates?|shows?|indicates?|suggests?|states?|claims?"+ r"|notes?|cites?|mentions?|reveals?|discloses?|publishes?|found"+ r"|calculated|computed|derived)\b"+ r"|"+ r"(?:论文|文献|研究|分析师|研究机构|学者)"+ r"[^。,\n]{0,6}?(?:报告|显示|表明|指出|称|估计|发现)"+ r")",+ re.IGNORECASE,+)# Measurement-shaped numbers for analysis claims: keeps the % sign and sign# (unlike _numbers_without_dates_or_percent, which drops percentages on# purpose). A bare integer is NOT a measurement — "252 个交易日年化" is the@@ -2624,6 +2646,13 @@ class GroundingLedger:)if not unknown or not self._numbers_without_dates_or_percent(segment):continue+ # Accept figures that are attributed to an external source+ # (e.g., "The paper reports a Sharpe ratio of 1.8.") rather+ # than model memory. Scoped to the clause: a line-level check+ # would let a citation in one clause launder an invented+ # sibling metric in the next.+ if _ATTRIBUTION_RE.search(segment):+ continuefor symbol in unknown:reported.add(symbol)issues.append(@@ -2736,6 +2765,12 @@ class GroundingLedger:continueif _DEFINITION_FRAME_RE.search(segment):continue+ # Attributed figures ("The paper reports a Sharpe of 1.8",+ # or the marker in a neighbouring clause: "据研究显示,策略+ # 年化收益 18.2%") are citations, not invented measurements+ # — skip the gate.+ if _ATTRIBUTION_RE.search(segment):+ continuevalues = self._measure_numbers(segment)if not values:continuediff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.pyindex 5bc1551fd..909fbc7e2 100644--- a/agent/tests/test_agent_grounding.py+++ b/agent/tests/test_agent_grounding.py@@ -3290,3 +3290,79 @@ def test_compound_metric_leaf_kind_resolution() -> None:assert _metric_kind_for_path("data.hit_rate_daily") == "win_rate"assert _metric_kind_for_path("data.risk_free_rate") is Noneassert _metric_kind_for_path("data.trade_count") is None+++def test_analysis_gates_accept_attributed_paper_restatement(tmp_path: Path) -> None:+ """An attributed figure is a citation, not an invented measurement (#1338+ review): 'The paper reports a Sharpe ratio of 1.8' must pass with zero tool+ results, in both languages, while unattributed phrasing stays blocked."""+ for answer in (+ "The paper reports a Sharpe ratio of 1.8 for the momentum factor.",+ "该论文报告其策略夏普比率为 1.8。",+ "研究显示该策略年化收益 18.2%。",+ "Analysts estimate an annualized volatility of 22%.",+ ):+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Research the factor.")+ issues = ledger.validate_final_answer(answer).issues+ assert not [i for i in issues if i.get("code") == "analysis_claim_unavailable"], (+ answer,+ issues,+ )+++def test_analysis_gate_still_rejects_unattributed_and_unsourced(+ tmp_path: Path,+) -> None:+ """The attribution exemption must not launder model-memory figures: the+ review's pinned reject pair and an invented metric with an ordinary prose+ subject keep failing."""+ for answer in (+ "TSLA.US last traded at 412.35 USD.",+ "特斯拉现价 412.35 美元。",+ "The strategy reports a Sharpe ratio of 1.8.",+ ):+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Analyze something.")+ ledger.ingest_tool_result(+ tool_name="get_market_data",+ arguments={"codes": ["AAPL.US"]},+ result=json.dumps(+ {+ "AAPL.US": [+ {+ "trade_date": "2026-09-02T00:00:00",+ "close": 112.4,+ }+ ]+ }+ ),+ call_id="md-1",+ success=True,+ )+ issues = ledger.validate_final_answer(answer).issues+ assert issues, answer+ assert not any(+ issue.get("code") == "analysis_claim_unavailable" and "1.8" not in str(issue)+ for issue in issues+ ), answer+++def test_attribution_exemption_cannot_launders_self_claims(tmp_path: Path) -> None:+ """The #1336 attack shape must not escape via attribution vocabulary:+ "the backtest/data shows" is a self-claim, and a citation in one clause+ must not exempt an invented sibling metric in the next."""+ for answer in (+ "The backtest data shows an annualized return of 25%.",+ "回测数据显示策略年化收益 25%。",+ "数据显示策略夏普比率为 3.5。",+ "根据本次回测,年化波动率 18.2%。",+ "The strategy reports a Sharpe ratio of 1.8.",+ # citation in clause 1, invented sibling in clause 2+ "The paper reports a Sharpe ratio of 1.8, and our strategy achieved "+ "an annualized return of 47.3%.",+ ):+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Research the factor.")+ issues = ledger.validate_final_answer(answer).issues+ assert [i for i in issues if i.get("code") == "analysis_claim_unavailable"], (+ answer,+ issues,+ )