"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(cli): name the dashboard instead of starting a server behind the user's back
Haozhe Wud53ef995e901
diff --git a/agent/cli/_legacy.py b/agent/cli/_legacy.pyindex d2038e276..fee9a4c3a 100644--- a/agent/cli/_legacy.py+++ b/agent/cli/_legacy.py@@ -351,6 +351,29 @@ def _read_metrics(path: Path) -> dict:return {}+def _read_metric_values(path: Path) -> dict[str, float]:+ """Read metrics.csv as raw floats, for callers that must do arithmetic.++ ``_read_metrics`` above pre-formats every value into a display string, so a+ caller that renders a ratio as a percentage cannot use it. An empty result+ also serves as the "this turn produced no backtest" signal.+ """+ if not path.exists():+ return {}+ try:+ with path.open(encoding="utf-8", newline="") as handle:+ row = next(csv.DictReader(handle), None) or {}+ except (OSError, csv.Error):+ return {}+ values: dict[str, float] = {}+ for key, value in row.items():+ try:+ values[str(key)] = float(value)+ except (TypeError, ValueError):+ continue+ return values++def _status_style(status: str) -> str:"""Return a consistent Rich color for status labels."""return {@@ -1503,15 +1526,19 @@ def cmd_run(prompt: str, max_iter: int, *, json_mode: bool = False, no_rich: boo_print_json_result(result)return _result_exit_code(result)_print_result(result, time.perf_counter() - start, no_rich=no_rich)- from cli.report_server import ensure_report_server, load_backtest_metrics-- if result.get("run_id") and load_backtest_metrics(result.get("run_dir")):- report_url = ensure_report_server(str(result["run_id"]))- if report_url:+ if result.get("run_id") and result.get("run_dir"):+ # Point at the dashboard without starting anything. Spawning a server+ # from a result-printing path would leave an unsupervised process behind+ # after the command exits.+ if _read_metric_values(Path(result["run_dir"]) / "artifacts" / "metrics.csv"):+ hint = (+ f"Dashboard: run `vibe-trading serve`, then open "+ f"/runs/{result['run_id']}?view=dashboard"+ )if no_rich:- print(f"Dashboard: {report_url}")+ print(hint)else:- console.print(f"[bold]Dashboard:[/bold] [link={report_url}]{report_url}[/link]")+ console.print(f"[dim]{hint}[/dim]")if result.get("run_id"):tip = f"--show {result['run_id']} | --continue {result['run_id']} \"...\" | --code {result['run_id']} | --pine {result['run_id']}"if no_rich:diff --git a/agent/cli/main.py b/agent/cli/main.pyindex 9ad6cf90c..0ae28cc4d 100644--- a/agent/cli/main.py+++ b/agent/cli/main.py@@ -776,23 +776,26 @@ def _print_interactive_result(console: Any, result: Dict[str, Any], elapsed: flo"""Print the assistant answer after the rail without boxed run panels."""from cli.ui.transcript import render_answer, render_elapsed_status- from cli.report_server import ensure_report_server, load_backtest_metrics+ from cli._legacy import _read_metric_valuescontent = (result.get("content") or "").strip()if content:console.print(render_answer(content))console.print()run_id = result.get("run_id")- metrics = load_backtest_metrics(result.get("run_dir"))+ run_dir = result.get("run_dir")+ metrics = (+ _read_metric_values(Path(run_dir) / "artifacts" / "metrics.csv") if run_dir else {}+ )if run_id and metrics:- console.print("[bold green]✓ 回测完成[/bold green]")+ console.print("[bold green]✓ Backtest complete[/bold green]")metric_rows = (- ("总收益率", "total_return", True),- ("年化收益率", "annual_return", True),+ ("Total return", "total_return", True),+ ("Annual return", "annual_return", True),("Sharpe", "sharpe", False),- ("最大回撤", "max_drawdown", True),- ("胜率", "win_rate", True),- ("交易次数", "trade_count", False),+ ("Max drawdown", "max_drawdown", True),+ ("Win rate", "win_rate", True),+ ("Trades", "trade_count", False),)for label, key, as_percent in metric_rows:value = metrics.get(key)@@ -801,14 +804,15 @@ def _print_interactive_result(console: Any, result: Dict[str, Any], elapsed: florendered = f"{value * 100:.1f}%" if as_percent else (f"{int(value)}" if key == "trade_count" else f"{value:.2f}")- console.print(f" {label:<10} [bold]{rendered}[/bold]")+ console.print(f" {label:<14} [bold]{rendered}[/bold]")console.print()console.print(f"[bold]Run ID:[/bold] [cyan]{run_id}[/cyan]")- report_url = ensure_report_server(str(run_id))- if report_url:- console.print(f"[bold]报告:[/bold] [link={report_url}]{report_url}[/link]")- else:- console.print("[yellow]报告服务未能启动;回测产物已经安全保存。[/yellow]")+ # Name the dashboard without starting a server: this is a print path, and+ # a process spawned here would outlive the command that created it.+ console.print(+ f"[dim]Dashboard: run `vibe-trading serve`, then open "+ f"/runs/{run_id}?view=dashboard[/dim]"+ )elif run_id:console.print(f"[dim]/show {run_id} · {elapsed:.1f}s[/dim]")console.print(render_elapsed_status(elapsed))diff --git a/agent/cli/report_server.py b/agent/cli/report_server.pydeleted file mode 100644index 514398852..000000000--- a/agent/cli/report_server.py+++ /dev/null@@ -1,137 +0,0 @@-"""Quiet local report-server bootstrap for interactive backtest runs."""--from __future__ import annotations--import csv-import os-import socket-import subprocess-import sys-import time-from pathlib import Path-from typing import Any-from urllib.error import HTTPError, URLError-from urllib.parse import quote-from urllib.request import Request, urlopen---DEFAULT_REPORT_PORT = 8000-_SPAWNED_PROCESSES: list[subprocess.Popen[Any]] = []---def load_backtest_metrics(run_dir: str | Path | None) -> dict[str, float]:- """Read the scalar metrics row that proves a turn produced a backtest."""- if not run_dir:- return {}- path = Path(run_dir) / "artifacts" / "metrics.csv"- try:- with path.open("r", encoding="utf-8", newline="") as handle:- row = next(csv.DictReader(handle), None) or {}- except (OSError, csv.Error):- return {}-- metrics: dict[str, float] = {}- for key, value in row.items():- try:- metrics[str(key)] = float(value)- except (TypeError, ValueError):- continue- return metrics---def _candidate_ports() -> list[int]:- raw = os.environ.get("VIBE_TRADING_REPORT_PORT", "").strip()- try:- preferred = int(raw) if raw else DEFAULT_REPORT_PORT- except ValueError:- preferred = DEFAULT_REPORT_PORT- preferred = preferred if 1 <= preferred <= 65535 else DEFAULT_REPORT_PORT-- candidates = [preferred, DEFAULT_REPORT_PORT, 8899]- candidates.extend(port for port in range(preferred + 1, min(preferred + 7, 65536)))- return list(dict.fromkeys(candidates))---def _port_is_open(port: int) -> bool:- try:- with socket.create_connection(("127.0.0.1", port), timeout=0.2):- return True- except OSError:- return False---def _run_url(port: int, run_id: str) -> str:- return f"http://127.0.0.1:{port}/runs/{quote(run_id, safe='')}?view=dashboard"---def _vibe_report_ready(port: int, run_id: str) -> bool:- headers: dict[str, str] = {"Accept": "application/json"}- api_key = os.environ.get("API_AUTH_KEY", "").strip()- if api_key:- headers["Authorization"] = f"Bearer {api_key}"- request = Request(_run_url(port, run_id), headers=headers)- try:- with urlopen(request, timeout=0.6) as response: # noqa: S310 - fixed loopback URL- return response.status == 200- except (HTTPError, URLError, OSError, TimeoutError):- return False---def _spawn_report_server(port: int) -> subprocess.Popen[Any] | None:- agent_dir = Path(__file__).resolve().parents[1]- command = [- sys.executable,- "-m",- "cli._legacy",- "serve",- "--host",- "127.0.0.1",- "--port",- str(port),- ]- kwargs: dict[str, Any] = {- "cwd": str(agent_dir),- "stdin": subprocess.DEVNULL,- "stdout": subprocess.DEVNULL,- "stderr": subprocess.DEVNULL,- }- if os.name == "nt":- kwargs["creationflags"] = (- getattr(subprocess, "CREATE_NO_WINDOW", 0x08000000)- | getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0x00000200)- )- else:- kwargs["start_new_session"] = True- try:- process = subprocess.Popen(command, **kwargs)- except OSError:- return None- _SPAWNED_PROCESSES.append(process)- return process---def ensure_report_server(run_id: str, *, timeout_seconds: float = 30.0) -> str | None:- """Return a working report URL, quietly starting a loopback server if needed."""- if not run_id:- return None-- candidates = _candidate_ports()- for port in candidates:- if _vibe_report_ready(port, run_id):- return _run_url(port, run_id)-- port = next((candidate for candidate in candidates if not _port_is_open(candidate)), None)- if port is None:- return None- process = _spawn_report_server(port)- if process is None:- return None-- deadline = time.monotonic() + max(0.1, timeout_seconds)- while time.monotonic() < deadline:- if _vibe_report_ready(port, run_id):- return _run_url(port, run_id)- if process.poll() is not None:- return None- time.sleep(0.15)- return Nonediff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.pyindex 5ee4bb774..ffe9cfb0a 100644--- a/agent/src/agent/loop.py+++ b/agent/src/agent/loop.py@@ -49,6 +49,7 @@ from src.config.accessor import get_env_configfrom src.config.paths import get_runs_dir, get_sessions_dirfrom src.tools.background_tools import get_background_managerfrom src.config.limits import truncate_tool_result+from src.tools.path_utils import safe_run_dirfrom src.tools.redaction import redact_payload, redact_tool_resultRUNS_DIR = get_runs_dir()@@ -585,6 +586,12 @@ def _archive_backtest_result(result: str, active_run_dir: str | None) -> bool:CLI and web API, however, identify the turn by ``active_run_dir``. Keepthat public identity stable by copying only deterministic backtest outputinto the active run as soon as the backtest tool succeeds.++ Both paths are re-validated here. ``backtest`` already refuses a run_dir+ outside the allowed roots, so today the source cannot be arbitrary — but+ that invariant lives in another module and this function reads its path back+ out of a *tool result* rather than from the validated arguments. Checking+ locally keeps a copy loop from depending on a guarantee made elsewhere."""if not active_run_dir:return False@@ -598,8 +605,12 @@ def _archive_backtest_result(result: str, active_run_dir: str | None) -> bool:source_value = payload.get("run_dir")if not source_value:return False- source = Path(str(source_value)).resolve()- target = Path(active_run_dir).resolve()+ try:+ source = safe_run_dir(str(source_value))+ target = safe_run_dir(str(active_run_dir))+ except ValueError:+ logger.warning("Refusing to archive backtest output from outside the allowed run roots")+ return Falseif source == target or not (source / "artifacts" / "metrics.csv").is_file():return Falsediff --git a/agent/tests/test_cli_backtest_summary.py b/agent/tests/test_cli_backtest_summary.pynew file mode 100644index 000000000..12f8e4488--- /dev/null+++ b/agent/tests/test_cli_backtest_summary.py@@ -0,0 +1,128 @@+"""CLI backtest summary: metric parsing, English labels, and no spawned server."""++from __future__ import annotations++import subprocess+from pathlib import Path++from rich.console import Console+from unittest.mock import Mock++from cli._legacy import _read_metric_values++_METRICS_CSV = (+ "total_return,annual_return,sharpe,max_drawdown,win_rate,trade_count\n"+ "0.824,0.131,1.08,-0.187,0.564,42\n"+)+++def _run_with_metrics(tmp_path: Path) -> Path:+ artifacts = tmp_path / "artifacts"+ artifacts.mkdir()+ (artifacts / "metrics.csv").write_text(_METRICS_CSV, encoding="utf-8")+ return tmp_path+++def _forbid_spawn(monkeypatch) -> None:+ """Fail the test if anything starts a process from a print path.++ The original implementation launched `cli._legacy serve` detached, with+ stdout discarded and no shutdown, so every run that produced a backtest could+ leave an unauthenticated loopback API listening after the CLI exited.+ """++ def _explode(*args, **kwargs): # noqa: ANN002, ANN003+ raise AssertionError(f"printing a result must not spawn a process: {args!r}")++ monkeypatch.setattr(subprocess, "Popen", _explode)+++def test_read_metric_values_parses_floats(tmp_path: Path) -> None:+ run_dir = _run_with_metrics(tmp_path)++ metrics = _read_metric_values(run_dir / "artifacts" / "metrics.csv")++ assert metrics["total_return"] == 0.824+ assert metrics["trade_count"] == 42.0+++def test_read_metric_values_returns_empty_when_absent(tmp_path: Path) -> None:+ assert _read_metric_values(tmp_path / "artifacts" / "metrics.csv") == {}+++def test_interactive_result_prints_english_metrics_and_a_hint(+ tmp_path: Path, monkeypatch+) -> None:+ from cli.main import _print_interactive_result++ _forbid_spawn(monkeypatch)+ run_dir = _run_with_metrics(tmp_path)+ console = Console(record=True, force_terminal=False, color_system=None, width=100)++ _print_interactive_result(+ console,+ {+ "status": "success",+ "run_id": "run_123",+ "run_dir": str(run_dir),+ "content": "Done.",+ },+ 3.5,+ )+ output = console.export_text()++ assert "Backtest complete" in output+ assert "82.4%" in output+ assert "Sharpe" in output and "1.08" in output+ assert "run_123" in output+ # The dashboard is named, not started.+ assert "vibe-trading serve" in output+ assert "/runs/run_123?view=dashboard" in output+ # CLI output is English, per the project's UI default.+ assert "回测" not in output and "报告" not in output+++def test_single_prompt_run_prints_the_hint_without_serving(+ tmp_path: Path, monkeypatch+) -> None:+ from cli import _legacy+ from src import preflight++ _forbid_spawn(monkeypatch)+ run_dir = _run_with_metrics(tmp_path)+ result = {+ "status": "success",+ "run_id": "single-run",+ "run_dir": str(run_dir),+ "content": "Done.",+ }+ monkeypatch.setattr(preflight, "run_preflight", lambda console: [])+ monkeypatch.setattr(_legacy, "_ensure_session_id", lambda prompt: "session")+ monkeypatch.setattr(_legacy, "_run_agent", lambda *args, **kwargs: result)+ monkeypatch.setattr(_legacy, "_print_result", Mock())+ test_console = Console(record=True, force_terminal=False, color_system=None, width=100)+ monkeypatch.setattr(_legacy, "console", test_console)++ exit_code = _legacy.cmd_run("Backtest AAPL", 10)+ output = test_console.export_text()++ assert exit_code == 0+ assert "vibe-trading serve" in output+ assert "/runs/single-run?view=dashboard" in output+++def test_no_hint_when_the_turn_produced_no_backtest(tmp_path: Path, monkeypatch) -> None:+ from cli.main import _print_interactive_result++ _forbid_spawn(monkeypatch)+ console = Console(record=True, force_terminal=False, color_system=None, width=100)++ _print_interactive_result(+ console,+ {"status": "success", "run_id": "run_9", "run_dir": str(tmp_path), "content": "Hi."},+ 1.0,+ )+ output = console.export_text()++ assert "Backtest complete" not in output+ assert "view=dashboard" not in outputdiff --git a/agent/tests/test_cli_report_server.py b/agent/tests/test_cli_report_server.pydeleted file mode 100644index 0fdd8b774..000000000--- a/agent/tests/test_cli_report_server.py+++ /dev/null@@ -1,93 +0,0 @@-from __future__ import annotations--from pathlib import Path--from rich.console import Console-from unittest.mock import Mock--from cli import report_server---def test_load_backtest_metrics_reads_scalar_row(tmp_path: Path) -> None:- artifacts = tmp_path / "artifacts"- artifacts.mkdir()- (artifacts / "metrics.csv").write_text(- "total_return,annual_return,sharpe,max_drawdown,win_rate,trade_count\n"- "0.824,0.131,1.08,-0.187,0.564,42\n",- encoding="utf-8",- )-- metrics = report_server.load_backtest_metrics(tmp_path)-- assert metrics["total_return"] == 0.824- assert metrics["trade_count"] == 42---def test_ensure_report_server_reuses_running_local_server(monkeypatch) -> None:- monkeypatch.setattr(report_server, "_candidate_ports", lambda: [8123])- monkeypatch.setattr(report_server, "_vibe_report_ready", lambda port, run_id: True)-- url = report_server.ensure_report_server("run 1")-- assert url == "http://127.0.0.1:8123/runs/run%201?view=dashboard"---def test_interactive_backtest_result_prints_metrics_and_dashboard_link(tmp_path: Path, monkeypatch) -> None:- from cli.main import _print_interactive_result-- artifacts = tmp_path / "artifacts"- artifacts.mkdir()- (artifacts / "metrics.csv").write_text(- "total_return,annual_return,sharpe,max_drawdown,win_rate,trade_count\n"- "0.824,0.131,1.08,-0.187,0.564,42\n",- encoding="utf-8",- )- monkeypatch.setattr(- report_server,- "ensure_report_server",- lambda run_id: f"http://127.0.0.1:8000/runs/{run_id}?view=dashboard",- )- console = Console(record=True, force_terminal=False, color_system=None, width=100)-- _print_interactive_result(- console,- {"status": "success", "run_id": "run_123", "run_dir": str(tmp_path), "content": "Done."},- 3.5,- )- output = console.export_text()-- assert "82.4%" in output- assert "Sharpe" in output and "1.08" in output- assert "run_123" in output- assert "http://127.0.0.1:8000/runs/run_123?view=dashboard" in output---def test_single_prompt_run_prints_dashboard_link(tmp_path: Path, monkeypatch) -> None:- from cli import _legacy- from src import preflight-- artifacts = tmp_path / "artifacts"- artifacts.mkdir()- (artifacts / "metrics.csv").write_text("total_return\n0.1\n", encoding="utf-8")- result = {- "status": "success",- "run_id": "single-run",- "run_dir": str(tmp_path),- "content": "Done.",- }- monkeypatch.setattr(preflight, "run_preflight", lambda console: [])- monkeypatch.setattr(_legacy, "_ensure_session_id", lambda prompt: "session")- monkeypatch.setattr(_legacy, "_run_agent", lambda *args, **kwargs: result)- monkeypatch.setattr(_legacy, "_print_result", Mock())- monkeypatch.setattr(- report_server,- "ensure_report_server",- lambda run_id: f"http://127.0.0.1:8000/runs/{run_id}?view=dashboard",- )- test_console = Console(record=True, force_terminal=False, color_system=None, width=100)- monkeypatch.setattr(_legacy, "console", test_console)-- exit_code = _legacy.cmd_run("Backtest AAPL", 10)-- assert exit_code == 0- assert "http://127.0.0.1:8000/runs/single-run?view=dashboard" in test_console.export_text()diff --git a/agent/tests/test_loop_helpers.py b/agent/tests/test_loop_helpers.pyindex 0f684e8d2..dca845b16 100644--- a/agent/tests/test_loop_helpers.py+++ b/agent/tests/test_loop_helpers.py@@ -329,7 +329,10 @@ class TestNormalizeToolRunDir:class TestArchiveBacktestResult:- def test_copies_detached_backtest_into_active_run(self, tmp_path: Path) -> None:+ def test_copies_detached_backtest_into_active_run(+ self, tmp_path: Path, monkeypatch+ ) -> None:+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(tmp_path))source = tmp_path / "detached"active = tmp_path / "active"(source / "artifacts").mkdir(parents=True)@@ -353,7 +356,8 @@ class TestArchiveBacktestResult:assert (active / "code" / "signal_engine.py").is_file()assert (active / "config.json").is_file()- def test_ignores_result_without_metrics(self, tmp_path: Path) -> None:+ def test_ignores_result_without_metrics(self, tmp_path: Path, monkeypatch) -> None:+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(tmp_path))source = tmp_path / "not-a-backtest"source.mkdir()@@ -362,3 +366,29 @@ class TestArchiveBacktestResult:)assert archived is False++ def test_refuses_a_source_outside_the_allowed_run_roots(+ self, tmp_path: Path, monkeypatch+ ) -> None:+ """The copy loop validates the path itself, not just upstream.++ ``run_dir`` is read back out of a tool result here, so the check that+ makes it safe must live in this function rather than in the tool that+ produced the string.+ """+ allowed = tmp_path / "allowed"+ outside = tmp_path / "outside"+ (outside / "artifacts").mkdir(parents=True)+ (outside / "artifacts" / "metrics.csv").write_text(+ "total_return\n0.5\n", encoding="utf-8"+ )+ active = allowed / "active"+ active.mkdir(parents=True)+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(allowed))++ archived = _archive_backtest_result(+ json.dumps({"status": "ok", "run_dir": str(outside)}), str(active)+ )++ assert archived is False+ assert not (active / "artifacts").exists()