Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): preserve magnitude units in formula operands

Haozhe Wu8162c77a4ff6
diff --git a/agent/src/agent/grounding/policies.py b/agent/src/agent/grounding/policies.py
index cf4b25bdc..831fb05a7 100644
--- a/agent/src/agent/grounding/policies.py
+++ b/agent/src/agent/grounding/policies.py
@@ -22,6 +22,7 @@ from src.agent.grounding.identity import (
)
from src.agent.grounding import identity_checks # noqa: F401 (registers declared checks)
from src.agent.grounding import figures_checks # noqa: F401 (registers declared checks)
+from src.agent.grounding.figures import _MAGNITUDES
from src.agent.grounding.registry import GROUNDING_CHECKS, grounding_check
from src.agent.grounding.evidence import (
EvidenceRecord,
@@ -312,7 +313,7 @@ def _evaluate_formula(
well-formed expression over at least two numeric operands.
"""
normalized = (
- expression.replace("×", "*")
+ _expand_formula_units(expression).replace("×", "*")
.replace("✕", "*")
.replace("÷", "/")
.replace("−", "-")
@@ -388,6 +389,24 @@ def _evaluate_formula(
#: A number written with a percent sign inside a formula.
_PERCENT_OPERAND_RE = re.compile(r"(\d+(?:\.\d+)?)\s*[%%]")
+# Units are syntax, not evidence: expanded operands still pass the same
+# observed-or-declared checks. Use the prose scanner's supported suffixes.
+_FORMULA_CURRENCY = r"(?:人民币|美元|美金|港元|日元|欧元|英镑|元|¥|¥|\$|€|£|円)"
+_FORMULA_QUANTITY_RE = re.compile(
+ r"(?<![A-Za-z0-9_.])(\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?)\s*"
+ r"(?:([" + re.escape("".join(_MAGNITUDES)) + r"])\s*(?:"
+ + _FORMULA_CURRENCY + r")?|" + _FORMULA_CURRENCY + r")"
+ r"(?![A-Za-z0-9_\u3400-\u9fff])"
+)
+
+
+def _expand_formula_units(text: str) -> str:
+ """Expand supported numeric suffixes before parsing or label removal."""
+ return _FORMULA_QUANTITY_RE.sub(
+ lambda match: repr(float(match[1].replace(",", "")) * _MAGNITUDES.get(match[2], 1.0)),
+ text,
+ )
+
def _is_small_exponent(node: ast.AST) -> bool:
"""Whether a power's exponent is a literal 2 or 3."""
@@ -411,7 +430,7 @@ def _without_labels(text: str) -> str:
("约−1.17pp") is a restatement of the result, not an operand. Nothing is
read from the words themselves.
"""
- return _LABEL_RE.sub(" ", _ANNOTATION_RE.sub(" ", text))
+ return _LABEL_RE.sub(" ", _expand_formula_units(_ANNOTATION_RE.sub(" ", text)))
def _formula_in_note(note: str) -> tuple[float, list[float], ast.Expression] | None:
diff --git a/agent/tests/test_grounding_operand_evidence.py b/agent/tests/test_grounding_operand_evidence.py
index 79a01a2c8..53a0e3fea 100644
--- a/agent/tests/test_grounding_operand_evidence.py
+++ b/agent/tests/test_grounding_operand_evidence.py
@@ -140,3 +140,46 @@ def test_non_percent_claim_does_not_gain_a_free_multiplier(tmp_path):
)
assert not verdict.valid
assert verdict.issues[0]["reason"] == "operand_not_observed"
+
+
[email protected](("formula", "expected"), [
+ ("345,624,709.24元 ÷ 1亿", 3.4562470924),
+ ("2亿元 / 2", 1e8),
+ ("2.5万人民币 / 5千人民币", 5),
+ ("6M / 2K", 3000),
+ ("-2亿元 / 2", -1e8),
+])
+def test_formula_quantity_units(formula, expected):
+ from src.agent.grounding.policies import _evaluate_formula
+
+ evaluated = _evaluate_formula(formula)
+ assert evaluated is not None
+ assert evaluated[0] == pytest.approx(expected)
+
+
[email protected]("formula", ["2T / 2", "2Q / 2", "2MISSING / 2"])
+def test_unknown_magnitude_is_not_evaluated(formula):
+ from src.agent.grounding.policies import _evaluate_formula
+
+ assert _evaluate_formula(formula) is None
+
+
+ "1-9月扣非归母净利 = 779,216,390.40元 ÷ 1亿",
+ "归母净利 779,216,390.40元 ÷ 1亿",
+])
+def test_formula_labels_preserve_magnitude(note):
+ from src.agent.grounding.policies import _formula_in_note
+
+ evaluated = _formula_in_note(note)
+ assert evaluated is not None
+ assert evaluated[0] == pytest.approx(7.792163904)
+ assert evaluated[1] == [779216390.4, 1e8]
+
+
[email protected]("values,valid", [((2e8, 2), True), ((2e8,), False)])
+def test_magnitude_conversion_still_requires_operand_evidence(tmp_path, values, valid):
+ verdict = _ledger(tmp_path, values).validate_final_answer(
+ "Result: 100000000.00.\n```figures\n100000000.00 | derived | 2亿元 / 2 | measure\n```"
+ )
+ assert verdict.valid is valid, verdict.issues