"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): bind artifact reader rows to unchanged engine evidence
Haozhe Wue29c8fb9b419
diff --git a/agent/src/agent/grounding/evidence.py b/agent/src/agent/grounding/evidence.pyindex 065e87b3e..49799cb75 100644--- a/agent/src/agent/grounding/evidence.py+++ b/agent/src/agent/grounding/evidence.py@@ -798,7 +798,11 @@ class _EvidenceMixin:room = _MAX_GENERIC_EVIDENCEfor name in _BACKTEST_SUMMARY_FILES:path = directory / name- if not path.is_file() or self._is_model_written(path):+ if (+ not path.is_file()+ or not path.resolve().is_relative_to(directory.resolve())+ or self._is_model_written(path)+ ):continueif name not in _MANIFEST_EXEMPT and manifest.get(name) != _file_sha256(path):continue@@ -827,7 +831,11 @@ class _EvidenceMixin:tables = directory / "artifacts"if tables.is_dir():for path in sorted(tables.glob("*.csv")):- if self._is_model_written(path) or f"artifacts/{path.name}" in _BACKTEST_SUMMARY_FILES:+ if (+ not path.resolve().is_relative_to(directory.resolve())+ or self._is_model_written(path)+ or f"artifacts/{path.name}" in _BACKTEST_SUMMARY_FILES+ ):continuedigest = _file_sha256(path)if digest is not None:@@ -857,7 +865,9 @@ class _EvidenceMixin:except OSError:return True- def _ingest_engine_table(self, payload: dict[str, Any], call_id: str) -> None:+ def _ingest_engine_table(+ self, payload: dict[str, Any], call_id: str, *, tool_name: str = "read_file"+ ) -> None:"""Record the rows of a backtest table the model just read back.Only a table a completed backtest wrote counts, and only while it is@@ -867,31 +877,48 @@ class _EvidenceMixin:chance. Price columns are left out, as in a summary file.Args:- payload: The ``read_file`` result.- call_id: The ``read_file`` call.+ payload: The file or structured artifact reader's result.+ call_id: The reader's call.+ tool_name: The reader that showed these rows to the model."""- raw, content = payload.get("path"), payload.get("content")- if not isinstance(raw, str) or not isinstance(content, str):+ raw = payload.get("path")+ if not isinstance(raw, str):returntry:path = Path(raw).resolve()except OSError:returnknown = self._engine_tables.get(str(path))- if known is None or _file_sha256(path) != known[0]:+ if known is None or self._is_model_written(path) or _file_sha256(path) != known[0]:returnscope = known[1]- truncated = "\n... (truncated)"- if content.endswith(truncated):- content = content[: -len(truncated)]- content = content[: content.rfind("\n") + 1]- try:- rows = list(csv.reader(content.splitlines()))- except csv.Error:- return- if len(rows) < 2:- return- header = [cell.strip() for cell in rows[0]]+ if tool_name == "read_run_artifact":+ columns, records = payload.get("columns"), payload.get("rows")+ if (+ not isinstance(columns, list)+ or not all(isinstance(cell, str) for cell in columns)+ or not isinstance(records, list)+ or not all(isinstance(row, list) and len(row) == len(columns) for row in records)+ ):+ return+ header = [cell.strip() for cell in columns]+ data_rows = [["" if cell is None else str(cell) for cell in row] for row in records]+ else:+ content = payload.get("content")+ if not isinstance(content, str):+ return+ truncated = "\n... (truncated)"+ if content.endswith(truncated):+ content = content[: -len(truncated)]+ content = content[: content.rfind("\n") + 1]+ try:+ rows = list(csv.reader(content.splitlines()))+ except csv.Error:+ return+ if len(rows) < 2:+ return+ header = [cell.strip() for cell in rows[0]]+ data_rows = rows[1:]folded = [cell.casefold() for cell in header]date_index = next((index for index, name in enumerate(folded) if name in _CSV_DATE_COLUMNS), None@@ -901,7 +928,7 @@ class _EvidenceMixin:)artifact = _relative_posix(path, self.run_dir.resolve())room = _MAX_GENERIC_EVIDENCE- for row in rows[1:]:+ for row in data_rows:timestamp = (row[date_index].strip()if date_index is not None and date_index < len(row)@@ -926,7 +953,7 @@ class _EvidenceMixin:self._evidence.append(EvidenceRecord(call_id=call_id,- tool="read_file",+ tool=tool_name,symbol=None,source="backtest",timestamp=timestamp,diff --git a/agent/src/agent/grounding/ledger.py b/agent/src/agent/grounding/ledger.pyindex 3c8948fd0..0392bba70 100644--- a/agent/src/agent/grounding/ledger.py+++ b/agent/src/agent/grounding/ledger.py@@ -280,6 +280,8 @@ class GroundingLedger(self._ingest_market_data(arguments, payload, call_id)elif tool_name == "read_file" and payload is not None:self._ingest_engine_table(payload, call_id)+ elif tool_name == "read_run_artifact" and payload is not None:+ self._ingest_engine_table(payload, call_id, tool_name=tool_name)elif payload is not None:self._ingest_generic_numeric(tool_name, arguments, payload, call_id)self.persist()diff --git a/agent/tests/test_enforcement_l6.py b/agent/tests/test_enforcement_l6.pyindex 729cf398a..29b1c8352 100644--- a/agent/tests/test_enforcement_l6.py+++ b/agent/tests/test_enforcement_l6.py@@ -45,7 +45,7 @@ _CANONICAL_READ = {"get_equity_quotes","get_equity_orders",}-_CANONICAL_WRITE = {"place_equity_order", "cancel_equity_order"}+_CANONICAL_WRITE = {"place_equity_order", "place_option_order", "cancel_equity_order"}def test_catalog_is_exactly_the_canonical_set() -> None:diff --git a/agent/tests/test_grounding_backtest_outputs.py b/agent/tests/test_grounding_backtest_outputs.pyindex 946e5ce83..96bfcd306 100644--- a/agent/tests/test_grounding_backtest_outputs.py+++ b/agent/tests/test_grounding_backtest_outputs.py@@ -837,3 +837,87 @@ def test_the_correction_asks_for_the_answer_alone(two_runs: GroundingLedger) ->assert "do not mention this rejection" in promptassert "in the user's language" in prompt++[email protected]("mode", ["rows", "downsample"])+def test_structured_artifact_reader_grounds_only_shown_rows(+ two_runs: GroundingLedger, monkeypatch: pytest.MonkeyPatch, mode: str+) -> None:+ from src.tools.run_artifact_tool import read_run_artifact++ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(two_runs.run_dir))+ result = read_run_artifact(+ str(two_runs.run_dir / "rp"), "target_positions", format=mode,+ max_rows=2, columns=["timestamp", "000001.SZ"],+ )+ payload = json.loads(result)+ assert payload["returned_rows"] == 2+ two_runs.ingest_tool_result(+ tool_name="read_run_artifact", arguments={"run_dir": str(two_runs.run_dir / "rp")},+ result=result, call_id="structured-weights", success=True,+ )+ observed = [r for r in two_runs._evidence if r.call_id == "structured-weights"]+ assert {r.field for r in observed} == {"000001.SZ"}+ assert {r.value for r in observed} == {row[1] for row in payload["rows"]}+ shown = _declared("初始权重 38.97%。", "38.97% | observed | 权重 | rp/artifacts/target_positions.csv")+ assert two_runs.validate_final_answer(shown).valid+ unseen = _declared("未读取权重 29.79%。", "29.79% | observed | 权重 | rp/artifacts/target_positions.csv")+ assert not two_runs.validate_final_answer(unseen).valid++[email protected]("changed", ["different_bytes", "same_bytes"])+def test_structured_reader_never_grounds_model_written_table(+ two_runs: GroundingLedger, monkeypatch: pytest.MonkeyPatch, changed: str+) -> None:+ from src.tools.run_artifact_tool import read_run_artifact++ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(two_runs.run_dir))+ table = two_runs.run_dir / "rp" / "artifacts" / "target_positions.csv"+ if changed == "different_bytes":+ table.write_text("timestamp,000001.SZ\n2024-01-02,0.4567\n", encoding="utf-8")+ two_runs.ingest_tool_result(+ tool_name="write_file", arguments={"path": str(table)},+ result=json.dumps({"status": "ok", "path": str(table)}),+ call_id="model-write-table", success=True,+ )+ result = read_run_artifact(str(table.parent.parent), "target_positions")+ two_runs.ingest_tool_result(+ tool_name="read_run_artifact", arguments={"run_dir": str(table.parent.parent)},+ result=result, call_id="read-model-table", success=True,+ )+ assert not any(r.call_id == "read-model-table" for r in two_runs._evidence)+++def test_structured_reader_checks_engine_hash_and_scope(+ two_runs: GroundingLedger, monkeypatch: pytest.MonkeyPatch+) -> None:+ from src.tools.run_artifact_tool import read_run_artifact++ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(two_runs.run_dir))+ table = two_runs.run_dir / "rp" / "artifacts" / "target_positions.csv"+ table.write_text("timestamp,000001.SZ\n2024-01-02,0.4567\n", encoding="utf-8")+ result = read_run_artifact(str(table.parent.parent), "target_positions")+ two_runs.ingest_tool_result(+ tool_name="read_run_artifact", arguments={"run_dir": str(table.parent.parent)},+ result=result, call_id="read-changed", success=True,+ )+ assert not any(r.call_id == "read-changed" for r in two_runs._evidence)+ result = read_run_artifact(str(two_runs.run_dir / "ew"), "target_positions")+ two_runs.ingest_tool_result(+ tool_name="read_run_artifact", arguments={"run_dir": str(two_runs.run_dir / "ew")},+ result=result, call_id="read-ew", success=True,+ )+ wrong = _declared("风险平价权重 33.33%。", "33.33% | observed | 权重 | rp/artifacts/target_positions.csv")+ assert not two_runs.validate_final_answer(wrong).valid+++def test_external_symlink_is_not_registered_as_engine_table(tmp_path: Path) -> None:+ _write_backtest(tmp_path / "rp", RP)+ _write_backtest(tmp_path / "ew", EW)+ target = tmp_path / "rp" / "artifacts" / "target_positions.csv"+ target.unlink()+ target.symlink_to(tmp_path / "ew" / "artifacts" / "target_positions.csv")+ ledger = GroundingLedger(run_dir=tmp_path, user_message="回测风险平价")+ _backtest(ledger, tmp_path / "rp", "bt-rp")+ _read(ledger, target, "read-sibling")+ assert not any(r.call_id == "read-sibling" for r in ledger._evidence)diff --git a/agent/tests/test_robinhood_live_account_binding.py b/agent/tests/test_robinhood_live_account_binding.pyindex b2863d329..f76b0bb9f 100644--- a/agent/tests/test_robinhood_live_account_binding.py+++ b/agent/tests/test_robinhood_live_account_binding.py@@ -92,6 +92,20 @@ def test_gate_stamps_the_mandate_account_on_reads_and_the_order(live_runtime: Paassert broker.order_calls[0]["account_number"] == ACCOUNT+def test_unsupported_option_order_never_reaches_broker_with_valid_mandate(live_runtime: Path) -> None:+ _write_mandate(live_runtime, _bound_mandate())+ broker = _GateBroker()+ spec = replace(_spec(), remote_name="place_option_order")+ guard = order_guard.LiveOrderGuardTool(broker, spec, broker="robinhood", session_id="s1")++ out = json.loads(guard.execute(symbol="AAPL", side="buy", instrument_type="option", notional_usd=100))++ assert out["status"] == "blocked"+ assert out["reason"] == "order intent could not be parsed"+ assert broker.reads == []+ assert broker.order_calls == []++def test_gate_refuses_every_order_when_the_mandate_names_no_account(live_runtime: Path) -> None:_write_mandate(live_runtime, _bound_mandate(account_ref=""))broker = _GateBroker()