Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(grounding): harden shapes and roles against the review's smuggles

Haozhe Wub45e6bb85230
diff --git a/agent/src/agent/context.py b/agent/src/agent/context.py
index 6b557f7f2..76042b10f 100644
--- a/agent/src/agent/context.py
+++ b/agent/src/agent/context.py
@@ -200,8 +200,9 @@ Decide which workflow to use based on the request:
percentage is not a level, so state the price it implies;
`cited` — from a source other than this session's tools: name the source in the
same sentence as the figure, and in `note`;
- `count` — an integer count, window or horizon. Plain integers, dates and
- security codes need no line. Example (zh):
+ `count` — a count, weight, threshold, window, probability or other parameter
+ you chose, never a price or an amount. Plain integers, dates and security
+ codes need no line. Example (zh):
```figures
0.666 | observed | 159516.SZ 收盘 2026-09-09 | 159516.SZ
0.646 | derived | 0.666 × 0.97 | 159516.SZ
diff --git a/agent/src/agent/grounding/evidence.py b/agent/src/agent/grounding/evidence.py
index 8a6fb71c5..2c341c29a 100644
--- a/agent/src/agent/grounding/evidence.py
+++ b/agent/src/agent/grounding/evidence.py
@@ -119,6 +119,12 @@ _ANALYSIS_KIND_ALIASES = {
"return": "return",
"returns": "return",
"ic_positive_ratio": "win_rate",
+ "var": "tail_risk",
+ "var_95": "tail_risk",
+ "var_99": "tail_risk",
+ "cvar": "tail_risk",
+ "es": "tail_risk",
+ "expected_shortfall": "tail_risk",
# Chinese TOOL FIELD NAMES from A-share tools, not answer prose.
"最大回撤": "drawdown",
"回撤": "drawdown",
@@ -134,6 +140,18 @@ _ANALYSIS_KIND_ALIASES = {
"收益率": "return",
}
+# Sample-size, window and duration leaves share a metric's token but hold a
+# count: ``return_observations = 81`` is 81 observations, never an 81% return
+# (#1420/#1426). Matched on the leaf's first and last field-name token.
+_METADATA_COUNT_HEADS = frozenset({"n"})
+
+_METADATA_COUNT_TAILS = frozenset(
+ {"obs", "observations", "window", "lookback", "count", "days", "duration"}
+)
+
+# Money-denominated row fields a currency-marked figure may quote besides a price.
+_AMOUNT_FIELDS = frozenset({"amount", "turnover", "成交额"})
+
def _symbol_from_csv_filename(stem: str) -> str | None:
"""Map a run-dir CSV stem (``BYN_V`` -> ``BYN.V``) to a canonical symbol.
@@ -247,6 +265,12 @@ def _timestamp_matches_claim_date(timestamp: str, date_value: str) -> bool:
return stamp_tuple == claim_tuple
+def _leaf_name(path: str) -> str:
+ """The last field name of an evidence path, without its list index."""
+ leaf = str(path or "").rsplit(".", 1)[-1]
+ return re.sub(r"\[\d+\]$", "", leaf).strip().casefold()
+
+
def _price_field_for_path(path: str) -> str | None:
"""Map a generic evidence JSON path to a canonical price field.
@@ -257,9 +281,25 @@ def _price_field_for_path(path: str) -> str | None:
The matching member of ``_PRICE_FIELDS``, or ``None`` when the leaf is
not an unambiguous quote field.
"""
- leaf = str(path or "").rsplit(".", 1)[-1]
- leaf = re.sub(r"\[\d+\]$", "", leaf).strip().casefold()
- return _GENERIC_PRICE_FIELD_ALIASES.get(leaf)
+ return _GENERIC_PRICE_FIELD_ALIASES.get(_leaf_name(path))
+
+
+def _is_metadata_count_leaf(path: str) -> bool:
+ """Whether an evidence leaf is a sample size, window or duration.
+
+ Args:
+ path: Recorded evidence field, e.g. ``"stats.return_observations"``.
+
+ Returns:
+ True for ``n_*`` and ``*_obs`` / ``*_observations`` / ``*_window`` /
+ ``*_lookback`` / ``*_count`` / ``*_days`` / ``*_duration`` leaves.
+ """
+ tokens = [token for token in _leaf_name(path).split("_") if token]
+ if not tokens:
+ return False
+ return tokens[-1] in _METADATA_COUNT_TAILS or (
+ len(tokens) > 1 and tokens[0] in _METADATA_COUNT_HEADS
+ )
# Price-denominated indicator leaves, registered per tool and matched by path
@@ -307,9 +347,13 @@ def _is_registered_price_indicator(tool: str, path: str) -> bool:
def _metric_kind_for_path(path: str) -> str | None:
- """Map an evidence JSON path to an analysis metric kind."""
- leaf = re.sub(r"\[\d+\]$", "", str(path or "").rsplit(".", 1)[-1])
- leaf = leaf.strip().casefold()
+ """Map an evidence JSON path to an analysis metric kind.
+
+ A metadata count leaf (:func:`_is_metadata_count_leaf`) has no kind.
+ """
+ if _is_metadata_count_leaf(path):
+ return None
+ leaf = _leaf_name(path)
kind = _ANALYSIS_KIND_ALIASES.get(leaf)
if kind is not None:
return kind
@@ -341,6 +385,24 @@ class EvidenceRecord:
currency_conversion: str | None = None
+def _is_price_kind(record: EvidenceRecord) -> bool:
+ """Whether a record is money-denominated: a quote, a registered level or an amount.
+
+ Args:
+ record: One evidence record, with its raw or canonical field.
+
+ Returns:
+ True for OHLC/quote fields, registered price indicators and
+ amount/turnover leaves.
+ """
+ return (
+ record.field in _PRICE_FIELDS
+ or _price_field_for_path(record.field) is not None
+ or _is_registered_price_indicator(record.tool, record.field)
+ or _leaf_name(record.field) in _AMOUNT_FIELDS
+ )
+
+
class _EvidenceMixin:
"""Evidence behaviour of :class:`GroundingLedger`."""
diff --git a/agent/src/agent/grounding/figures.py b/agent/src/agent/grounding/figures.py
index 4787cf33b..9fbb0574a 100644
--- a/agent/src/agent/grounding/figures.py
+++ b/agent/src/agent/grounding/figures.py
@@ -5,13 +5,21 @@ declarations, classifies every prose number by SHAPE (date, symbol, list
marker, measurement, bare integer) and matches prose numbers to declarations.
It reads no natural-language word; roles are declared by the model and shape
is language-independent.
+
+Numbers are read as a renderer shows them: invisible characters, Markdown
+escapes, character references and look-alike separators are normalized first,
+and every offset is mapped back to the original text.
"""
from __future__ import annotations
+import html
import re
+import string
+import unicodedata
from dataclasses import dataclass
-from typing import Sequence
+from decimal import Decimal, InvalidOperation
+from typing import Iterable, Sequence
from src.agent.grounding.identity import _CANONICAL_SYMBOL_RE
@@ -28,27 +36,32 @@ _NUMBER_RE = re.compile(
r"(?<![A-Za-z0-9_])[-+]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?(?!\d)"
)
-# SHAPE 2 — a calendar date or a bare year: structure, never a measurement
-# (spec §3). A year-less "08-10" is two bare integers and needs no mask.
+# SHAPE 2 — dates, times and years: structure, never a measurement (spec §3).
+# A year-less "08-10" is two bare integers and needs no mask.
_DATE_RE = re.compile(
- r"(?P<full>(?:19|20)\d{2}\s*[-/年]\s*\d{1,2}\s*[-/月]\s*\d{1,2}\s*[日号]?)"
+ r"(?P<full>(?:19|20)\d{2}(?:\s*[-/年]\s*\d{1,2}\s*[-/月]\s*\d{1,2}\s*[日号]?"
+ # A dotted date has both dots and no spacing: "2001.5 - 2002.5" is a range.
+ r"|\.\d{1,2}\.\d{1,2}(?!\d|\.\d)))"
# A year-less MM-DD / MM/DD; see _short_date_is_structural.
r"|(?P<short>(?<![\d.])(?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12]\d|3[01])(?!\d|\.\d))"
+ r"|(?<![\d.:])(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?(?![\d:]|\.\d)"
r"|\d{1,2}\s*月\s*\d{1,2}\s*[日号]"
r"|(?:19|20)\d{2}\s*年"
- r"|(?:19|20)\d{2}"
+ # A bare year or compact YYYYMMDD loses to a measurement mark ("$2050").
+ r"|(?P<soft>(?<![\d.])(?:19|20)\d{2}(?:(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01]))?(?!\d|\.\d))"
)
# SHAPE 3 — a line-leading list marker or numbered heading. The punctuation is
# required, so a line opening with a figure ("1.171 元是收盘价") is untouched.
-_ORDINAL_RE = re.compile(r"(?m)^[^\S\n]*(?:#{1,6}[^\S\n]*)?\d{1,3}[.)、.][^\S\n]+")
+_ORDINAL_RE = re.compile(r"(?m)^[^\S\n]*(?:#{1,6}[^\S\n]*)?\d{1,3}[.)、][^\S\n]+")
-# SHAPE 4 — a fenced block: code or the figures block, never prose.
-_FENCE_RE = re.compile(r"(?m)^[^\S\n]*(?:`{3,}|~{3,})[^\n]*$")
+# SHAPE 4 — a CommonMark fence line: at most three spaces of indent, a run of
+# backticks or tildes, and the info string.
+_FENCE_RE = re.compile(r"(?m)^ {0,3}(`{3,}|~{3,})(.*)$")
# Currency is a symbol set, not a vocabulary: a money character or ISO code
# touching a bare integer ("$100", "820 CNY") makes it measurement-shaped.
-_CURRENCY_CHARS = frozenset("$¥¥€£₩₹元币圆镑")
+_CURRENCY_CHARS = frozenset("$¥¥€£₩₹元币圆镑円원")
_CURRENCY_CODES = frozenset(
{
@@ -59,6 +72,9 @@ _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))
+
# 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.
_MAGNITUDES = {"K": 1e3, "M": 1e6, "B": 1e9, "千": 1e3, "万": 1e4, "亿": 1e8}
@@ -67,10 +83,26 @@ _MAGNITUDES = {"K": 1e3, "M": 1e6, "B": 1e9, "千": 1e3, "万": 1e4, "亿": 1e8}
# keeps 元宵/元件, inside longer CJK runs, from reading as money.
_MAX_CURRENCY_WORD = 3
+# What a renderer does not show, and look-alikes of a number's own separators.
+_INVISIBLE = frozenset("\u200b\u200c\u200d\u2060\ufeff\u00ad")
+
+_LOOKALIKES = {
+ "\uff0e": ".", "\u066b": ".", "\u066c": ",", "\u2212": "-",
+ "\u00a0": " ", "\u202f": " ", "\u2007": " ", "\u2009": " ",
+}
+
+_LINE_BREAKS = frozenset("\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029")
+
+_ESCAPABLE = frozenset(string.punctuation)
+
@dataclass(frozen=True)
class Declaration:
- """One parsed line of the model's figures block."""
+ """One parsed line of the model's figures block.
+
+ ``value_text`` is the value's canonical spelling at its written precision,
+ in percent units when it is a percent ("5200bp" → "52.00%").
+ """
index: int
value_text: str
@@ -83,13 +115,18 @@ class Declaration:
@dataclass(frozen=True)
class FiguresBlock:
- """The declaration block, or the absence of one."""
+ """The declaration blocks, or the absence of one.
+
+ ``span`` is the first block's (streaming holds back from it); ``spans``
+ covers every block, all of which are merged and stripped.
+ """
present: bool
span: tuple[int, int] | None
raw: str
declarations: tuple[Declaration, ...]
malformed: tuple[tuple[int, str], ...]
+ spans: tuple[tuple[int, int], ...] = ()
def match(self, value: float, percent: bool) -> Declaration | None:
"""Return the declaration covering ``value``, or None.
@@ -114,7 +151,13 @@ class FiguresBlock:
@dataclass(frozen=True)
class Figure:
- """One number located in the prose, with the shape it was written in."""
+ """One number located in the prose, with the shape it was written in.
+
+ ``text``, ``start`` and ``end`` are the original spelling and offsets.
+ ``value`` is in percent units for a percent, pp or bp figure ("5200bp" is
+ 52.0, ``scale`` 1.0). ``digits`` and ``sign`` are the normalized reading
+ ("0\\.888" → "0.888", "−5,13%" → "-", "5.13"), for precision and sign.
+ """
text: str
value: float
@@ -129,6 +172,13 @@ class Figure:
scale: float = 1.0
# A currency mark or ISO code touches the digits ("$2050", "0.95 元").
currency: bool = False
+ digits: str = ""
+ sign: str = ""
+ # The span a redaction replaces: the figure plus its glued currency and
+ # magnitude marks ("HK$1.10", "0.95 元", "24.6M").
+ extent: tuple[int, int] | None = None
+ # The info string of the fenced block holding the figure, or None.
+ fence: str | None = None
@dataclass(frozen=True)
@@ -141,6 +191,35 @@ class TableRow:
date_column: int | None
symbol_column: int | None
+
+@dataclass(frozen=True)
+class _Normalized:
+ """A document as a renderer shows its numbers, with offsets into the source."""
+
+ text: str
+ starts: tuple[int, ...]
+ ends: tuple[int, ...]
+ size: int
+
+ def start(self, index: int) -> int:
+ """The source offset where normalized position ``index`` begins."""
+ return self.starts[index] if index < len(self.starts) else self.size
+
+ def end(self, index: int) -> int:
+ """The source offset where a normalized span ending at ``index`` ends."""
+ return self.ends[index - 1] if index > 0 else 0
+
+
+@dataclass(frozen=True)
+class _Token:
+ """One number in normalized text: its span, sign and digits as written."""
+
+ start: int
+ end: int
+ sign: str
+ digits: str
+
+
# Header spellings binding a table column to an OHLC field: the table's own
# schema, like a tool field name, not prose inference (spec §4).
_TABLE_FIELD_ALIASES = {
@@ -182,51 +261,268 @@ def _lines_with_offsets(content: str) -> list[tuple[str, int]]:
return positions
-def _fenced_blocks(content: str) -> list[tuple[int, int, str, tuple[int, int]]]:
- """Return ``(start, end, info, body)`` for every fenced block.
+def _entity(content: str, index: int) -> tuple[str, int] | None:
+ """Decode the character reference (``&#46;``, ``&nbsp;``) at ``index``.
- An unterminated fence runs to the end of the document, which is how a
- truncated answer ends and must not silently un-fence the rest of it.
+ Returns:
+ ``(character, source width)``, or None when there is no reference or it
+ would decode to a line break or control character.
"""
- fences = list(_FENCE_RE.finditer(content))
+ semicolon = content.find(";", index + 2, index + 12)
+ if semicolon < 0:
+ return None
+ piece = content[index : semicolon + 1]
+ name = piece[1:-1]
+ if name.startswith("#"):
+ digits, allowed, base = name[1:], string.digits, 10
+ if digits[:1] in ("x", "X"):
+ digits, allowed, base = digits[1:], string.hexdigits, 16
+ if not digits or any(char not in allowed for char in digits):
+ return None
+ code = int(digits, base)
+ if not 0 < code <= 0x10FFFF or 0xD800 <= code <= 0xDFFF:
+ return None
+ char = chr(code)
+ else:
+ char = html.unescape(piece) if name.isascii() and name.isalnum() else piece
+ if len(char) != 1:
+ return None
+ if char in _LINE_BREAKS or unicodedata.category(char) == "Cc":
+ return None
+ return char, len(piece)
+
+
+def _glued_code(chars: Sequence[str]) -> bool:
+ """Whether ``chars`` ends in an uppercase ISO code standing on its own ("CNY")."""
+ run = 0
+ while run < min(len(chars), 5) and chars[-1 - run].isascii() and chars[-1 - run].isupper():
+ run += 1
+ before = chars[-1 - run] if len(chars) > run else ""
+ return (
+ "".join(chars[len(chars) - run :]) in _CURRENCY_CODES
+ and not (before.isalnum() or before == "_")
+ )
+
+
+def _normalize(content: str) -> _Normalized:
+ """Read ``content`` as a renderer shows its numbers.
+
+ Invisible characters are dropped; a backslash before ASCII punctuation and a
+ character reference are resolved; "." and "٫" read as a decimal point, "٬"
+ as a group separator, "−" as a minus, no-break and figure spaces as spaces;
+ an ISO code glued to digits ("CNY0.888") gets a space so the number is read
+ whole. Line breaks are never introduced or removed.
+ """
+ chars: list[str] = []
+ starts: list[int] = []
+ ends: list[int] = []
+ index = 0
+ while index < len(content):
+ char, width = content[index], 1
+ if char == "\\" and content[index + 1 : index + 2] in _ESCAPABLE:
+ char, width = content[index + 1], 2
+ elif char == "&":
+ char, width = _entity(content, index) or (char, width)
+ if char in _INVISIBLE:
+ index += width
+ continue
+ char = _LOOKALIKES.get(char, char)
+ if char.isdigit() and _glued_code(chars):
+ chars.append(" ")
+ starts.append(index)
+ ends.append(index)
+ chars.append(char)
+ starts.append(index)
+ ends.append(index + width)
+ index += width
+ return _Normalized("".join(chars), tuple(starts), tuple(ends), len(content))
+
+
+def _digit_run(text: str, index: int) -> str:
+ """The run of digits starting at ``index``."""
+ end = index
+ while end < len(text) and text[end].isdigit():
+ end += 1
+ return text[index:end]
+
+
+def _numbers(text: str) -> list[_Token]:
+ """Every number in normalized text, with a decimal comma read as one (#1418).
+
+ A comma is a decimal point when the integer part is exactly "0" ("0,666"),
+ or when a one- or two-digit fraction carries a percent, pp/bp or currency
+ mark ("12,5 %", "3,95 EUR", "€3,95"). A valid grouping stays grouped.
+ """
+ tokens: list[_Token] = []
+ cursor = 0
+ for match in _NUMBER_RE.finditer(text):
+ if match.start() < cursor:
+ continue
+ raw = match.group(0)
+ sign = raw[0] if raw[0] in "+-" else ""
+ body, end = raw[len(sign) :], match.end()
+ if body.startswith("0,") and body.count(",") == 1 and "." not in body:
+ body = body.replace(",", ".")
+ elif body.isdigit() and text[end : end + 1] == ",":
+ fraction = _digit_run(text, end + 1)
+ stop = end + 1 + len(fraction)
+ if fraction and (
+ body == "0"
+ or (
+ len(fraction) <= 2
+ and (
+ _currency_before(text, match.start())
+ or _currency_after(text, stop)
+ or _percent_mark(text, stop)[0] > 0
+ )
+ )
+ ):
+ body, end = f"{body}.{fraction}", stop
+ tokens.append(_Token(match.start(), end, sign, body.replace(",", "")))
+ cursor = end
+ return tokens
+
+
+def _percent_mark(text: str, end: int) -> tuple[float, int]:
+ """The percent a figure's trailing mark denotes, and where the mark ends.
+
+ A percent sign may follow a space; ``pp`` / ``bp`` / ``bps`` must be glued
+ and not run into a word ("3.6ppm" is not a percentage point).
+
+ Returns:
+ ``(1.0, end)`` for % and pp, ``(0.01, end)`` for bp, ``(0.0, end)``.
+ """
+ rest = text[end:]
+ spaced = rest.lstrip(" \t")
+ if spaced[:1] and spaced[0] in _PERCENT_CHARS:
+ return 1.0, end + len(rest) - len(spaced) + 1
+ for mark, unit in _POINT_MARKS:
+ after = rest[len(mark) : len(mark) + 1]
+ if rest[: len(mark)].casefold() == mark and not (after.isascii() and after.isalnum()):
+ return unit, end + len(mark)
+ return 0.0, end
+
+
+def _reading(sign: str, digits: str, unit: float) -> tuple[float, str] | None:
+ """A number's value and canonical spelling, in percent units for bp."""
+ try:
+ amount = Decimal(sign + digits)
+ except InvalidOperation:
+ return None
+ if unit == 0.01:
+ amount = amount.scaleb(-2)
+ return float(amount), format(amount, "f")
+
+
+def _fenced_blocks(content: str) -> list[tuple[int, int, str, tuple[int, int]]]:
+ """Return ``(start, end, info, body)`` for every fenced block (CommonMark).
+
+ A backtick opener's info string holds no backtick; a closer repeats the
+ opener's character, at least as long, with nothing after it. An unterminated
+ ``figures`` fence runs to the end, so a truncated answer keeps its
+ declarations; any other unterminated fence is prose and its numbers are
+ checked. A ``figures`` opener at least as long ends a search for a closer,
+ so a stray opener cannot pair with the declaration block's closing fence.
+ """
+ fences = []
+ for match in _FENCE_RE.finditer(content):
+ marker, info = match.group(1), match.group(2).strip()
+ if marker[0] == "`" and "`" in info:
+ continue
+ words = info.split()
+ fences.append((match, marker, words[0].casefold() if words else "", not info))
blocks: list[tuple[int, int, str, tuple[int, int]]] = []
index = 0
while index < len(fences):
- opener = fences[index]
- info = opener.group(0).strip().lstrip("`~").strip().casefold()
- if index + 1 < len(fences):
- closer = fences[index + 1]
+ opener, marker, info, _ = fences[index]
+ cursor, closer = index + 1, None
+ while cursor < len(fences):
+ candidate, other, other_info, bare = fences[cursor]
+ if bare and other[0] == marker[0] and len(other) >= len(marker):
+ closer = candidate
+ break
+ if other_info == BLOCK_LANGUAGE and len(other) >= len(marker):
+ break
+ cursor += 1
+ if closer is not None:
blocks.append((opener.start(), closer.end(), info, (opener.end(), closer.start())))
- index += 2
+ index = cursor + 1
+ elif info == BLOCK_LANGUAGE:
+ stop = fences[cursor][0].start() if cursor < len(fences) else len(content)
+ blocks.append((opener.start(), stop, info, (opener.end(), stop)))
+ index = cursor
else:
- blocks.append((opener.start(), len(content), info, (opener.end(), len(content))))
index += 1
return blocks
-def _parse_value(text: str) -> tuple[float, bool] | None:
- """Parse a declared value, tolerating currency marks and separators."""
- raw = text.strip().replace(",", "").replace(" ", "").replace(" ", "")
- percent = raw.endswith(tuple(_PERCENT_CHARS))
- if percent:
- raw = raw[:-1]
- raw = raw.strip("".join(_CURRENCY_CHARS))
- if raw and raw[-1] in _MAGNITUDES:
- raw = raw[:-1]
- if not raw:
+def _is_currency_word(run: str) -> bool:
+ """Whether a CJK run is short enough to be a currency word and holds a money mark."""
+ return len(run) <= _MAX_CURRENCY_WORD and any(char in _CURRENCY_CHARS for char in run)
+
+
+def _is_currency_mark(word: str) -> bool:
+ """Whether a declared value's decoration is empty or one currency mark.
+
+ Accepts an ISO code ("USD"), a CJK currency word ("港元"), or money
+ characters behind at most three letters ("$", "HK$", "NT$").
+ """
+ if not word or word.upper() in _CURRENCY_CODES:
+ return True
+ if all(_is_cjk(char) for char in word):
+ return _is_currency_word(word)
+ symbols = word.lstrip(string.ascii_letters)
+ return (
+ bool(symbols)
+ and len(word) - len(symbols) <= 3
+ and all(char in _CURRENCY_CHARS for char in symbols)
+ )
+
+
+def _parse_value(text: str) -> tuple[float, bool, str] | None:
+ """Read a declared value: one number with its currency, magnitude and percent marks.
+
+ Returns:
+ ``(value, percent, canonical spelling)``, or None when the field is not
+ exactly one number and its marks.
+ """
+ field = _normalize(text).text.strip()
+ tokens = _numbers(field)
+ if len(tokens) != 1:
return None
- try:
- return float(raw), percent
- except ValueError:
+ token = tokens[0]
+ rest = field[token.end :].strip()
+ if rest[:1] in _MAGNITUDES and not _is_currency_mark(rest):
+ rest = rest[1:].lstrip()
+ unit, consumed = _percent_mark(rest, 0)
+ if not (
+ _is_currency_mark(rest[consumed:].strip())
+ and _is_currency_mark(field[: token.start].strip())
+ ):
+ return None
+ reading = _reading(token.sign, token.digits, unit)
+ if reading is None:
return None
+ value, canonical = reading
+ return value, unit > 0, canonical + ("%" if unit > 0 else "")
+
+
+def _is_header_or_rule(parts: Sequence[str]) -> bool:
+ """Whether a block line is a ``value | role`` header or a Markdown separator row."""
+ if len(parts) >= 2 and parts[0].casefold() == "value" and parts[1].casefold() == "role":
+ return True
+ cells = [part.replace(" ", "") for part in parts if part]
+ return bool(cells) and all(set(cell) <= {"-", ":"} and "-" in cell for cell in cells)
def parse_figures_block(content: str) -> FiguresBlock:
- """Parse the model's ``figures`` block out of a draft.
+ """Parse the model's ``figures`` blocks out of a draft.
- Parsing is lenient (full-width pipe, run-on spacing, missing ``ref``), but a
- line that cannot be read as ``value | role | note | ref`` is reported as
- malformed: a skipped declaration is a figure the gate never checked.
+ Parsing is lenient (full-width pipe, run-on spacing, missing ``ref``, a
+ header or separator row, currency and unit marks on the value), but a line
+ that cannot be read as ``value | role | note | ref`` is reported as
+ malformed: a skipped declaration is a figure the gate never checked. Every
+ ``figures`` fence contributes; ``span`` is the first one.
Args:
content: The candidate answer.
@@ -234,32 +530,34 @@ def parse_figures_block(content: str) -> FiguresBlock:
Returns:
The parsed block, or an absent one when the draft has no block.
"""
- target: tuple[int, int, str, tuple[int, int]] | None = None
- for block in _fenced_blocks(content):
- if block[2] == BLOCK_LANGUAGE:
- target = block
- if target is None:
+ blocks = [block for block in _fenced_blocks(content) if block[2] == BLOCK_LANGUAGE]
+ if not blocks:
return FiguresBlock(False, None, "", (), ())
- start, end, _, (body_start, body_end) = target
- body = content[body_start:body_end]
+ raw = "".join(content[start:end] for _, _, _, (start, end) in blocks)
declarations: list[Declaration] = []
malformed: list[tuple[int, str]] = []
- for offset, line in enumerate(body.splitlines()):
- stripped = line.strip()
+ for number, line in enumerate(raw.splitlines(), start=1):
+ stripped = line.strip().replace("|", "|")
if not stripped:
continue
- parts = [part.strip() for part in stripped.replace("|", "|").split("|")]
- parts = [part for part in parts if part != ""] or [""]
+ parts = stripped.split("|")
+ if stripped.startswith("|"):
+ parts = parts[1:]
+ if len(parts) > 1 and stripped.endswith("|"):
+ parts = parts[:-1]
+ parts = [part.strip() for part in parts]
+ if _is_header_or_rule(parts):
+ continue
parsed = _parse_value(parts[0]) if parts else None
role = parts[1].casefold() if len(parts) > 1 else ""
if parsed is None or role not in ROLES:
- malformed.append((offset + 1, stripped[:120]))
+ malformed.append((number, line.strip()[:120]))
continue
- value, percent = parsed
+ value, percent, canonical = parsed
declarations.append(
Declaration(
- index=offset + 1,
- value_text=parts[0],
+ index=number,
+ value_text=canonical,
value=value,
percent=percent,
role=role,
@@ -267,15 +565,24 @@ def parse_figures_block(content: str) -> FiguresBlock:
ref=parts[3] if len(parts) > 3 else "",
)
)
- return FiguresBlock(True, (start, end), body, tuple(declarations), tuple(malformed))
+ return FiguresBlock(
+ True,
+ (blocks[0][0], blocks[0][1]),
+ raw,
+ tuple(declarations),
+ tuple(malformed),
+ tuple((start, end) for start, end, _, _ in blocks),
+ )
def strip_figures_block(content: str, block: FiguresBlock) -> str:
- """Return the text to release: the draft without its declaration block."""
- if block.span is None:
- return content
- start, end = block.span
- return (content[:start].rstrip() + "\n" + content[end:].lstrip()).strip()
+ """Return the text to release: the draft without any declaration block."""
+ spans = block.spans or ((block.span,) if block.span else ())
+ text = content
+ for start, end in sorted(spans, reverse=True):
+ head, tail = text[:start].rstrip(), text[end:].lstrip()
+ text = head + ("\n\n" if head and tail else "") + tail
+ return text.strip() if spans else content
def _table_cells(line: str, offset: int) -> list[tuple[str, int, int]]:
@@ -351,10 +658,27 @@ def table_rows(content: str) -> list[TableRow]:
return rows
+def _code_before(head: str) -> str:
+ """The uppercase ISO code ending ``head`` as a word of its own, or ""."""
+ letters = head[len(head.rstrip(string.ascii_uppercase)) :]
+ before = head[-len(letters) - 1 : -len(letters)] if letters else ""
+ if letters in _CURRENCY_CODES and not (before.isalnum() or before == "_"):
+ return letters
+ return ""
+
+
def _currency_before(text: str, start: int) -> bool:
- """Whether a currency symbol touches the number on its left."""
+ """Whether a currency symbol or ISO code touches the number on its left."""
head = text[:start].rstrip()
- return bool(head) and head[-1] in _CURRENCY_CHARS
+ return bool(head) and (head[-1] in _CURRENCY_CHARS or bool(_code_before(head)))
+
+
+def _cjk_run(text: str) -> str:
+ """The run of CJK ideographs ``text`` opens with."""
+ end = 0
+ while end < len(text) and _is_cjk(text[end]):
+ end += 1
+ return text[:end]
def _currency_after(text: str, end: int) -> bool:
@@ -369,14 +693,7 @@ def _currency_after(text: str, end: int) -> bool:
if tail[0] in _CURRENCY_CHARS and not _is_cjk(tail[0]):
return True
if _is_cjk(tail[0]):
- run = ""
- for char in tail:
- if not _is_cjk(char):
- break
- run += char
- return len(run) <= _MAX_CURRENCY_WORD and any(
- char in _CURRENCY_CHARS for char in run
- )
+ return _is_currency_word(_cjk_run(tail))
code = ""
for char in tail:
if not char.isascii() or not char.isalpha():
@@ -407,45 +724,35 @@ def magnitude_suffix(text: str, end: int) -> tuple[float, int]:
return _MAGNITUDES[mark], end + 1
-def currency_prefix_start(text: str, start: int) -> int:
- """Where a currency symbol attached to the left of a figure ("$1.10") begins."""
- head = text[:start]
- stripped = head.rstrip()
- if stripped and stripped[-1] in _CURRENCY_CHARS and not _is_cjk(stripped[-1]):
- return len(stripped) - 1
- return start
+def _currency_prefix_start(text: str, start: int) -> int:
+ """Where a currency mark attached to the left of a figure begins ("HK$1.10", "USD 100")."""
+ head = text[:start].rstrip(" \t")
+ if head and head[-1] in _CURRENCY_CHARS and not _is_cjk(head[-1]):
+ letters = len(head) - 1 - len(head[:-1].rstrip(string.ascii_uppercase))
+ return len(head) - 1 - (letters if letters <= 3 else 0)
+ code = _code_before(head)
+ return len(head) - len(code) if code else start
-def currency_suffix_end(text: str, end: int) -> int:
+def _currency_suffix_end(text: str, end: int) -> int:
"""Where a currency unit attached to the right of a figure ("0.95 元") ends.
A compound unit (元/股, USD/share) is left whole, or its denominator would be
left with nothing above it.
"""
- tail = text[end:]
- lead = len(tail) - len(tail.lstrip(" \t"))
- body = tail[lead:]
+ body = text[end:].lstrip(" \t")
+ lead = len(text) - end - len(body)
if not body:
return end
- run = ""
if _is_cjk(body[0]):
- for char in body:
- if not _is_cjk(char):
- break
- run += char
- if len(run) > _MAX_CURRENCY_WORD or not any(
- char in _CURRENCY_CHARS for char in run
- ):
+ run = _cjk_run(body)
+ if not _is_currency_word(run):
return end
else:
- for char in body:
- if not char.isascii() or not char.isalpha():
- break
- run += char
+ run = body[: len(body) - len(body.lstrip(string.ascii_letters))]
if run.upper() not in _CURRENCY_CODES:
return end
- following = body[len(run) : len(run) + 1]
- if following in {"/", "/"}:
+ if body[len(run) : len(run) + 1] in {"/", "/"}:
return end
return end + lead + len(run)
@@ -491,7 +798,7 @@ def segment_bounds(content: str, start: int, end: int) -> tuple[int, int]:
return left, right
-def _within(span: tuple[int, int], spans: Sequence[tuple[int, int]]) -> bool:
+def _within(span: tuple[int, int], spans: Iterable[tuple[int, int]]) -> bool:
"""Whether ``span`` sits inside any of ``spans``."""
return any(start <= span[0] and span[1] <= end for start, end in spans)
@@ -521,94 +828,99 @@ def _short_date_is_structural(content: str, match: re.Match[str], full_dates: Se
def scan_figures(content: str, block: FiguresBlock) -> list[Figure]:
"""Locate and classify every number in the prose of a draft (spec §3).
- * ``exempt`` — a date, year, security-code digits, line-leading ordinal, a
- table's date/symbol column, or anything fenced: structure.
- * ``measured`` — a decimal point, percent sign, touching currency mark or
- table cell: the shape a fabricated price or metric takes; must be declared.
+ * ``exempt`` — a date, time, security-code digits, line-leading ordinal or
+ anything fenced; a bare year, compact date or a table date/symbol cell
+ only while it carries no decimal point, percent or currency mark (and a
+ year not under a price column).
+ * ``measured`` — a decimal point, percent / pp / bp, touching currency mark
+ or table cell: the shape a fabricated price or metric takes.
* ``bare`` — a plain integer (counts, horizons, window lengths): unchecked.
Args:
content: The candidate answer.
- block: The parsed figures block (its span is exempt).
+ block: The parsed figures block (its spans are exempt).
Returns:
- Every number in document order, each with its span and shape.
+ Every number in document order, each with its original span and shape.
"""
- positions = _lines_with_offsets(content)
+ view = _normalize(content)
+ text = view.text
line_of: list[tuple[int, int, int]] = [
- (start, start + len(line), index) for index, (line, start) in enumerate(positions)
- ]
- # Every fenced block is exempt, the figures block included.
- exempt: list[tuple[int, int]] = [
- (start, end) for start, end, _, _ in _fenced_blocks(content)
+ (start, start + len(line), index)
+ for index, (line, start) in enumerate(_lines_with_offsets(content))
]
- dates = list(_DATE_RE.finditer(content))
+ fences = _fenced_blocks(content)
+ hard: list[tuple[int, int]] = [(start, end) for start, end, _, _ in fences]
+ soft: list[tuple[int, int]] = []
+ dates = list(_DATE_RE.finditer(text))
full_dates = [match for match in dates if match.group("full")]
- exempt.extend(
- (match.start(), match.end())
- for match in dates
- if not match.group("short") or _short_date_is_structural(content, match, full_dates)
- )
+ for match in dates:
+ if match.group("short") and not _short_date_is_structural(text, match, full_dates):
+ continue
+ span = (view.start(match.start()), view.end(match.end()))
+ (soft if match.group("soft") else hard).append(span)
for pattern in (_CANONICAL_SYMBOL_RE, _ORDINAL_RE):
- exempt.extend((match.start(), match.end()) for match in pattern.finditer(content))
-
- cell_at: list[tuple[int, int, TableRow, int]] = []
- for row in table_rows(content):
- for position, (_, start, end) in enumerate(row.cells):
- if position == row.date_column or position == row.symbol_column:
- exempt.append((start, end))
- continue
- cell_at.append((start, end, row, position))
+ hard.extend(
+ (view.start(match.start()), view.end(match.end()))
+ for match in pattern.finditer(text)
+ )
+ cells = [
+ (start, end, row, position)
+ for row in table_rows(content)
+ for position, (_, start, end) in enumerate(row.cells)
+ ]
figures: list[Figure] = []
- for match in _NUMBER_RE.finditer(content):
- start, end = match.start(), match.end()
- percent = False
- tail = content[end:]
- stripped = len(tail) - len(tail.lstrip(" \t"))
- if tail[stripped : stripped + 1] and tail[stripped] in _PERCENT_CHARS:
- percent = True
- end = end + stripped + 1
- try:
- value = float(match.group(0).replace(",", ""))
- except ValueError:
+ for token in _numbers(text):
+ unit, unit_end = _percent_mark(text, token.end)
+ reading = _reading(token.sign, token.digits, unit)
+ if reading is None:
continue
- line = next(
- (index for low, high, index in line_of if low <= start <= high), 0
- )
- cell = next(
- (entry for entry in cell_at if entry[0] <= start and end <= entry[1]), None
- )
- currency = _currency_before(content, start) or _currency_after(content, match.end())
- if _within((start, match.end()), exempt):
+ percent = unit > 0
+ start, digits_end, end = view.start(token.start), view.end(token.end), view.end(unit_end)
+ cell = next((entry for entry in cells if entry[0] <= start and end <= entry[1]), None)
+ row, position = (cell[2], cell[3]) if cell else (None, None)
+ structural = row is not None and position in (row.date_column, row.symbol_column)
+ currency = _currency_before(text, token.start) or _currency_after(text, token.end)
+ marked = percent or currency or "." in token.digits
+ if _within((start, digits_end), hard):
shape = "exempt"
- elif percent or currency or "." in match.group(0) or cell is not None:
+ elif structural or _within((start, digits_end), soft):
+ priced = row is not None and not structural and position in row.columns
+ shape = "measured" if marked or priced else "exempt"
+ elif marked or row is not None:
shape = "measured"
else:
shape = "bare"
column = date_value = symbol_value = None
- if cell is not None:
- row, position = cell[2], cell[3]
+ if row is not None and not structural:
column = row.columns.get(position)
if row.date_column is not None and row.date_column < len(row.cells):
date_value = row.cells[row.date_column][0] or None
if row.symbol_column is not None and row.symbol_column < len(row.cells):
symbol_value = row.cells[row.symbol_column][0] or None
- scale = 1.0 if percent else magnitude_suffix(content, match.end())[0]
+ mark_end = _currency_suffix_end(text, magnitude_suffix(text, unit_end)[1])
figures.append(
Figure(
text=content[start:end],
- value=value,
+ value=reading[0],
percent=percent,
start=start,
end=end,
- line=line,
+ line=next((index for low, high, index in line_of if low <= start <= high), 0),
shape=shape,
column=column,
date=date_value,
symbol=symbol_value,
- scale=scale,
+ scale=1.0 if percent else magnitude_suffix(text, token.end)[0],
currency=currency,
+ digits=token.digits,
+ sign=token.sign,
+ extent=(view.start(_currency_prefix_start(text, token.start)), view.end(mark_end)),
+ fence=next(
+ (info for low, high, info, _ in fences if low <= start and digits_end <= high),
+ None,
+ ),
)
)
return figures
diff --git a/agent/src/agent/grounding/policies.py b/agent/src/agent/grounding/policies.py
index 16055ba08..6fc12306e 100644
--- a/agent/src/agent/grounding/policies.py
+++ b/agent/src/agent/grounding/policies.py
@@ -11,7 +11,7 @@ import ast
import json
import math
from dataclasses import dataclass, field
-from typing import Any, Iterable, Sequence
+from typing import Any, Callable, Iterable, Sequence
from src.agent.grounding.identity import (
_CANONICAL_SYMBOL_RE,
@@ -20,8 +20,11 @@ from src.agent.grounding.identity import (
)
from src.agent.grounding.evidence import (
EvidenceRecord,
+ _is_metadata_count_leaf,
_is_number,
+ _is_price_kind,
_metric_kind_for_path,
+ _price_field_for_path,
_timestamp_matches_claim_date,
)
from src.agent.grounding.figures import (
@@ -81,6 +84,9 @@ _OTHER_CURRENCY_PREFIXES = "港美日欧韩台新加澳"
#: Relative band a value must fall in to count as matching evidence.
_TOLERANCE = 0.005
+#: Price fields a rejected prose figure is pointed at, in order (#1433).
+_CITABLE_FIELDS = ("close", "price", "adj_close")
+
@dataclass(frozen=True)
class ValidationResult:
@@ -127,15 +133,135 @@ def _written_half_unit(text: str) -> float:
return 0.5 * 10.0 ** (-decimals)
-def _evaluate_formula(expression: str) -> tuple[float, list[float]] | None:
+def _explicit_sign(text: str) -> int:
+ """``1`` or ``-1`` when a figure was written with a sign ("+36.8%"), else ``0``."""
+ head = text.strip()[:1]
+ return 1 if head == "+" else -1 if head == "-" else 0
+
+
+def _is_plain_count(figure: Figure) -> bool:
+ """Whether a figure can be a count or a parameter the model chose.
+
+ A weight, threshold, window, probability or multiplier is unchecked; a
+ figure with a currency mark is a price or an amount and is checked as
+ observed. A price column is refused before this is asked.
+ """
+ return not figure.currency
+
+
+def _note_tokens(note: str) -> set[str]:
+ """Citation fragments in a note: CJK character pairs and ASCII words of four letters or more.
+
+ Pairs, because Chinese is not space-separated: "财报毛利率桥" is visible in
+ "毛利率下降" the way "gross margin bridge" is visible in "gross margin".
+ Four letters, because "the" or "and" would make any English line visible.
+
+ Args:
+ note: A declaration's free-text note.
+
+ Returns:
+ The casefolded fragments; digits, spaces and punctuation separate runs.
+ """
+ tokens: set[str] = set()
+ run, kind = "", ""
+ for char in note.casefold() + " ":
+ if "㐀" <= char <= "鿿":
+ current = "cjk"
+ elif char.isascii() and char.isalpha():
+ current = "ascii"
+ else:
+ current = ""
+ if current != kind:
+ if kind == "cjk" and len(run) >= 2:
+ tokens.update(run[index : index + 2] for index in range(len(run) - 1))
+ elif kind == "ascii" and len(run) >= 4:
+ tokens.add(run)
+ run, kind = "", current
+ if current:
+ run += char
+ return tokens
+
+
+def _strip_sign(node: ast.AST) -> ast.AST:
+ """The operand under any unary ``+`` or ``-``."""
+ while isinstance(node, ast.UnaryOp):
+ node = node.operand
+ return node
+
+
+def _is_sum(node: ast.AST) -> bool:
+ """Whether a node is a binary ``+`` or ``-``."""
+ return isinstance(node, ast.BinOp) and isinstance(node.op, (ast.Add, ast.Sub))
+
+
+def _is_unit_factor(node: ast.AST) -> bool:
+ """Whether a sum is ``1 ± c ± …`` over constants: a percentage change as a factor."""
+ node = _strip_sign(node)
+ if not _is_sum(node):
+ return False
+ while _is_sum(node):
+ if not isinstance(_strip_sign(node.right), ast.Constant):
+ return False
+ node = _strip_sign(node.left)
+ return isinstance(node, ast.Constant) and node.value == 1
+
+
+def _unanchored_term(tree: ast.Expression, observed: Callable[[float], bool]) -> bool:
+ """Whether a formula adds or subtracts a term holding no observed operand.
+
+ Multiplicative constants are free, so two forms are not offsets: ``1 ± c``
+ used as a factor ("0.666 × (1 − 0.03)") and the 1 beside a quotient
+ ("0.666 / 1.053 − 1"). Neither reaches a value a free multiplier could not.
+ Both are recognised by structure: near a price of 1 the constant 1 is itself
+ within tolerance of an observation.
+
+ Args:
+ tree: The parsed formula.
+ observed: Whether an operand is a value this session observed.
+
+ Returns:
+ True when some added or subtracted term is unanchored.
+ """
+
+ def anchored(node: ast.AST) -> bool:
+ return any(
+ observed(float(item.value))
+ for item in ast.walk(node)
+ if isinstance(item, ast.Constant) and _is_number(item.value)
+ )
+
+ def visit(node: ast.AST, factor: bool) -> bool:
+ node = _strip_sign(node)
+ if not isinstance(node, ast.BinOp):
+ return False
+ if not _is_sum(node):
+ return visit(node.left, True) or visit(node.right, True)
+ if factor and _is_unit_factor(node):
+ return False
+ for side, other in ((node.left, node.right), (node.right, node.left)):
+ side, other = _strip_sign(side), _strip_sign(other)
+ unit_beside_ratio = (
+ isinstance(side, ast.Constant)
+ and side.value == 1
+ and isinstance(other, ast.BinOp)
+ and isinstance(other.op, ast.Div)
+ )
+ if not _is_sum(side) and not unit_beside_ratio and not anchored(side):
+ return True
+ return visit(node.left, False) or visit(node.right, False)
+
+ return visit(tree.body, False)
+
+
+def _evaluate_formula(expression: str) -> tuple[float, list[float], ast.Expression] | None:
"""Evaluate a numeric ``+ - * /`` expression without executing code.
Args:
expression: An arithmetic run, possibly using ``× ÷ −`` and commas.
Returns:
- ``(result, operands)``, or None when the run is not a well-formed
- expression over at least two numeric operands.
+ ``(result, operands, parsed tree)``, or None when the run is not a
+ well-formed expression over at least two numeric operands.
"""
normalized = (
expression.replace("×", "*")
@@ -189,10 +315,10 @@ def _evaluate_formula(expression: str) -> tuple[float, list[float]] | None:
return None
if len(inputs) < 2 or not math.isfinite(value):
return None
- return value, inputs
+ return value, inputs, tree
-def _formula_in_note(note: str) -> tuple[float, list[float]] | None:
+def _formula_in_note(note: str) -> tuple[float, list[float], ast.Expression] | None:
"""Find the derivation a note states.
The whole note is tried first, then each segment between result separators
@@ -202,7 +328,7 @@ def _formula_in_note(note: str) -> tuple[float, list[float]] | None:
note: The declaration's free-text note.
Returns:
- ``(result, operands)`` for the first parseable segment, or None.
+ ``(result, operands, parsed tree)`` for the first parseable segment, or None.
"""
candidates = [note]
parts = [note]
@@ -322,28 +448,73 @@ class _PolicyMixin:
if figure.shape != "measured":
continue
declaration = block.match(figure.value, figure.percent)
- if block.present and declaration is None:
+ symbol = self._figure_symbol(
+ content, figure, declaration, line_symbols, document_symbol, records
+ )
+ if declaration is not None:
+ written = self._written_symbol(content, figure, line_symbols, records)
+ if written and symbol and written != symbol:
+ # A declaration names where a number came from; it cannot
+ # move a figure the sentence attaches to another instrument.
+ issues.append(
+ self._figure_issue(
+ "numeric_claim_conflict",
+ figure,
+ declaration.role,
+ symbol,
+ "symbol_mismatch",
+ f"is declared for {symbol} but the answer writes it about {written}",
+ )
+ )
+ continue
+ if declaration is None:
+ found = self._check_observed(figure, None, symbol, records)
+ if block.present and found:
+ issues.append(
+ self._figure_issue(
+ "figure_undeclared",
+ figure,
+ None,
+ symbol,
+ "undeclared",
+ "is not declared in the figures block and is not an observed "
+ "value; declare it as observed / derived / proposed / cited / "
+ "count, or remove it",
+ )
+ )
+ continue
+ checked_price = True
+ issues.extend(found)
+ continue
+ role = declaration.role
+ if figure.column and role != "observed":
issues.append(
self._figure_issue(
- "figure_undeclared",
+ "numeric_claim_conflict",
figure,
- None,
- None,
- "undeclared",
- "is not declared in the figures block; declare it as "
- "observed / derived / proposed / cited / count, or remove it",
+ role,
+ symbol,
+ "cited_as_observed" if role == "cited" else "role_in_price_column",
+ f"is declared {role}, but it sits in the {figure.column} column, "
+ "which holds observed prints only",
)
)
continue
- role = declaration.role if declaration else "observed"
- symbol = self._figure_symbol(
- content, figure, declaration, line_symbols, document_symbol, records
- )
if role == "count":
+ if not _is_plain_count(figure):
+ checked_price = True
+ issues.extend(
+ self._count_as_observed(figure, declaration, symbol, records)
+ )
continue
if role == "cited":
+ line = (
+ positions[figure.line][0]
+ if 0 <= figure.line < len(positions)
+ else ""
+ )
issues.extend(
- self._check_cited(figure, declaration, symbol, declared_observed)
+ self._check_cited(figure, declaration, symbol, declared_observed, line)
)
continue
checked_price = True
@@ -409,6 +580,20 @@ class _PolicyMixin:
)
if declared:
return declared
+ return self._written_symbol(content, figure, line_symbols, records) or document_symbol
+
+ def _written_symbol(
+ self,
+ content: str,
+ figure: Figure,
+ line_symbols: Sequence[str | None],
+ records: Sequence[EvidenceRecord],
+ ) -> str | None:
+ """The instrument the answer's own text attaches a figure to, if one.
+
+ The table row's symbol column, then the figure's punctuation segment,
+ then its line; the whole-answer fallback is left to the caller.
+ """
if figure.symbol:
normalized = _normalize_symbol(figure.symbol)
if normalized:
@@ -419,30 +604,63 @@ class _PolicyMixin:
return segment_symbol
if 0 <= figure.line < len(line_symbols) and line_symbols[figure.line]:
return line_symbols[figure.line]
- return document_symbol
+ return None
+
+ def _referenced(
+ self,
+ ref: str,
+ symbol: str | None,
+ figure: Figure | None,
+ ) -> tuple[list[EvidenceRecord], list[float]] | None:
+ """The evidence one call, or every call of one tool, produced.
- def _referenced_values(self, ref: str) -> list[float] | None:
- """Every observed value one tool call produced, or None when unknown.
+ A ``ref`` naming a call id or a tool name is the tightest scoping, and the
+ only one that can ground a non-price figure (revenue, IC, volume). Records
+ of another symbol are dropped when the figure's symbol is known; a
+ currency-marked figure keeps only money-denominated records, a percent
+ only the others, less metadata counts.
- A ``ref`` naming a call id is the tightest scoping, and the only one that
- can ground a non-price figure (revenue, IC, volume).
+ Args:
+ ref: The declaration's ``ref``.
+ symbol: The figure's resolved symbol, or None.
+ figure: The figure whose shape narrows the kind, or None for the
+ operands of a derivation.
+
+ Returns:
+ ``(records, metric values)``, or None when ``ref`` names no call or tool.
"""
key = (ref or "").strip()
if not key:
return None
- values = [
- float(record.value)
+ records = [
+ record
for record in self._evidence
- if record.call_id == key
+ if key in (record.call_id, record.tool)
and record.status == "observed"
and record.value is not None
]
- values.extend(
+ metrics = [
float(entry["value"])
for entry in self._analysis_metrics
- if entry.get("call_id") == key and entry.get("value") is not None
- )
- return values or None
+ if key in (entry.get("call_id"), entry.get("tool"))
+ and entry.get("value") is not None
+ ]
+ if not records and not metrics:
+ return None
+ if symbol:
+ records = [
+ record for record in records if not record.symbol or record.symbol == symbol
+ ]
+ if figure is not None and figure.percent:
+ records = [
+ record
+ for record in records
+ if not _is_price_kind(record) and not _is_metadata_count_leaf(record.field)
+ ]
+ elif figure is not None and figure.currency:
+ records = [record for record in records if _is_price_kind(record)]
+ metrics = []
+ return records, metrics
def _price_pool(
self,
@@ -452,7 +670,21 @@ class _PolicyMixin:
column: str | None = None,
date: str | None = None,
) -> list[float]:
- """Observed price values a figure may be compared against.
+ """Observed price values a figure may be compared against."""
+ return [
+ float(record.value)
+ for record in self._price_candidates(symbol, records, column=column, date=date)
+ ]
+
+ def _price_candidates(
+ self,
+ symbol: str | None,
+ records: Sequence[EvidenceRecord],
+ *,
+ column: str | None = None,
+ date: str | None = None,
+ ) -> list[EvidenceRecord]:
+ """Observed price records a figure may be compared against.
Filtered by symbol, then by OHLC field and trade date when the figure sits
under those table headers (spec §4).
@@ -473,13 +705,14 @@ class _PolicyMixin:
if record.timestamp
and _timestamp_matches_claim_date(record.timestamp, date)
]
- return [float(record.value) for record in candidates if record.value is not None]
+ return [record for record in candidates if record.value is not None]
- def _row_pool(self, symbol: str | None) -> list[float]:
- """Non-price numbers a market-data row carried (volume, turnover, …).
+ def _row_pool(self, symbol: str | None, *, money_only: bool = False) -> list[float]:
+ """Numbers a market-data row carried (volume, amount, turnover, …).
Only ``get_market_data`` and run-dir CSV rows count, so a generic tool's
- numeric leaves never widen the check.
+ numeric leaves never widen the check. ``money_only`` keeps the
+ money-denominated fields a currency-marked figure may quote.
"""
return [
float(record.value)
@@ -488,8 +721,59 @@ class _PolicyMixin:
and record.value is not None
and record.tool in {"get_market_data", "bash"}
and (not symbol or record.symbol == symbol)
+ and (not money_only or _is_price_kind(record))
]
+ @staticmethod
+ def _nearest_prints(
+ figure: Figure,
+ scope: Sequence[EvidenceRecord],
+ fallback: Sequence[float],
+ ) -> list[float]:
+ """The observed values a rejected figure is pointed at (#1433).
+
+ A non-percent figure is pointed at its own table column, else at the
+ closes (then last or adjusted prices) in scope, never at every field of
+ every bar; a percent at the values it was compared with.
+
+ Args:
+ figure: The rejected figure.
+ scope: The evidence records its check was scoped to.
+ fallback: The values its check compared it against.
+
+ Returns:
+ Up to three observed values, closest first.
+ """
+ if not figure.percent:
+ for name in (figure.column,) if figure.column else _CITABLE_FIELDS:
+ values = [
+ float(record.value)
+ for record in scope
+ if record.value is not None
+ and (_price_field_for_path(record.field) or record.field) == name
+ ]
+ if values:
+ return _nearest(figure.value, values)
+ return _nearest(figure.value, fallback)
+
+ def _count_as_observed(
+ self,
+ figure: Figure,
+ declaration: Declaration,
+ symbol: str | None,
+ records: Sequence[EvidenceRecord],
+ ) -> list[dict[str, Any]]:
+ """Check a ``count`` that carries a measurement as the observation it is."""
+ found = self._check_observed(figure, declaration, symbol, records)
+ for issue in found:
+ issue["role"] = "count"
+ issue["message"] = (
+ f"{figure.text} is declared count, but a count cannot carry a currency "
+ "mark, so it was checked as observed: "
+ + issue["message"][len(figure.text) + 1 :]
+ )
+ return found
+
def _metric_pool(self, symbol: str | None) -> list[float]:
"""Metric values from completed analysis results and metric-named leaves."""
values = [
@@ -533,12 +817,17 @@ class _PolicyMixin:
symbol: str | None,
records: Sequence[EvidenceRecord],
) -> list[dict[str, Any]]:
- """An observed figure must appear in the evidence it claims to quote."""
- referenced = (
- self._referenced_values(declaration.ref) if declaration else None
- )
- if referenced is not None:
- if self._matches_evidence(figure, referenced, referenced):
+ """An observed figure must appear in evidence of its own kind.
+
+ A currency-marked figure is answered only by money-denominated values
+ and a percent only by the rest; a table cell only by its column's field.
+ """
+ scoped = self._referenced(declaration.ref, symbol, figure) if declaration else None
+ if scoped is not None:
+ scoped_records, metric_values = scoped
+ values = [float(record.value) for record in scoped_records] + metric_values
+ money = figure.currency and not figure.percent
+ if self._matches_evidence(figure, values, [] if money else values):
return []
return [
self._figure_issue(
@@ -547,23 +836,26 @@ class _PolicyMixin:
"observed",
symbol,
"not_in_referenced_call",
- f"is declared observed from {declaration.ref}, whose results do "
- "not contain it",
+ f"is declared observed from {declaration.ref}, whose results "
+ f"{'for ' + symbol + ' ' if symbol else ''}do not contain it",
source_tool_call_ids=[declaration.ref],
- observed_nearest=_nearest(figure.value, referenced),
+ observed_nearest=self._nearest_prints(figure, scoped_records, values),
)
]
- prices = self._price_pool(
+ candidates = self._price_candidates(
symbol, records, column=figure.column, date=figure.date
)
+ prices = [float(record.value) for record in candidates]
if figure.percent:
# A percent is a ratio; no price or volume may answer it.
direct: list[float] = []
+ scaled = [] if figure.column else self._metric_pool(symbol)
elif figure.column:
- direct = list(prices)
+ direct, scaled = prices, []
+ elif figure.currency:
+ direct, scaled = prices + self._row_pool(symbol, money_only=True), []
else:
- direct = list(prices) + self._row_pool(symbol)
- scaled = [] if figure.column else self._metric_pool(symbol)
+ direct, scaled = prices + self._row_pool(symbol), self._metric_pool(symbol)
if not direct and not scaled:
return [
self._figure_issue(
@@ -581,6 +873,9 @@ class _PolicyMixin:
if self._matches_evidence(figure, direct, scaled):
return []
observed = sorted(direct or scaled)
+ attributable = symbol is not None or len(
+ {record.symbol for record in records if record.symbol}
+ ) <= 1
return [
self._figure_issue(
"numeric_claim_conflict",
@@ -595,7 +890,11 @@ class _PolicyMixin:
date=figure.date,
observed_min=observed[0],
observed_max=observed[-1],
- observed_nearest=_nearest(figure.value, observed),
+ observed_nearest=(
+ self._nearest_prints(figure, candidates, observed)
+ if attributable
+ else []
+ ),
)
]
@@ -604,11 +903,14 @@ class _PolicyMixin:
declaration: Declaration | None,
symbol: str | None,
records: Sequence[EvidenceRecord],
+ *,
+ money: bool = False,
) -> tuple[float, list[float]] | str | None:
"""Evaluate a declaration's note as an observation-anchored formula.
Returns the ``(result, operands)`` pair when the note is arithmetic
- over at least two operands, at least one of which the run observed;
+ over at least two operands, at least one of which the run observed and
+ every added or subtracted term of which holds an observed operand;
otherwise the reason it is not.
"""
if declaration is None or not declaration.note.strip():
@@ -616,43 +918,57 @@ class _PolicyMixin:
evaluated = _formula_in_note(declaration.note)
if evaluated is None:
return "formula_not_evaluable"
- result, operands = evaluated
+ result, operands, tree = evaluated
if not symbol and len({record.symbol for record in records if record.symbol}) > 1:
# Two instruments' bars and no resolved symbol: any arithmetic would
# look anchored, with nothing to anchor it to.
return "no_symbol"
- anchors = (
- self._price_pool(symbol, records)
- + self._row_pool(symbol)
- + self._metric_pool(symbol)
- )
- referenced = self._referenced_values(declaration.ref)
- if referenced:
- anchors.extend(referenced)
+ # A money-marked result is derived from money: an RSI or a volume is not
+ # a price to take a discount of.
+ anchors = self._price_pool(symbol, records) + self._row_pool(symbol, money_only=money)
+ if not money:
+ anchors += self._metric_pool(symbol)
+ scoped = self._referenced(declaration.ref, symbol, None)
+ if scoped is not None:
+ anchors.extend(
+ float(record.value)
+ for record in scoped[0]
+ if not money or _is_price_kind(record)
+ )
+ if not money:
+ anchors.extend(scoped[1])
if not anchors:
return "no_evidence"
- if not any(_close_any(operand, anchors) for operand in operands):
+
+ def observed(operand: float) -> bool:
+ return _close_any(operand, anchors)
+
+ if not any(observed(operand) for operand in operands):
return "formula_not_anchored"
+ if _unanchored_term(tree, observed):
+ return "additive_operand_not_observed"
return result, operands
@staticmethod
- def _result_matches(declaration: Declaration, result: float) -> bool:
- """Whether a formula's result is the value the declaration states.
-
- The band is half a unit of the last written digit ("约 37%" for 36.75%
- asserts rounding). A "%" figure is compared only in percentage points,
- since against the fraction a half-unit band spans fifty points; a bare
- figure is tried both ways. Magnitudes are compared because a fall is
- noted either as ``(low − high) / high`` or as the drop.
+ def _result_matches(figure: Figure, result: float) -> bool:
+ """Whether a formula's result is the value the prose figure states.
+
+ The band is half a unit of the last digit the PROSE was written with
+ ("约 37%" for 36.75%), so a coarser declaration cannot widen it. A "%"
+ figure is compared only in percentage points, since against the fraction
+ a half-unit band spans fifty points; a bare figure is tried both ways.
+ Magnitudes are compared, because a fall is noted either as
+ ``(low − high) / high`` or as the drop, unless the prose wrote a sign.
"""
- half_unit = _written_half_unit(declaration.value_text)
- targets = (
- {result * 100.0} if declaration.percent else {result, result * 100.0}
- )
- value = abs(declaration.value)
+ # The normalized reading, so "0,666" is three decimals and "−5,13%" is signed.
+ half_unit = _written_half_unit(figure.digits or figure.text)
+ targets = {result * 100.0} if figure.percent else {result, result * 100.0}
+ sign = _explicit_sign(figure.sign or figure.text)
+ value = abs(figure.value)
return any(
abs(value - abs(target)) <= max(abs(target) * _TOLERANCE, half_unit, 1e-9)
for target in targets
+ if not sign or target * sign >= 0
)
def _check_derived(
@@ -663,7 +979,7 @@ class _PolicyMixin:
records: Sequence[EvidenceRecord],
) -> list[dict[str, Any]]:
"""A derived figure must be the arithmetic its note states."""
- derivation = self._derivation(declaration, symbol, records)
+ derivation = self._derivation(declaration, symbol, records, money=figure.currency)
if isinstance(derivation, str):
return [
self._figure_issue(
@@ -674,12 +990,15 @@ class _PolicyMixin:
"derived",
symbol,
derivation,
- "is declared derived, but its note is not arithmetic over at "
+ "is declared derived, but its note adds or subtracts an operand "
+ "this session did not observe"
+ if derivation == "additive_operand_not_observed"
+ else "is declared derived, but its note is not arithmetic over at "
"least two operands with one of them observed in this session",
)
]
result, _ = derivation
- if declaration is not None and self._result_matches(declaration, result):
+ if self._result_matches(figure, result):
return []
# Reported in the figure's own units, as ``_result_matches`` compares it.
scaled = result * 100.0 if figure.percent else result
@@ -707,15 +1026,38 @@ class _PolicyMixin:
It cannot be required to equal a print, only to be anchored; a level far
outside what the session saw is the invention this gate exists to stop.
+ A level is a price, so a percent is never one, and a level attributed to
+ no instrument of several has no range to lie in.
"""
- derivation = self._derivation(declaration, symbol, records)
- if (
- not isinstance(derivation, str)
- and declaration is not None
- and self._result_matches(declaration, derivation[0])
- ):
+ if figure.percent:
+ return [
+ self._figure_issue(
+ "numeric_claim_conflict",
+ figure,
+ "proposed",
+ symbol,
+ "proposed_not_a_price",
+ "is declared proposed, but a proposed level is a price and a percent "
+ "is not one; declare it derived with its arithmetic, or cited",
+ )
+ ]
+ if not symbol and len({record.symbol for record in records if record.symbol}) > 1:
+ return [
+ self._figure_issue(
+ "numeric_claim_conflict",
+ figure,
+ "proposed",
+ symbol,
+ "no_symbol",
+ "is a proposed level, but the run holds prices for more than one "
+ "instrument and nothing attributes it to one",
+ )
+ ]
+ derivation = self._derivation(declaration, symbol, records, money=figure.currency)
+ if not isinstance(derivation, str) and self._result_matches(figure, derivation[0]):
return []
- prices = self._price_pool(symbol, records)
+ candidates = self._price_candidates(symbol, records)
+ prices = [float(record.value) for record in candidates]
if not prices:
return [
self._figure_issue(
@@ -728,7 +1070,7 @@ class _PolicyMixin:
"the instrument to anchor it to",
)
]
- if not figure.percent and min(prices) <= figure.value <= max(prices):
+ if min(prices) <= figure.value <= max(prices):
return []
return [
self._figure_issue(
@@ -741,23 +1083,26 @@ class _PolicyMixin:
f"{min(prices):g}–{max(prices):g} and its note derives no value",
observed_min=min(prices),
observed_max=max(prices),
- observed_nearest=_nearest(figure.value, prices),
+ observed_nearest=self._nearest_prints(figure, candidates, prices),
)
]
def _check_cited(
self,
figure: Figure,
- declaration: Declaration | None,
+ declaration: Declaration,
symbol: str | None,
declared_observed: set[float],
+ line: str,
) -> list[dict[str, Any]]:
- """A cited figure names its source and does not pose as a print.
+ """A cited figure names a source the reader can see and does not pose as a print.
- Its value is unchecked, so the citation may not launder an observation:
- the value may not also be declared observed or sit in an OHLC column.
+ Its value is unchecked, so the citation may not launder an observation
+ (the value may not also be declared observed). The block is stripped
+ before release, so a source only the note names is no citation: a note
+ token (``_note_tokens``) must appear on the figure's own line.
"""
- if declaration is not None and not declaration.note.strip():
+ if not declaration.note.strip():
return [
self._figure_issue(
"numeric_claim_conflict",
@@ -768,9 +1113,7 @@ class _PolicyMixin:
"is declared cited but names no source in its note",
)
]
- if figure.column or any(
- _close(figure.value, value) for value in declared_observed
- ):
+ if any(_close(figure.value, value) for value in declared_observed):
return [
self._figure_issue(
"numeric_claim_conflict",
@@ -782,6 +1125,19 @@ class _PolicyMixin:
"instrument",
)
]
+ folded = line.casefold()
+ if not any(token in folded for token in _note_tokens(declaration.note)):
+ return [
+ self._figure_issue(
+ "numeric_claim_conflict",
+ figure,
+ "cited",
+ symbol,
+ "citation_not_visible",
+ "is declared cited, but no source its note names appears on its "
+ "line, and the note is stripped before anyone reads the answer",
+ )
+ ]
return []
def _validate_unsourced_symbols(
diff --git a/agent/src/agent/grounding/release.py b/agent/src/agent/grounding/release.py
index 65c12ccdd..1149b328e 100644
--- a/agent/src/agent/grounding/release.py
+++ b/agent/src/agent/grounding/release.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import hashlib
import re
-from typing import Any, Sequence
+from typing import Any, Iterable, Sequence
from src.agent.grounding.identity import (
_CANONICAL_SYMBOL_RE,
@@ -14,12 +14,12 @@ from src.agent.grounding.identity import (
)
from src.agent.grounding.evidence import EvidenceRecord
from src.agent.grounding.figures import (
+ BLOCK_LANGUAGE,
+ Figure,
_lines_with_offsets,
- currency_prefix_start,
- currency_suffix_end,
- magnitude_suffix,
parse_figures_block,
scan_figures,
+ strip_figures_block,
)
from src.agent.grounding.policies import ValidationResult
@@ -83,6 +83,11 @@ _CORRECTION_REASONS = {
"cited_as_observed": "it is presented as an observed value of this instrument",
"unparseable_declaration": "the declaration line could not be read as `value | role | note | ref`",
"symbol_never_handled": "no tool call in this session passed in or returned that symbol",
+ "additive_operand_not_observed": "a number its note adds or subtracts is not a value this session observed",
+ "role_in_price_column": "it sits in a price column, which holds only observed prints",
+ "proposed_not_a_price": "a proposed level is a price, not a percentage; state the price it implies",
+ "symbol_mismatch": "it is declared for one instrument but the sentence writes it about another",
+ "citation_not_visible": "its source is not named on the figure's own line, and the note is stripped before the user reads the answer",
}
@@ -121,22 +126,6 @@ def _correction_line(issue: dict[str, Any]) -> str:
return f"{subject} | {declared} | {evidence}"
-def _digit_key(text: str) -> str:
- """The digits a figure was written with, ignoring decoration.
-
- ``37%``, ``37 %`` and ``37`` share a key; ``3`` and ``3.0`` do not, so a
- sweep never cuts an unrelated count.
-
- Args:
- text: The figure exactly as it appears in the answer.
-
- Returns:
- The comparable digit string, or "" when there is none.
- """
- body = text.strip().rstrip("%\uff05").strip().lstrip("+-")
- return body.replace(",", "").strip()
-
-
def _strip_release_markers(content: str) -> str:
"""Remove redaction markers and ※ note lines the model wrote itself.
@@ -476,10 +465,15 @@ class _ReleaseMixin:
"""Release the last rejected draft with its unverified figures cut out.
Each rejected figure becomes a marker at its own span, missing provenance
- is appended, bare restatements of a cut figure are swept, and the result
- and its footnote are re-validated by the same gate. Fail-closed: None when
- no price was observed, an issue cannot be cut (an identity finding), a
- flagged figure cannot be located, or the text still fails.
+ is appended, unchecked restatements of a cut figure (bare, or inside a
+ code fence, compared by normalized digits) are swept, and the result and
+ its footnote are re-validated by the same gate. A block with an
+ unreadable declaration line is dropped first, so every measured figure is
+ checked as observed; if that alone passes, nothing is footnoted.
+ Fail-closed: None when a market answer observed no price, an issue cannot
+ be cut (an identity finding), a flagged figure cannot be located, or the
+ text still fails. A general answer with no tool evidence is released cut,
+ with a footnote that names no range.
Args:
content: The rejected draft.
@@ -489,12 +483,22 @@ class _ReleaseMixin:
The redacted, re-validated answer without its declaration block and
with a note stating how many figures were removed, or None.
"""
- if not self._price_records():
+ # A market answer with no observed price has nothing to stand on once its
+ # figures are cut; a general answer (no instrument asked about) does.
+ if self._identity_required and not self._price_records():
return None
text = _strip_release_markers(content)
# Stripping shifts offsets and the cuts anchor on issue spans, so the
# verdict is retaken on the stripped text.
check = validation if text == content else self._validate(text, record=False)
+ # An unreadable declaration cannot be cut, but the block is not answer
+ # text either: without it the draft is checked in undeclared mode.
+ dropped_block = any(
+ issue.get("code") == "figures_block_malformed" for issue in check.issues
+ )
+ if dropped_block:
+ text = strip_figures_block(text, parse_figures_block(text))
+ check = self._validate(text, record=False)
removed = 0
keys: set[str] = set()
for _ in range(_MAX_REDACTION_PASSES):
@@ -503,39 +507,41 @@ class _ReleaseMixin:
codes = {issue.get("code") for issue in check.issues}
if not codes <= (_REDACTABLE_CODES | _REPAIRABLE_PROVENANCE_CODES):
return None
- cut, cut_texts = self._cut_flagged(text, check.issues)
+ cut, cut_keys = self._cut_flagged(text, check.issues)
if cut is None:
return None
- if cut_texts:
+ if cut_keys:
text = cut
- removed += len(cut_texts)
- keys.update(_digit_key(item) for item in cut_texts)
+ removed += len(cut_keys)
+ keys.update(cut_keys)
check = self._validate(text, record=False)
if check.valid:
break
repaired = self.repair_provenance(text, check)
if repaired is None:
- if not cut_texts:
+ if not cut_keys:
return None
continue
text = repaired
check = self._validate(text, record=False)
- if not check.valid or removed == 0:
+ if not check.valid or (removed == 0 and not dropped_block):
return None
# A cut figure can still be standing where the shape rules never look.
- swept, swept_texts = self._sweep_same_values(text, keys - {""})
- if swept_texts:
+ swept, swept_keys = self._sweep_same_values(text, keys - {""})
+ if swept_keys:
recheck = self._validate(swept, record=False)
if not recheck.valid:
return None
- text, check, removed = swept, recheck, removed + len(swept_texts)
+ text, check, removed = swept, recheck, removed + len(swept_keys)
body = check.released_text
- note = self._release_note(removed, body)
- # The note is answer text too (range, symbols), so it passes the same
- # gate, in undeclared mode.
- if not self._validate(note, record=False).valid:
- return None
- released = body.rstrip() + "\n\n" + note
+ released = body
+ if removed:
+ note = self._release_note(removed, body)
+ # The note is answer text too (range, symbols), so it passes the
+ # same gate, in undeclared mode.
+ if not self._validate(note, record=False).valid:
+ return None
+ released = body.rstrip() + "\n\n" + note
# Recorded beside the drafts, not as one: every recheck here is
# unrecorded, so this is the artifact's only evidence of the release.
self._released = {
@@ -580,19 +586,21 @@ class _ReleaseMixin:
"""Replace every flagged figure with the omission marker.
An issue without a value (``unsourced_symbol_figures``) cuts every
- measured figure inside its span.
+ measured figure inside its span; any other issue must name the span of
+ a figure the scan located.
Args:
text: The document to rewrite.
issues: The issues raised against it.
Returns:
- ``(rewritten text, figures replaced as written)``, or ``(None, [])``
- when a flagged issue cannot be located and release must fail closed.
+ ``(rewritten text, normalized digits of each figure cut)``, or
+ ``(None, [])`` when a flagged figure cannot be located and release
+ must fail closed.
"""
- block = parse_figures_block(text)
- figures = scan_figures(text, block)
- cuts: set[tuple[int, int]] = set()
+ figures = scan_figures(text, parse_figures_block(text))
+ located = {(figure.start, figure.end): figure for figure in figures}
+ cuts: dict[tuple[int, int], Figure] = {}
for issue in issues:
if issue.get("code") not in _REDACTABLE_CODES:
continue
@@ -601,66 +609,70 @@ class _ReleaseMixin:
return None, []
if issue.get("value") is None:
cuts.update(
- (figure.start, figure.end)
+ ((figure.start, figure.end), figure)
for figure in figures
if figure.shape == "measured"
and span[0] <= figure.start
and figure.end <= span[1]
)
+ elif span in located:
+ cuts[span] = located[span]
else:
- cuts.add(span)
- return self._apply_cuts(text, cuts)
+ return None, []
+ return self._apply_cuts(text, cuts.values())
def _sweep_same_values(self, text: str, keys: set[str]) -> tuple[str, list[str]]:
- """Cut every bare-integer restatement of a figure that was already cut.
+ """Cut every unchecked restatement of a figure that was already cut.
After "回撤 37%" is cut, "回撤 37 个百分点" still carries an unchecked bare
- "37". Measured survivors were checked and grounded, and exempt ones are
- structure (dates, codes, fenced text), so only bare figures are swept.
+ "37", and a code fence can still print it. Figures compare by normalized
+ digits, so an escaped spelling is the same restatement. Measured prose
+ survivors were checked and grounded, and the figures block is not
+ answer text, so neither is swept.
Args:
text: The already-cut document.
- keys: Digit keys of the figures that were cut.
+ keys: Normalized digits of the figures that were cut.
Returns:
- ``(rewritten text, figures swept as written)``.
+ ``(rewritten text, normalized digits of each figure swept)``.
"""
if not keys:
return text, []
- block = parse_figures_block(text)
- cuts = {
- (figure.start, figure.end)
- for figure in scan_figures(text, block)
- if figure.shape == "bare" and _digit_key(figure.text) in keys
- }
- swept, cut_texts = self._apply_cuts(text, cuts)
- return (text, []) if swept is None else (swept, cut_texts)
+ stale = [
+ figure
+ for figure in scan_figures(text, parse_figures_block(text))
+ if figure.digits in keys
+ and (
+ figure.shape == "bare"
+ or (figure.fence is not None and figure.fence != BLOCK_LANGUAGE)
+ )
+ ]
+ return self._apply_cuts(text, stale)
def _apply_cuts(
self,
text: str,
- cuts: set[tuple[int, int]],
- ) -> tuple[str | None, list[str]]:
- """Replace each span with the omission marker in the script it sits in.
+ figures: Iterable[Figure],
+ ) -> tuple[str, list[str]]:
+ """Replace each figure and its glued marks with the marker in its line's script.
Args:
text: The document to rewrite.
- cuts: Spans of the figures to remove.
+ figures: The figures to remove.
Returns:
- ``(rewritten text, the figures removed as written)``.
+ ``(rewritten text, normalized digits of each figure removed)``.
"""
- if not cuts:
- return text, []
pieces: list[str] = []
removed: list[str] = []
cursor = 0
- for start, end in sorted(cuts):
- if start < cursor:
+ for figure in sorted(figures, key=lambda item: (item.start, item.end)):
+ if figure.start < cursor:
continue
- removed.append(text[start:end])
- start = max(cursor, currency_prefix_start(text, start))
- end = currency_suffix_end(text, magnitude_suffix(text, end)[1])
+ removed.append(figure.digits)
+ start, end = figure.extent or (figure.start, figure.end)
+ start = max(cursor, start)
marker = self._marker_for(text, start)
if marker is _REDACTION_MARKER_ZH:
# "建议买入价 0.95 元" → "建议买入价(略※)": flush against the word,
@@ -701,6 +713,13 @@ class _ReleaseMixin:
"""Explain the redaction to the user, with the observed range."""
is_zh = self._user_writes_chinese()
joined = self._observed_range_summary(is_zh, content)
+ if joined is None:
+ if is_zh:
+ return f"※ 略去 {removed} 处无法与本会话工具数据对上的数值。"
+ return (
+ f"※ {removed} figure(s) that could not be matched to this session's "
+ "tool data were omitted."
+ )
if is_zh:
return (
f"※ 略去 {removed} 处无法与本会话工具数据对上的数值。"
diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.py
index 085ef2188..0659873f3 100644
--- a/agent/tests/test_agent_grounding.py
+++ b/agent/tests/test_agent_grounding.py
@@ -1749,7 +1749,7 @@ def test_reference_level_and_currency_thresholds_pass_end_to_end(tmp_path: Path)
result = ledger.validate_final_answer(
"SPCX.US 8/10 收 138.74 USD、8/12 高 149.60 USD(source: yahoo)。"
- "8/12 高 149.60 为 6/16 ATH 225.64 以来最高;"
+ "8/12 高 149.60 为 6/16 盘中历史高点 225.64 以来最高;"
"8/10 收盘 138.74 高于 $135.00 且高于 $119.68。\n\n"
"```figures\n"
"138.74 | observed | close 2026-08-10 | prices\n"
@@ -1801,7 +1801,7 @@ def test_validation_summary_with_counts_and_line_cites_passes_end_to_end(
result = ledger.validate_final_answer(
"SPCX.US 核验(USD, source: yahoo):❌ 1 项事实错误 — "
"文档 ~line 206「8/12 高 149.60 为 6/16 ATH 以来最高」不成立,"
- "6/17–7/10 有 16 个交易日高点高于 149.60,正确为 7/10(150.57)以来最高。\n\n"
+ "6/17–7/10 有 16 个交易日高点高于 149.60,正确为 7/10 盘中高点(150.57)以来最高。\n\n"
"```figures\n"
"149.60 | observed | high 2026-08-12 | prices\n"
"150.57 | cited | 7/10 盘中高点,本会话未取\n"
@@ -2871,7 +2871,7 @@ def test_analysis_definition_is_declared_not_phrased(tmp_path: Path) -> None:
ledger = GroundingLedger(run_dir=tmp_path, user_message="什么是夏普比率?")
declared = ledger.validate_final_answer(
- "夏普比率大于 1.0 通常被认为较好。\n\n"
+ "按行业惯例,夏普比率大于 1.0 被认为较好。\n\n"
"```figures\n1.0 | cited | 行业惯例阈值\n```"
)
assert declared.valid is True, declared.issues
@@ -3192,7 +3192,7 @@ def test_a_citation_subject_no_longer_exempts_anything(tmp_path: Path) -> None:
# NOT carry the sibling the same sentence invents.
ledger = GroundingLedger(run_dir=tmp_path, user_message="Research the factor.")
partial = ledger.validate_final_answer(
- "The paper reports a Sharpe ratio of 1.8, and our strategy achieved "
+ "The Fama-French paper reports a Sharpe ratio of 1.8, and our strategy achieved "
"an annualized return of 47.3%.\n\n"
"```figures\n1.8 | cited | Fama-French 2024, table 3\n```"
)
diff --git a/agent/tests/test_agent_output_discipline.py b/agent/tests/test_agent_output_discipline.py
index ffb103389..7b7fd0ef9 100644
--- a/agent/tests/test_agent_output_discipline.py
+++ b/agent/tests/test_agent_output_discipline.py
@@ -299,7 +299,7 @@ class TestTheGateDoesNotKillCorrectAnswers:
naming every subject by hand. The model declares the role instead.
"""
cited = _ledger(tmp_path).validate_final_answer(
- f"{_GROUNDED_ANSWER}\n600519.SH 年内涨约 12%。"
+ f"{_GROUNDED_ANSWER}\n据券商研报,600519.SH 年内涨约 12%。"
"\n\n```figures\n212.5 | observed | close | q1\n"
"12% | cited | 券商研报 2026-08\n```"
)
diff --git a/agent/tests/test_grounding_figures_contract.py b/agent/tests/test_grounding_figures_contract.py
index bbb2948e5..2f084d4be 100644
--- a/agent/tests/test_grounding_figures_contract.py
+++ b/agent/tests/test_grounding_figures_contract.py
@@ -505,7 +505,8 @@ def test_cited_needs_a_source_and_may_not_pose_as_a_print(tmp_path: Path) -> Non
ledger = _ledger(tmp_path)
with_source = ledger.validate_final_answer(
- HDR + " 论文报告夏普 1.8。" + _block(HDR_ROW, "1.8 | cited | Fama-French 2024 table 3")
+ HDR + " Fama-French(2024)论文报告夏普 1.8。"
+ + _block(HDR_ROW, "1.8 | cited | Fama-French 2024 table 3")
)
without_source = ledger.validate_final_answer(
HDR + " 论文报告夏普 1.8。" + _block(HDR_ROW, "1.8 | cited | ")
@@ -521,13 +522,17 @@ def test_cited_needs_a_source_and_may_not_pose_as_a_print(tmp_path: Path) -> Non
def test_count_is_not_checked_but_is_still_declared(tmp_path: Path) -> None:
- """``count`` is the escape hatch, and using it is a statement on the record."""
+ """``count`` covers a plain integer, and using it is a statement on the record.
+
+ A plain integer is measurement-shaped only inside a table. A count with a
+ decimal point, percent or currency mark is checked as observed instead
+ (``test_grounding_role_hardening``).
+ """
ledger = _ledger(tmp_path)
+ table = "\n\n| 持有期(月) |\n|---|\n| 3 |\n"
- declared = ledger.validate_final_answer(
- HDR + " 建议持有 3.5 个月。" + _block(HDR_ROW, "3.5 | count | 月")
- )
- undeclared = ledger.validate_final_answer(HDR + " 建议持有 3.5 个月。" + _block(HDR_ROW))
+ declared = ledger.validate_final_answer(HDR + table + _block(HDR_ROW, "3 | count | 月"))
+ undeclared = ledger.validate_final_answer(HDR + table + _block(HDR_ROW))
assert declared.valid is True, declared.issues
assert [issue["code"] for issue in undeclared.issues] == ["figure_undeclared"]
@@ -580,7 +585,8 @@ def test_the_correction_prompt_names_each_figure_and_its_reason(
assert "declared proposed" in line
assert "0.567" in line and "1.053" in line
- assert "nearest observed 1.053, 1.04, 1.02" in line
+ # The nearest values are the symbol's closes, not every field of every bar.
+ assert "nearest observed 1.04, 0.666" in line
assert "figures" in prompt
assert "DECLARE" in prompt and "REWRITE" in prompt and "REMOVE" in prompt
diff --git a/agent/tests/test_grounding_language_parity.py b/agent/tests/test_grounding_language_parity.py
index 7d79918c9..68907b7ad 100644
--- a/agent/tests/test_grounding_language_parity.py
+++ b/agent/tests/test_grounding_language_parity.py
@@ -65,14 +65,14 @@ _MUST_ACCEPT = [
# The same figure, declared: the declaration is what moves the verdict,
# and it moves both languages together.
(
- "The deal closed at a 30.5% premium.\n\n```figures\n"
+ "Per the merger announcement, the deal closed at a 30.5% premium.\n\n```figures\n"
"30.5% | cited | merger announcement 2026-08-03\n```",
- "交易以 30.5% 溢价成交。\n\n```figures\n"
+ "据并购公告,交易以 30.5% 溢价成交。\n\n```figures\n"
"30.5% | cited | 并购公告 2026-08-03\n```",
),
(
"Gross margin fell 3.6pp.\n\n```figures\n"
- "3.6 | cited | 2026 Q2 filing, gross margin bridge\n```",
+ "3.6pp | cited | 2026 Q2 filing, gross margin bridge\n```",
"毛利率下降 3.6 个百分点。\n\n```figures\n"
"3.6 | cited | 2026 Q2 财报毛利率桥\n```",
),
diff --git a/agent/tests/test_grounding_percentage_points.py b/agent/tests/test_grounding_percentage_points.py
index 1c038dcca..28ae83fff 100644
--- a/agent/tests/test_grounding_percentage_points.py
+++ b/agent/tests/test_grounding_percentage_points.py
@@ -89,14 +89,15 @@ def test_a_declared_delta_passes_in_every_spelling(text: str) -> None:
"""The upper arm: one declaration settles it, in either language.
Without this arm the file is satisfied by a gate that rejects everything,
- which is the failure mode the mask had in the other direction.
+ which is the failure mode the mask had in the other direction. The source
+ is written on the figure's line because the block never reaches the reader.
"""
figure = next(
figure.text
for figure in scan_figures(text, parse_figures_block(text))
if figure.shape == "measured"
)
- declared = text + (
+ declared = text + " (2026 Q2 filing)" + (
"\n\n```figures\n" + f"{figure} | cited | 2026 Q2 filing, margin bridge\n```"
)
diff --git a/agent/tests/test_grounding_release.py b/agent/tests/test_grounding_release.py
index 615255f1a..923e6b362 100644
--- a/agent/tests/test_grounding_release.py
+++ b/agent/tests/test_grounding_release.py
@@ -1181,7 +1181,7 @@ def test_redacted_release_never_cuts_inside_another_number(tmp_path: Path) -> No
draft = (
"562500.SS(Yahoo,CNY)最新收盘价 1.171 元。成交额约 21.10 亿元。"
"\n\n| 日期 | 收盘价 |\n|---|---|\n| 2026-06-24 | 1.10 |\n"
- + _block(HDR_ROW, "21.10 | count | 成交额(亿元)", "1.10 | observed | close | prices")
+ + _block(HDR_ROW, "21.10 | cited | 成交额(亿元)", "1.10 | observed | close | prices")
)
validation = ledger.validate_final_answer(draft)
assert [i["value"] for i in validation.issues] == ["1.10"]
diff --git a/agent/tests/test_grounding_role_hardening.py b/agent/tests/test_grounding_role_hardening.py
new file mode 100644
index 000000000..029097e28
--- /dev/null
+++ b/agent/tests/test_grounding_role_hardening.py
@@ -0,0 +1,643 @@
+"""Each declared role carries only what it can honestly carry.
+
+The figures block lets the model name a number's role, and every role was a way
+to smuggle a number past the evidence: a price declared ``count``, a source only
+the gate could see, an invented operand added to an observed one, a percent
+grounded by a close that happened to share its digits. Every rule here is
+pinned from both sides on a sparse ledger — two bars per symbol — so a probe
+value matches evidence only when it is meant to.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+from typing import Any
+
+import pytest
+
+from src.agent.grounding import GroundingLedger
+from src.agent.grounding.evidence import _metric_kind_for_path
+from src.agent.grounding.policies import _note_tokens
+
+pytestmark = pytest.mark.unit
+
+A = "159516.SZ"
+B = "600519.SH"
+C = "510300.SH"
+D = "600000.SH"
+
+_A_ROWS = [
+ {
+ "trade_date": "2026-05-06",
+ "open": 1.020,
+ "high": 1.053,
+ "low": 1.001,
+ "close": 1.040,
+ "volume": 123456,
+ "amount": 1234567,
+ },
+ {
+ "trade_date": "2026-09-09",
+ "open": 0.670,
+ "high": 0.681,
+ "low": 0.567,
+ "close": 0.666,
+ "volume": 234567,
+ },
+]
+
+_B_ROWS = [
+ {"trade_date": "2026-05-06", "open": 1400.0, "high": 1500.0, "low": 1390.0, "close": 1450.0, "volume": 1000},
+ {"trade_date": "2026-09-09", "open": 1420.0, "high": 1440.0, "low": 1395.0, "close": 1410.0, "volume": 2000},
+]
+
+
+def _bars(symbol: str, rows: list[dict[str, Any]]) -> str:
+ return json.dumps(
+ {symbol: rows, "_provenance": {symbol: {"source": "akshare", "currency_conversion": "none"}}}
+ )
+
+
+MARKET_A = ("get_market_data", {"codes": [A]}, _bars(A, _A_ROWS), "c1")
+MARKET_B = ("get_market_data", {"codes": [B]}, _bars(B, _B_ROWS), "c2")
+MARKET_C = (
+ "get_market_data",
+ {"codes": [C]},
+ _bars(C, [{"trade_date": "2026-09-09", "open": 3.9, "high": 3.95, "low": 3.88, "close": 3.912, "volume": 5}]),
+ "c3",
+)
+MARKET_D = (
+ "get_market_data",
+ {"codes": [D]},
+ _bars(D, [{"trade_date": "2026-09-09", "open": 17.8, "high": 18.2, "low": 17.7, "close": 18.0, "volume": 9}]),
+ "cd",
+)
+INDICATORS_A = (
+ "technical_indicators",
+ {"symbol": A},
+ {"ok": True, "symbol": A, "latest_close": 0.666, "indicators": {"rsi_14": 55.2, "sma_20": 0.700}},
+ "ind",
+)
+FACTOR_A = ("factor_analysis", {"symbol": A}, {"status": "ok", "sharpe": 0.888, "win_rate": 0.573}, "fa")
+
+HDR = f"{A}(akshare,CNY)最新收盘 0.666 元。"
+ROW = "0.666 | observed | close 2026-09-09 | c1"
+TWO = f"{A} 与 {B}(akshare,CNY)对比。\n"
+HDR_B = f"{B}(akshare,CNY)最新收盘 1410.00 元。"
+ROW_B = "1410.00 | observed | close 2026-09-09 | c2"
+
+
+def _ledger(tmp_path: Path, *calls: tuple[str, dict[str, Any], Any, str], message: str = "") -> GroundingLedger:
+ ledger = GroundingLedger(run_dir=tmp_path, user_message=message or f"分析 {A} 并给出买入价")
+ for tool, arguments, payload, call_id in calls:
+ ledger.ingest_tool_result(
+ tool_name=tool,
+ arguments=arguments,
+ result=payload if isinstance(payload, str) else json.dumps(payload),
+ call_id=call_id,
+ success=True,
+ )
+ return ledger
+
+
+def _block(*rows: str) -> str:
+ return "\n\n```figures\n" + "\n".join(rows) + "\n```"
+
+
+def _reasons(result: Any) -> list[str]:
+ return [str(issue.get("reason")) for issue in result.issues]
+
+
+# ---------------------------------------------------------------------------
+# B1 — count
+# ---------------------------------------------------------------------------
+
+
+ ("prose", "row"),
+ [
+ ("最新收盘价 9.99 元。", "9.99 | count | n"), # decimal and currency
+ ("成本 820 元。", "820 | count | n"), # integer with a currency mark
+ ("entry at $12.5 today.", "12.5 | count | n"), # currency symbol before
+ ],
+)
+def test_a_count_cannot_carry_a_currency_mark(tmp_path: Path, prose: str, row: str) -> None:
+ """A price or an amount declared count is checked as the observation it claims to be."""
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(HDR + "\n" + prose + _block(ROW, row))
+
+ assert result.valid is False
+ assert [issue["role"] for issue in result.issues] == ["count"]
+ assert "a count cannot carry a currency mark" in result.issues[0]["message"]
+
+
+ ("prose", "row"),
+ [
+ ("建议持有 3.5 个月。", "3.5 | count | 月"),
+ ("建议持有 3.0 个月。", "3.0 | count | 月"),
+ ("仓位上限 5%。", "5% | count | 仓位参数"),
+ ("折扣系数 0.97。", "0.97 | count | 系数"),
+ ("主观判断上涨概率 70%。", "70% | count | 主观判断"),
+ ],
+)
+def test_a_parameter_without_a_currency_mark_is_a_count(
+ tmp_path: Path, prose: str, row: str
+) -> None:
+ """A window, weight, threshold, multiplier or probability is the model's own choice."""
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(HDR + "\n" + prose + _block(ROW, row))
+
+ assert result.valid is True, result.issues
+
+
+def test_a_plain_integer_is_a_count(tmp_path: Path) -> None:
+ table = "\n\n| 持有期(月) | 成分股数 |\n|---|---|\n| 3 | 12 |\n"
+
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(
+ HDR + table + _block(ROW, "3 | count | 月", "12 | count | 只")
+ )
+
+ assert result.valid is True, result.issues
+
+
+def test_a_measurement_declared_count_is_checked_not_refused(tmp_path: Path) -> None:
+ """It is checked as the observation it is, so a real print still passes."""
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(
+ HDR + " 最高 0.681 元。" + _block(ROW, "0.681 | count | n")
+ )
+
+ assert result.valid is True, result.issues
+
+
+# ---------------------------------------------------------------------------
+# B2 — cited
+# ---------------------------------------------------------------------------
+
+
+def test_a_source_only_the_note_names_is_no_citation(tmp_path: Path) -> None:
+ """The block is stripped before release, so the reader must see the source."""
+ ledger = _ledger(tmp_path, MARKET_A)
+ row = "9.99 | cited | 新浪财经"
+
+ invisible = ledger.validate_final_answer(HDR + "\n最新收盘价 9.99 元。" + _block(ROW, row))
+ on_another_line = ledger.validate_final_answer(
+ HDR + "\n新浪财经另有报道。\n单位净值 9.99 元。" + _block(ROW, row)
+ )
+ digits_only = ledger.validate_final_answer(
+ HDR + "\n2024 年报单位净值 9.99 元。" + _block(ROW, "9.99 | cited | 2024")
+ )
+ visible = ledger.validate_final_answer(HDR + "\n新浪财经报道其单位净值 9.99 元。" + _block(ROW, row))
+ visible_english = ledger.validate_final_answer(
+ HDR + "\nFama-French (2024) report a Sharpe of 1.8."
+ + _block(ROW, "1.8 | cited | FAMA-FRENCH 2024 table 3")
+ )
+
+ assert _reasons(invisible) == ["citation_not_visible"]
+ assert _reasons(on_another_line) == ["citation_not_visible"]
+ assert _reasons(digits_only) == ["citation_not_visible"]
+ assert visible.valid is True, visible.issues
+ assert visible_english.valid is True, visible_english.issues
+
+
+def test_a_note_token_is_a_cjk_pair_or_a_four_letter_word() -> None:
+ assert _note_tokens("2026 Q2 财报毛利率, WSJ ok 新") == {"财报", "报毛", "毛利", "利率"}
+ assert _note_tokens("新浪 abc Reuters") == {"新浪", "reuters"}
+ assert _note_tokens("新 ab the 12") == set()
+
+
+# ---------------------------------------------------------------------------
+# B3 — derived
+# ---------------------------------------------------------------------------
+
+
+ ("prose", "row"),
+ [
+ ("第一档 0.888 元。", "0.888 | derived | 0.666 + 0.222 | c1"),
+ ("第一档 9.99 元。", "9.99 | derived | 9.99 + 0.666 − 0.666 | c1"),
+ ("第一档 0.888 元。", "0.888 | derived | (0.666 + 0.222) × 1 | c1"),
+ ("第一档 0.866 元。", "0.866 | derived | 0.1 × 2 + 0.666 | c1"),
+ ],
+)
+def test_an_added_operand_must_be_observed(tmp_path: Path, prose: str, row: str) -> None:
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(HDR + " " + prose + _block(ROW, row))
+
+ assert _reasons(result) == ["additive_operand_not_observed"]
+
+
+ ("prose", "row"),
+ [
+ ("第一档 0.646 元。", "0.646 | derived | 0.666 × 0.97 | c1"),
+ ("第一档 0.646 元。", "0.646 | derived | 0.666 × (1 − 0.03) | c1"),
+ ("较高点回撤 36.8%。", "36.8% | derived | (0.666 − 1.053) / 1.053 | c1"),
+ ("中枢 0.860 元。", "0.860 | derived | (0.666 + 1.053) / 2 | c1"),
+ ],
+)
+def test_observed_sums_and_free_multipliers_still_derive(tmp_path: Path, prose: str, row: str) -> None:
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(HDR + " " + prose + _block(ROW, row))
+
+ assert result.valid is True, result.issues
+
+
+ ("prose", "row"),
+ [
+ # Far from any print of 1, so only the structure can admit these.
+ ("第一档 1358.00 元。", "1358.00 | derived | 1400 × (1 − 0.03) | c2"),
+ ("较高点 -7.3%。", "-7.3% | derived | 1390 / 1500 − 1 | c2"),
+ ],
+)
+def test_a_percentage_factor_and_a_return_ratio_are_not_offsets(tmp_path: Path, prose: str, row: str) -> None:
+ result = _ledger(tmp_path, MARKET_B, message=f"分析 {B}").validate_final_answer(
+ HDR_B + " " + prose + _block(ROW_B, row)
+ )
+
+ assert result.valid is True, result.issues
+
+
+def test_a_derived_result_is_compared_at_the_prose_precision(tmp_path: Path) -> None:
+ """A declaration written coarser than the prose cannot widen the band."""
+ ledger = _ledger(tmp_path, MARKET_A)
+
+ coarse = ledger.validate_final_answer(HDR + " 目标价 1.00 元。" + _block(ROW, "1 | derived | 0.666 × 0.9 | c1"))
+ exact = ledger.validate_final_answer(HDR + " 目标价 0.60 元。" + _block(ROW, "0.6 | derived | 0.666 × 0.9 | c1"))
+
+ assert _reasons(coarse) == ["derivation_result_mismatch"]
+ assert exact.valid is True, exact.issues
+
+
+def test_an_explicit_sign_must_agree_with_the_formula(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A)
+ fall = "(0.666 − 1.053) / 1.053 | c1"
+
+ rise_claimed = ledger.validate_final_answer(HDR + " 较高点 +36.8%。" + _block(ROW, "36.8% | derived | " + fall))
+ fall_signed = ledger.validate_final_answer(HDR + " 较高点 -36.8%。" + _block(ROW, "-36.8% | derived | " + fall))
+ unsigned = ledger.validate_final_answer(HDR + " 较高点回撤 36.8%。" + _block(ROW, "36.8% | derived | " + fall))
+ rise_signed = ledger.validate_final_answer(
+ HDR + " 低点以来 +58.1%。" + _block(ROW, "58.1% | derived | (1.053 − 0.666) / 0.666 | c1")
+ )
+
+ assert _reasons(rise_claimed) == ["derivation_result_mismatch"]
+ assert fall_signed.valid is True, fall_signed.issues
+ assert unsigned.valid is True, unsigned.issues
+ assert rise_signed.valid is True, rise_signed.issues
+
+
+# ---------------------------------------------------------------------------
+# B4 — an OHLC column holds prints
+# ---------------------------------------------------------------------------
+
+_CLOSE_TABLE = "\n\n| 日期 | 收盘 |\n|---|---|\n| 2026-09-09 | {} |\n"
+
+
+ "row",
+ ["0.700 | proposed | entry", "0.700 | derived | 0.666 × 1.051 | c1", "0.700 | count | n"],
+)
+def test_a_price_column_holds_observed_prints_only(tmp_path: Path, row: str) -> None:
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(HDR + _CLOSE_TABLE.format("0.700") + _block(ROW, row))
+
+ assert _reasons(result) == ["role_in_price_column"]
+
+
+def test_a_print_in_a_price_column_and_a_level_in_another_column_pass(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A)
+
+ print_ = ledger.validate_final_answer(HDR + _CLOSE_TABLE.format("0.666") + _block(ROW))
+ level = ledger.validate_final_answer(
+ HDR + "\n\n| 档位 | 建议价 |\n|---|---|\n| 第一档 | 0.700 |\n" + _block(ROW, "0.700 | proposed | entry")
+ )
+
+ assert print_.valid is True, print_.issues
+ assert level.valid is True, level.issues
+
+
+def test_a_price_column_cell_is_not_grounded_by_a_metric(tmp_path: Path) -> None:
+ """A close cell is compared with closes; a Sharpe of 0.888 is not one."""
+ ledger = _ledger(tmp_path, MARKET_A, FACTOR_A)
+
+ metric = ledger.validate_final_answer(HDR + _CLOSE_TABLE.format("0.888"))
+ close = ledger.validate_final_answer(HDR + _CLOSE_TABLE.format("0.666"))
+
+ assert _reasons(metric) == ["value_mismatch"]
+ assert close.valid is True, close.issues
+
+
+# ---------------------------------------------------------------------------
+# B5 — proposed
+# ---------------------------------------------------------------------------
+
+
+def test_a_proposed_level_needs_an_instrument_when_the_run_holds_several(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, MARKET_B, message=f"对比 {A} 和 {B}")
+
+ unattributed = ledger.validate_final_answer(TWO + "建议买入价 800.00 元。" + _block("800.00 | proposed | entry"))
+ in_prose = ledger.validate_final_answer(TWO + f"{B} 建议买入价 1400.00 元。" + _block("1400.00 | proposed | entry"))
+ in_note = ledger.validate_final_answer(TWO + "建议买入价 1400.00 元。" + _block(f"1400.00 | proposed | {B} entry"))
+
+ assert _reasons(unattributed) == ["no_symbol"]
+ assert in_prose.valid is True, in_prose.issues
+ assert in_note.valid is True, in_note.issues
+
+
[email protected]("row", ["30% | proposed | position", "30% | proposed | 0.666 × 0.45 | c1"])
+def test_a_percent_is_never_a_proposed_level(tmp_path: Path, row: str) -> None:
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(HDR + " 建议仓位 30%。" + _block(ROW, row))
+
+ assert _reasons(result) == ["proposed_not_a_price"]
+ assert "a proposed level is a price" in result.issues[0]["message"]
+
+
+def test_the_same_percent_declared_derived_passes(tmp_path: Path) -> None:
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(
+ HDR + " 建议仓位 30%。" + _block(ROW, "30% | derived | 0.666 × 0.45 | c1")
+ )
+
+ assert result.valid is True, result.issues
+
+
[email protected](("level", "inside"), [("0.567", True), ("1.053", True), ("0.566", False), ("1.054", False)])
+def test_the_observed_range_includes_its_extremes(tmp_path: Path, level: str, inside: bool) -> None:
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(
+ HDR + f" 建议买入价 {level} 元。" + _block(ROW, f"{level} | proposed | entry")
+ )
+
+ assert result.valid is inside, result.issues
+
+
+# ---------------------------------------------------------------------------
+# B6 — observed with a ref
+# ---------------------------------------------------------------------------
+
+
+def test_a_ref_is_filtered_to_the_figures_own_symbol(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, MARKET_C)
+ row = "3.912 | observed | close | c3"
+
+ borrowed = ledger.validate_final_answer(f"{A}(akshare,CNY)最新收盘 3.912 元。" + _block(row))
+ own = ledger.validate_final_answer(f"{C}(akshare,CNY)最新收盘 3.912 元。" + _block(row))
+
+ assert _reasons(borrowed) == ["not_in_referenced_call"]
+ assert own.valid is True, own.issues
+
+
+def test_a_percent_is_never_grounded_by_a_price(tmp_path: Path) -> None:
+ by_close = _ledger(tmp_path, MARKET_D, message=f"分析 {D}").validate_final_answer(
+ f"{D}(akshare,CNY)本周上涨 18%。" + _block("18% | observed | weekly | cd")
+ )
+ by_scaled_low = _ledger(tmp_path, MARKET_A).validate_final_answer(
+ HDR + " 胜率 56.7%。" + _block(ROW, "56.7% | observed | win_rate | c1")
+ )
+ by_metric = _ledger(tmp_path, MARKET_A, FACTOR_A).validate_final_answer(
+ HDR + " 胜率 57.3%。" + _block(ROW, "57.3% | observed | win_rate | fa")
+ )
+
+ assert _reasons(by_close) == ["not_in_referenced_call"]
+ assert _reasons(by_scaled_low) == ["not_in_referenced_call"]
+ assert by_metric.valid is True, by_metric.issues
+
+
+def test_a_currency_figure_is_grounded_only_by_a_price_kind_value(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, INDICATORS_A)
+
+ rsi_as_price = ledger.validate_final_answer(HDR + " 现价 55.2 元。" + _block(ROW, "55.2 | observed | price | ind"))
+ sma_level = ledger.validate_final_answer(HDR + " SMA20 位于 0.700 元。" + _block(ROW, "0.700 | observed | sma_20 | ind"))
+ # Money is never a percent of a print: 66.6 元 is not the close 0.666.
+ hundredfold = ledger.validate_final_answer(HDR + " 现价 66.6 元。" + _block(ROW, "66.6 | observed | close | c1"))
+
+ assert _reasons(rsi_as_price) == ["not_in_referenced_call"]
+ assert _reasons(hundredfold) == ["not_in_referenced_call"]
+ assert sma_level.valid is True, sma_level.issues
+
+
+def test_a_ref_may_name_the_tool(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, INDICATORS_A)
+
+ right_tool = ledger.validate_final_answer(HDR + " RSI 为 55.2。" + _block(ROW, "55.2 | observed | rsi_14 | technical_indicators"))
+ wrong_tool = ledger.validate_final_answer(HDR + " RSI 为 55.2。" + _block(ROW, "55.2 | observed | rsi_14 | get_market_data"))
+
+ assert right_tool.valid is True, right_tool.issues
+ assert _reasons(wrong_tool) == ["not_in_referenced_call"]
+
+
+def test_a_symbol_the_declaration_names_outranks_the_prose(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, MARKET_B, message=f"对比 {A} 和 {B}")
+ prose = TWO + "最新收盘 1410.00 元。"
+
+ right = ledger.validate_final_answer(prose + _block(f"1410.00 | observed | close | {B}"))
+ wrong = ledger.validate_final_answer(prose + _block(f"1410.00 | observed | close | {A}"))
+
+ assert right.valid is True, right.issues
+ assert _reasons(wrong) == ["value_mismatch"]
+
+
+# ---------------------------------------------------------------------------
+# B7 — observed without a ref
+# ---------------------------------------------------------------------------
+
+
+def test_a_currency_figure_is_not_grounded_by_volume_or_a_metric(tmp_path: Path) -> None:
+ by_volume = _ledger(tmp_path, MARKET_A).validate_final_answer(f"{A}(akshare,CNY)最新收盘 234567.0 元。")
+ by_sharpe = _ledger(tmp_path, MARKET_A, FACTOR_A).validate_final_answer(f"{A}(akshare,CNY)最新收盘 0.888 元。")
+
+ assert _reasons(by_volume) == ["value_mismatch"]
+ assert _reasons(by_sharpe) == ["value_mismatch"]
+
+
+def test_a_currency_figure_is_grounded_by_a_price_or_an_amount(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A)
+
+ price = ledger.validate_final_answer(HDR)
+ amount = ledger.validate_final_answer(HDR + " 成交额 1234567.0 元。")
+ plain_volume = ledger.validate_final_answer(HDR + " 成交量 234567.0。")
+
+ assert price.valid is True, price.issues
+ assert amount.valid is True, amount.issues
+ # A plain decimal keeps the row pool: without a mark there is no kind to narrow to.
+ assert plain_volume.valid is True, plain_volume.issues
+
+
+# ---------------------------------------------------------------------------
+# B8 — an undeclared figure in a draft that has a block
+# ---------------------------------------------------------------------------
+
+
+def test_an_undeclared_observed_value_is_not_refused(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A)
+
+ observed = ledger.validate_final_answer(HDR + " 最高 0.681 元。" + _block(ROW))
+ invented = ledger.validate_final_answer(HDR + " 目标 0.900 元。" + _block(ROW))
+
+ assert observed.valid is True, observed.issues
+ assert [issue["code"] for issue in invented.issues] == ["figure_undeclared"]
+ assert "declare it as observed / derived / proposed / cited / count" in invented.issues[0]["message"]
+
+
+# ---------------------------------------------------------------------------
+# B10 — metadata counts and tail risk
+# ---------------------------------------------------------------------------
+
+
+ "leaf",
+ [
+ "return_obs",
+ "return_observations",
+ "n_returns",
+ "return_window",
+ "vol_lookback",
+ "max_drawdown_duration",
+ "aligned_days",
+ "sharpe_count",
+ "stats.drawdown_days",
+ "returns[3]",
+ ],
+)
+def test_a_metadata_count_leaf_has_no_metric_kind(leaf: str) -> None:
+ expected = "return" if leaf == "returns[3]" else None
+
+ assert _metric_kind_for_path(leaf) == expected
+
+
+ ("leaf", "kind"),
+ [
+ ("annualized_return", "return"),
+ ("strategy_max_drawdown", "drawdown"),
+ ("var", "tail_risk"),
+ ("var_95", "tail_risk"),
+ ("var_99", "tail_risk"),
+ ("cvar", "tail_risk"),
+ ("es", "tail_risk"),
+ ("expected_shortfall", "tail_risk"),
+ ("risk.portfolio_cvar_95", "tail_risk"),
+ ],
+)
+def test_a_metric_leaf_keeps_its_kind(leaf: str, kind: str) -> None:
+ assert _metric_kind_for_path(leaf) == kind
+
+
+def test_a_sample_size_does_not_ground_a_return(tmp_path: Path) -> None:
+ meta = ("factor_analysis", {"symbol": A}, {"status": "ok", "return_observations": 81, "aligned_days": 63}, "meta")
+ real = ("factor_analysis", {"symbol": A}, {"status": "ok", "annualized_return": 0.81}, "ret")
+
+ unreferenced = _ledger(tmp_path, MARKET_A, meta).validate_final_answer(HDR + " 年化收益 81%。")
+ referenced = _ledger(tmp_path, MARKET_A, meta).validate_final_answer(
+ HDR + " 胜率 63%。" + _block(ROW, "63% | observed | win_rate | meta")
+ )
+ honest = _ledger(tmp_path, MARKET_A, real).validate_final_answer(HDR + " 年化收益 81%。")
+
+ assert unreferenced.valid is False
+ assert _reasons(referenced) == ["not_in_referenced_call"]
+ assert honest.valid is True, honest.issues
+
+
+def test_a_tail_risk_figure_a_tool_returned_is_grounded(tmp_path: Path) -> None:
+ var = ("quantlib_call", {"action": "call", "function": "var"}, {"ok": True, "result": {"var_95": -0.0234}}, "q1")
+ ledger = _ledger(tmp_path, MARKET_A, var)
+
+ returned = ledger.validate_final_answer(HDR + " 单日 VaR 为 2.34%。")
+ invented = ledger.validate_final_answer(HDR + " 单日 VaR 为 3.10%。")
+
+ assert returned.valid is True, returned.issues
+ assert invented.valid is False
+
+
+# ---------------------------------------------------------------------------
+# B11 — what a rejected figure is pointed at
+# ---------------------------------------------------------------------------
+
+
+def _nearest(result: Any, value: str) -> list[float]:
+ return next(issue for issue in result.issues if issue["value"] == value)["observed_nearest"]
+
+
+def test_a_rejected_figure_is_pointed_at_its_own_symbols_closes(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, MARKET_B, message=f"对比 {A} 和 {B}")
+
+ validation = ledger.validate_final_answer(TWO + f"{A} 最新收盘 0.900 元。")
+ line = next(row for row in ledger.correction_prompt(validation).splitlines() if row.startswith("- 0.900"))
+
+ assert _nearest(validation, "0.900") == [1.04, 0.666]
+ assert "nearest observed 1.04, 0.666" in line
+
+
+def test_a_rejected_figure_of_no_instrument_is_pointed_at_nothing(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, MARKET_B, message=f"对比 {A} 和 {B}")
+
+ validation = ledger.validate_final_answer(TWO + "最新收盘 0.900 元。")
+
+ assert _nearest(validation, "0.900") == []
+
+
+def test_a_rejected_ref_figure_is_pointed_at_the_referenced_closes(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A, MARKET_C)
+
+ validation = ledger.validate_final_answer(
+ f"{A}(akshare,CNY)最新收盘 0.900 元。" + _block("0.900 | observed | close | c1")
+ )
+
+ assert _nearest(validation, "0.900") == [1.04, 0.666]
+
+
+def test_a_rejected_table_cell_is_pointed_at_its_own_column(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path, MARKET_A)
+ table = HDR + "\n\n| 最高 |\n|---|\n| 0.700 |\n"
+
+ unreferenced = ledger.validate_final_answer(table)
+ # A ref scopes to the whole call, so the column is what narrows it to highs.
+ referenced = ledger.validate_final_answer(table + _block(ROW, "0.700 | observed | high | c1"))
+
+ assert _nearest(unreferenced, "0.700") == [0.681, 1.053]
+ assert _nearest(referenced, "0.700") == [0.681, 1.053]
+
+
+# ---------------------------------------------------------------------------
+# Review follow-ups: a declaration cannot move a figure, and money derives from money
+# ---------------------------------------------------------------------------
+
+
+def test_a_declaration_may_not_move_a_figure_to_another_instrument(tmp_path: Path) -> None:
+ """"159516.SZ 最新收盘 3.912 元" is wrong even if 3.912 is 510300.SH's close."""
+ block = _block("3.912 | observed | 510300.SH close | c3")
+
+ moved = _ledger(tmp_path / "moved", MARKET_A, MARKET_C).validate_final_answer(
+ "159516.SZ(akshare,CNY)最新收盘 3.912 元。" + block
+ )
+ kept = _ledger(tmp_path / "kept", MARKET_A, MARKET_C).validate_final_answer(
+ "510300.SH(akshare,CNY)最新收盘 3.912 元。" + block
+ )
+
+ assert moved.valid is False
+ assert "symbol_mismatch" in _reasons(moved)
+ assert kept.valid is True, kept.issues
+
+
+ ("markets", "prose", "row"),
+ [
+ ((MARKET_A, INDICATORS_A), " 第一档 1.104 元。", "1.104 | derived | 55.2 × 0.02 | ind"),
+ ((MARKET_A,), " 目标价 2.346 元。", "2.346 | derived | 234567 / 100000 | c1"),
+ ],
+)
+def test_a_money_figure_is_derived_from_money(
+ tmp_path: Path, markets: tuple[Any, ...], prose: str, row: str
+) -> None:
+ result = _ledger(tmp_path, *markets).validate_final_answer(HDR + prose + _block(ROW, row))
+
+ assert result.valid is False
+ assert "formula_not_anchored" in _reasons(result)
+
+
+ ("prose", "row"),
+ [
+ (" 第一档 0.646 元。", "0.646 | derived | 0.666 × 0.97 | c1"),
+ (" 较高点回撤 37%。", "37% | derived | (1.053 − 0.666) / 1.053 | c1"),
+ ],
+)
+def test_a_price_discount_and_a_drawdown_still_derive(tmp_path: Path, prose: str, row: str) -> None:
+ result = _ledger(tmp_path, MARKET_A).validate_final_answer(HDR + prose + _block(ROW, row))
+
+ assert result.valid is True, result.issues
diff --git a/agent/tests/test_grounding_shape_hardening.py b/agent/tests/test_grounding_shape_hardening.py
new file mode 100644
index 000000000..d75c21b84
--- /dev/null
+++ b/agent/tests/test_grounding_shape_hardening.py
@@ -0,0 +1,662 @@
+"""Shape-layer hardening: a number is read the way the reader sees it.
+
+Every smuggle below is a spelling that rendered as a measurement while the
+shape rules read it as something unchecked — a zero-width character splitting
+"0.888" into two bare integers, a lone "```注```" line fencing off the rest of
+the answer, a bare-year exemption covering "$2050". Each rule is pinned from
+both sides on the sparse two-bar ledger: the smuggle is refused AND the
+legitimate spelling of the same thing still passes.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+
+import pytest
+
+from src.agent.grounding import GroundingLedger
+from src.agent.grounding.figures import Figure, parse_figures_block, scan_figures, strip_figures_block
+
+pytestmark = pytest.mark.unit
+
+SYMBOL = "159516.SZ"
+HDR = "159516.SZ(akshare,CNY)最新收盘 0.666 元。"
+HDR_ROW = "0.666 | observed | close 2026-09-09 | c1"
+LEAD = "159516.SZ(akshare,CNY)"
+
+BACKSLASH = chr(92)
+NBSP, NNBSP, FIGURE_SPACE = chr(0x00A0), chr(0x202F), chr(0x2007)
+MINUS, EN_DASH = chr(0x2212), chr(0x2013)
+FULLWIDTH_STOP, ARABIC_DECIMAL, ARABIC_GROUP = chr(0xFF0E), chr(0x066B), chr(0x066C)
+INVISIBLE = (chr(0x200B), chr(0x200C), chr(0x200D), chr(0x2060), chr(0xFEFF))
+
+
+def _market_payload() -> str:
+ """A sparse two-bar ledger: 0.567–1.053, so a probe value is unambiguous."""
+ return json.dumps(
+ {
+ SYMBOL: [
+ {"trade_date": "2026-05-06", "open": 1.020, "high": 1.053, "low": 1.001,
+ "close": 1.040, "volume": 123456},
+ {"trade_date": "2026-09-09", "open": 0.670, "high": 0.681, "low": 0.567,
+ "close": 0.666, "volume": 234567},
+ ],
+ "_provenance": {
+ SYMBOL: {"source": "akshare", "requested_source": "auto",
+ "detected_source": "akshare", "fallback_used": False,
+ "currency_conversion": "none"}
+ },
+ }
+ )
+
+
+def _ledger(tmp_path: Path) -> GroundingLedger:
+ ledger = GroundingLedger(run_dir=tmp_path, user_message="分析 159516.SZ 并给出买入价")
+ ledger.ingest_tool_result(
+ tool_name="get_market_data",
+ arguments={"codes": [SYMBOL]},
+ result=_market_payload(),
+ call_id="c1",
+ success=True,
+ )
+ return ledger
+
+
+def _block(*rows: str) -> str:
+ return "\n\n```figures\n" + "\n".join(rows) + "\n```"
+
+
+def _figures(text: str) -> list[Figure]:
+ return scan_figures(text, parse_figures_block(text))
+
+
+def _shapes(text: str) -> dict[str, str]:
+ return {figure.text: figure.shape for figure in _figures(text)}
+
+
+def _values(result) -> list[str]:
+ return [issue["value"] for issue in result.issues if issue.get("value") is not None]
+
+
+# ---------------------------------------------------------------------------
+# A1 — numerals are normalized, offsets stay in the original text
+# ---------------------------------------------------------------------------
+
+
+_DISGUISED = [
+ *[(f"0{char}.888", f"0{char}.666") for char in INVISIBLE],
+ (f"0{FULLWIDTH_STOP}888", f"0{FULLWIDTH_STOP}666"),
+ (f"0{ARABIC_DECIMAL}888", f"0{ARABIC_DECIMAL}666"),
+ (f"0{BACKSLASH}.888", f"0{BACKSLASH}.666"),
+ ("0&#46;888", "0&#46;666"),
+ ("0&#x2e;888", "0&#X2E;666"),
+ ("0&period;888", "0&period;666"),
+]
+
+
[email protected](("smuggled", "legitimate"), _DISGUISED)
+def test_a_disguised_decimal_is_checked_as_the_decimal_it_renders(
+ tmp_path: Path, smuggled: str, legitimate: str
+) -> None:
+ """Split by an invisible or escaped separator, "0.888" used to be two bare integers."""
+ ledger = _ledger(tmp_path)
+
+ refused = ledger.validate_final_answer(f"{LEAD}最新收盘 {smuggled}。")
+ passed = ledger.validate_final_answer(f"{LEAD}最新收盘 {legitimate}。")
+
+ assert _values(refused) == [smuggled]
+ assert passed.valid, passed.issues
+
+
+def test_every_figure_keeps_its_original_spelling_and_offsets() -> None:
+ text = f"A 0{chr(0x200B)}.888 B 5{BACKSLASH}% C 0&#46;5 D 52{NBSP}% E CNY0.888"
+
+ figures = _figures(text)
+
+ assert [figure.text for figure in figures] == [
+ f"0{chr(0x200B)}.888", f"5{BACKSLASH}%", "0&#46;5", f"52{NBSP}%", "0.888"
+ ]
+ assert all(text[figure.start : figure.end] == figure.text for figure in figures)
+ assert [figure.digits for figure in figures] == ["0.888", "5", "0.5", "52", "0.888"]
+ assert text[slice(*figures[-1].extent)] == "CNY0.888"
+
+
[email protected]("space", [NBSP, NNBSP, FIGURE_SPACE])
+def test_a_no_break_space_before_the_percent_sign_is_still_a_percent(
+ tmp_path: Path, space: str
+) -> None:
+ ledger = _ledger(tmp_path)
+ derived = "37% | derived | (0.666 − 1.053) / 1.053 | c1"
+
+ refused = ledger.validate_final_answer(f"{HDR} 较高点回撤 52{space}%。")
+ passed = ledger.validate_final_answer(
+ f"{HDR} 较高点回撤 37{space}%。" + _block(HDR_ROW, derived)
+ )
+
+ assert _values(refused) == [f"52{space}%"]
+ assert passed.valid, passed.issues
+
+
+def test_an_arabic_group_separator_keeps_a_grouped_number_whole() -> None:
+ assert [figure.value for figure in _figures(f"1{ARABIC_GROUP}234 سهم")] == [1234.0]
+ assert [figure.value for figure in _figures("1 234")] == [1.0, 234.0]
+
+
+def test_a_unicode_minus_is_a_sign_and_an_en_dash_is_a_range() -> None:
+ figures = _figures(f"回撤 {MINUS}37%,区间 0.632{EN_DASH}0.636,差 0.632-0.636")
+
+ assert [(figure.value, figure.sign) for figure in figures] == [
+ (-37.0, "-"), (0.632, ""), (0.636, ""), (0.632, ""), (0.636, "")
+ ]
+
+
[email protected](("written", "value"), [("5\\%", 5.0), ("12&#37;", 12.0)])
+def test_an_escaped_percent_sign_is_a_percent(written: str, value: float) -> None:
+ [figure] = _figures(f"回撤 {written.replace(chr(92) + chr(92), BACKSLASH)}")
+
+ assert (figure.percent, figure.value, figure.shape) == (True, value, "measured")
+
+
+def test_an_iso_code_glued_to_digits_is_a_currency_mark(tmp_path: Path) -> None:
+ """The identifier lookbehind used to turn "CNY0.888" into a bare "888"."""
+ ledger = _ledger(tmp_path)
+
+ refused = ledger.validate_final_answer(f"{LEAD}最新收盘 CNY0.888。")
+ passed = ledger.validate_final_answer(f"{LEAD}最新收盘 CNY0.666。")
+
+ assert _values(refused) == ["0.888"]
+ assert passed.valid, passed.issues
+ assert _shapes("成本 USD100")["100"] == "measured"
+ assert _figures("成本 USD100")[0].currency is True
+ # A code inside a longer identifier is not a currency mark.
+ assert _shapes("见 XUSD100 与 SMA20") == {}
+
+
+# ---------------------------------------------------------------------------
+# A2 — a decimal comma (#1418)
+# ---------------------------------------------------------------------------
+
+
+ ("text", "value", "percent"),
+ [
+ ("Schlusskurs 0,666 CNY", 0.666, False),
+ ("0,5 im Mittel", 0.5, False),
+ ("12,5 %", 12.5, True),
+ ("3,95 EUR", 3.95, False),
+ ("€3,95", 3.95, False),
+ ("drawdown −5,13%", -5.13, True),
+ ("3,50 美元", 3.5, False),
+ ],
+)
+def test_a_decimal_comma_is_read_as_a_decimal(text: str, value: float, percent: bool) -> None:
+ [figure] = _figures(text.replace("−", MINUS))
+
+ assert (figure.value, figure.percent, figure.shape) == (value, percent, "measured")
+
+
+ ("text", "values"),
+ [
+ ("-1,250.00 total", [-1250.0]),
+ ("1,234 shares", [1234.0]),
+ ("2,237 rows", [2237.0]),
+ ("VaR −5,132%", [-5132.0]),
+ ("1,50 without a mark", [1.0, 50.0]),
+ ("choose 3,95 or 4", [3.0, 95.0, 4.0]),
+ ],
+)
+def test_a_thousands_grouping_stays_grouped(text: str, values: list[float]) -> None:
+ assert [figure.value for figure in _figures(text.replace("−", MINUS))] == values
+
+
+def test_a_true_decimal_comma_close_passes_against_its_print(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+
+ passed = ledger.validate_final_answer(f"{LEAD}Schlusskurs 0,666 CNY.")
+ refused = ledger.validate_final_answer(f"{LEAD}Schlusskurs 0,888 CNY.")
+
+ assert passed.valid, passed.issues
+ assert _values(refused) == ["0,888"]
+
+
+# ---------------------------------------------------------------------------
+# A3 — fences follow CommonMark
+# ---------------------------------------------------------------------------
+
+
+ ("text", "measured"),
+ [
+ ("```注```\n收盘 0.888 元", {"0.888"}), # info holds a backtick
+ ("```注```\n收盘 0.888 元\n```\n", {"0.888"}), # ...so it opens nothing
+ ("见下:\n```python\nentry = 9.99", {"9.99"}), # unterminated
+ (" ```python\nentry = 9.99\n ```", {"9.99"}), # indented four spaces
+ (" ```python\nentry = 9.99\n ```\n收盘 0.888 元", {"0.888"}), # three is a fence
+ ("````python\nx = 9.99\n```\ny = 1.5\n````\n收盘 0.888 元", {"0.888"}), # short closer
+ ("~~~python\nx = 9.99\n```\ny = 1.5\n~~~\n收盘 0.888 元", {"0.888"}), # other char
+ ("```python\nx = 9.99\n```js\ny = 1.5\n```\n收盘 0.888 元", {"0.888"}), # closer with info
+ ],
+)
+def test_only_a_commonmark_fence_exempts_what_it_holds(text: str, measured: set[str]) -> None:
+ shapes = _shapes(text)
+
+ assert {figure for figure, shape in shapes.items() if shape == "measured"} == measured
+ assert set(shapes.values()) <= {"measured", "exempt"}
+
+
+def test_a_stray_opener_cannot_swallow_the_declaration_block(tmp_path: Path) -> None:
+ """A stray "```python" used to pair with "```figures" and leave the block in the answer."""
+ ledger = _ledger(tmp_path)
+
+ refused = ledger.validate_final_answer(
+ "```python\n" + HDR + " 建议买入价 0.888 元。" + _block(HDR_ROW)
+ )
+ passed = ledger.validate_final_answer("```python\n" + HDR + _block(HDR_ROW))
+
+ assert _values(refused) == ["0.888"]
+ assert passed.valid, passed.issues
+ assert passed.released_text == "```python\n" + HDR
+ assert "observed" not in refused.released_text
+
+
+def test_a_figures_fence_quoted_inside_a_longer_fence_declares_nothing() -> None:
+ quoted = "````markdown\n```figures\n0.95 | proposed | entry\n```\n````\n" + HDR
+
+ assert parse_figures_block(quoted).present is False
+ assert [d.value for d in parse_figures_block(quoted + _block(HDR_ROW)).declarations] == [0.666]
+
+
+# ---------------------------------------------------------------------------
+# A4 — years, compact dates and times
+# ---------------------------------------------------------------------------
+
+
+ ("text", "figure"),
+ [
+ ("黄金报价 $2050", "2050"),
+ ("成本 1999 元", "1999"),
+ ("目标 2050 USDT", "2050"),
+ ("涨幅 2020%", "2020%"),
+ ("| 日期 | 收盘 |\n|---|---|\n| 2026-09-09 | 2050 |", "2050"),
+ ("| 批次 | 收盘 |\n|---|---|\n| 20260909 元 | 0.666 |", "20260909"),
+ ],
+)
+def test_a_year_shaped_number_with_a_measurement_mark_is_measured(text: str, figure: str) -> None:
+ assert _shapes(text)[figure] == "measured"
+
+
+ ("text", "figure"),
+ [
+ ("回顾 2024 年的行情", "2024"),
+ ("On 2024 levels", "2024"),
+ ("| 年份 | 收益率 |\n|---|---|\n| 2024 | 12% |", "2024"),
+ ("| 批次 | 收盘 |\n|---|---|\n| 20260909 | 0.666 |", "20260909"),
+ ("trade_date 20260909 收盘", "20260909"),
+ ("2026.09.09 收盘", "2026.09"),
+ ("09:30:00.123 成交", "00.123"),
+ ("| 时段 | 收盘 |\n|---|---|\n| 09:30 | 0.666 |", "30"),
+ ],
+)
+def test_dates_years_and_times_without_marks_are_structure(text: str, figure: str) -> None:
+ shapes = _shapes(text)
+
+ assert shapes[figure] == "exempt"
+
+
+ ("text", "figure"),
+ [
+ ("区间 2001.5 - 2002.5", "2001.5"), # a dotted date needs both dots, unspaced
+ ("12:30.5 分位", "30.5"), # a fraction on minutes is not a time
+ ("| 日期 | 收盘 |\n|---|---|\n| 2026-09-09 | 20261399 |", "20261399"), # no month 13
+ ],
+)
+def test_near_dates_are_still_measurements(text: str, figure: str) -> None:
+ assert _shapes(text)[figure] == "measured"
+
+
+def test_a_year_under_a_price_column_is_checked(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+
+ refused = ledger.validate_final_answer(
+ HDR + "\n\n| 日期 | 收盘 |\n|---|---|\n| 2026-09-09 | 2050 |\n"
+ )
+ passed = ledger.validate_final_answer(
+ HDR + "\n\n| 批次 | 收盘 |\n|---|---|\n| 20260909 | 0.666 |\n\n09:30:00.123 成交。"
+ )
+
+ assert _values(refused) == ["2050"]
+ assert passed.valid, passed.issues
+
+
+# ---------------------------------------------------------------------------
+# A5 — a table's date / time / symbol column exempts structure only
+# ---------------------------------------------------------------------------
+
+
+ ("header", "cell"),
+ [("日期", "0.888"), ("时间", "12.5%"), ("代码", "$3"), ("date", "0,95 EUR")],
+)
+def test_a_measurement_under_a_structural_column_is_checked(header: str, cell: str) -> None:
+ text = f"| {header} | 收盘 |\n|---|---|\n| {cell} | 0.666 |"
+
+ assert _shapes(text)[cell.split(" ")[0].lstrip("$")] == "measured"
+
+
+ ("header", "cell", "figure"),
+ [
+ ("代码", "159516", "159516"),
+ ("日期", "2026.09.09", "2026.09"),
+ ("日期", "20260909", "20260909"),
+ ("时间", "09:30", "09"),
+ ],
+)
+def test_structure_under_a_structural_column_stays_exempt(header: str, cell: str, figure: str) -> None:
+ text = f"| {header} | 收盘 |\n|---|---|\n| {cell} | 0.666 |"
+
+ assert _shapes(text)[figure] == "exempt"
+
+
+def test_a_price_under_the_date_column_is_refused(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+ row = HDR + "\n\n| 日期 | 收盘 |\n|---|---|\n| {} | 0.666 |\n"
+
+ # The price itself is refused; the close beside it loses its date too.
+ assert "0.888" in _values(ledger.validate_final_answer(row.format("0.888")))
+ # evidence.py reads no dotted date, so the ledger arm uses the ISO spelling;
+ # the dotted date's own shape is pinned above.
+ passed = ledger.validate_final_answer(row.format("2026-09-09"))
+ assert passed.valid, passed.issues
+
+
+# ---------------------------------------------------------------------------
+# A6 — the block parser
+# ---------------------------------------------------------------------------
+
+
+def _parse(*rows: str):
+ return parse_figures_block("text" + _block(*rows))
+
+
+ "rows",
+ [
+ ("value | role | note | ref", "---|---|---|---", "0.666 | observed | close | c1"),
+ ("| Value | ROLE | note | ref |", "|:---|---:|:-:|---|", "| 0.666 | observed | close | c1 |"),
+ ],
+)
+def test_a_header_and_a_separator_row_are_skipped(rows: tuple[str, ...]) -> None:
+ block = _parse(*rows)
+
+ assert block.malformed == ()
+ assert [(d.value, d.role, d.note, d.ref) for d in block.declarations] == [
+ (0.666, "observed", "close", "c1")
+ ]
+
+
[email protected]("row", ["value | observed | close | c1", "values | role | note", "--- 0.5"])
+def test_a_line_that_only_resembles_a_header_is_still_malformed(row: str) -> None:
+ assert len(_parse(row).malformed) == 1
+
+
+def test_an_empty_interior_field_keeps_its_place(tmp_path: Path) -> None:
+ """"0.888 | cited | | c1" used to make "c1" the citation."""
+ [declaration] = _parse("0.888 | cited | | c1").declarations
+ assert (declaration.note, declaration.ref) == ("", "c1")
+ [trailing] = _parse("0.666 | observed | close |").declarations
+ assert (trailing.note, trailing.ref) == ("close", "")
+
+ ledger = _ledger(tmp_path)
+ prose = HDR + " Fama-French 2024 论文报告夏普 1.8。"
+ refused = ledger.validate_final_answer(prose + _block(HDR_ROW, "1.8 | cited | | c1"))
+ passed = ledger.validate_final_answer(prose + _block(HDR_ROW, "1.8 | cited | Fama-French 2024 | c1"))
+ assert [issue["reason"] for issue in refused.issues] == ["citation_without_source"]
+ assert passed.valid, passed.issues
+
+
+ ("written", "value", "percent", "canonical"),
+ [
+ ("HK$320.5", 320.5, False, "320.5"),
+ ("182.4 USD", 182.4, False, "182.4"),
+ ("0.95 港元", 0.95, False, "0.95"),
+ ("¥0.95元", 0.95, False, "0.95"),
+ ("100 원", 100.0, False, "100"),
+ ("100 KRW", 100.0, False, "100"),
+ (f"{MINUS}0.5", -0.5, False, "-0.5"),
+ ("0.235M", 0.235, False, "0.235"),
+ ("2.4万元", 2.4, False, "2.4"),
+ ("37 %", 37.0, True, "37%"),
+ ("52pp", 52.0, True, "52%"),
+ ("5200bp", 52.0, True, "52.00%"),
+ ("4.5 bps", 0.045, True, "0.045%"),
+ ("0,666", 0.666, False, "0.666"),
+ ("3,95 EUR", 3.95, False, "3.95"),
+ ("1,234.50", 1234.5, False, "1234.50"),
+ ],
+)
+def test_a_declared_value_tolerates_its_marks(
+ written: str, value: float, percent: bool, canonical: str
+) -> None:
+ block = _parse(f"{written} | observed | close | c1")
+
+ assert block.malformed == (), written
+ [declaration] = block.declarations
+ assert (declaration.value, declaration.percent, declaration.value_text) == (
+ value, percent, canonical
+ )
+
+
+ "written", ["0.95 is entry", "0.95 0.97", "0.95 元/股", "ABCD$3", "3.6ppm", "2026-09-09"]
+)
+def test_a_declared_value_with_anything_else_is_malformed(written: str) -> None:
+ assert len(_parse(f"{written} | proposed | entry").malformed) == 1, written
+
+
+# ---------------------------------------------------------------------------
+# A7 — several blocks
+# ---------------------------------------------------------------------------
+
+
+def test_every_figures_block_declares_and_every_one_is_stripped(tmp_path: Path) -> None:
+ first = _block(HDR_ROW, "junk line")
+ draft = HDR + first + "\n\n第一档 0.646 元。" + _block("0.646 | derived | 0.666 × 0.97 | c1")
+
+ block = parse_figures_block(draft)
+ assert [d.value for d in block.declarations] == [0.666, 0.646]
+ assert [raw for _, raw in block.malformed] == ["junk line"]
+ assert block.span == (len(HDR) + 2, len(HDR) + len(first))
+ assert len(block.spans) == 2
+
+ stripped = strip_figures_block(draft, block)
+ assert stripped == HDR + "\n\n第一档 0.646 元。"
+
+ ledger = _ledger(tmp_path)
+ passed = ledger.validate_final_answer(HDR + _block(HDR_ROW) + "\n\n第一档 0.646 元。" + _block("0.646 | derived | 0.666 × 0.97 | c1"))
+ refused = ledger.validate_final_answer(HDR + _block(HDR_ROW) + "\n\n第一档 0.700 元。" + _block("0.700 | derived | 0.666 × 0.97 | c1"))
+ assert passed.valid, passed.issues
+ assert passed.released_text == HDR + "\n\n第一档 0.646 元。"
+ assert _values(refused) == ["0.700"]
+ assert "```" not in refused.released_text
+
+
+def test_a_second_figures_opener_ends_an_unclosed_first_block() -> None:
+ block = parse_figures_block("t\n\n```figures\n0.666 | observed | close | c1\n```figures\n0.5 | count | n\n```")
+
+ assert [d.value for d in block.declarations] == [0.666, 0.5]
+ assert block.malformed == ()
+
+
+def test_streaming_holds_back_from_the_first_block() -> None:
+ text = HDR + _block(HDR_ROW) + "\n\n更多。" + _block("3 | count | n")
+
+ assert GroundingLedger.streamable_length(text) == len(HDR) + 2
+
+
+# ---------------------------------------------------------------------------
+# A8 — glued pp / bp
+# ---------------------------------------------------------------------------
+
+
+ ("text", "value"),
+ [("溢价扩大 52pp", 52.0), ("利差 5200bp", 52.0), ("spread 4.5bps wider", 0.045), ("down 3PP", 3.0)],
+)
+def test_a_glued_point_mark_is_a_measured_percent(text: str, value: float) -> None:
+ [figure] = _figures(text)
+
+ assert (figure.shape, figure.percent, figure.value, figure.scale) == ("measured", True, value, 1.0)
+
+
[email protected]("text", ["3.6ppm impurity", "a 52 pp gap", "500bpm tempo", "52pages"])
+def test_a_point_mark_must_be_glued_and_whole(text: str) -> None:
+ assert not any(figure.percent for figure in _figures(text))
+
+
+def test_a_basis_point_figure_is_the_percent_it_scales_to(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+
+ refused = ledger.validate_final_answer(HDR + " 据研报,溢价扩大 52pp。")
+ as_percent = ledger.validate_final_answer(HDR + " 据研报溢价 5200bp。" + _block(HDR_ROW, "52% | cited | 研报 2026"))
+ as_points = ledger.validate_final_answer(HDR + " 据研报溢价扩大 52pp。" + _block(HDR_ROW, "52pp | cited | 研报 2026"))
+
+ assert _values(refused) == ["52pp"]
+ assert as_percent.valid, as_percent.issues
+ assert as_points.valid, as_points.issues
+
+
+# ---------------------------------------------------------------------------
+# A9 — yen and won
+# ---------------------------------------------------------------------------
+
+
[email protected](("text", "shape"), [("100 円", "measured"), ("100円です", "measured"),
+ ("100 원", "measured"), ("100 回", "bare"), ("100 人", "bare")])
+def test_yen_and_won_are_currency_marks(text: str, shape: str) -> None:
+ assert _shapes(text)["100"] == shape
+
+
+# ---------------------------------------------------------------------------
+# A10 — the release path
+# ---------------------------------------------------------------------------
+
+
+def test_a_cut_figure_is_also_cut_where_a_code_fence_restates_it(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+ draft = (
+ HDR + " 建议买入价 2.50 元。\n\n```python\nentry = 2.50\nalso = 2" + BACKSLASH
+ + ".50\nstop = 0.60\n```\n" + _block(HDR_ROW, "2.50 | proposed | entry")
+ )
+
+ released = ledger.redacted_release(draft, ledger.validate_final_answer(draft))
+
+ assert released is not None
+ assert "2.50" not in released and "2" + BACKSLASH + ".50" not in released
+ assert "stop = 0.60" in released
+ assert "最新收盘 0.666 元" in released
+ assert "※ 略去 3 处" in released
+ assert ledger.figures_removed == 3
+
+
+def test_a_malformed_block_is_dropped_and_the_draft_is_cut_as_usual(tmp_path: Path) -> None:
+ """An unreadable declaration used to force the canned fallback."""
+ ledger = _ledger(tmp_path)
+ draft = HDR + " 建议买入价 0.65 元,止损 2.50 元。" + _block(HDR_ROW, "0.65 | proposed | entry", "2.50 is my stop")
+
+ validation = ledger.validate_final_answer(draft)
+ assert "figures_block_malformed" in [issue["code"] for issue in validation.issues]
+ released = ledger.redacted_release(draft, validation)
+
+ assert released is not None
+ assert "最新收盘 0.666 元" in released
+ # Without the block, 0.65 is an observed claim like any other and is cut too.
+ assert "0.65" not in released and "2.50" not in released
+ assert "figures" not in released and "observed" not in released
+ assert "※ 略去 2 处" in released
+
+
+def test_a_malformed_line_with_nothing_to_cut_releases_the_checked_text(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+ draft = HDR + _block(HDR_ROW, "see above")
+
+ released = ledger.redacted_release(draft, ledger.validate_final_answer(draft))
+
+ assert released == HDR
+
+
+def test_a_malformed_block_does_not_release_what_still_fails(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+ draft = "600000.SH 最新收盘 9.99 元。" + _block("9.99 | observed | close", "see above")
+
+ assert ledger.redacted_release(draft, ledger.validate_final_answer(draft)) is None
+
+
+def test_a_flagged_span_that_is_not_a_located_figure_refuses_release(tmp_path: Path) -> None:
+ """The cut anchors on a figure the scan found; any other span fails closed."""
+ ledger = _ledger(tmp_path)
+ draft = HDR + " 建议买入价 2.50 元。"
+ validation = ledger.validate_final_answer(draft)
+ [issue] = validation.issues
+ shifted = dict(issue, span=[issue["span"][0] - 2, issue["span"][1]])
+
+ assert ledger.redacted_release(draft, validation) is not None
+ assert ledger.redacted_release(draft, type(validation)(False, [shifted])) is None
+ # A locatable issue beside it does not carry the unlocatable one through.
+ assert ledger.redacted_release(draft, type(validation)(False, [issue, shifted])) is None
+
+
+def test_an_undeclared_figure_on_the_last_draft_is_cut_not_fallen_back(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+ draft = HDR + " 建议持有 3.5 个月。" + _block(HDR_ROW)
+
+ validation = ledger.validate_final_answer(draft)
+ assert [issue["code"] for issue in validation.issues] == ["figure_undeclared"]
+ released = ledger.redacted_release(draft, validation)
+
+ assert released is not None
+ assert "3.5" not in released and "最新收盘 0.666 元" in released
+ assert "※ 略去 1 处" in released
+
+
+# ---------------------------------------------------------------------------
+# A11 — the currency-word bound and the ordinal's whitespace
+# ---------------------------------------------------------------------------
+
+
+def test_a_three_character_currency_word_is_money_and_a_four_character_run_is_not(
+ tmp_path: Path,
+) -> None:
+ assert _shapes("报价 100 人民币")["100"] == "measured"
+ assert _shapes("报价 100 人民币元宵")["100"] == "bare"
+
+ ledger = _ledger(tmp_path)
+ draft = HDR + " 另报价 100 人民币。"
+ released = ledger.redacted_release(draft, ledger.validate_final_answer(draft))
+ assert released is not None
+ assert "另报价(略※)。" in released and "人民币" not in released
+
+
+def test_a_line_opening_with_a_figure_is_not_an_ordinal(tmp_path: Path) -> None:
+ ledger = _ledger(tmp_path)
+
+ refused = ledger.validate_final_answer(LEAD + "\n1.171 元是收盘价")
+ passed = ledger.validate_final_answer(LEAD + "\n0.666 元是收盘价\n1. 第一条")
+
+ assert _values(refused) == ["1.171"]
+ assert passed.valid, passed.issues
+
+
+def test_an_ordinal_needs_the_space_after_its_punctuation() -> None:
+ """A row number glued to the pipe is a table cell, like "| 1 |" — not a list marker."""
+ table = "| 序号 | 收盘 |\n|---|---|\n{}\n"
+
+ assert _shapes(table.format("| 1 | 0.666 |"))["1"] == "measured"
+ assert _shapes(table.format("1.| 0.666 |"))["1"] == "measured"
+ assert _shapes("1. 第一条")["1"] == "exempt"