"""Frozen V8 routing plans preserve general, specialized, and support scopes.""" import pytest from pydantic import ValidationError from polyhedra.core.active_routing import build_active_routing_plan def build(tickers, context="YOLO"): return build_active_routing_plan( run_id="run1", execution_context=context, tickers=list(tickers), ) def names(plan): return [item.agent for item in plan.active_agents] def dependencies(plan, agent): return { item.agent: list(item.overlapping_ticker_scope) for item in plan.for_agent(agent).required_dependencies } def test_general_ticker_preserves_general_quant_path(): plan = build(["MSFT"]) assert names(plan) == [ "DataFetcher", "TrendAnalyst", "SentimentAnalyst", "QuantResearcher", "Backtester", "RiskAnalyst", "QuantStrategist", "Publisher", ] assert dependencies(plan, "QuantResearcher") == { "TrendAnalyst": ["MSFT"], "SentimentAnalyst": ["MSFT"], } assert plan.for_agent("SentimentAnalyst").declared_scope == "GENERAL" def test_spy_uses_specialized_path_and_options_join(): plan = build(["SPY"]) assert "SentimentAnalyst" not in names(plan) assert "SPYAnalyst" in names(plan) assert "BTCAnalyst" not in names(plan) assert dependencies(plan, "MacroContextWatcher") == { "MarketEventWatcher": ["SPY"], "OptionsFlowWatcher": ["SPY"], } assert dependencies(plan, "QuantResearcher") == { "TrendAnalyst": ["SPY"], "SPYAnalyst": ["SPY"], } def test_btc_prunes_options_without_weakening_specialized_path(): plan = build(["BTC"]) assert "OptionsFlowWatcher" not in names(plan) assert dependencies(plan, "MacroContextWatcher") == {"MarketEventWatcher": ["BTC"]} assert dependencies(plan, "UnusualEventDetector") == { "MarketEventWatcher": ["BTC"], "MacroContextWatcher": ["BTC"], } assert dependencies(plan, "QuantResearcher")["BTCAnalyst"] == ["BTC"] def test_mixed_scope_keeps_both_branches_and_overlap_metadata(): plan = build([" msft ", "SPY", "spy", "BTC"]) assert plan.requested_ticker_scope == ["MSFT", "SPY", "BTC"] assert plan.for_agent("SPYAnalyst").supported_ticker_scope == ("SPY", "BTC") assert plan.for_agent("SPYAnalyst").effective_ticker_scope == ("SPY",) assert dependencies(plan, "MacroContextWatcher")["OptionsFlowWatcher"] == ["SPY"] assert dependencies(plan, "QuantResearcher") == { "TrendAnalyst": ["MSFT", "SPY", "BTC"], "SentimentAnalyst": ["MSFT"], "BTCAnalyst": ["BTC"], "SPYAnalyst": ["SPY"], } def test_support_contexts_activate_only_their_registered_root_agent(): visual = build(["SPY"], "VISUALIZATION") monitor = build(["SPY"], "PORTFOLIO_MONITOR") assert names(visual) == ["VizAgent"] assert names(monitor) == ["PortfolioMonitor"] assert len(visual.active_agents) + len(visual.inactive_agents) == 19 assert len(monitor.active_agents) + len(monitor.inactive_agents) == 19 def test_plan_is_frozen_and_rejects_empty_or_invalid_scope(): plan = build(["SPY"]) with pytest.raises(ValidationError): plan.active_agents[0].effective_ticker_scope = ("QQQ",) with pytest.raises(ValidationError): build([]) with pytest.raises(ValidationError): build(["SPY*"])