Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): list exact call refs when a field ref uses an unknown call id

zeus22907fa25becbed
diff --git a/agent/src/agent/context.py b/agent/src/agent/context.py
index c53fa1544..fc0730b38 100644
--- a/agent/src/agent/context.py
+++ b/agent/src/agent/context.py
@@ -197,7 +197,9 @@ Decide which workflow to use based on the request:
as `ref`: `data.tail_risk.var_95` or just `var_95` from `portfolio_risk_xray`,
`historical_var` from `quantlib_call`. When more than one call returned the
same field, name the exact call as `call_id::field` (for example
- `q1::historical_var`); a tool name is not a call id.
+ `<call_id>::historical_var`), where `<call_id>` is the tool_call_id of that
+ tool result copied verbatim; never invent a short alias. A tool name is not
+ a call id.
Once this session holds more than one tail-risk measurement (a VaR and an ES,
or 95% and 99%), EVERY tail-risk figure needs that field ref — a call id or no
declaration at all cannot say which of them you are quoting, and the figure is
diff --git a/agent/src/agent/grounding/policies.py b/agent/src/agent/grounding/policies.py
index 4768b60c6..a591324a3 100644
--- a/agent/src/agent/grounding/policies.py
+++ b/agent/src/agent/grounding/policies.py
@@ -91,6 +91,9 @@ _OTHER_CURRENCY_PREFIXES = "港美日欧韩台新加澳"
#: Relative band a value must fall in to count as matching evidence.
_TOLERANCE = 0.005
+#: Most exact refs a correction lists for a ref whose call id names nothing.
+_MAX_FIELD_REF_CANDIDATES = 5
+
#: A plain integer is read as a price only for an instrument quoted in the
#: thousands (600519.SH, an index, BTC). Below that, a prose integer is a window,
#: a horizon or a count ("20 日均线", "200-day") and stays unchecked.
@@ -1099,6 +1102,52 @@ class _PolicyMixin:
}
return sorted(refs)
+ def _unknown_call_field_ref_candidates(
+ self, ref: str, symbol: str | None, figure: Figure
+ ) -> list[str]:
+ """Exact ``call_id::path`` refs for a ``scope::field`` whose scope names nothing.
+
+ A model that writes an alias (``p1::field``) instead of the call id it
+ was given names no call, tool or run of this session, so the ref selects
+ no evidence. This only lists where the field really lives, so the next
+ draft can copy an exact ref; it grants nothing, and the figure stays
+ rejected until it is re-declared with one of them. Refs whose value the
+ figure matches come first; at most :data:`_MAX_FIELD_REF_CANDIDATES` are
+ returned.
+ """
+ key = (ref or "").strip()
+ if "::" not in key:
+ return []
+ scope, field = (part.strip() for part in key.split("::", 1))
+ if not scope or not field or self._names_session_source(scope):
+ return []
+ records, entries = self._field_sources(field, symbol)
+ found: dict[str, list[float]] = {}
+ for record in records:
+ if record.call_id and record.field:
+ label = self._ref_source(record.call_id, record.field, record.scope)[1]
+ found.setdefault(label, []).append(float(record.value))
+ for entry in entries:
+ if entry.get("call_id") and entry.get("field"):
+ label = self._ref_source(str(entry["call_id"]), str(entry["field"]), None)[1]
+ found.setdefault(label, []).append(float(entry["value"]))
+ money = bool(figure.currency and not figure.percent)
+ compatible = {
+ label
+ for label, values in found.items()
+ if self._matches_evidence(figure, values, [] if money else values)
+ }
+ ranked = sorted(found, key=lambda label: (label not in compatible, label))
+ return ranked[:_MAX_FIELD_REF_CANDIDATES]
+
+ def _names_session_source(self, name: str) -> bool:
+ """Whether ``name`` is a call id, tool name or backtest run of this session."""
+ return (
+ any(name in (record.call_id, record.tool) for record in self._evidence)
+ or any(name in (entry.get("call_id"), entry.get("tool")) for entry in self._analysis_metrics)
+ or bool(self._artifact_scope(name))
+ )
+
def _tail_risk_ref_required(
self,
figure: Figure,
@@ -1402,6 +1451,23 @@ class _PolicyMixin:
field_ref_candidates=call_field_candidates,
)
]
+ unknown_scope_candidates = self._unknown_call_field_ref_candidates(
+ declaration.ref, symbol, figure
+ )
+ if unknown_scope_candidates:
+ return [
+ self._figure_issue(
+ "numeric_claim_conflict",
+ figure,
+ "observed",
+ symbol,
+ "unknown_call_id",
+ f"is declared observed from {declaration.ref}, whose left side "
+ "is not a call id, tool or run of this session",
+ source_tool_call_ids=[declaration.ref],
+ field_ref_candidates=unknown_scope_candidates,
+ )
+ ]
values = [float(record.value) for record in scoped_records] + metric_values
money = figure.currency and not figure.percent
# A call- or tool-scoped ref pools every field that call returned,
diff --git a/agent/src/agent/grounding/release.py b/agent/src/agent/grounding/release.py
index 9d6ec64ce..f0f499ab9 100644
--- a/agent/src/agent/grounding/release.py
+++ b/agent/src/agent/grounding/release.py
@@ -76,6 +76,7 @@ _CORRECTION_REASONS = {
"ambiguous_field_ref": "{ref} names {sources}, which hold different values; use the one quoted as the ref",
"tail_risk_needs_field_ref": "this session holds {sources}, which are different measurements; declare the figure with an exact field ref",
"field_ref_needs_call_id": "{ref} uses a tool name before ::; use one exact call_id::field ref from {sources}",
+ "unknown_call_id": "{ref} names no call, tool or run of this session; copy a real tool_call_id, not an alias",
"no_formula": "its note states no arithmetic",
"formula_not_evaluable": "its note is not an arithmetic expression over two or more operands",
"formula_not_anchored": "no operand of its note is a value this session observed",
diff --git a/agent/tests/test_grounding_unknown_call_id_refs.py b/agent/tests/test_grounding_unknown_call_id_refs.py
new file mode 100644
index 000000000..7e88fba88
--- /dev/null
+++ b/agent/tests/test_grounding_unknown_call_id_refs.py
@@ -0,0 +1,112 @@
+"""An alias before ``::`` names no call: the correction lists the real refs, grants nothing."""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+
+from src.agent.context import _SYSTEM_PROMPT
+from src.agent.grounding import GroundingLedger
+
+pytestmark = pytest.mark.unit
+
+PATH = "data.summary.total_return"
+
+
+def _ledger(tmp_path: Path, values: dict[str, float]) -> GroundingLedger:
+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Report the total return")
+ for call_id, value in values.items():
+ ledger.ingest_tool_result(
+ tool_name="report_tool",
+ arguments={"id": call_id},
+ result=json.dumps({"data": {"summary": {"total_return": value}}}),
+ call_id=call_id,
+ success=True,
+ )
+ return ledger
+
+
+def _check(ledger: GroundingLedger, ref: str, written: str = "12.5"):
+ return ledger.validate_final_answer(
+ f"Total return {written}.\n```figures\n{written} | observed | total return | {ref}\n```"
+ )
+
+
+def test_alias_stays_rejected_and_lists_the_real_ref(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, {"call_9f3a": 12.5})
+
+ result = _check(ledger, f"r1::{PATH}")
+
+ assert result.valid is False
+ issue = result.issues[0]
+ assert issue["reason"] == "unknown_call_id"
+ assert issue["field_ref_candidates"] == [f"call_9f3a::{PATH}"]
+ correction = ledger.correction_prompt(result)
+ assert f"call_9f3a::{PATH}" in correction
+ assert "not an alias" in correction
+ # The suggestion is advisory: only the exact ref is accepted.
+ assert _check(ledger, f"call_9f3a::{PATH}").valid is True
+
+
+def test_alias_with_a_shortened_path_lists_the_full_path(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, {"call_9f3a": 12.5})
+
+ result = _check(ledger, "r1::summary.total_return")
+
+ assert result.valid is False
+ assert result.issues[0]["field_ref_candidates"] == [f"call_9f3a::{PATH}"]
+
+
+def test_candidates_matching_the_value_come_first(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, {"a_call": 3.0, "b_call": 12.5, "c_call": 7.0})
+
+ result = _check(ledger, f"r1::{PATH}")
+
+ assert result.valid is False
+ candidates = result.issues[0]["field_ref_candidates"]
+ assert candidates[0] == f"b_call::{PATH}"
+ assert set(candidates) == {f"{call}::{PATH}" for call in ("a_call", "b_call", "c_call")}
+
+
+def test_candidates_are_capped(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, {f"call_{index}": float(index) for index in range(9)})
+
+ result = _check(ledger, f"r1::{PATH}", "12.5")
+
+ assert result.valid is False
+ assert len(result.issues[0]["field_ref_candidates"]) == 5
+
+
+def test_alias_for_a_field_no_call_returned_lists_nothing(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, {"call_9f3a": 12.5})
+
+ result = _check(ledger, "r1::data.summary.missing_field")
+
+ assert result.valid is False
+ assert result.issues[0]["reason"] == "not_in_referenced_call"
+ assert not result.issues[0].get("field_ref_candidates")
+
+
+def test_a_real_call_without_the_field_is_not_treated_as_an_alias(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, {"call_9f3a": 12.5})
+
+ result = _check(ledger, "call_9f3a::data.summary.missing_field")
+
+ assert result.valid is False
+ assert result.issues[0]["reason"] != "unknown_call_id"
+
+
+def test_a_bare_unknown_ref_does_not_get_candidates(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, {"call_9f3a": 12.5})
+
+ result = _check(ledger, "r1")
+
+ assert not any(issue.get("reason") == "unknown_call_id" for issue in result.issues)
+
+
+def test_prompt_example_is_a_placeholder_not_a_call_id_shape() -> None:
+ assert "q1::" not in _SYSTEM_PROMPT
+ assert "<call_id>::historical_var" in _SYSTEM_PROMPT
+ assert "never invent a short alias" in _SYSTEM_PROMPT