"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix: count sector lookup budget by A-shares and reject symlinked cache file
shadowinlife7ba36fcb4bdf
diff --git a/agent/src/api/runs_routes.py b/agent/src/api/runs_routes.pyindex 7991cba07..821f7e462 100644--- a/agent/src/api/runs_routes.py+++ b/agent/src/api/runs_routes.py@@ -570,16 +570,18 @@ def _build_positions_sector_map(run_dir: Path, run_id: str, *, refresh: bool) ->from backtest.engines._market_hooks import _detect_marketartifacts_dir = run_dir / "artifacts"+ cache_path = artifacts_dir / "sector_map.json"try:- artifacts_symlinked = artifacts_dir.is_symlink()+ symlinked = artifacts_dir.is_symlink() or cache_path.is_symlink()except OSError:- artifacts_symlinked = False- if artifacts_symlinked:+ symlinked = False+ if symlinked:# Mirror the factor scan's symlink rejection: a symlinked artifacts dir- # is treated as having no positions artifact rather than followed.+ # — or a symlinked sector_map.json cache file inside a real one — is+ # treated as having no positions artifact rather than followed, so+ # neither the cache read nor the cache rewrite can escape the run dir.return {"ok": True, "run_id": run_id, "symbols": {}, "note": "no positions artifact"}- cache_path = run_dir / "artifacts" / "sector_map.json"if not refresh:cached = _load_json_file(cache_path)if isinstance(cached, dict) and isinstance(cached.get("symbols"), dict):@@ -593,14 +595,17 @@ def _build_positions_sector_map(run_dir: Path, run_id: str, *, refresh: bool) ->resolved: Dict[str, Any] = {}a_share_to_resolve: List[str] = []- for index, symbol in enumerate(symbols):+ for symbol in symbols:asset_class = _detect_market(symbol)resolved[symbol] = {"asset_class": asset_class,"industry": None,"industry_source": None,}- if asset_class == "a_share" and index < _POSITIONS_SECTOR_MAX_SYMBOLS:+ # The budget counts A-share network lookups only — non-A-share symbols+ # never consume it, so a mixed book with a long non-A-share prefix+ # still resolves its first _POSITIONS_SECTOR_MAX_SYMBOLS A-shares.+ if asset_class == "a_share" and len(a_share_to_resolve) < _POSITIONS_SECTOR_MAX_SYMBOLS:a_share_to_resolve.append(symbol)if a_share_to_resolve:diff --git a/agent/tests/api/test_positions_sectors.py b/agent/tests/api/test_positions_sectors.pyindex 688b6037d..dcb033c0a 100644--- a/agent/tests/api/test_positions_sectors.py+++ b/agent/tests/api/test_positions_sectors.py@@ -279,6 +279,36 @@ def test_symlinked_artifacts_dir_returns_no_positions_note(tmp_path: Path, monkeget.assert_not_called()+def test_symlinked_cache_file_rejected_without_write_through(tmp_path: Path, monkeypatch) -> None:+ """A symlinked ``sector_map.json`` is rejected like a symlinked artifacts dir.++ The artifacts directory itself is real here; only the cache file is a+ symlink. Neither the cache read nor the cache rewrite may follow it, or a+ planted symlink becomes a write primitive outside the run directory.+ """+ run_dir = _make_run(tmp_path, "timestamp,600519.SH", ["2026-08-01T00:00:00,1.0"])+ target = tmp_path / "elsewhere.json"+ target.write_text("sentinel", encoding="utf-8")+ (run_dir / "artifacts" / "sector_map.json").symlink_to(target)+ client = _client(tmp_path, monkeypatch)++ with patch("src.tools.sector_tool.resolve_secid") as resolve, patch(+ "src.tools.sector_tool.get_json"+ ) as get:+ response = client.get(f"/runs/{RUN_ID}/positions/sectors")++ assert response.status_code == 200+ assert response.json() == {+ "ok": True,+ "run_id": RUN_ID,+ "symbols": {},+ "note": "no positions artifact",+ }+ resolve.assert_not_called()+ get.assert_not_called()+ assert target.read_text(encoding="utf-8") == "sentinel"++# ============================================================================# Endpoint: bounded contract for large books# ============================================================================@@ -328,6 +358,87 @@ def test_large_symbol_list_caps_network_lookups(tmp_path: Path, monkeypatch) ->assert payload["unresolved"] == symbols[200:]+def _us_symbols(count: int) -> list[str]:+ return [f"{chr(65 + i // 26)}{chr(65 + i % 26)}.US" for i in range(count)]+++def _any_secid(symbol: str) -> str | None:+ return f"1.{symbol.split('.')[0]}"+++def _any_get_json(url: str, *, params: dict) -> dict:+ code = params["secid"].split(".")[1]+ return {+ "data": {+ "diff": {+ "0": {"f12": code, "f13": 1, "f14": "S"},+ "1": {"f12": "BK0001", "f13": 90, "f14": "银行Ⅱ"},+ }+ }+ }+++def test_mixed_book_resolves_a_shares_after_non_a_share_prefix(tmp_path: Path, monkeypatch) -> None:+ """The lookup budget counts A-share lookups, not list position.++ Regression: gating on the symbol's index in the full list pushed every+ A-share past the cap once 200+ non-A-share names came first, leaving the+ whole book unresolved while the 200-lookup budget sat unused.+ """+ us = _us_symbols(205)+ a_shares = [f"6{i:05d}.SH" for i in range(5)]+ symbols = us + a_shares+ _make_run(+ tmp_path,+ "timestamp," + ",".join(symbols),+ ["2026-08-01T00:00:00," + ",".join(["0.001"] * len(symbols))],+ )+ client = _client(tmp_path, monkeypatch)++ with patch(+ "src.tools.sector_tool.resolve_secid", side_effect=_any_secid+ ), patch("src.tools.sector_tool.get_json", side_effect=_any_get_json) as get:+ response = client.get(f"/runs/{RUN_ID}/positions/sectors")++ assert response.status_code == 200+ payload = response.json()+ assert payload["total_symbols"] == len(symbols)+ # Every A-share resolves despite sitting at list indices 205-209.+ assert get.call_count == len(a_shares)+ for symbol in a_shares:+ assert payload["symbols"][symbol]["industry"] == "银行Ⅱ"+ assert payload["unresolved"] == []+ for symbol in us:+ assert payload["symbols"][symbol]["asset_class"] == "us_equity"+ assert payload["symbols"][symbol]["industry"] is None+++def test_mixed_book_cap_still_bounds_a_share_lookups(tmp_path: Path, monkeypatch) -> None:+ us = _us_symbols(205)+ a_shares = [f"6{i:05d}.SH" for i in range(250)]+ symbols = us + a_shares+ _make_run(+ tmp_path,+ "timestamp," + ",".join(symbols),+ ["2026-08-01T00:00:00," + ",".join(["0.001"] * len(symbols))],+ )+ client = _client(tmp_path, monkeypatch)++ with patch(+ "src.tools.sector_tool.resolve_secid", side_effect=_any_secid+ ), patch("src.tools.sector_tool.get_json", side_effect=_any_get_json) as get:+ response = client.get(f"/runs/{RUN_ID}/positions/sectors")++ assert response.status_code == 200+ payload = response.json()+ assert payload["total_symbols"] == len(symbols)+ assert payload["symbol_limit"] == 200+ assert get.call_count == 200+ assert payload["symbols"][a_shares[0]]["industry"] == "银行Ⅱ"+ assert payload["symbols"][a_shares[-1]]["industry"] is None+ assert payload["unresolved"] == a_shares[200:]++def test_shared_executor_created_once_and_bounded(tmp_path: Path, monkeypatch) -> None:"""One process-lifetime executor serves every request; the 200 cap holds.