"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): read run_dir refs and signs before currency marks
Haozhe Wu56eab134055d
diff --git a/agent/src/agent/grounding/figures.py b/agent/src/agent/grounding/figures.pyindex a77e28719..4ba4fe2d6 100644--- a/agent/src/agent/grounding/figures.py+++ b/agent/src/agent/grounding/figures.py@@ -17,7 +17,7 @@ import htmlimport reimport stringimport unicodedata-from dataclasses import dataclass+from dataclasses import dataclass, replacefrom decimal import Decimal, InvalidOperationfrom typing import Iterable, Sequence@@ -481,7 +481,40 @@ def _numbers(text: str, *, decimal_commas: bool | None = None) -> list[_Token]:body, end = f"{body}.{fraction}", stoptokens.append(_Token(match.start(), end, sign, body.replace(",", "")))cursor = end- return sorted(tokens + protected, key=lambda token: token.start)+ signed = [+ replace(token, sign=text[lead]) if not token.sign and lead is not None else token+ for token in tokens + protected+ for lead in (_sign_before_currency(text, token.start),)+ ]+ return sorted(signed, key=lambda token: token.start)+++# What may stand before a sign written outside a currency mark ("PnL: -$3.20",+# "(−$5.00)"): a hyphen after anything else joins two figures ("$10-$20"), and+# one followed by a space ("$10 - $20") is a range, as "10 - 20" already is.+_SIGN_BOUNDARY = frozenset("([:=,;|([:=,;|")+++def _sign_before_currency(text: str, start: int) -> int | None:+ """Where a sign glued to the left of a number's currency mark stands ("−$1,847.78").++ The minus is normalized to "-" before this is read.++ Args:+ text: Normalized text.+ start: Where the number's digits begin.++ Returns:+ The sign's index, or None when the mark carries no sign or the sign+ does not stand at a boundary.+ """+ mark = _currency_prefix_start(text, start)+ if mark == start or mark == 0 or text[mark - 1] not in "+-":+ return None+ before = text[mark - 2] if mark >= 2 else ""+ if before and not before.isspace() and before not in _SIGN_BOUNDARY:+ return None+ return mark - 1def _writes_decimal_commas(text: str) -> bool:@@ -643,7 +676,12 @@ def _parse_value(if not unit and tail in _MULTIPLE_MARKS:# "5.5x": a multiple, written the way the answer writes it.tail = ""- if not (_is_currency_mark(tail) and _is_currency_mark(field[: token.start].strip())):+ head = field[: token.start].strip()+ lead = _sign_before_currency(field, token.start)+ if lead is not None and not field[:lead].strip():+ # "−$1,847.78": the sign stands outside the mark and is the number's.+ head = field[lead + 1 : token.start].strip()+ if not (_is_currency_mark(tail) and _is_currency_mark(head)):return Nonereading = _reading(token.sign, token.digits, unit)if reading is None:@@ -1169,6 +1207,9 @@ def scan_figures(content: str, block: FiguresBlock) -> list[Figure]:r"^(?:\*\*|__)(.*?)(?:\*\*|__)$", r"\1", symbol_value).strip()mark_end = _currency_suffix_end(text, magnitude_suffix(text, unit_end)[1])+ # A sign written before the currency mark is cut with the figure.+ lead = _sign_before_currency(text, token.start) if token.sign else None+ prefix_start = lead if lead is not None else _currency_prefix_start(text, token.start)figures.append(Figure(text=content[start:end],@@ -1186,7 +1227,7 @@ def scan_figures(content: str, block: FiguresBlock) -> list[Figure]:currency_code=currency_code,digits=token.digits,sign=token.sign,- extent=(view.start(_currency_prefix_start(text, token.start)), view.end(mark_end)),+ extent=(view.start(prefix_start), view.end(mark_end)),fence=next((info for low, high, info, _ in fences if low <= start and digits_end <= high),None,diff --git a/agent/src/agent/grounding/policies.py b/agent/src/agent/grounding/policies.pyindex 1a3b77b1b..01a0b9924 100644--- a/agent/src/agent/grounding/policies.py+++ b/agent/src/agent/grounding/policies.py@@ -1018,7 +1018,9 @@ class _PolicyMixin:two backtests hold two different Sharpes — so a ref naming severalruns pools them, and a ref naming only a file pools that file acrossruns, like a ref naming the tool. A file or field the ref also names- narrows the set when the run holds it and is ignored otherwise.+ narrows the set when the run holds it and is ignored otherwise. The+ active run written as ``run_dir/`` or by its own name is read as the+ path below it (:func:`_run_dir_alias`).Args:key: The declaration's ref, or one half of ``scope::field``.@@ -1046,6 +1048,12 @@ class _PolicyMixin:if not token:continuehits = _records_at(token, owned, root)+ alias = None if hits else _run_dir_alias(token, root)+ if alias is not None:+ # "run_dir/artifacts/metrics.csv": the active run under a+ # stand-in name, read as the path or file below it.+ hits = _records_at(alias, owned, root)+ token = alias if hits or "/" not in alias else tokenif hits:chosen.update((id(record), record) for record in hits)elif token != key.strip() and any(record.tool == token for record in owned):@@ -2524,6 +2532,32 @@ def _file_stem(path: str) -> str:return str(path).rsplit("/", 1)[-1].split(".", 1)[0].casefold()+#: How a model names the active run directory when it does not know its path.+_RUN_DIR_PLACEHOLDER = "run_dir"+++def _run_dir_alias(token: str, root: Path) -> str | None:+ """The root-relative path a ref token names through a stand-in for the active run.++ A model writes the active run directory as the placeholder ``run_dir`` or+ by its own name (``20261021_195945_86_52d4e2/artifacts/metrics.csv``); both+ stand for the ledger's root, so the rest is read from there exactly as if+ written relative to it. A path leaving the run directory (absolute, or+ ``..`` once normalized) starts with neither and has no alias.++ Args:+ token: One part of a ref that named no record as written.+ root: The ledger's resolved run directory.++ Returns:+ The path below the stand-in, or None.+ """+ parts = Path(os.path.normpath(token)).parts+ if len(parts) > 1 and parts[0] in (_RUN_DIR_PLACEHOLDER, root.name):+ return "/".join(parts[1:])+ return None++def _records_at(token: str, owned: Sequence[EvidenceRecord], root: Path) -> list[EvidenceRecord]:diff --git a/agent/tests/test_grounding_backtest_outputs.py b/agent/tests/test_grounding_backtest_outputs.pyindex 96bfcd306..994c33a67 100644--- a/agent/tests/test_grounding_backtest_outputs.py+++ b/agent/tests/test_grounding_backtest_outputs.py@@ -32,6 +32,7 @@ import pytestfrom src.agent.grounding import GroundingLedgerfrom src.agent.grounding.evidence import ARCHIVE_MANIFEST+from src.agent.grounding.figures import parse_figures_block, scan_figuresfrom src.agent.tool_results import _archive_backtest_resultRP = {@@ -520,9 +521,13 @@ def test_a_detached_backtests_archived_copy_counts_once_vouched_for(tmp_path: Paassert RP["sharpe"] in recorded-def test_the_active_runs_copy_belongs_to_the_backtest_the_archive_names(tmp_path: Path) -> None:[email protected]("ref", ["artifacts/metrics.csv", "run_dir/artifacts/metrics.csv"])+def test_the_active_runs_copy_belongs_to_the_backtest_the_archive_names(+ tmp_path: Path, ref: str+) -> None:"""The loop copies each finished backtest into the active run; a ref to that- copy names the backtest it came from, and only once it has come."""+ copy names the backtest it came from, and only once it has come. ``run_dir/``+ names that copy too, never the file of the same name in another run."""ledger = GroundingLedger(run_dir=tmp_path, user_message="回测")_write_backtest(tmp_path / "rp", RP)_write_backtest(tmp_path / "ew", EW)@@ -537,7 +542,7 @@ def test_the_active_runs_copy_belongs_to_the_backtest_the_archive_names(tmp_pathdef copy_says(value: str) -> bool:return ledger.validate_final_answer(- _declared(f"Sortino {value}。", f"{value} | observed | Sortino | artifacts/metrics.csv")+ _declared(f"Sortino {value}。", f"{value} | observed | Sortino | {ref}")).validarchive("ew", "earlier-call") # an earlier archive: not rp's output, and not credited to rp@@ -921,3 +926,205 @@ def test_external_symlink_is_not_registered_as_engine_table(tmp_path: Path) -> N_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)+++# ---------------------------------------------------------------------------+# The active run under a stand-in name (live DeepSeek run, 2026-10-21)+# ---------------------------------------------------------------------------++# How the engine names a run directory; the model copies it or writes ``run_dir``.+RUN_NAME = "20261021_195945_86_52d4e2"+++def own_run(tmp_path: Path) -> GroundingLedger:+ """One backtest written into the active run directory itself, trades read back."""+ root = tmp_path / RUN_NAME+ _write_backtest(root, RP)+ ledger = GroundingLedger(run_dir=root, user_message="回测风险平价")+ _backtest(ledger, root, "bt-own")+ _read(ledger, root / "artifacts" / "trades.csv", "read-trades")+ return ledger+++_STAND_INS = ["run_dir/artifacts/{file}", f"{RUN_NAME}/artifacts/{{file}}", "run_dir/{file}"]++[email protected]("spelling", ["{file}", "artifacts/{file}", *_STAND_INS])+def test_the_active_run_named_by_a_stand_in_is_the_file_named_bare(+ own_run: GroundingLedger, spelling: str+) -> None:+ """``run_dir/artifacts/metrics.csv`` grounded nothing while ``metrics.csv`` grounded+ the same draft: the final value, trade count and trade PnL were all cut. The+ count sits in a table cell, where an integer is a measurement."""+ metrics, trades = spelling.format(file="metrics.csv"), spelling.format(file="trades.csv")+ result = own_run.validate_final_answer(+ _declared(+ "期末净值 1,129,817.32,Sortino 1.133,一笔盈利 11,488.33。\n\n"+ "| 指标 | 数值 |\n|---|---|\n| 交易笔数 | 10 |",+ f"1,129,817.32 | observed | 期末净值 | {metrics}",+ f"10 | observed | 交易笔数 | {metrics}",+ f"1.133 | observed | Sortino | {metrics}",+ f"11,488.33 | observed | 盈亏 | {trades}",+ )+ )++ assert result.valid, result.issues++[email protected]("spelling", ["{file}", *_STAND_INS])+def test_a_value_the_file_does_not_hold_is_refused_under_every_stand_in(+ own_run: GroundingLedger, spelling: str+) -> None:+ metrics, trades = spelling.format(file="metrics.csv"), spelling.format(file="trades.csv")+ result = own_run.validate_final_answer(+ _declared(+ "期末净值 1,129,900.00,Sortino 1.190,一笔盈利 11,400.00。\n\n"+ "| 指标 | 数值 |\n|---|---|\n| 交易笔数 | 11 |",+ f"1,129,900.00 | observed | 期末净值 | {metrics}",+ f"11 | observed | 交易笔数 | {metrics}",+ f"1.190 | observed | Sortino | {metrics}",+ f"11,400.00 | observed | 盈亏 | {trades}",+ )+ )++ assert sorted(issue["value"] for issue in result.issues) == [+ "1,129,900.00", "1.190", "11", "11,400.00",+ ]+ assert set(_figure_reasons(result)) == {"not_in_referenced_call"}+++ "ref",+ [+ "run_dir/config.json",+ f"{RUN_NAME}/config.json",+ "config.json",+ "run_dir/../outside/artifacts/metrics.csv",+ f"{RUN_NAME}/../outside/artifacts/metrics.csv",+ "../outside/artifacts/metrics.csv",+ "outside/artifacts/metrics.csv",+ ],+)+def test_a_stand_in_reaches_no_model_written_file_and_nothing_outside_the_run(+ own_run: GroundingLedger, ref: str+) -> None:+ """The model's own config.json stays non-evidence, and a sibling directory holding+ a metrics file of the same name is not the active run."""+ root = own_run.run_dir+ config = root / "config.json"+ config.write_text(json.dumps({"initial_cash": 777777}), encoding="utf-8")+ own_run.ingest_tool_result(+ tool_name="write_file",+ arguments={"path": str(config)},+ result=json.dumps({"status": "ok", "path": str(config)}),+ call_id="model-write-config",+ success=True,+ )+ _write_backtest(root.parent / "outside", {"sortino": 1.4567})++ assert own_run._artifact_scope(ref) is None+ for written in ("777,777.00", "1.4567"):+ result = own_run.validate_final_answer(+ _declared(f"数值 {written}。", f"{written} | observed | 数值 | {ref}")+ )+ assert not result.valid, (ref, written)+++# ---------------------------------------------------------------------------+# A sign written before the currency mark (same live run: "−$1,847.78")+# ---------------------------------------------------------------------------+++ ("text", "values"),+ [+ ("Trade three lost −$1,847.78.", [-1847.78]),+ ("-$3.20", [-3.2]),+ ("(−$5.00)", [-5.0]),+ ("PnL: -$3.20", [-3.2]),+ ("盈亏:−$3.20", [-3.2]),+ ("+$2.50", [2.5]),+ ("−HK$1.10", [-1.1]),+ # A hyphen joining two figures, or a spaced one, is a range, not a sign.+ ("$10-$20", [10.0, 20.0]),+ ("$10–$20", [10.0, 20.0]),+ ("$10 - $20", [10.0, 20.0]),+ ("a-$5", [5.0]),+ ("loss of $1,847.78", [1847.78]),+ ],+)+def test_a_sign_before_the_currency_mark_is_the_figures_sign(+ text: str, values: list[float]+) -> None:+ figures = scan_figures(text, parse_figures_block(text))++ assert [figure.value for figure in figures] == values+ for figure in figures:+ # The sign is cut with the figure, never left behind by a redaction.+ written = text[figure.extent[0] : figure.extent[1]]+ assert written[0] in "+-−" if figure.sign else written[0] not in "+-−", written+++ ("value", "declared"),+ [+ ("−$1,847.78", -1847.78),+ ("-$3.20", -3.2),+ ("+$2.50", 2.5),+ ("−HK$1.10", -1.1),+ ("$10-$20", None),+ ("$10 - $20", None),+ ("x -$5", None),+ ],+)+def test_a_declaration_reads_the_sign_the_prose_reads(value: str, declared: float | None) -> None:+ """``−$1,847.78 | observed | …`` was ``unparseable_declaration``."""+ block = parse_figures_block(f"text\n\n```figures\n{value} | observed | trade PnL | trades.csv\n```")++ if declared is None:+ assert not block.declarations and block.malformed+ else:+ assert [item.value for item in block.declarations] == [declared]+ assert not block.malformed++[email protected]("ref", ["trades.csv", "run_dir/artifacts/trades.csv"])+def test_a_loss_written_with_its_sign_before_the_mark_grounds(+ own_run: GroundingLedger, ref: str+) -> None:+ """The draft's trade loss was refused or cut although trades.csv holds it."""+ loss = own_run.validate_final_answer(+ _declared("一笔亏损 −$17,998.19。", f"−$17,998.19 | observed | 盈亏 | {ref}")+ )+ gain = own_run.validate_final_answer(+ _declared("一笔盈利 +$11,488.33。", f"+$11,488.33 | observed | 盈亏 | {ref}")+ )++ assert loss.valid, loss.issues+ assert gain.valid, gain.issues++[email protected]("written", ["+$17,998.19", "$17,998.19"])+def test_a_loss_written_as_a_gain_is_refused(own_run: GroundingLedger, written: str) -> None:+ result = own_run.validate_final_answer(+ _declared(f"一笔交易 {written}。", f"{written} | observed | 盈亏 | trades.csv")+ )++ assert not result.valid+ assert _figure_reasons(result) == ["not_in_referenced_call"]+++def test_a_cut_signed_figure_leaves_no_sign_behind(own_run: GroundingLedger) -> None:+ draft = _declared(+ "Sortino 1.133,一笔亏损 −$17,000.00。",+ "1.133 | observed | Sortino | metrics.csv",+ "−$17,000.00 | observed | 盈亏 | trades.csv",+ )+ validation = own_run.validate_final_answer(draft)++ released = own_run.redacted_release(draft, validation)++ assert released is not None+ assert "1.133" in released and "17,000.00" not in released+ assert "−" not in released, released