"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): ground period valuations, grouped thousands, horizon labels and derived percent points
zeus229edae2b8f692d
diff --git a/agent/src/agent/grounding/evidence.py b/agent/src/agent/grounding/evidence.pyindex 586d4c2b7..fe11c2920 100644--- a/agent/src/agent/grounding/evidence.py+++ b/agent/src/agent/grounding/evidence.py@@ -182,7 +182,8 @@ _METADATA_COUNT_TAILS = frozenset()# Money-denominated fields a currency-marked figure may quote besides a price:-# a market-data row's amount, and the backtest engine's equity and P&L leaves.+# a market-data row's amount, the backtest engine's equity and P&L leaves, and the+# opening and closing valuation of a reported period._AMOUNT_FIELDS = frozenset({"amount",@@ -195,6 +196,8 @@ _AMOUNT_FIELDS = frozenset("total_pnl","avg_pnl","notional",+ "value_start",+ "value_end",})@@ -1258,7 +1261,12 @@ class _EvidenceMixin:unit = unit.strip().upper() if isinstance(unit, str) else Nonemetric_currency = {"RMB": "CNY", "CNH": "CNY"}.get(unit, unit) if unit in _CURRENCY_CODES else None- def visit(value: Any, path: str, timestamp: str | None = None) -> None:+ def visit(+ value: Any,+ path: str,+ timestamp: str | None = None,+ declared_currency: str | None = None,+ ) -> None:nonlocal remainingif remaining <= 0:return@@ -1278,7 +1286,12 @@ class _EvidenceMixin:if tool_name == "get_sec_filings"and path.startswith("data.metric.points[")and path.endswith("].val")- else _infer_currency(symbol or "")+ else (+ declared_currency+ if declared_currency+ and _leaf_name(path) in _AMOUNT_FIELDS+ else _infer_currency(symbol or "")+ )),venue=_infer_venue(symbol or ""),)@@ -1286,6 +1299,15 @@ class _EvidenceMixin:remaining -= 1returnif isinstance(value, dict):+ # An object that names its own ISO currency ("currency": "USD")+ # denominates the amount leaves beside it; nothing else inherits it.+ sibling = value.get("currency")+ sibling = sibling.strip().upper() if isinstance(sibling, str) else None+ local_currency = (+ {"RMB": "CNY", "CNH": "CNY"}.get(sibling, sibling)+ if sibling in _CURRENCY_CODES+ else declared_currency+ )local_timestamp = next((str(value[key])@@ -1303,10 +1325,11 @@ class _EvidenceMixin:item,f"{path}.{key}" if path else str(key),local_timestamp,+ local_currency,)elif isinstance(value, list):for index, item in enumerate(value):- visit(item, f"{path}[{index}]", timestamp)+ visit(item, f"{path}[{index}]", timestamp, declared_currency)visit(payload, "")diff --git a/agent/src/agent/grounding/figures.py b/agent/src/agent/grounding/figures.pyindex 4ba4fe2d6..15536b3e2 100644--- a/agent/src/agent/grounding/figures.py+++ b/agent/src/agent/grounding/figures.py@@ -73,6 +73,11 @@ _ORDINAL_RE = re.compile(r"(?m)^[^\S\n]*(?:#{1,6}[^\S\n]*)?\d{1,3}[.)、][^\S\n]_INDEX_CELL_RE = re.compile(r"\d{1,3}[.)]?")_CELL_WORD_RE = re.compile(r"[^\W\d_]{2,}")+# A compact year-horizon label ("1Y", "10y") in a table cell names a column or row, not+# a measurement. Deliberately narrow: a one-letter "M" stays a magnitude suffix ("5M"),+# and multi-letter labels ("12mo", "1yr") already read as worded cells.+_YEAR_HORIZON_CELL_RE = re.compile(r"\d{1,3}[Yy]")+# SHAPE 4 — a CommonMark fence line: at most three spaces of indent, a run of# backticks or tildes, and the info string._FENCE_RE = re.compile(r"(?m)^ {0,3}(`{3,}|~{3,})(.*)$")@@ -399,9 +404,11 @@ _DOTTED_DECIMAL_RE = re.compile(r"(?<![\d.,])[+-]?[1-9]\d{0,2}(?:\.\d{3})+,\d+(?!\d|\.\d)")-# A dot-only grouped integer ("5.165", "502.408", "1.234.567") is ambiguous on its-# own because the same spelling can be a decimal. It is grouping only after the-# surrounding document has independently established decimal-comma notation.+# A dot-only grouped integer with a single group ("5.165", "502.408") is ambiguous on+# its own because the same spelling can be a decimal. It is grouping only after the+# surrounding document has independently established decimal-comma notation. Two or+# more groups ("1.234.567") cannot be a decimal in any notation, so they are grouping+# regardless of the document._DOTTED_GROUPING_RE = re.compile(r"(?<![\d.,])[+-]?[1-9]\d{0,2}(?:\.\d{3})+(?![\d.,])")@@ -449,7 +456,8 @@ def _numbers(text: str, *, decimal_commas: bool | None = None) -> list[_Token]:True,)for match in _DOTTED_GROUPING_RE.finditer(text)- ] if comma_decimals else []+ if comma_decimals or match.group(0).count(".") >= 2+ ]protected = dotted + dotted_groupedtokens: list[_Token] = []cursor = 0@@ -1184,7 +1192,10 @@ def scan_figures(content: str, block: FiguresBlock) -> list[Figure]:worded = (row is not Noneand position not in row.columns- and _CELL_WORD_RE.search(row.cells[position][0]) is not None+ and (+ _CELL_WORD_RE.search(row.cells[position][0]) is not None+ or _YEAR_HORIZON_CELL_RE.fullmatch(row.cells[position][0].strip()) is not None+ ))if _within((start, digits_end), hard) or (cell is not None and cell[:2] in index_cells):shape = "exempt"diff --git a/agent/src/agent/grounding/policies.py b/agent/src/agent/grounding/policies.pyindex 01a0b9924..6d6129490 100644--- a/agent/src/agent/grounding/policies.py+++ b/agent/src/agent/grounding/policies.py@@ -2011,11 +2011,12 @@ class _PolicyMixin:# The result's unit follows the evidence the operands anchored to:# percent-named fields already carry points (#1820), an explicit# trailing *100 was normalized above, anything else is a fraction.- points = (- percent- and self._operands_read_as_points(- operands, symbol, records, scalar_parameters- )+ points = percent and self._operands_read_as_points(+ operands,+ symbol,+ records,+ scalar_parameters,+ referenced=scoped[0] if scoped is not None else (),)if points:# Convert the inputs before evaluating: two point-valued inputs@@ -2041,6 +2042,7 @@ class _PolicyMixin:symbol: str | None,records: Sequence[EvidenceRecord],scalar_parameters: Sequence[float],+ referenced: Sequence[EvidenceRecord] = (),) -> bool:"""Whether every data operand anchored only to percent-named fields.@@ -2049,7 +2051,9 @@ class _PolicyMixin:must meet the result directly, not ``result * 100``. Conservative byconstruction: an operand whose magnitude also appears under anon-percent field makes the unit undecidable, and the caller keeps- the fraction reading.+ the fraction reading. ``referenced`` are the records the declaration's+ own ref resolved to: a field the claim names counts as an anchor of+ the operand's unit even when its tool is not a market-data one."""data = [operand@@ -2078,6 +2082,11 @@ class _PolicyMixin:and _metric_kind_for_path(record.field) is not Noneand (not symbol or not record.symbol or record.symbol == symbol)]+ candidates += [+ record+ for record in referenced+ if record.status == "observed" and record.value is not None+ ]for operand in data:saw_percent = saw_plain = Falsefor record in candidates:diff --git a/agent/tests/test_grounding_unit_and_scanner_gaps.py b/agent/tests/test_grounding_unit_and_scanner_gaps.pynew file mode 100644index 000000000..34223b1cc--- /dev/null+++ b/agent/tests/test_grounding_unit_and_scanner_gaps.py@@ -0,0 +1,261 @@+"""Generic grounding gaps found while grounding real multi-tool reports.++Each case pairs the accepted shape with the near miss that must stay rejected:+a currency-stamped period valuation, thousands grouped by periods, a compact+year-horizon label, and a derived percent difference over percent-named fields.+"""++from __future__ import annotations++import json+from pathlib import Path++import pytest++from src.agent.grounding import GroundingLedger+from src.agent.grounding.figures import parse_figures_block, scan_figures++pytestmark = pytest.mark.unit+++def _ledger(+ tmp_path: Path, payload: dict, tool: str = "account_summary"+) -> GroundingLedger:+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Report the figures.")+ ledger.ingest_tool_result(+ tool_name=tool,+ arguments={},+ result=json.dumps(payload),+ call_id="c1",+ success=True,+ )+ return ledger+++def _answer(prose: str, *lines: str) -> str:+ return prose + "\n\n```figures\n" + "\n".join(lines) + "\n```"+++def _reasons(result) -> list[str]:+ return [issue.get("reason") or issue.get("code") for issue in result.issues]+++# -- period valuation stamped with the currency its own object declares ------------++ACCOUNT = {+ "status": "ok",+ "data": {+ "account": {"value_start": 1250.75, "value_end": 1400.25, "currency": "USD"}+ },+}+++def test_period_valuation_takes_the_currency_its_object_declares(+ tmp_path: Path,+) -> None:+ result = _ledger(tmp_path, ACCOUNT).validate_final_answer(+ _answer(+ "Opened at 1250.75 USD and closed at 1400.25 USD.",+ "1250.75 | observed | opening value | c1::data.account.value_start",+ "1400.25 | observed | closing value | c1::data.account.value_end",+ )+ )++ assert result.valid is True, result.issues+++ ("prose", "line"),+ [+ (+ "Opened at 1250.75 EUR.",+ "1250.75 | observed | opening | c1::data.account.value_start",+ ),+ (+ "Opened at 1251.75 USD.",+ "1251.75 | observed | opening | c1::data.account.value_start",+ ),+ (+ "Closed at 1250.75 USD.",+ "1250.75 | observed | closing | c1::data.account.value_end",+ ),+ ],+)+def test_period_valuation_stays_closed_to_other_currency_value_and_field(+ tmp_path: Path, prose: str, line: str+) -> None:+ result = _ledger(tmp_path, ACCOUNT).validate_final_answer(_answer(prose, line))++ assert result.valid is False+++def test_declared_currency_does_not_leak_to_non_amount_leaves(tmp_path: Path) -> None:+ payload = {+ "status": "ok",+ "data": {"account": {"currency": "USD", "positions": 12}},+ }+ ledger = _ledger(tmp_path, payload)++ assert {record.field: record.currency for record in ledger._evidence} == {+ "data.account.positions": None+ }+++def test_declared_currency_is_scoped_to_its_own_object(tmp_path: Path) -> None:+ payload = {+ "status": "ok",+ "data": {+ "usd": {"value_end": 10.0, "currency": "USD"},+ "other": {"value_end": 20.0},+ },+ }+ ledger = _ledger(tmp_path, payload)++ assert {record.field: record.currency for record in ledger._evidence} == {+ "data.usd.value_end": "USD",+ "data.other.value_end": None,+ }+++# -- thousands grouped by periods ---------------------------------------------------++REPORT = {"status": "ok", "data": {"net_income_millions": 1618596}}+++def test_period_grouped_thousands_read_as_one_figure() -> None:+ text = "Resultado neto: 1.618.596 millones."+ measured = [+ f+ for f in scan_figures(text, parse_figures_block(text))+ if f.shape == "measured"+ ]++ assert [(f.text, f.value) for f in measured] == [("1.618.596", 1618596.0)]+++def test_period_grouped_thousands_ground_against_observed_value(tmp_path: Path) -> None:+ result = _ledger(tmp_path, REPORT).validate_final_answer(+ _answer(+ "Resultado neto: 1.618.596 millones.",+ "1618596 | observed | net income | c1::data.net_income_millions",+ )+ )++ assert result.valid is True, result.issues+++def test_period_grouped_thousands_stay_closed_to_a_wrong_value(tmp_path: Path) -> None:+ result = _ledger(tmp_path, REPORT).validate_final_answer(+ _answer(+ "Resultado neto: 1.628.596 millones.",+ "1628596 | observed | net income | c1::data.net_income_millions",+ )+ )++ assert result.valid is False+++def test_a_single_period_group_keeps_its_decimal_reading() -> None:+ text = "El ratio fue 45.850 veces."+ figures = scan_figures(text, parse_figures_block(text))++ assert [f.value for f in figures] == [45.85]++[email protected]("text", ["1.618.5", "1.61.596", "1.618.5960"])+def test_malformed_period_groups_are_not_thousands(text: str) -> None:+ figures = scan_figures(f"Valor {text}.", parse_figures_block(f"Valor {text}."))++ assert 1618596.0 not in {f.value for f in figures}+ assert 16185960.0 not in {f.value for f in figures}+++# -- compact year-horizon table labels ---------------------------------------------+++def _table_shapes(table: str) -> dict[str, str]:+ return {f.text: f.shape for f in scan_figures(table, parse_figures_block(table))}++[email protected]("label", ["1Y", "5y", "10Y"])+def test_year_horizon_label_is_not_a_measurement(label: str) -> None:+ shapes = _table_shapes(f"| Horizon | Return |\n|---|---|\n| {label} | 0.5 |")++ assert shapes[label.rstrip("Yy")] == "bare"+ assert shapes["0.5"] == "measured"++[email protected]("cell", ["5M", "1.5Y"])+def test_other_suffixed_cells_are_still_measured(cell: str) -> None:+ shapes = _table_shapes(f"| Horizon | Return |\n|---|---|\n| {cell} | 0.5 |")++ assert "measured" in {shape for text, shape in shapes.items() if text != "0.5"}+++def test_year_horizon_label_does_not_exempt_the_return_it_labels(+ tmp_path: Path,+) -> None:+ ledger = _ledger(tmp_path, {"status": "ok", "data": {"r": 0.5}})+ table = "| Horizon | Return |\n|---|---|\n| 1Y | 0.9 |\n"+ result = ledger.validate_final_answer(+ _answer(table, "0.9 | observed | return | c1::data.r")+ )++ assert result.valid is False+++# -- derived percent difference over percent-named fields ---------------------------++RISK = {"data": {"snapshot_id": "s", "top1_pct": 20.36, "top5_pct": 61.56}}+REFS = "c1::data.top5_pct; c1::data.top1_pct"+++def test_derived_percent_over_exact_percent_refs_reads_as_points(+ tmp_path: Path,+) -> None:+ result = _ledger(tmp_path, RISK, tool="concentration_report").validate_final_answer(+ _answer("Spread: 41.2%.", f"41.2% | derived | 61.56 - 20.36 | {REFS}")+ )++ assert result.valid is True, result.issues+++def test_derived_percent_over_exact_percent_refs_rejects_the_100x_reading(+ tmp_path: Path,+) -> None:+ result = _ledger(tmp_path, RISK, tool="concentration_report").validate_final_answer(+ _answer("Spread: 4120%.", f"4120% | derived | 61.56 - 20.36 | {REFS}")+ )++ assert result.valid is False+++def test_derived_percent_rejects_a_wrong_difference(tmp_path: Path) -> None:+ result = _ledger(tmp_path, RISK, tool="concentration_report").validate_final_answer(+ _answer("Spread: 40.2%.", f"40.2% | derived | 61.56 - 20.36 | {REFS}")+ )++ assert result.valid is False+++def test_missing_ref_is_not_substituted_for_the_operand(tmp_path: Path) -> None:+ result = _ledger(tmp_path, RISK, tool="concentration_report").validate_final_answer(+ _answer(+ "Spread: 41.2%.",+ "41.2% | derived | 61.56 - 20.36 | c1::data.top5_pct; c1::data.ghost_pct",+ )+ )++ assert result.valid is False+++def test_plain_ref_fields_keep_the_fraction_reading(tmp_path: Path) -> None:+ payload = {"data": {"top5_weight": 61.56, "top1_weight": 20.36}}+ plain_refs = "c1::data.top5_weight; c1::data.top1_weight"+ ledger = _ledger(tmp_path, payload, tool="concentration_report")++ points = ledger.validate_final_answer(+ _answer("Spread: 41.2%.", f"41.2% | derived | 61.56 - 20.36 | {plain_refs}")+ )++ assert points.valid is False