Download tests/test_quant_v6/test_scope_routing.py from polyHedra/PolyHedra: direct link, hf CLI and curl.
- Browser
- Download file 14.9 kB
-
https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_quant_v6/test_scope_routing.py
- Command line
-
hf download hf://spaces/polyHedra/PolyHedra/tests/test_quant_v6/test_scope_routing.py
-
curl -L -o test_scope_routing.py https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_quant_v6/test_scope_routing.py
14.9 kB
| from __future__ import annotations | |
| import asyncio | |
| from datetime import date | |
| from pathlib import Path | |
| import sys | |
| import types | |
| from types import SimpleNamespace | |
| from typing import Any | |
| import pytest | |
| from polyhedra.agents.base import Agent, AgentResponse, Mention | |
| sentence_transformers_stub = types.ModuleType("sentence_transformers") | |
| sentence_transformers_stub.SentenceTransformer = object | |
| sys.modules.setdefault("sentence_transformers", sentence_transformers_stub) | |
| from polyhedra.agents.coordinator import CoordinatorService | |
| from polyhedra.agents.quant import ( | |
| EventRouter, | |
| MacroContextWatcher, | |
| MarketEventWatcher, | |
| OptionsFlowWatcher, | |
| QuantStrategistAgent, | |
| RiskAnalystAgent, | |
| SPYAnalyst, | |
| TrendAnalystAgent, | |
| UnusualEventDetector, | |
| ) | |
| from polyhedra.agents.quant.market_scope import split_v5_v6_markets | |
| from polyhedra.core.role_registry import RoleEntry | |
| from polyhedra.core.session import SessionManager | |
| from polyhedra.messaging.slack import SlackIntegration, _extract_tickers_from_text | |
| from polyhedra.services.file_writer import FileWriter | |
| class _FakeClient: | |
| def __init__(self) -> None: | |
| self.messages: list[str] = [] | |
| async def chat_postMessage(self, channel: str, text: str = "", **_: object) -> dict[str, str]: | |
| self.messages.append(text) | |
| return {"ts": str(len(self.messages))} | |
| class _FakeService: | |
| def __init__(self, session_dir: Path) -> None: | |
| self.dispatched: list[str] = [] | |
| self.runs: list[list[str]] = [] | |
| self._mode = "investigation" | |
| self.session = SimpleNamespace(session_dir=session_dir) | |
| async def dispatch(self, from_role: str, text: str) -> None: | |
| self.dispatched.append(text) | |
| async def run_yolo(self, tickers: list[str]): | |
| self.runs.append(tickers) | |
| class _FakeYoloService(_FakeService): | |
| def __init__(self, session_dir: Path) -> None: | |
| super().__init__(session_dir) | |
| self.session.goal = "YOLO & SPY Analysis Session" | |
| self.prepared: list[list[str]] = [] | |
| self.runs: list[list[str]] = [] | |
| self._mode = "investigation" | |
| self._yolo_update_fn = None | |
| def prepare_yolo_run(self, tickers: list[str]): | |
| self.prepared.append(tickers) | |
| return SimpleNamespace(label="Session-delta YOLO", run_mode="session_delta") | |
| async def run_yolo(self, tickers: list[str]): | |
| self.runs.append(tickers) | |
| class _FakeLLM: | |
| async def complete(self, prompt: str, model: str | None = None): | |
| if "UnusualEventDetector" in prompt: | |
| return ("SPY PRICE_EVENT significance 0.55. Monitor confirmation.", 10, 10) | |
| if "MacroContextWatcher" in prompt: | |
| return ("Main line: equity risk appetite. SPY relevance weight 0.7.", 10, 10) | |
| return ( | |
| "SPY specialized read: WATCH_FOR_CONFIRMATION. " | |
| "Confirmation: reclaim VWAP. Invalidation: lose prior low.\n" | |
| "Paper trading simulation only. Not financial advice.", | |
| 10, | |
| 10, | |
| ) | |
| class _RoutingLLM: | |
| async def complete(self, prompt: str, model: str | None = None): | |
| return ( | |
| "## SPY\nTechnical analysis complete.\n" | |
| '{"next": "sentimentanalyst", "reason": "normal V5 next step"}', | |
| 10, | |
| 10, | |
| ) | |
| class _HumanRoutingRiskLLM: | |
| async def complete(self, prompt: str, model: str | None = None): | |
| return ( | |
| "Risk report: all signals are Reject due to poor validation.\n" | |
| '{"next": "human", "reason": "All signals rejected; human review needed."}', | |
| 10, | |
| 10, | |
| ) | |
| class _NoTradeStrategyLLM: | |
| async def complete(self, prompt: str, model: str | None = None): | |
| return ( | |
| "No paper trade proposal passed risk review.\n\n" | |
| "Outcome: NO_TRADE. Watch for a reclaim of SPY 768 with volume " | |
| "or a clean break below 762 before rerunning.\n" | |
| '{"next": "publisher", "reason": "Strategy review ready for Finley."}\n' | |
| '{"proposals": []}', | |
| 10, | |
| 10, | |
| ) | |
| class _UnavailableTrendAgent(Agent): | |
| name = "TrendAnalyst" | |
| role = "trendanalyst" | |
| role_title = "Technical Analyst" | |
| system_prompt = "" | |
| async def handle( | |
| self, | |
| mention: Mention, | |
| thread_context: list[dict[str, Any]], | |
| research_state: dict[str, Any], | |
| ) -> AgentResponse: | |
| return AgentResponse( | |
| agent_role=self.role, | |
| content="Technical analysis could not be completed (LLM unavailable).", | |
| mentions=["sentimentanalyst"], | |
| ) | |
| async def can_handle(self, mention: Mention) -> bool: | |
| return mention.mentioned_role == self.role | |
| class _HumanRoutingBacktesterAgent(Agent): | |
| name = "Backtester" | |
| role = "backtester" | |
| role_title = "Backtester" | |
| system_prompt = "" | |
| async def handle( | |
| self, | |
| mention: Mention, | |
| thread_context: list[dict[str, Any]], | |
| research_state: dict[str, Any], | |
| ) -> AgentResponse: | |
| return AgentResponse( | |
| agent_role=self.role, | |
| content="Backtest complete. Human review is needed.", | |
| mentions=["human"], | |
| ) | |
| async def can_handle(self, mention: Mention) -> bool: | |
| return mention.mentioned_role == self.role | |
| class _RecordingRiskAgent(Agent): | |
| name = "RiskAnalyst" | |
| role = "riskanalyst" | |
| role_title = "Risk Analyst" | |
| system_prompt = "" | |
| async def handle( | |
| self, | |
| mention: Mention, | |
| thread_context: list[dict[str, Any]], | |
| research_state: dict[str, Any], | |
| ) -> AgentResponse: | |
| return AgentResponse(agent_role=self.role, content="RiskAnalyst ran after backtest.") | |
| async def can_handle(self, mention: Mention) -> bool: | |
| return mention.mentioned_role == self.role | |
| def test_split_v5_v6_markets() -> None: | |
| assert split_v5_v6_markets(["SPY", "AAPL", "BTC", "SPY"]) == ( | |
| ["AAPL"], | |
| ["SPY", "BTC"], | |
| ) | |
| def test_yolo_is_not_extracted_as_ticker() -> None: | |
| assert _extract_tickers_from_text("YOLO SPY") == ["SPY"] | |
| assert _extract_tickers_from_text("run yolo QQQ") == ["QQQ"] | |
| def test_yolo_status_shows_v6_planned_path_for_v6_goal() -> None: | |
| service = CoordinatorService.__new__(CoordinatorService) | |
| service.session = SimpleNamespace(goal="YOLO & SPY Analysis Session") | |
| service._yolo_completed = ["datafetcher"] | |
| status = service._build_yolo_status("datafetcher", done=True) | |
| assert "V6 specialized quant path" in status | |
| assert "DataFetcher" in status | |
| assert "TrendAnalyst" in status | |
| assert "MarketEventWatcher" in status | |
| assert "OptionsFlowWatcher" in status | |
| assert "MacroContextWatcher" in status | |
| assert "UnusualEventDetector" in status | |
| assert "EventRouter" in status | |
| assert "SPYAnalyst" in status | |
| assert "SentimentAnalyst" not in status | |
| async def test_yolo_blocks_llm_unavailable_agent_output(tmp_path: Path) -> None: | |
| manager = SessionManager(sessions_dir=tmp_path) | |
| session = manager.create(goal="YOLO & SPY Analysis Session", platform="web") | |
| session.registry.register(RoleEntry(role="trendanalyst", agent=_UnavailableTrendAgent())) | |
| posts: list[tuple[str, str]] = [] | |
| async def post_fn(role: str, content: str) -> None: | |
| posts.append((role, content)) | |
| async def checkpoint_fn(summary: str) -> None: | |
| raise AssertionError("YOLO LLM-unavailable guard must not request checkpoints") | |
| service = CoordinatorService( | |
| session=session, | |
| post_fn=post_fn, | |
| checkpoint_fn=checkpoint_fn, | |
| mode="yolo", | |
| ) | |
| await service.dispatch("human", "@trendanalyst SPY") | |
| await asyncio.sleep(0) | |
| assert any("LLM service is unavailable" in content for _, content in posts) | |
| assert not any(message.from_role == "sentimentanalyst" for message in session.full_thread()) | |
| async def test_risk_routes_rejected_v6_assessment_to_quantstrategist(tmp_path: Path) -> None: | |
| files = FileWriter(tmp_path) | |
| today = date.today().isoformat() | |
| quant_dir = tmp_path / "quant" | |
| quant_dir.mkdir() | |
| (quant_dir / f"backtest_results_{today}.md").write_text( | |
| "All candidate signals failed validation.", | |
| encoding="utf-8", | |
| ) | |
| agent = RiskAnalystAgent(llm_service=_HumanRoutingRiskLLM()) | |
| agent.files = files | |
| response = await agent.handle( | |
| Mention("riskanalyst", "backtester", "continue"), | |
| [], | |
| {"quant_state": {"watchlist": ["SPY"], "portfolio": []}}, | |
| ) | |
| assert response.mentions == ["quantstrategist"] | |
| assert "all signals are Reject" in response.content | |
| async def test_quantstrategist_allows_zero_trade_proposals(tmp_path: Path) -> None: | |
| files = FileWriter(tmp_path) | |
| today = date.today().isoformat() | |
| quant_dir = tmp_path / "quant" | |
| quant_dir.mkdir() | |
| (quant_dir / f"risk_assessment_{today}.md").write_text( | |
| "All signals are Reject.", | |
| encoding="utf-8", | |
| ) | |
| (quant_dir / f"backtest_results_{today}.md").write_text( | |
| "No signal has enough valid evidence.", | |
| encoding="utf-8", | |
| ) | |
| agent = QuantStrategistAgent(llm_service=_NoTradeStrategyLLM()) | |
| agent.files = files | |
| response = await agent.handle( | |
| Mention("quantstrategist", "riskanalyst", "continue"), | |
| [], | |
| {"quant_state": {"watchlist": ["SPY"], "portfolio": []}}, | |
| ) | |
| assert response.mentions == ["publisher"] | |
| assert "Strategy review ready" in response.content | |
| assert "NO_TRADE" in response.content | |
| assert response.state_patch == {} | |
| async def test_yolo_converts_upstream_human_route_to_next_planned_agent(tmp_path: Path) -> None: | |
| manager = SessionManager(sessions_dir=tmp_path) | |
| session = manager.create(goal="YOLO & SPY Analysis Session", platform="web") | |
| session.registry.register(RoleEntry(role="backtester", agent=_HumanRoutingBacktesterAgent())) | |
| session.registry.register(RoleEntry(role="riskanalyst", agent=_RecordingRiskAgent())) | |
| async def post_fn(role: str, content: str) -> None: | |
| return None | |
| async def checkpoint_fn(summary: str) -> None: | |
| raise AssertionError("YOLO upstream human routing must not request checkpoints") | |
| service = CoordinatorService( | |
| session=session, | |
| post_fn=post_fn, | |
| checkpoint_fn=checkpoint_fn, | |
| mode="yolo", | |
| ) | |
| await service.dispatch("human", "@backtester SPY") | |
| for _ in range(10): | |
| if any(message.from_role == "riskanalyst" for message in session.full_thread()): | |
| break | |
| await asyncio.sleep(0.05) | |
| messages = session.full_thread() | |
| assert any(message.from_role == "riskanalyst" for message in messages) | |
| async def test_quant_command_uses_shared_runtime_for_specialized_markets(tmp_path: Path) -> None: | |
| integration = SlackIntegration.__new__(SlackIntegration) | |
| service = _FakeService(tmp_path) | |
| integration._sessions = {"C123": service} | |
| await SlackIntegration._cmd_quant(integration, _FakeClient(), "C123", "SPY") | |
| assert service.runs == [["SPY"]] | |
| assert service.dispatched == [] | |
| assert service._mode == "yolo" | |
| async def test_trend_analyst_hands_v6_scope_to_market_event_watcher(tmp_path: Path) -> None: | |
| files = FileWriter(tmp_path) | |
| raw_dir = tmp_path / "quant" / "raw" | |
| raw_dir.mkdir(parents=True) | |
| from datetime import date | |
| today = date.today().isoformat() | |
| (raw_dir / f"SPY_{today}_summary.md").write_text("SPY raw summary", encoding="utf-8") | |
| agent = TrendAnalystAgent(llm_service=_RoutingLLM()) | |
| agent.files = files | |
| response = await agent.handle( | |
| Mention("trendanalyst", "datafetcher", "continue"), | |
| [], | |
| {"quant_state": {"watchlist": ["SPY"]}}, | |
| ) | |
| assert response.mentions == ["marketeventwatcher"] | |
| async def test_quant_command_uses_shared_runtime_for_general_markets(tmp_path: Path) -> None: | |
| integration = SlackIntegration.__new__(SlackIntegration) | |
| service = _FakeService(tmp_path) | |
| integration._sessions = {"C123": service} | |
| await SlackIntegration._cmd_quant(integration, _FakeClient(), "C123", "AAPL") | |
| assert service.runs == [["AAPL"]] | |
| assert service.dispatched == [] | |
| assert service._mode == "yolo" | |
| async def test_yolo_command_reuses_active_session_and_invokes_shared_runtime(tmp_path: Path) -> None: | |
| integration = SlackIntegration.__new__(SlackIntegration) | |
| service = _FakeYoloService(tmp_path) | |
| integration._sessions = {"C123": service} | |
| client = _FakeClient() | |
| await SlackIntegration._cmd_yolo(integration, client, "C123", "SPY") | |
| assert service._mode == "yolo" | |
| assert service.runs == [["SPY"]] | |
| assert service.prepared == [] | |
| assert service.dispatched == [] | |
| assert any("YOLO starting" in message for message in client.messages) | |
| async def test_v6_agent_nodes_route_to_specialized_asset_analyst(tmp_path: Path) -> None: | |
| files = FileWriter(tmp_path) | |
| state = {"quant_state": {"watchlist": ["SPY"]}} | |
| mention = Mention(mentioned_role="marketeventwatcher", from_role="human", content="analyze SPY") | |
| market = MarketEventWatcher() | |
| market.files = files | |
| response = await market.handle(mention, [], state) | |
| assert response.mentions == ["optionsflowwatcher"] | |
| options = OptionsFlowWatcher() | |
| options.files = files | |
| response = await options.handle( | |
| Mention("optionsflowwatcher", "marketeventwatcher", response.content), | |
| [], | |
| state, | |
| ) | |
| assert response.mentions == ["macrocontextwatcher"] | |
| macro = MacroContextWatcher(llm_service=_FakeLLM()) | |
| macro.files = files | |
| response = await macro.handle( | |
| Mention("macrocontextwatcher", "optionsflowwatcher", response.content), | |
| [], | |
| state, | |
| ) | |
| assert response.mentions == ["unusualeventdetector"] | |
| detector = UnusualEventDetector(llm_service=_FakeLLM()) | |
| detector.files = files | |
| response = await detector.handle( | |
| Mention("unusualeventdetector", "macrocontextwatcher", response.content), | |
| [], | |
| state, | |
| ) | |
| assert response.mentions == ["eventrouter"] | |
| router = EventRouter() | |
| router.files = files | |
| response = await router.handle(Mention("eventrouter", "unusualeventdetector", response.content), [], state) | |
| assert response.mentions == ["spyanalyst"] | |
| analyst = SPYAnalyst(llm_service=_FakeLLM()) | |
| analyst.files = files | |
| response = await analyst.handle(Mention("spyanalyst", "eventrouter", response.content), [], state) | |
| assert response.mentions == ["quantresearcher"] | |
| assert (tmp_path / "quant" / "asset_analysis_").exists() is False | |
| assert any(path.name.startswith("asset_analysis_") for path in (tmp_path / "quant").iterdir()) | |