Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): accept horizontal spacing before magnitude units

tangwj469582d119cf09343
diff --git a/agent/src/agent/grounding/figures.py b/agent/src/agent/grounding/figures.py
index 8b634edb7..c6ad4ad58 100644
--- a/agent/src/agent/grounding/figures.py
+++ b/agent/src/agent/grounding/figures.py
@@ -1062,7 +1062,7 @@ def _currency_code_for_figure(text: str, start: int, end: int) -> str | None:
def magnitude_suffix(text: str, end: int) -> tuple[float, int]:
- """The magnitude mark glued to the right of a figure, if any.
+ """The magnitude mark following a figure, allowing horizontal spacing.
``K`` / ``M`` / ``B`` count only when no further letter follows, so "5MB"
and "3Mn" are not millions.
@@ -1074,13 +1074,16 @@ def magnitude_suffix(text: str, end: int) -> tuple[float, int]:
Returns:
``(multiplier, end past the mark)``, or ``(1.0, end)`` without one.
"""
- mark = text[end : end + 1]
+ start = end
+ while start < len(text) and text[start] in " \t\u00a0\u202f\u3000":
+ start += 1
+ mark = text[start : start + 1]
if not mark or mark not in _MAGNITUDES:
return 1.0, end
- after = text[end + 1 : end + 2]
+ after = text[start + 1 : start + 2]
if mark.isascii() and after.isascii() and after.isalpha():
return 1.0, end
- return _MAGNITUDES[mark], end + 1
+ return _MAGNITUDES[mark], start + 1
def _currency_prefix_start(text: str, start: int) -> int:
diff --git a/agent/tests/test_grounding_spaced_magnitude_units.py b/agent/tests/test_grounding_spaced_magnitude_units.py
new file mode 100644
index 000000000..11c498174
--- /dev/null
+++ b/agent/tests/test_grounding_spaced_magnitude_units.py
@@ -0,0 +1,26 @@
+"""Horizontal spacing must not change the meaning of a monetary amount."""
+import json
+
+import pytest
+
+from src.agent.grounding import GroundingLedger
+from src.agent.grounding.figures import magnitude_suffix
+
+
[email protected]("space", [" ", "\t", "\u00a0", "\u202f", "\u3000"])
+def test_spaced_money_matches_the_same_observation(tmp_path, space):
+ ledger = GroundingLedger(run_dir=tmp_path, user_message="Report revenue")
+ ledger.ingest_tool_result(tool_name="financial_data", arguments={}, call_id="c1", success=True,
+ result=json.dumps({"revenue": {"amount": 1_234_000_000}}))
+ answer = f"营业收入为12.34{space}亿元。\n```figures\n12.34亿元 | observed | 营业收入 | c1::revenue.amount\n```"
+ result = ledger.validate_final_answer(answer)
+ assert result.valid, result.issues
+ wrong = ledger.validate_final_answer(answer.replace("12.34", "13.34"))
+ assert not wrong.valid
+
+
[email protected]("text,expected", [(" 亿", (1e8, 2)), (" M", (1e6, 2)),
+ (" MB", (1.0, 0)), (" March", (1.0, 0)),
+ ("\n亿", (1.0, 0)), (" ", (1.0, 0))])
+def test_spacing_preserves_word_and_line_boundaries(text, expected):
+ assert magnitude_suffix(text, 0) == expected