"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(tools): repair lockup expiry report handling
lorenzozanee657f79c4ec55
diff --git "a/agent/src/skills/eastmoney/references/\345\217\202\350\200\203\346\225\260\346\215\256/\351\231\220\345\224\256\350\247\243\347\246\201.md" "b/agent/src/skills/eastmoney/references/\345\217\202\350\200\203\346\225\260\346\215\256/\351\231\220\345\224\256\350\247\243\347\246\201.md"index d49196043..2bb075fad 100644--- "a/agent/src/skills/eastmoney/references/\345\217\202\350\200\203\346\225\260\346\215\256/\351\231\220\345\224\256\350\247\243\347\246\201.md"+++ "b/agent/src/skills/eastmoney/references/\345\217\202\350\200\203\346\225\260\346\215\256/\351\231\220\345\224\256\350\247\243\347\246\201.md"@@ -9,7 +9,7 @@**端点**-`https://datacenter-web.eastmoney.com/api/data/v1/get`,reportName = `RPT_LIFT_STOCK`。+`https://datacenter-web.eastmoney.com/api/data/v1/get`,reportName = `RPT_LIFT_STAGE`。<br>@@ -28,7 +28,7 @@ horizon_days | int | N | 全市场日历的未来窗口天数,clamp 至 [1, 36名称 | 取值 | 描述---- | ---- | -----reportName | `RPT_LIFT_STOCK` | 报表名+reportName | `RPT_LIFT_STAGE` | 报表名columns | `SECURITY_CODE,SECURITY_NAME_ABBR,FREE_DATE,FREE_SHARES_TYPE,FREE_SHARES,CURRENT_FREE_SHARES,ABLE_FREE_SHARES,LIFT_MARKET_CAP,FREE_RATIO,TOTAL_RATIO` | 请求列filter | 个股=`(SECURITY_CODE="<code>")`;全市场=`(FREE_DATE>='start')(FREE_DATE<='end')` | 过滤谓词sortColumns / sortTypes | `FREE_DATE` / 个股 `-1`(最新在前)、全市场 `1`(最近解禁在前) | 排序diff --git a/agent/src/tools/lockup_expiry_tool.py b/agent/src/tools/lockup_expiry_tool.pyindex b352827c3..dcc4307ce 100644--- a/agent/src/tools/lockup_expiry_tool.py+++ b/agent/src/tools/lockup_expiry_tool.py@@ -9,7 +9,7 @@ calendar when no code is given.All requests reuse the shared, per-host-throttled Eastmoney client(:mod:`backtest.loaders.eastmoney_client`); Eastmoney rate-limits by source IPand temporarily bans bursting callers, so this module never issues an-un-throttled GET. It only knows Eastmoney's ``RPT_LIFT_STOCK`` report layout,+un-throttled GET. It only knows Eastmoney's ``RPT_LIFT_STAGE`` report layout,not any loader's DataFrame conventions."""@@ -27,7 +27,7 @@ logger = logging.getLogger(__name__)# Eastmoney datacenter report endpoint + the restricted-share unlock report._DATACENTER_URL = "https://datacenter-web.eastmoney.com/api/data/v1/get"-_REPORT_NAME = "RPT_LIFT_STOCK"+_REPORT_NAME = "RPT_LIFT_STAGE"# Per-bar columns requested from the report, in our display order._COLUMNS = (@@ -156,6 +156,22 @@ def _shape_record(raw: Any) -> dict[str, Any] | None:}+_EMPTY_RESULT_MESSAGES = ("返回数据为空",)+++def _upstream_rejection(payload: Any) -> str | None:+ if not isinstance(payload, dict) or payload.get("success") is not False:+ return None+ message = payload.get("message")+ if not isinstance(message, str) or not message.strip():+ return "request rejected without a message"+ message = message.strip()+ code = payload.get("code")+ if code == 9201 and message in _EMPTY_RESULT_MESSAGES:+ return None+ return f"code={code} message={message}" if code is not None else message++def _extract_rows(payload: Any) -> list[dict]:"""Pull the ``result.data`` list out of a datacenter payload.@@ -209,6 +225,10 @@ def _fetch_lockups(code: str | None, horizon_days: int) -> list[dict]:},)+ rejection = _upstream_rejection(payload)+ if rejection is not None:+ raise ValueError(f"eastmoney datacenter rejected the request: {rejection}")+records: list[dict] = []for raw in _extract_rows(payload):shaped = _shape_record(raw)diff --git a/agent/tests/test_lockup_expiry_tool.py b/agent/tests/test_lockup_expiry_tool.pyindex 992b1357e..0cfd03a95 100644--- a/agent/tests/test_lockup_expiry_tool.py+++ b/agent/tests/test_lockup_expiry_tool.py@@ -19,11 +19,11 @@ from src.tools.lockup_expiry_tool import LockupExpiryTooldef _payload(rows: list[dict]) -> dict:"""Wrap report rows in the datacenter ``result.data`` envelope."""- return {"result": {"data": rows, "count": len(rows)}}+ return {"success": True, "result": {"data": rows, "count": len(rows)}}def _row(code: str, free_date: str) -> dict:- """One RPT_LIFT_STOCK-shaped record."""+ """One RPT_LIFT_STAGE-shaped record."""return {"SECURITY_CODE": code,"SECURITY_NAME_ABBR": "贵州茅台",@@ -65,15 +65,13 @@ class TestToolContract:class TestSuccess:def test_single_code_returns_history(self) -> None:rows = [_row("600519", "2024-06-01"), _row("600519", "2023-06-01")]- with patch.object(- eastmoney_client, "get_json", return_value=_payload(rows)- ) as get_json:+ with patch.object(eastmoney_client, "get_json", return_value=_payload(rows)) as get_json:out = json.loads(LockupExpiryTool().execute(code="600519.SH"))# Single-code path keys on the bare numeric code, newest-first._, kwargs = get_json.call_argsparams = kwargs["params"]- assert params["reportName"] == "RPT_LIFT_STOCK"+ assert params["reportName"] == "RPT_LIFT_STAGE"assert 'SECURITY_CODE="600519"' in params["filter"]assert params["sortTypes"] == "-1"@@ -89,11 +87,10 @@ class TestSuccess:def test_market_calendar_uses_horizon_window(self) -> None:rows = [_row("000001", "2026-07-01")]- with patch.object(- lockup_expiry_tool, "_today", return_value=date(2026, 6, 19)- ), patch.object(- eastmoney_client, "get_json", return_value=_payload(rows)- ) as get_json:+ with (+ patch.object(lockup_expiry_tool, "_today", return_value=date(2026, 6, 19)),+ patch.object(eastmoney_client, "get_json", return_value=_payload(rows)) as get_json,+ ):out = json.loads(LockupExpiryTool().execute(horizon_days=30))_, kwargs = get_json.call_args@@ -108,17 +105,13 @@ class TestSuccess:assert out["data"]["as_of"] == "2026-06-19"def test_horizon_clamped_to_max(self) -> None:- with patch.object(- eastmoney_client, "get_json", return_value=_payload([])- ):+ with patch.object(eastmoney_client, "get_json", return_value=_payload([])):out = json.loads(LockupExpiryTool().execute(horizon_days=99999))assert out["data"]["horizon_days"] == 365def test_payload_capped(self) -> None:rows = [_row(f"6005{i:02d}", "2026-07-01") for i in range(250)]- with patch.object(- eastmoney_client, "get_json", return_value=_payload(rows)- ):+ with patch.object(eastmoney_client, "get_json", return_value=_payload(rows)):out = json.loads(LockupExpiryTool().execute(horizon_days=90))assert out["data"]["count"] == 200assert out["data"]["truncated"] is True@@ -136,13 +129,49 @@ class TestErrors:assert "unrecognized" in out["error"]def test_upstream_failure_returns_error_envelope(self) -> None:- with patch.object(- eastmoney_client, "get_json", side_effect=RuntimeError("eastmoney boom")- ):+ with patch.object(eastmoney_client, "get_json", side_effect=RuntimeError("eastmoney boom")):out = json.loads(LockupExpiryTool().execute(code="600519"))assert out["ok"] is Falseassert "eastmoney boom" in out["error"]+ def test_provider_rejection_is_not_reported_as_empty(self) -> None:+ rejected = {+ "success": False,+ "code": 9501,+ "message": "FREE_SHARES_TYPE返回字段不存在",+ "result": None,+ }+ with patch.object(eastmoney_client, "get_json", return_value=rejected):+ out = json.loads(LockupExpiryTool().execute(code="600519"))+ assert out["ok"] is False+ assert "9501" in out["error"]+ assert "FREE_SHARES_TYPE返回字段不存在" in out["error"]++ def test_empty_provider_result_remains_successful_empty_schedule(self) -> None:+ empty = {+ "success": False,+ "code": 9201,+ "message": "返回数据为空",+ "result": None,+ }+ with patch.object(eastmoney_client, "get_json", return_value=empty):+ out = json.loads(LockupExpiryTool().execute(code="600519"))+ assert out["ok"] is True+ assert out["data"]["count"] == 0+ assert out["data"]["records"] == []++ def test_empty_message_with_rejection_code_is_not_reported_as_empty(self) -> None:+ rejected = {+ "success": False,+ "code": 9501,+ "message": "返回数据为空",+ "result": None,+ }+ with patch.object(eastmoney_client, "get_json", return_value=rejected):+ out = json.loads(LockupExpiryTool().execute(code="600519"))+ assert out["ok"] is False+ assert "9501" in out["error"]+# ---------------------------------------------------------------------------# End-to-end — only the frozen HTTP boundary mocked, real client routing runs.@@ -152,14 +181,12 @@ class TestErrors:class TestEndToEndHttpMocked:def test_single_code_through_real_client(self) -> None:rows = [_row("600519", "2024-06-01")]- with patch.object(- eastmoney_client, "throttled_get_json", return_value=_payload(rows)- ) as http:+ with patch.object(eastmoney_client, "throttled_get_json", return_value=_payload(rows)) as http:out = json.loads(LockupExpiryTool().execute(code="600519"))http.assert_called_once()_, kwargs = http.call_argsassert kwargs["host_key"] == "eastmoney"- assert kwargs["params"]["reportName"] == "RPT_LIFT_STOCK"+ assert kwargs["params"]["reportName"] == "RPT_LIFT_STAGE"assert out["ok"] is Trueassert out["data"]["records"][0]["name"] == "贵州茅台"