"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(loaders): preserve bounded BaoStock 0.9.4 connections
Haozhe Wu23c26c8ac5df
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ba0a5ebac..af8809f35 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -35,6 +35,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).#1666, #1665); align Vitest with its coverage plugin. Keep `uuid-utils` at0.17.0 because the proposed 1.0.0 upgrade (#1670) conflicts with the currentLangChain constraint. Preserve mootdx's retirement from the public canary.+ BaoStock's bounded connection wrapper accepts the new 0.9.4 API-key argument+ while retaining public/VIP routing and compatibility with 0.9.3.### Maintenance — 2026-10-09diff --git a/agent/backtest/loaders/baostock_loader.py b/agent/backtest/loaders/baostock_loader.pyindex 1fb9a140e..1c6f4372f 100644--- a/agent/backtest/loaders/baostock_loader.py+++ b/agent/backtest/loaders/baostock_loader.py@@ -103,13 +103,19 @@ def _bounded_send_msg(msg: str, timeout: float) -> Optional[str]:return None-def _connect_with_timeout(self, timeout: float) -> None:- """SocketUtil.connect with a deadline and no unbound-socket NameError."""+def _connect_with_timeout(self, timeout: float, api_key: str = "0") -> None:+ """Bound SDK 0.9.3/0.9.4 connections while preserving SDK host routing.++ Args:+ timeout: Connection deadline in seconds.+ api_key: SDK 0.9.4 routing argument; omitted by SDK 0.9.3.+ """import baostock.common.contants as consimport baostock.common.context as contexttry:- sock = socket.create_connection((cons.BAOSTOCK_SERVER_IP, cons.BAOSTOCK_SERVER_PORT), timeout=timeout)+ host = cons.BAOSTOCK_VIP_SERVER_IP if api_key.startswith("bs-") else cons.BAOSTOCK_SERVER_IP+ sock = socket.create_connection((host, cons.BAOSTOCK_SERVER_PORT), timeout=timeout)except OSError as exc:logger.warning("baostock connect failed: %s", exc)sock = None@@ -134,7 +140,7 @@ def _baostock_socket_guard(timeout: float):previous_socket = getattr(context, "default_socket", None)context.default_socket = Nonesocketutil.send_msg = lambda msg: _bounded_send_msg(msg, timeout)- socketutil.SocketUtil.connect = lambda util: _connect_with_timeout(util, timeout)+ socketutil.SocketUtil.connect = lambda util, api_key="0": _connect_with_timeout(util, timeout, api_key)try:yield Truefinally:diff --git a/agent/tests/test_baostock_socket_deadline.py b/agent/tests/test_baostock_socket_deadline.pyindex 73fe5d1d0..64721148f 100644--- a/agent/tests/test_baostock_socket_deadline.py+++ b/agent/tests/test_baostock_socket_deadline.py@@ -135,6 +135,24 @@ def test_guard_patches_and_restores() -> None:assert socketutil.SocketUtil.connect is original_connect[email protected]("args,vip", [((), False), (("0",), False), (("bs-test",), True)])+def test_guard_accepts_legacy_and_api_key_connect(monkeypatch, args, vip) -> None:+ """Keep SDK 0.9.3/0.9.4 routing and deadlines through the same guard."""+ from unittest.mock import Mock++ monkeypatch.setattr(cons, "BAOSTOCK_VIP_SERVER_IP", "vip.example", raising=False)+ sock = Mock()+ create = Mock(return_value=sock)+ monkeypatch.setattr(socket, "create_connection", create)+ with _baostock_socket_guard(0.5) as acquired:+ assert acquired+ bs.util.socketutil.SocketUtil().connect(*args)+ host = cons.BAOSTOCK_VIP_SERVER_IP if vip else cons.BAOSTOCK_SERVER_IP+ create.assert_called_once_with((host, cons.BAOSTOCK_SERVER_PORT), timeout=0.5)+ assert context.default_socket is sock+ sock.close.assert_called_once_with()++def test_read_timeout_env(monkeypatch) -> None:assert _read_timeout() == _DEFAULT_READ_TIMEOUTmonkeypatch.setenv(_READ_TIMEOUT_ENV, "0.7")