Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(report-audit): exclude fenced examples from financial claims (#1951)

Rudy Mizrahi Celeklibd5624b78343
diff --git a/agent/src/tools/report_audit_tool.py b/agent/src/tools/report_audit_tool.py
index d7979d0fe..d510c5ecf 100644
--- a/agent/src/tools/report_audit_tool.py
+++ b/agent/src/tools/report_audit_tool.py
@@ -186,6 +186,30 @@ def _parse_md_tables(lines: list[str]) -> list[tuple[str, str, float, str, int,
return results
+def _without_fenced_code(lines: list[str]) -> list[str]:
+ """Blank fenced examples without changing the report's line numbers."""
+ result = []
+ fence = ""
+ for line in lines:
+ match = re.match(r"^ {0,3}(`{3,}|~{3,})(.*)$", line)
+ if fence:
+ result.append("")
+ if (
+ match
+ and match[1][0] == fence[0]
+ and len(match[1]) >= len(fence)
+ and not match[2].strip()
+ ):
+ fence = ""
+ continue
+ if match and (match[1][0] != "`" or "`" not in match[2]):
+ fence = match[1]
+ result.append("")
+ else:
+ result.append(line)
+ return result
+
+
def extract_data_points(md_text: str) -> list[dict[str, Any]]:
"""Extract recognizable financial data points from a markdown report.
@@ -225,8 +249,7 @@ def extract_data_points(md_text: str) -> list[dict[str, Any]]:
}
)
- lines = md_text.split("\n")
- in_code = False
+ lines = _without_fenced_code(md_text.split("\n"))
# 1. Multi-column tables.
_YOY_HEADERS = {"YOY", "YOY增速", "增速", "同比", "变化", "趋势", "说明", "备注"}
@@ -245,10 +268,7 @@ def extract_data_points(md_text: str) -> list[dict[str, Any]]:
# 2. ``label: value unit`` lines.
for lineno, line in enumerate(lines, start=1):
stripped = line.strip()
- if stripped.startswith("```"):
- in_code = not in_code
- continue
- if in_code or stripped.startswith("> ") or re.match(r"^#{1,6}\s", stripped):
+ if stripped.startswith("> ") or re.match(r"^#{1,6}\s", stripped):
continue
if "|" in stripped:
continue # handled as a table above
diff --git a/agent/tests/test_report_audit_code_fences.py b/agent/tests/test_report_audit_code_fences.py
new file mode 100644
index 000000000..303888c64
--- /dev/null
+++ b/agent/tests/test_report_audit_code_fences.py
@@ -0,0 +1,66 @@
+"""Report examples inside fenced code are not auditable report facts."""
+
+import mistune
+import pytest
+
+from src.tools.report_audit_tool import extract_data_points
+
+
+ "opener,inner,closer",
+ [
+ ("```markdown", "", "```"),
+ ("~~~markdown", "", "~~~"),
+ ("````markdown", "```\n", "````"),
+ ("```markdown", "~~~\n", "```"),
+ ("```markdown", "", "`````"),
+ (" ```markdown", "", " ```"),
+ ],
+)
+def test_extract_only_visible_report_facts(opener, inner, closer):
+ report = (
+ "Actual revenue: 120M\n\n"
+ + opener
+ + "\n"
+ + inner
+ + "Example profit: 888M\n"
+ + "| Metric | Example |\n| --- | --- |\n| Revenue | 999M |\n"
+ + closer
+ + "\n\nActual profit: 25M\n"
+ )
+ # An independent Markdown parser confirms the examples are one code block,
+ # while the two actual facts remain ordinary rendered paragraphs.
+ ast = mistune.create_markdown(renderer="ast", plugins=["table"])(report)
+ blocks = [block for block in ast if block["type"] == "block_code"]
+ assert len(blocks) == 1
+ assert "999M" in blocks[0]["raw"]
+ assert not any(block["type"] == "table" for block in ast)
+ points = extract_data_points(report)
+ assert [(p["label"], p["reported_value"]) for p in points] == [
+ ("Actual revenue", 120.0),
+ ("Actual profit", 25.0),
+ ]
+ assert points[-1]["line_number"] == len(report.splitlines())
+
+
+def test_unclosed_fence_hides_remainder_but_keeps_earlier_fact():
+ report = (
+ "Actual revenue: 120M\n\n~~~markdown\n"
+ "| Metric | Example |\n| --- | --- |\n| Revenue | 999M |\n"
+ "Example profit: 888M\n"
+ )
+ points = extract_data_points(report)
+ assert [(p["label"], p["reported_value"]) for p in points] == [
+ ("Actual revenue", 120.0)
+ ]
+
+
+def test_real_table_and_inline_code_marker_remain_auditable():
+ report = (
+ "The inline marker `~~~` is not a fence.\n\n"
+ "| Metric | 2025 |\n| --- | --- |\n| Revenue | 120M |\n"
+ )
+ points = extract_data_points(report)
+ assert [(p["label"], p["reported_value"], p["line_number"]) for p in points] == [
+ ("Revenue · 2025", 120.0, 5)
+ ]