"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
Merge PR #2108: bind integer and decimal point figures without weakening percent checks
Haozhe Wuad79c9ad4611
diff --cc agent/src/agent/grounding/figures.pyindex 1bb259423,74372ae6f..8b634edb7--- a/agent/src/agent/grounding/figures.py+++ b/agent/src/agent/grounding/figures.py@@@ -180,16 -200,27 +200,26 @@@ class FiguresBlockfor declaration in candidates:if abs(declaration.value - value) <= max(abs(value) * 1e-9, 1e-9):return declaration-- if not digits or "." not in digits:-- return None-- half_unit = 0.5 * 10.0 ** -len(digits.split(".", 1)[1])-- rounded = [-- (abs(item.value - value), item)-- for item in candidates-- if abs(item.value - value) <= half_unit * (1 + 1e-9)-- and abs(item.value - value) <= abs(item.value) * ROUNDED_BAND-- ]- return min(rounded, key=lambda pair: pair[0])[1] if rounded else None- if rounded:- return min(rounded, key=lambda pair: pair[0])[1]++ if digits and "." in digits:++ half_unit = 0.5 * 10.0 ** -len(digits.split(".", 1)[1])++ rounded = [++ (abs(item.value - value), item)++ for item in candidates++ if abs(item.value - value) <= half_unit * (1 + 1e-9)++ and abs(item.value - value) <= abs(item.value) * ROUNDED_BAND++ ]++ if rounded:++ return min(rounded, key=lambda pair: pair[0])[1]+ # #2107: the point-marked borrow. Reached only when the percent-exact+ # arms above found nothing, which for "1.13个百分点" is the normal+ # case — its conventionally bare declaration lives below.+ if point_marked and percent:+ for declaration in self.declarations:+ if not declaration.percent and abs(declaration.value - value) <= max(+ abs(value) * 1e-9, 1e-9+ ):+ return declaration+ return None@dataclass(frozen=True)diff --cc agent/tests/test_grounding_percentage_points.pyindex 28ae83fff,685ca3f30..562e5d39f--- a/agent/tests/test_grounding_percentage_points.py+++ b/agent/tests/test_grounding_percentage_points.py@@@ -102,3 -102,64 +102,93 @@@ def test_a_declared_delta_passes_in_eve)assert _verdict(declared) is True, text+++ # ---------------------------------------------------------------------------+ # #2107: the percentage-point unit itself, declared the conventional way.+ # A CN filing states its moves in 个百分点; the block convention declares the+ # bare number, and the gate used to redact the prose figure for lacking a+ # matching percent declaration.+++ _BARE_DECLARED = {+ "bare": "1.13",+ "unit word": "1.13个百分点",+ "percent": "1.13%",+ "pct": "1.13 pct",+ }+++ @pytest.mark.parametrize("spelling", _BARE_DECLARED.values(), ids=_BARE_DECLARED.keys())+ def test_every_conventional_declaration_grounded_the_same_delta(spelling: str) -> None:+ """``1.13`` and ``1.13个百分点`` declare the same figure, one verdict (#2107).++ The delta is written in the answer with its unit word glued, as a CN filing+ writes it, and the declaration spells the same figure each way the+ convention allows. All four are ONE case: declared → passes.+ """+ text = "ROE同比增加1.13个百分点 (2026 Q3 filing)。"+ declared = text + "\n\n```figures\n" + f"{spelling} | cited | 2026 Q3 filing, ROE bridge\n```"++ assert _verdict(declared) is True, spelling+++ def test_a_bare_declaration_does_not_ground_a_plain_percent_figure() -> None:+ """The borrow is one-way: ``37%`` in the prose needs a percent declaration.++ ``1.13`` may ground ``1.13个百分点`` because the unit word carries the+ percent-ness. ``37%`` written with a real percent sign cannot borrow the+ count declaration ``37`` — that is the assertion-vs-fraction distinction+ the contract pins (§2).+ """+ text = "市占率 37%。"+ declared = text + "\n\n```figures\n" + "37 | count | 客户数 | c1\n```"++ assert _verdict(declared) is False, "a count declaration must not ground 37%"+++ def test_a_declared_percent_range_is_two_endpoints_not_malformed() -> None:+ """``20%-30%`` declares both endpoints; the scan reads no bogus ``-30%`` (#2107).++ A quarterly report's forward guidance is frequently a range. The block+ used to reject the value as malformed and the scan then emitted ``20%``+ and ``-30%`` as two undeclared figures.+ """+ text = "业绩指引:预计年度营收增长 20%-30%。"+ declared = text + "\n\n```figures\n" + "20%-30% | cited | 2026 Q3 filing, guidance\n```"++ block = parse_figures_block(declared)+ figures = scan_figures(text, block)++ assert not block.malformed+ assert [round(item.value, 2) for item in block.declarations] == [20.0, 30.0]+ assert all(figure.sign != "-" for figure in figures if figure.percent)++++[email protected]("value", ["3", "0", "-3", "1.13"])++def test_integer_and_decimal_points_bind_bare_declarations(value):++ text = f"ROE change {value}pp (Q3 filing)."++ declared = text + f"\n```figures\n{value} | cited | Q3 filing\n```"++ assert _verdict(declared)++ block = parse_figures_block(declared)++ assert block.match(float(value), True, point_marked=True) is not None++++[email protected]("value", ["3", "1.13"])++def test_plain_percentage_never_borrows_bare_cited_value(value):++ text = f"Margin {value}% (Q3 filing)."++ declared = text + f"\n```figures\n{value} | cited | Q3 filing\n```"++ assert not _verdict(declared)++++++def test_points_do_not_round_bare_declarations_or_accept_wrong_values():++ block = parse_figures_block("```figures\n1.134 | cited | Q3 filing\n```")++ assert block.match(1.13, True, "1.13", point_marked=True) is None++ assert block.match(3, True, "3", point_marked=True) is None++++++def test_range_preserves_explicit_negative_endpoint():++ text = "Guidance 20%- -30% (Q3 filing)."++ block = parse_figures_block(text + "\n```figures\n20%- -30% | cited | Q3 filing\n```")++ assert [d.value for d in block.declarations] == [20, -30]++ assert [f.value for f in scan_figures(text, block) if f.percent] == [20, -30]