"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): hand the correction prompt the figures it should keep
Yufeng Hef8a5a63dae16
diff --git a/agent/src/agent/grounding/ledger.py b/agent/src/agent/grounding/ledger.pyindex 0392bba70..10761234f 100644--- a/agent/src/agent/grounding/ledger.py+++ b/agent/src/agent/grounding/ledger.py@@ -19,7 +19,7 @@ from src.agent.grounding.identity import (_utc_now,)from src.agent.grounding.evidence import EvidenceRecord, _EvidenceMixin, _json_object-from src.agent.grounding.figures import parse_figures_block, scan_figures, strip_figures_block+from src.agent.grounding.figures import Figure, parse_figures_block, scan_figures, strip_figures_blockfrom src.agent.grounding.policies import ValidationResult, _PolicyMixinfrom src.agent.grounding.registry import GROUNDING_CHECKSfrom src.agent.grounding.release import (@@ -42,6 +42,38 @@ _ANALYSIS_TOOLS = frozenset()+def _passed_figures(+ figures: Sequence[Figure], issues: Sequence[Mapping[str, Any]]+) -> tuple[str, ...]:+ """The measured figures no issue flags, as written, in document order.++ The correction prompt needs them for its keep list. A spelling that fails+ anywhere in the draft is endorsed nowhere, so the list only carries values+ the gate can vouch for outright. Bare integers stay off: only the+ price-posing ones are checked, so a plain "bare" pass proves nothing.+ """+ flagged_spans = {+ (int(span[0]), int(span[1]))+ for issue in issues+ if isinstance((span := issue.get("span")), (list, tuple)) and len(span) == 2+ }+ flagged_values = {+ str(issue.get("value")) for issue in issues if issue.get("value") is not None+ }+ passed: list[str] = []+ seen: set[str] = set()+ for figure in figures:+ if figure.shape != "measured":+ continue+ if (figure.start, figure.end) in flagged_spans:+ continue+ if figure.text in flagged_values or figure.text in seen:+ continue+ seen.add(figure.text)+ passed.append(figure.text)+ return tuple(passed)++_ACTIONABLE_MARKET_RE = re.compile(r"(?:\bbuy\b|\bsell\b|\bentry\b|\btarget price\b|\bcurrent price\b|"r"\blatest price\b|\bprice of\b|\btrade\b|"@@ -342,6 +374,7 @@ class GroundingLedger(valid=not issues,issues=issues,released_text=strip_figures_block(content, block),+ passed_figures=_passed_figures(figures, issues),)if not record:return resultdiff --git a/agent/src/agent/grounding/policies.py b/agent/src/agent/grounding/policies.pyindex 05cadac9b..168cbc9f4 100644--- a/agent/src/agent/grounding/policies.py+++ b/agent/src/agent/grounding/policies.py@@ -114,11 +114,16 @@ class ValidationResult:``released_text`` is the draft without its declaration block, which is acontract with the gate and never reaches the user.++ ``passed_figures`` names the measured figures the gate checked and let+ through, as written, so the correction prompt can tell the model what to+ keep, not only what to fix."""valid: boolissues: list[dict[str, Any]] = field(default_factory=list)released_text: str = ""+ passed_figures: tuple[str, ...] = ()def _close(value: float, target: float) -> bool:diff --git a/agent/src/agent/grounding/release.py b/agent/src/agent/grounding/release.pyindex 002269eac..c98f8fc54 100644--- a/agent/src/agent/grounding/release.py+++ b/agent/src/agent/grounding/release.py@@ -214,6 +214,20 @@ class _ReleaseMixin:+ ", ".join(repeated)+ ". Take option (2) or (3) for them.")+ passed = list(validation.passed_figures)+ if passed:+ shown = passed[:24]+ keep = ", ".join(shown)+ if len(passed) > len(shown):+ keep += f", and {len(passed) - len(shown)} more"+ lines.extend(+ [+ "Every other measured figure in the draft checked clean. Keep these "+ "values exactly as written, where they stand: " + keep + ".",+ "Cutting them or swapping whole sections for qualitative prose is not "+ "a fix; only the figures listed above need work.",+ ]+ )lines.extend(["End the answer with a ```figures``` block declaring every number that "diff --git a/agent/tests/test_grounding_release.py b/agent/tests/test_grounding_release.pyindex 1eae2a11a..77b4ab4be 100644--- a/agent/tests/test_grounding_release.py+++ b/agent/tests/test_grounding_release.py@@ -2355,3 +2355,70 @@ def test_the_sweep_does_not_key_on_a_single_digit(tmp_path: Path) -> None:assert "5%" not in releasedassert "跌破 5 日均线,关注 5 只同类基金" in releasedassert "※ 略去 1 处" in released+++# ---------------------------------------------------------------------------+# The correction prompt's keep list (#1622 post-failure action semantics)+# ---------------------------------------------------------------------------+++def test_the_correction_prompt_names_the_figures_to_keep(tmp_path: Path) -> None:+ """A mixed rejection must not talk the model into deleting clean figures.++ zeus229's production run on #1622: 65 issues over a draft whose evidence+ was complete, and the correction rewrote whole numeric sections as+ qualitative prose, deleting figures the gate had passed. The prompt only+ named the failures. The keep list pins the other half of the contract.+ """+ ledger = _ledger(tmp_path)+ draft = (+ HDR+ + "昨日收盘 1.137 元,20 日均线 1.150 元,50 日均线 1.090 元。"+ + "参考买入价 1.136 元(1.171 × 0.97)。目标价 2.50 元,传闻报价 9.99 元。"+ + _block(+ HDR_ROW,+ "1.137 | observed | close 2026-06-23 | prices",+ "1.150 | observed | sma_20 | indicators",+ "1.090 | observed | sma_50 | indicators",+ "1.136 | derived | 1.171 × 0.97 | prices",+ "2.50 | proposed | target",+ )+ )+ validation = ledger.validate_final_answer(draft)++ assert validation.valid is False+ prompt = ledger.correction_prompt(validation)+ keep = next(row for row in prompt.splitlines() if "checked clean" in row)++ # Document order, exactly as written.+ assert "1.171, 1.137, 1.150, 1.090, 1.136" in keep+ for rejected in ("2.50", "9.99", "0.97"):+ assert rejected not in keep+ assert "only the figures listed above need work" in prompt+++def test_the_keep_list_is_absent_when_nothing_passed(tmp_path: Path) -> None:+ """No clean figure, no keep list: the section must not render empty."""+ ledger = _ledger(tmp_path)+ validation = ledger.validate_final_answer("562500.SS(Yahoo,CNY)最新收盘价 9.99 元。")++ assert validation.valid is False+ assert "checked clean" not in ledger.correction_prompt(validation)+++def test_the_keep_list_caps_and_counts_the_rest(tmp_path: Path) -> None:+ """A long keep list stays one line: 24 values spelled, the rest counted."""+ extra = {f"m{i:02d}": 11.0 + i / 100 for i in range(1, 27)}+ ledger = _ledger(tmp_path, **extra)+ draft = HDR + "指标读数:" + "、".join(f"{v:.2f}" for v in extra.values()) + "。传闻报价 9.99 元。"+ rows = [HDR_ROW] + [f"{v:.2f} | observed | {k} | indicators" for k, v in extra.items()]+ validation = ledger.validate_final_answer(draft + _block(*rows))++ assert validation.valid is False+ prompt = ledger.correction_prompt(validation)+ keep = next(row for row in prompt.splitlines() if "checked clean" in row)++ # 27 clean figures (1.171 plus the 26 indicators), 24 shown, 3 counted.+ assert "and 3 more" in keep+ assert "11.23" in keep+ assert "11.24" not in keep