Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): a real historical_var / parametric_var result grounds the figure it returned (#1464)

Haozhe Wu554dcee71f6e
diff --git a/agent/src/agent/grounding/evidence.py b/agent/src/agent/grounding/evidence.py
index 5c6fe8157..12cce633c 100644
--- a/agent/src/agent/grounding/evidence.py
+++ b/agent/src/agent/grounding/evidence.py
@@ -122,6 +122,11 @@ _ANALYSIS_KIND_ALIASES = {
"var": "tail_risk",
"var_95": "tail_risk",
"var_99": "tail_risk",
+ # quantlib_call records a scalar result under the function name, and "var"
+ # alone matches only a whole leaf (_EXACT_ONLY_ALIASES), so these two need
+ # their own entries or a real VaR result grounds nothing (#1464).
+ "historical_var": "tail_risk",
+ "parametric_var": "tail_risk",
"cvar": "tail_risk",
"es": "tail_risk",
"expected_shortfall": "tail_risk",
diff --git a/agent/tests/test_grounding_role_hardening.py b/agent/tests/test_grounding_role_hardening.py
index f1e44e3e0..2fb6fe8bd 100644
--- a/agent/tests/test_grounding_role_hardening.py
+++ b/agent/tests/test_grounding_role_hardening.py
@@ -671,6 +671,30 @@ def test_a_referenced_table_cell_is_checked_against_its_own_column(tmp_path: Pat
assert close.valid is True, close.issues
[email protected]("function", ["historical_var", "parametric_var", "historical_cvar"])
+def test_a_real_quantlib_tail_risk_result_grounds_its_own_figure(tmp_path: Path, function: str) -> None:
+ """#1464: the scalar result of a real ``quantlib_call`` is evidence for the figure it returned."""
+ from src.tools.quantlib_tool import QuantlibCallTool
+
+ returns = [0.01, -0.02, 0.003, -0.0157, 0.004, -0.01, 0.02, -0.005, 0.007, -0.012] * 3
+ arguments = {
+ "action": "call",
+ "module": "risk",
+ "function": function,
+ "kwargs": {"returns": returns, "confidence": 0.95},
+ }
+ raw = QuantlibCallTool().execute(**arguments)
+ quoted = f"{json.loads(raw)['result'] * 100:.2f}%"
+ ledger = _ledger(tmp_path, MARKET_A, ("quantlib_call", arguments, raw, "q1"))
+ confidence = _block(ROW, "95% | count | confidence level")
+
+ returned = ledger.validate_final_answer(HDR + f" 单日 95% 尾部损失为 {quoted}。" + confidence)
+ invented = ledger.validate_final_answer(HDR + " 单日 95% 尾部损失为 9.87%。" + confidence)
+
+ assert returned.valid is True, returned.issues
+ assert invented.valid is False
+
+
@pytest.mark.parametrize(
("leaf", "kind"),
[
@@ -679,8 +703,13 @@ def test_a_referenced_table_cell_is_checked_against_its_own_column(tmp_path: Pat
("strategy_var_95", "tail_risk"),
("cvar", "tail_risk"),
("es", "tail_risk"),
+ ("historical_var", "tail_risk"),
+ ("parametric_var", "tail_risk"),
("var_explained", None),
("sales_es", None),
+ # A variance is not a VaR: only the named quantlib VaR functions are added.
+ ("residual_var", None),
+ ("conditional_var", None),
],
)
def test_short_tail_risk_names_count_only_as_the_whole_leaf(leaf: str, kind: str | None) -> None: