"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): preserve explicitly sourced document amounts and units
Haozhe Wud37b2e7f1183
diff --git a/agent/src/agent/grounding/figures.py b/agent/src/agent/grounding/figures.pyindex 15536b3e2..1bb259423 100644--- a/agent/src/agent/grounding/figures.py+++ b/agent/src/agent/grounding/figures.py@@ -97,7 +97,7 @@ _CURRENCY_CODES = frozenset(_PERCENT_CHARS = "%%"# Glued percentage-point and basis-point marks, and the percent each one is.-_POINT_MARKS = (("bps", 0.01), ("pp", 1.0), ("bp", 0.01))+_POINT_MARKS = (("个百分点", 1.0), ("bps", 0.01), ("pp", 1.0), ("bp", 0.01))# Magnitude marks glued to a figure ("24.6M", "2.4万"): a symbol set like the# currency marks. They scale the comparison with evidence and never decide shape.diff --git a/agent/src/agent/grounding/policies.py b/agent/src/agent/grounding/policies.pyindex 6d6129490..a2ad289cb 100644--- a/agent/src/agent/grounding/policies.py+++ b/agent/src/agent/grounding/policies.py@@ -170,7 +170,7 @@ def _explicit_sign(text: str) -> int:def _is_percent_field(name: str) -> bool:"""Whether a field name says its value is already in percentage points."""lowered = name.lower()- return "percent" in lowered or "pct" in lowered or lowered.endswith("%")+ return name == "document.ratio" or "percent" in lowered or "pct" in lowered or lowered.endswith("%")def _is_plain_count(figure: Figure) -> bool:@@ -881,9 +881,19 @@ class _PolicyMixin:recordfor record in recordsif not _is_price_kind(record) and not _is_metadata_count_leaf(record.field)+ and (record.source != "document" or record.field == "document.ratio")]elif figure is not None and figure.currency:- records = [record for record in records if _is_price_kind(record)]+ # Documents often put the unit in a table header. An explicitly+ # referenced numeric cell can be restated with that unit; it is+ # still matched to this exact source and written precision. Do not+ # promote it to a price globally or admit ratios as amounts. Known+ # currency identity is checked by _check_observed below.+ records = [+ record for record in records+ if _is_price_kind(record)+ or (record.source == "document" and record.field == "document.figure")+ ]metrics = []return records, metrics@@ -1977,10 +1987,22 @@ class _PolicyMixin:anchors.extend(float(record.value)for record in scoped[0]- if (not money or _is_price_kind(record))+ if (+ not money or _is_price_kind(record)+ or (record.source == "document" and record.field == "document.figure")+ )and (currency_code is None or record.currency == currency_code)and not (unattributed and record.symbol))+ # A literal percent in a formula is parsed as its fraction, while+ # document.ratio records retain the percent as printed. This+ # conversion belongs only to the explicitly named document scope.+ anchors.extend(+ float(record.value) / 100.0+ for record in scoped[0]+ if record.source == "document" and record.field == "document.ratio"+ and not money and currency_code is None+ )if not money and not unattributed:anchors.extend(scoped[1])if not anchors:@@ -2118,13 +2140,14 @@ class _PolicyMixin:``(low − high) / high`` or as the drop, unless the prose wrote a sign."""# The normalized reading, so "0,666" is three decimals and "−5,13%" is signed.- half_unit = _written_half_unit(figure.digits or figure.text)+ scale = float(figure.scale or 1.0)+ half_unit = _written_half_unit(figure.digits or figure.text) * scaleif figure.percent:targets = {result} if points else {result * 100.0}else:targets = {result, result * 100.0}sign = _explicit_sign(figure.sign or figure.text)- value = abs(figure.value)+ value = abs(figure.value) * scalereturn any(abs(value - abs(target)) <= max(abs(target) * _TOLERANCE, half_unit, 1e-9)for target in targetsdiff --git a/agent/src/tools/doc_reader_tool.py b/agent/src/tools/doc_reader_tool.pyindex 250f64367..95efa0d89 100644--- a/agent/src/tools/doc_reader_tool.py+++ b/agent/src/tools/doc_reader_tool.py@@ -475,7 +475,10 @@ class DocReaderTool(BaseTool):parameters = {"type": "object","properties": {- "file_path": {"type": "string", "description": "Absolute path to the file."},+ "file_path": {+ "type": "string",+ "description": "Absolute file path or the uploads/<name> handle returned by upload.",+ },"pages": {"type": "string","description": "PDF only: page range (e.g. '1-10', '5', '1,3,5-8'). Ignored for other formats.",@@ -492,8 +495,11 @@ class DocReaderTool(BaseTool):repeatable = Truedef execute(self, **kwargs: Any) -> str:+ file_path = kwargs.get("file_path")+ if not isinstance(file_path, str) or not file_path.strip():+ return _err("file_path is required: pass the uploaded uploads/<name> handle or an absolute file path.")return read_document(- kwargs["file_path"],+ file_path,kwargs.get("pages", ""),min_text_per_page=kwargs.get("min_text_per_page", _MIN_TEXT_PER_PAGE),)diff --git a/agent/tests/test_doc_reader_security.py b/agent/tests/test_doc_reader_security.pyindex 56338f34c..f145d4da4 100644--- a/agent/tests/test_doc_reader_security.py+++ b/agent/tests/test_doc_reader_security.py@@ -6,7 +6,7 @@ import jsonimport pytest-from src.tools.doc_reader_tool import read_document+from src.tools.doc_reader_tool import DocReaderTool, read_documentfrom src.tools.path_utils import safe_document_path, safe_user_path@@ -46,3 +46,22 @@ def test_read_document_allows_configured_import_root(assert result["status"] == "ok"assert result["text"] == "VT_DOC_OK"assert safe_document_path(str(doc)) == doc.resolve()++[email protected]("arguments", [{}, {"file_path": ""}, {"file_path": None}])+def test_missing_document_path_names_the_required_argument(arguments) -> None:+ result = _read_json(DocReaderTool().execute(**arguments))+ assert result["status"] == "error"+ assert "file_path is required" in result["error"]+++def test_uploaded_handle_uses_upload_root_even_when_a_run_dir_is_supplied(tmp_path, monkeypatch) -> None:+ from src.config.paths import get_uploads_dir++ monkeypatch.setenv("VIBE_TRADING_HOME", str(tmp_path))+ doc = get_uploads_dir() / "report.txt"+ doc.parent.mkdir(parents=True, exist_ok=True)+ doc.write_text("UPLOADED_REPORT", encoding="utf-8")+ result = _read_json(DocReaderTool().execute(file_path="uploads/report.txt", run_dir=str(tmp_path / "runs/r1")))+ assert result["status"] == "ok"+ assert result["text"] == "UPLOADED_REPORT"diff --git a/agent/tests/test_grounding_document_figures.py b/agent/tests/test_grounding_document_figures.pyindex f21870ae9..f311d89db 100644--- a/agent/tests/test_grounding_document_figures.py+++ b/agent/tests/test_grounding_document_figures.py@@ -22,6 +22,8 @@ from __future__ import annotationsimport jsonfrom pathlib import Path+import pytest+from src.agent.grounding import GroundingLedgerREPORT = """第一节 经营回顾@@ -165,3 +167,69 @@ def test_an_error_envelope_records_nothing(tmp_path: Path) -> None:)assert not result.valid++[email protected]("ref", ["doc-1", "read_document", "ar2025.pdf", "doc-1::document.figure"])+ ("value", "written"),+ [("763,154,445.01", "763,154,445.01 元"), ("191,586,814.48", "1.92亿元")],+)+def test_document_table_amount_can_be_restated_with_a_unit(+ tmp_path: Path, ref: str, value: str, written: str,+) -> None:+ ledger, document = _ledger_with_upload(tmp_path)+ _read_document(ledger, document, "doc-1", f"单位:元\n| 项目 | 金额 |\n| 营业收入 | {value} |")+ result = ledger.validate_final_answer(+ _declared(f"营业收入为 {written}。", f"{written} | observed | 营业收入 | {ref}")+ )+ assert result.valid, result.issues++[email protected]("written", ["863,154,445.01 元", "763,154,445.01 USD", "763,154,445.01%"])+def test_document_unit_restatement_does_not_invent_value_currency_or_ratio(+ tmp_path: Path, written: str,+) -> None:+ ledger, document = _ledger_with_upload(tmp_path)+ _read_document(ledger, document, "doc-1", "| 营业收入 | 763,154,445.01 |")+ result = ledger.validate_final_answer(+ _declared(f"营业收入为 {written}。", f"{written} | observed | 营业收入 | doc-1")+ )+ assert not result.valid+++def test_document_unit_restatement_keeps_exact_source_binding(tmp_path: Path) -> None:+ ledger, document = _ledger_with_upload(tmp_path)+ _read_document(ledger, document, "doc-1", "| 营业收入 | 763,154,445.01 |")+ _read_document(ledger, document.with_name("other.pdf"), "doc-2", "| 营业收入 | 863,154,445.01 |")+ result = ledger.validate_final_answer(+ _declared("营业收入为 863,154,445.01 元。", "863,154,445.01 元 | observed | 营业收入 | doc-1")+ )+ assert not result.valid++[email protected]("formula", ["39.66% - 38.96%", "39.66 - 38.96"])+def test_document_percentage_points_can_be_declared_as_written(tmp_path: Path, formula: str) -> None:+ ledger, document = _ledger_with_upload(tmp_path)+ _read_document(ledger, document, "doc-1", "毛利率分别为 39.66% 和 38.96%。")+ result = ledger.validate_final_answer(+ _declared("毛利率上升 0.70个百分点。", f"0.70个百分点 | derived | {formula} | doc-1")+ )+ assert result.valid, result.issues+++def test_document_percentage_point_derivation_still_needs_both_operands(tmp_path: Path) -> None:+ ledger, document = _ledger_with_upload(tmp_path)+ _read_document(ledger, document, "doc-1", "毛利率为 39.66%。")+ result = ledger.validate_final_answer(+ _declared("毛利率上升 0.70个百分点。", "0.70个百分点 | derived | 39.66% - 38.96% | doc-1")+ )+ assert not result.valid+++def test_money_can_be_derived_from_explicitly_referenced_document_cells(tmp_path: Path) -> None:+ ledger, document = _ledger_with_upload(tmp_path)+ _read_document(ledger, document, "doc-1", "| 本期 | 上期 |\n| 763,154,445.01 | 663,154,445.01 |")+ result = ledger.validate_final_answer(+ _declared("营业收入增加 1.00亿元。", "1.00亿元 | derived | 763154445.01 - 663154445.01 | doc-1")+ )+ assert result.valid, result.issues