PolyHedra / tests /test_quant_runtime.py
yf176
fix: preserve failed agent token usage in trace
44d82a2
Raw History Blame Contribute Delete
14.1 kB
"""Integrated V8 runtime coverage through the existing quant agent classes."""
import json
import zipfile
from datetime import datetime, timezone
from pathlib import Path
from types import SimpleNamespace
import pytest
from polyhedra.agents.quant import (
BacktesterAgent, DataFetcherAgent, EventRouter, MacroContextWatcher,
MarketEventWatcher, OptionsFlowWatcher, PublisherAgent, QuantResearcherAgent,
QuantStrategistAgent, RiskAnalystAgent, SentimentAnalystAgent, SPYAnalyst,
TrendAnalystAgent,
UnusualEventDetector,
)
from polyhedra.agents.quant.product_contracts import PublisherPayload
from polyhedra.core.research_state import QuantState, ResearchStateManager
from polyhedra.core.session import SessionManager
from polyhedra.core.trace_logger import TraceLogger
from polyhedra.services.file_writer import FileWriter
from polyhedra.services.llm_service import StructuredOutputError
from polyhedra.services.options_data import OptionsDataSnapshot
from polyhedra.services.quant_runtime import QuantRuntime
AT = datetime(2026, 9, 20, 12, tzinfo=timezone.utc)
class FailingFinnhub:
async def fetch_ticker_data(self, ticker, session_dir):
raise RuntimeError("controlled market-data outage")
class UnavailableOptions:
provider = "controlled-options"
max_expirations = 1
max_contracts_per_side = 1
def fetch(self, ticker):
return OptionsDataSnapshot(
symbol=ticker,
provider=self.provider,
retrieved_at=AT.isoformat(),
status="unavailable",
note="Controlled options outage.",
)
class OnlyFinleyLLM:
async def complete_structured(self, prompt, schema):
if schema is not PublisherPayload:
raise AssertionError(f"unexpected LLM call for {schema.__name__}")
typed_input, _ = json.JSONDecoder().raw_decode(prompt.split("Typed input:\n", 1)[1])
strategy = typed_input["dependencies"]["strategy"]
strategy_ref = {
"schema_version": "8.0",
"artifact_id": strategy["artifact_id"],
"run_id": strategy["run_id"],
"session_id": strategy["session_id"],
"producer": "QuantStrategist",
"path": f"quant/runs/{strategy['run_id']}/trade_proposals.json",
"format": "JSON",
}
evidence_refs = [{
"artifact": strategy_ref,
"field_path": "/payload/outcome",
}]
prior_post = next((
reference for reference in typed_input["baseline_refs"]
if reference["producer"] == "Publisher"
), None)
if prior_post is not None:
evidence_refs.append({
"artifact": prior_post,
"field_path": "/payload/market_summary",
})
return SimpleNamespace(
output=PublisherPayload(
headline="Analysis unavailable",
market_summary="Required market evidence was unavailable.",
what_to_watch=["Restore current market data before drawing a conclusion."],
displayed_proposal_ids=[],
no_proposal_reason="DATA_LOSS",
disclaimer="Paper trading simulation only. Not financial advice.",
approximate_word_count=1,
slack_rendering="placeholder",
delta_state="UNCHANGED" if prior_post else "NOT_COMPARED",
evidence_refs=evidence_refs,
data_gaps=[],
),
input_tokens=40,
output_tokens=20,
)
class FailingTrend:
role = "trendanalyst"
def __init__(self, error: Exception):
self.error = error
async def execute_typed(self, inputs, **kwargs):
raise self.error
@pytest.mark.asyncio
async def test_specialized_unavailable_run_reaches_finley_with_typed_provenance(tmp_path: Path):
session_dir = tmp_path / "session1"
session_dir.mkdir()
manager = ResearchStateManager(session_dir)
state = manager.load()
state.quant_state = QuantState(watchlist=["SPY"], mode="yolo")
manager.save(state)
bomb = OnlyFinleyLLM()
agents = {
"DataFetcher": DataFetcherAgent(FailingFinnhub()),
"OptionsFlowWatcher": OptionsFlowWatcher(UnavailableOptions()),
"TrendAnalyst": TrendAnalystAgent(bomb),
"MarketEventWatcher": MarketEventWatcher(bomb),
"MacroContextWatcher": MacroContextWatcher(bomb),
"UnusualEventDetector": UnusualEventDetector(bomb),
"EventRouter": EventRouter(bomb),
"SPYAnalyst": SPYAnalyst(bomb),
"QuantResearcher": QuantResearcherAgent(bomb),
"Backtester": BacktesterAgent(bomb),
"RiskAnalyst": RiskAnalystAgent(bomb),
"QuantStrategist": QuantStrategistAgent(bomb),
"Publisher": PublisherAgent(bomb),
}
runtime = QuantRuntime(
session_dir=session_dir,
session_id="session1",
agents=agents,
writer=FileWriter(session_dir, session_id="session1"),
trace=TraceLogger(session_dir),
)
result = await runtime.run_yolo(run_id="run1", tickers=["SPY"], started_at=AT)
assert result.record.status == "completed", result.record.execution.model_dump_json(indent=2)
assert result.finley is not None
assert result.finley.payload.no_proposal_reason == "DATA_LOSS"
assert "No paper trade proposal" in result.finley.payload.slack_rendering
assert all(item.state == "COMPLETED" for item in result.record.execution.records)
assert set(result.record.permission_snapshots) == {
item.agent for item in result.record.routing_plan.active_agents
}
assert (session_dir / f"yolo_runs/{result.record.run_id}.json").is_file()
assert (session_dir / f"quant/evidence_ledger_{result.record.run_id}.json").is_file()
ledger = (session_dir / f"quant/evidence_ledger_{result.record.run_id}.json").read_text()
assert "typed_runtime_reference" in ledger
assert "controlled market-data outage" in ledger
assert len((session_dir / "trace.jsonl").read_text().splitlines()) == len(
result.record.routing_plan.active_agents
)
@pytest.mark.asyncio
async def test_second_run_freezes_first_typed_run_as_delta_baseline(tmp_path: Path):
session_dir = tmp_path / "session1"
session_dir.mkdir()
manager = ResearchStateManager(session_dir)
state = manager.load()
state.quant_state = QuantState(watchlist=["SPY"], mode="yolo")
manager.save(state)
bomb = OnlyFinleyLLM()
agents = {
"DataFetcher": DataFetcherAgent(FailingFinnhub()),
"OptionsFlowWatcher": OptionsFlowWatcher(UnavailableOptions()),
"TrendAnalyst": TrendAnalystAgent(bomb),
"MarketEventWatcher": MarketEventWatcher(bomb),
"MacroContextWatcher": MacroContextWatcher(bomb),
"UnusualEventDetector": UnusualEventDetector(bomb),
"EventRouter": EventRouter(bomb),
"SPYAnalyst": SPYAnalyst(bomb),
"QuantResearcher": QuantResearcherAgent(bomb),
"Backtester": BacktesterAgent(bomb),
"RiskAnalyst": RiskAnalystAgent(bomb),
"QuantStrategist": QuantStrategistAgent(bomb),
"Publisher": PublisherAgent(bomb),
}
runtime_args = {
"session_dir": session_dir,
"session_id": "session1",
"agents": agents,
"writer": FileWriter(session_dir, session_id="session1"),
"trace": TraceLogger(session_dir),
}
first = await QuantRuntime(**runtime_args).run_yolo(
run_id="run1", tickers=["SPY"], started_at=AT,
)
second = await QuantRuntime(**runtime_args).run_yolo(
run_id="run2", tickers=["SPY"], started_at=AT,
)
assert first.record.run_mode == "BASELINE"
assert second.record.run_mode == "DELTA"
assert second.record.baseline_run_id == "run1"
assert second.finley is not None
assert second.finley.payload.delta_state == "UNCHANGED"
assert any(ref.run_id == "run1" for ref in second.finley.baseline_refs)
assert len(json.loads(
(session_dir / "quant/strategist_performance.json").read_text()
)["vote_history"]) == 0
@pytest.mark.asyncio
async def test_mixed_general_and_specialized_scope_joins_in_one_typed_run(tmp_path: Path):
session_dir = tmp_path / "session1"
session_dir.mkdir()
bomb = OnlyFinleyLLM()
agents = {
"DataFetcher": DataFetcherAgent(FailingFinnhub()),
"OptionsFlowWatcher": OptionsFlowWatcher(UnavailableOptions()),
"TrendAnalyst": TrendAnalystAgent(bomb),
"SentimentAnalyst": SentimentAnalystAgent(bomb),
"MarketEventWatcher": MarketEventWatcher(bomb),
"MacroContextWatcher": MacroContextWatcher(bomb),
"UnusualEventDetector": UnusualEventDetector(bomb),
"EventRouter": EventRouter(bomb),
"SPYAnalyst": SPYAnalyst(bomb),
"QuantResearcher": QuantResearcherAgent(bomb),
"Backtester": BacktesterAgent(bomb),
"RiskAnalyst": RiskAnalystAgent(bomb),
"QuantStrategist": QuantStrategistAgent(bomb),
"Publisher": PublisherAgent(bomb),
}
result = await QuantRuntime(
session_dir=session_dir,
session_id="session1",
agents=agents,
writer=FileWriter(session_dir, session_id="session1"),
trace=TraceLogger(session_dir),
).run_yolo(run_id="mixed1", tickers=["AAPL", "SPY"], started_at=AT)
active = {item.agent: item for item in result.record.routing_plan.active_agents}
assert result.record.status == "completed"
assert active["SentimentAnalyst"].effective_ticker_scope == ("AAPL",)
assert active["SPYAnalyst"].effective_ticker_scope == ("SPY",)
assert {item.agent for item in active["QuantResearcher"].required_dependencies} == {
"TrendAnalyst", "SentimentAnalyst", "SPYAnalyst",
}
assert result.finley is not None
@pytest.mark.asyncio
@pytest.mark.parametrize(
("error", "expected_tokens"),
[
(RuntimeError("controlled TrendAnalyst failure"), (0, 0)),
(StructuredOutputError(("invalid response",), 41, 17, 2), (41, 17)),
],
)
async def test_failed_agent_blocks_dependents_without_finley_opinion(
tmp_path: Path, error: Exception, expected_tokens: tuple[int, int],
):
session_dir = tmp_path / "session1"
session_dir.mkdir()
agents = {
"DataFetcher": DataFetcherAgent(FailingFinnhub()),
"OptionsFlowWatcher": OptionsFlowWatcher(UnavailableOptions()),
"TrendAnalyst": FailingTrend(error),
"MarketEventWatcher": MarketEventWatcher(OnlyFinleyLLM()),
"MacroContextWatcher": MacroContextWatcher(OnlyFinleyLLM()),
"UnusualEventDetector": UnusualEventDetector(OnlyFinleyLLM()),
"EventRouter": EventRouter(OnlyFinleyLLM()),
"SPYAnalyst": SPYAnalyst(OnlyFinleyLLM()),
"QuantResearcher": QuantResearcherAgent(OnlyFinleyLLM()),
"Backtester": BacktesterAgent(OnlyFinleyLLM()),
"RiskAnalyst": RiskAnalystAgent(OnlyFinleyLLM()),
"QuantStrategist": QuantStrategistAgent(OnlyFinleyLLM()),
"Publisher": PublisherAgent(OnlyFinleyLLM()),
}
trace = TraceLogger(session_dir)
result = await QuantRuntime(
session_dir=session_dir,
session_id="session1",
agents=agents,
writer=FileWriter(session_dir, session_id="session1"),
trace=trace,
).run_yolo(run_id="failed1", tickers=["SPY"], started_at=AT)
records = {item.agent: item for item in result.record.execution.records}
assert result.record.status == "failed"
assert records["TrendAnalyst"].state == "FAILED"
assert str(error) in records["TrendAnalyst"].error
assert records["SPYAnalyst"].state == "BLOCKED"
assert records["QuantResearcher"].state == "BLOCKED"
assert records["Publisher"].state == "BLOCKED"
assert result.finley is None
assert not (session_dir / "quant/runs/failed1/post.json").exists()
failed_trace = next(
entry for entry in trace.read_trace() if entry["agent_role"] == "trendanalyst"
)
assert (failed_trace["prompt_tokens"], failed_trace["completion_tokens"]) == expected_tokens
assert failed_trace["estimated_cost_usd"] is None
@pytest.mark.asyncio
async def test_export_contains_every_recorded_input_output_and_ledger(tmp_path: Path):
sessions_dir = tmp_path / "sessions"
manager = SessionManager(sessions_dir)
session = manager.create(goal="SPY export", platform="web")
bomb = OnlyFinleyLLM()
agents = {
"DataFetcher": DataFetcherAgent(FailingFinnhub()),
"OptionsFlowWatcher": OptionsFlowWatcher(UnavailableOptions()),
"TrendAnalyst": TrendAnalystAgent(bomb),
"MarketEventWatcher": MarketEventWatcher(bomb),
"MacroContextWatcher": MacroContextWatcher(bomb),
"UnusualEventDetector": UnusualEventDetector(bomb),
"EventRouter": EventRouter(bomb),
"SPYAnalyst": SPYAnalyst(bomb),
"QuantResearcher": QuantResearcherAgent(bomb),
"Backtester": BacktesterAgent(bomb),
"RiskAnalyst": RiskAnalystAgent(bomb),
"QuantStrategist": QuantStrategistAgent(bomb),
"Publisher": PublisherAgent(bomb),
}
result = await QuantRuntime(
session_dir=session.session_dir,
session_id=session.session_id,
agents=agents,
writer=FileWriter(session.session_dir, session_id=session.session_id),
trace=TraceLogger(session.session_dir),
).run_yolo(run_id="export1", tickers=["SPY"], started_at=AT)
archive_path = manager.export(session.session_id, tmp_path / "exports")
with zipfile.ZipFile(archive_path) as archive:
names = set(archive.namelist())
assert f"yolo_runs/{result.record.run_id}.json" in names
assert f"quant/evidence_ledger_{result.record.run_id}.json" in names
assert "quant/strategist_performance.json" in names
assert set(result.record.input_snapshot_paths.values()).issubset(names)
assert {item.path for item in result.record.artifact_inventory}.issubset(names)