File size: 14,050 Bytes
30cc2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44d82a2
30cc2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44d82a2
 
 
30cc2cc
44d82a2
30cc2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44d82a2
 
 
 
 
 
 
 
 
 
30cc2cc
 
 
 
 
44d82a2
30cc2cc
 
 
 
 
 
 
 
 
 
 
44d82a2
30cc2cc
 
 
 
 
44d82a2
30cc2cc
 
 
 
 
44d82a2
30cc2cc
 
 
 
 
44d82a2
 
 
 
 
30cc2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""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)