File size: 28,165 Bytes
30cc2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5fcb2d
44d82a2
 
30cc2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5fcb2d
30cc2cc
 
e5fcb2d
 
30cc2cc
44d82a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5fcb2d
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
"""Shared typed quant runtime used by Slack and the browser YOLO entry."""

from __future__ import annotations

import inspect
from collections.abc import Awaitable, Callable, Mapping, Sequence
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any

from pydantic import BaseModel

from polyhedra.agents.base import AgentResponse
from polyhedra.agents.quant.asset_contracts import (
    AssetAnalysisCollection, BTCAnalystOutput, GLDAnalystOutput,
    QQQAnalystOutput, SPYAnalystOutput,
)
from polyhedra.agents.quant.decision_contracts import CommitteePerformance
from polyhedra.agents.quant.io_registry import quant_artifact_schema, quant_io_binding
from polyhedra.agents.quant.market_data_contracts import MarketDataArtifact
from polyhedra.agents.quant.product_contracts import PublisherOutput
from polyhedra.core.active_routing import ActiveAgentPlan, build_active_routing_plan
from polyhedra.core.artifacts import JsonArtifactRef, RunMode
from polyhedra.core.baseline_selector import BaselineSelection, select_typed_yolo_baseline
from polyhedra.core.dependency_executor import (
    AgentCompletion, AgentExecutionState, DependencyExecutionResult,
    DependencyExecutor,
)
from polyhedra.core.quant_contracts import AgentName, ExecutionContext, load_quant_contracts
from polyhedra.core.research_state import ResearchStateManager
from polyhedra.core.state_patcher import StatePatcher
from polyhedra.core.trace_logger import TraceLogger
from polyhedra.services.artifact_publisher import ArtifactPublisher
from polyhedra.services.asset_analysis_collection import build_asset_analysis_collection
from polyhedra.services.evidence_ledger import write_typed_evidence_ledger
from polyhedra.services.error_sanitizer import sanitize_exception
from polyhedra.services.file_writer import FileWriter, PublicationRegistry, ScopedFileWriter
from polyhedra.services.llm_service import StructuredOutputError
from polyhedra.services.published_inputs import ActivationArtifactSnapshot, PublishedInputResolver
from polyhedra.services.quant_run_record import (
    QuantRunRecord, QuantRunRecordStore, QuantRunStatus, build_quant_run_record,
)
from polyhedra.services.strategist_performance import StrategistPerformanceTracker


ProgressFn = Callable[[AgentName, AgentExecutionState, str], Awaitable[None]]
BeforeAgentFn = Callable[[AgentName, BaseModel], Awaitable[None]]


@dataclass(frozen=True)
class QuantRuntimeResult:
    record: QuantRunRecord
    finley: PublisherOutput | None
    baseline: BaselineSelection


@dataclass(frozen=True)
class _Activation:
    inputs: BaseModel
    files: ScopedFileWriter


class QuantRuntime:
    """Execute existing quant agents from the packaged routing and ACL tables."""

    def __init__(
        self,
        *,
        session_dir,
        session_id: str,
        agents: Mapping[AgentName, Any],
        writer: FileWriter,
        trace: TraceLogger,
        progress: ProgressFn | None = None,
        before_agent: BeforeAgentFn | None = None,
    ) -> None:
        self._session_dir = session_dir
        self._session_id = session_id
        self._agents = dict(agents)
        self._writer = writer
        self._trace = trace
        self._progress = progress
        self._before_agent = before_agent
        self._patcher = StatePatcher()
        self._publications = PublicationRegistry()
        self._outputs: dict[AgentName, BaseModel] = {}
        self._asset_collection_ref: JsonArtifactRef | None = None
        self._input_paths: dict[AgentName, str] = {}
        self._research_state: dict[str, Any] = {}
        self._selection: BaselineSelection | None = None
        self._publisher: ArtifactPublisher | None = None
        self._as_of: datetime | None = None

    async def run_yolo(
        self,
        *,
        run_id: str,
        tickers: list[str],
        started_at: datetime | None = None,
    ) -> QuantRuntimeResult:
        """Run the frozen YOLO graph and persist its immutable typed record."""
        self._as_of = started_at or datetime.now(tz=timezone.utc)
        store = QuantRunRecordStore(self._session_dir, session_id=self._session_id)
        self._selection = select_typed_yolo_baseline(
            store.baseline_candidates(),
            session_id=self._session_id,
            current_run_id=run_id,
            ticker_scope=tickers,
        )
        for reference in self._selection.baseline_artifacts:
            self._publications.publish(reference.path, reference)

        plan = build_active_routing_plan(
            run_id=run_id,
            execution_context=ExecutionContext.YOLO,
            tickers=tickers,
        )
        missing = {item.agent for item in plan.active_agents} - set(self._agents)
        if missing:
            raise RuntimeError(f"active quant agents are not registered: {', '.join(sorted(missing))}")

        self._publisher = ArtifactPublisher(
            self._writer, session_id=self._session_id, run_id=run_id,
        )
        self._research_state = ResearchStateManager(self._session_dir).load().to_dict()
        self._update_run_state()

        executor = DependencyExecutor(
            plan,
            session_id=self._session_id,
            runner=self._run_agent,
            input_capture=self._capture_input,
        )
        execution = await executor.run()
        await self._report_blocked(execution)
        completed_at = datetime.now(tz=timezone.utc)
        additional = [
            reference for reference in self._publications.references()
            if reference.run_id == run_id
        ]
        record = build_quant_run_record(
            plan=plan,
            execution=execution,
            session_id=self._session_id,
            run_mode=self._selection.run_mode,
            baseline_run_id=self._selection.baseline_run_id,
            started_at=self._as_of,
            completed_at=completed_at,
            input_snapshot_paths=dict(self._input_paths),
            permission_templates=self._permission_templates(plan),
            permission_snapshots=self._permission_snapshots(plan),
            additional_artifacts=_unique_additional_artifacts(execution, additional),
        )
        write_typed_evidence_ledger(self._session_dir, record)
        store.save(record)
        self._persist_state(record, completed_at)
        finley = self._outputs.get("Publisher")
        return QuantRuntimeResult(
            record=record,
            finley=finley if isinstance(finley, PublisherOutput) else None,
            baseline=self._selection,
        )

    def _capture_input(
        self,
        item: ActiveAgentPlan,
        dependencies,
    ) -> _Activation:
        if item.agent == "QuantResearcher":
            self._publish_asset_collection(item)
        files = self._scoped_files(item)
        readable = set(files.readable_paths())
        required_refs = tuple(
            record.artifact_ref
            for record in dependencies
            if record.artifact_ref is not None and record.artifact_ref.path in readable
        )
        snapshot = PublishedInputResolver(files).capture(required_refs=required_refs)
        inputs = self._build_input(item, snapshot, files)
        return _Activation(inputs=inputs, files=files)

    async def _run_agent(
        self,
        item: ActiveAgentPlan,
        activation: _Activation,
    ) -> AgentCompletion:
        if self._publisher is None:
            raise RuntimeError("quant runtime publisher is not initialized")
        agent = self._agents[item.agent]
        if self._before_agent is not None:
            await self._before_agent(item.agent, activation.inputs)
        await self._notify(item.agent, AgentExecutionState.RUNNING, "")
        trace_id = self._trace.start_run(
            getattr(agent, "role", item.agent),
            prompt=activation.inputs.model_dump_json(),
            session_id=self._session_id,
        )
        token_log = _token_log(self._research_state)
        role = getattr(agent, "role", item.agent)
        role_count = sum(entry.get("agent_role") == role for entry in token_log)
        try:
            kwargs: dict[str, Any] = {
                "files": activation.files,
                "publisher": self._publisher,
            }
            signature = inspect.signature(agent.execute_typed)
            if "research_state" in signature.parameters:
                kwargs["research_state"] = self._research_state
            completion = await agent.execute_typed(activation.inputs, **kwargs)
            output = self._load_output(completion.artifact_ref)
            self._outputs[item.agent] = output
            self._input_paths[item.agent] = (
                f"quant/runs/{completion.artifact_ref.run_id}/inputs/{item.agent}.json"
            )
            if item.agent == "QuantStrategist":
                self._record_committee(output, activation.files)
            usage = [
                entry for entry in _token_log(self._research_state)
                if entry.get("agent_role") == role
            ][role_count:]
            response = AgentResponse(
                agent_role=role,
                content=output.model_dump_json(),
                artifact_path=completion.artifact_ref.path,
                output=output,
            )
            self._trace.end_run(
                trace_id,
                response,
                prompt_tokens=sum(int(entry.get("prompt_tokens", 0)) for entry in usage),
                completion_tokens=sum(int(entry.get("completion_tokens", 0)) for entry in usage),
                estimated_cost_usd=sum(float(entry.get("estimated_cost_usd", 0)) for entry in usage),
            )
            await self._notify(item.agent, AgentExecutionState.COMPLETED, completion.result_status)
            return completion
        except BaseException as exc:
            safe_error = sanitize_exception(exc)
            response = AgentResponse(
                agent_role=role,
                content=safe_error,
                error=safe_error,
            )
            usage = [
                entry for entry in _token_log(self._research_state)
                if entry.get("agent_role") == role
            ][role_count:]
            prompt_tokens = sum(int(entry.get("prompt_tokens", 0)) for entry in usage)
            completion_tokens = sum(int(entry.get("completion_tokens", 0)) for entry in usage)
            if isinstance(exc, StructuredOutputError):
                prompt_tokens += exc.input_tokens
                completion_tokens += exc.output_tokens
            self._trace.end_run(
                trace_id,
                response,
                prompt_tokens=prompt_tokens,
                completion_tokens=completion_tokens,
                estimated_cost_usd=None,
            )
            await self._notify(item.agent, AgentExecutionState.FAILED, safe_error)
            raise

    def _build_input(
        self,
        item: ActiveAgentPlan,
        snapshot: ActivationArtifactSnapshot,
        files: ScopedFileWriter,
    ) -> BaseModel:
        if self._selection is None or self._as_of is None:
            raise RuntimeError("quant runtime input context is not initialized")
        current = [*snapshot.required, *snapshot.optional]
        baseline = list(snapshot.baseline)
        output = lambda producer: _agent_output(current, producer)
        prior = lambda producer: _agent_output(baseline, producer)
        markets = lambda scope: [
            loaded.value for loaded in current
            if isinstance(loaded.value, MarketDataArtifact) and loaded.value.ticker in scope
        ]
        refs = [loaded.reference for loaded in baseline]
        scope = list(item.effective_ticker_scope)
        common: dict[str, Any] = {
            "run_id": self._selection.current_run_id,
            "session_id": self._session_id,
            "run_mode": self._selection.run_mode,
            "as_of": self._as_of,
            "ticker_scope": scope,
        }
        agent = item.agent
        if agent == "DataFetcher":
            raw = {
                loaded.value.ticker: loaded.reference
                for loaded in baseline
                if isinstance(loaded.value, MarketDataArtifact) and loaded.value.ticker in scope
            }
            data = {**common, "baseline_refs": raw}
        else:
            data = {**common, "baseline_refs": refs}
            if agent == "OptionsFlowWatcher":
                data.update(dependencies={}, optional_context={"provider_fetch_cutoff": self._as_of})
            elif agent == "TrendAnalyst":
                data.update(
                    dependencies={"data_fetch": output("DataFetcher"), "market_data": markets(scope)},
                    optional_context={"prior_trend": prior("TrendAnalyst")},
                )
            elif agent in {"SentimentAnalyst", "MarketEventWatcher"}:
                prior_name = "SentimentAnalyst" if agent == "SentimentAnalyst" else "MarketEventWatcher"
                prior_field = "prior_sentiment" if agent == "SentimentAnalyst" else "prior_market_events"
                data.update(
                    dependencies={"data_fetch": output("DataFetcher"), "market_data": markets(scope)},
                    optional_context={
                        "current_trend": output("TrendAnalyst"),
                        prior_field: prior(prior_name),
                    },
                )
            elif agent == "MacroContextWatcher":
                data.update(
                    dependencies={
                        "market_events": output("MarketEventWatcher"),
                        "options_flow": output("OptionsFlowWatcher"),
                    },
                    optional_context={"prior_macro": prior("MacroContextWatcher")},
                )
            elif agent == "UnusualEventDetector":
                data.update(
                    dependencies={
                        "market_events": output("MarketEventWatcher"),
                        "macro_context": output("MacroContextWatcher"),
                        "options_flow": output("OptionsFlowWatcher"),
                    },
                    optional_context={"prior_events": prior("UnusualEventDetector")},
                )
            elif agent == "EventRouter":
                data.update(
                    dependencies={"unusual_events": output("UnusualEventDetector")},
                    optional_context={
                        "market_events": output("MarketEventWatcher"),
                        "options_flow": output("OptionsFlowWatcher"),
                        "prior_route": prior("EventRouter"),
                    },
                )
            elif agent in {"BTCAnalyst", "SPYAnalyst", "QQQAnalyst", "GLDAnalyst"}:
                scoped_sentiment = output("SentimentAnalyst")
                if (
                    scoped_sentiment is not None
                    and not set(scope).intersection(scoped_sentiment.ticker_scope)
                ):
                    scoped_sentiment = None
                data.update(
                    dependencies={
                        "trend": output("TrendAnalyst"),
                        "event_route": output("EventRouter"),
                    },
                    optional_context={
                        "market_events": output("MarketEventWatcher"),
                        "options_flow": None if agent == "BTCAnalyst" else output("OptionsFlowWatcher"),
                        "macro_context": output("MacroContextWatcher"),
                        "unusual_events": output("UnusualEventDetector"),
                        "sentiment": scoped_sentiment,
                    },
                )
            elif agent == "QuantResearcher":
                data.update(
                    dependencies={
                        "trend": output("TrendAnalyst"),
                        "sentiment": output("SentimentAnalyst"),
                        "asset_analyses": _runtime_collection(current),
                    },
                    optional_context={
                        "macro_context": output("MacroContextWatcher"),
                        "unusual_events": output("UnusualEventDetector"),
                    },
                )
            elif agent == "Backtester":
                data.update(
                    dependencies={"signals": output("QuantResearcher")},
                    optional_context={"market_data": markets(scope)},
                )
            elif agent == "RiskAnalyst":
                data.update(
                    dependencies={"backtests": output("Backtester")},
                    optional_context={
                        "trend": output("TrendAnalyst"),
                        "asset_analyses": _runtime_collection(current),
                        "macro_context": output("MacroContextWatcher"),
                        "open_positions": self._risk_positions(scope),
                    },
                )
            elif agent == "QuantStrategist":
                tracker = StrategistPerformanceTracker.load(
                    files.for_trusted_performance_tool()
                )
                data.update(
                    dependencies={"risk": output("RiskAnalyst")},
                    optional_context={
                        "signals": output("QuantResearcher"),
                        "backtests": output("Backtester"),
                        "sentiment": output("SentimentAnalyst"),
                        "asset_analyses": _runtime_collection(current),
                        "macro_context": output("MacroContextWatcher"),
                        "committee_performance": CommitteePerformance(
                            weights=tracker.current_weights(), recorded_at=self._as_of,
                        ),
                    },
                )
            elif agent == "Publisher":
                data.update(
                    dependencies={"strategy": output("QuantStrategist")},
                    optional_context={
                        "trend": output("TrendAnalyst"),
                        "sentiment": output("SentimentAnalyst"),
                        "risk": output("RiskAnalyst"),
                        "asset_analyses": _runtime_collection(current),
                        "macro_context": output("MacroContextWatcher"),
                    },
                )
            else:
                raise ValueError(f"{agent} is not a YOLO agent")
        return quant_io_binding(agent).input_schema.model_validate(data)

    def _scoped_files(self, item: ActiveAgentPlan) -> ScopedFileWriter:
        if self._selection is None:
            raise RuntimeError("baseline selection is unavailable")
        return ScopedFileWriter(
            self._writer,
            agent=item.agent,
            run_id=self._selection.current_run_id,
            ticker_scope=list(item.effective_ticker_scope),
            baseline_run_id=self._selection.baseline_run_id,
            publications=self._publications,
        )

    def _load_output(self, reference: JsonArtifactRef) -> BaseModel:
        raw = self._writer.read(reference.path)
        if raw is None:
            raise FileNotFoundError(reference.path)
        return quant_artifact_schema(reference.producer).model_validate_json(raw, strict=True)

    def _publish_asset_collection(self, item: ActiveAgentPlan) -> None:
        if self._asset_collection_ref is not None:
            return
        specialized = [ticker for ticker in item.effective_ticker_scope if ticker in {"BTC", "SPY", "QQQ", "GLD"}]
        if not specialized:
            return
        if self._selection is None:
            raise RuntimeError("baseline selection is unavailable")
        outputs = [
            value for value in self._outputs.values()
            if isinstance(value, (BTCAnalystOutput, SPYAnalystOutput, QQQAnalystOutput, GLDAnalystOutput))
            and value.ticker_scope[0] in specialized
        ]
        collection = build_asset_analysis_collection(
            run_id=self._selection.current_run_id,
            session_id=self._session_id,
            created_at=datetime.now(tz=timezone.utc),
            ticker_scope=specialized,
            outputs=outputs,
            publications=self._publications,
        )
        path = f"quant/runs/{self._selection.current_run_id}/asset_analysis.json"
        self._writer.write_atomic(path, collection.model_dump_json(indent=2), agent_role="Runtime")
        reference = JsonArtifactRef(
            schema_version="8.0",
            artifact_id=collection.artifact_id,
            run_id=collection.run_id,
            session_id=collection.session_id,
            producer="Runtime",
            path=path,
        )
        self._publications.publish(path, reference)
        self._asset_collection_ref = reference

    def _risk_positions(self, scope: Sequence[str]) -> list[dict[str, Any]]:
        quant = self._research_state.get("quant_state") or {}
        positions = []
        for position in quant.get("portfolio") or []:
            if position.get("status", "open") != "open" or position.get("ticker") not in scope:
                continue
            positions.append({
                "position_id": position["position_id"],
                "ticker": position["ticker"],
                "direction": str(position["direction"]).upper(),
                "entry_price": float(position["entry_price"]),
                "portfolio_fraction": float(position["size_pct"]) / 100.0,
            })
        return positions

    def _record_committee(self, output: BaseModel, files: ScopedFileWriter) -> None:
        payload = output.payload
        trusted_files = files.for_trusted_performance_tool()
        tracker = StrategistPerformanceTracker.load(trusted_files)
        votes = [
            {
                "member_id": vote.member_id,
                "vote": vote.vote.value,
                "confidence": vote.confidence.value,
                "rationale": vote.thesis,
                "evidence_used": [item.model_dump(mode="json") for item in vote.cited_evidence],
                "proposal_impact": vote.proposal_impact,
            }
            for vote in payload.committee_votes
        ]
        result = tracker.record_votes(
            trusted_files,
            votes,
            run_mode=self._selection.run_mode.value if self._selection else "BASELINE",
            outcome=payload.outcome.value,
        )
        quant = self._research_state.get("quant_state")
        if not isinstance(quant, dict):
            quant = {}
            self._research_state["quant_state"] = quant
        quant["strategist_committee_votes"] = tracker.vote_history
        quant["strategist_committee_weights"] = result["weights"]
        quant["strategist_weight_updates"] = result["weight_updates"]
        quant["strategist_proposal_updates"] = [
            item.model_dump(mode="json") for item in payload.proposal_updates
        ]
        quant["trade_proposals_raw"] = [
            item.model_dump(mode="json") for item in payload.proposals
        ]

    def _update_run_state(self) -> None:
        if self._selection is None or self._as_of is None:
            return
        self._patcher.apply(self._session_dir, {
            "yolo_run_mode": (
                "session_delta" if self._selection.run_mode == RunMode.DELTA else "baseline"
            ),
            "yolo_run_id": self._selection.current_run_id,
            "yolo_run_started_at": self._as_of.isoformat(),
            "yolo_baseline_complete": self._selection.baseline_run_id is not None,
            "yolo_baseline_artifacts": {
                reference.producer: reference.path
                for reference in self._selection.baseline_artifacts
            },
            "yolo_missing_baseline_artifacts": list(self._selection.missing_tickers),
        })

    def _persist_state(self, record: QuantRunRecord, completed_at: datetime) -> None:
        quant = self._research_state.get("quant_state") or {}
        patch = {
            key: quant[key]
            for key in (
                "token_log", "trade_proposals_raw", "strategist_committee_votes",
                "strategist_committee_weights", "strategist_weight_updates",
                "strategist_proposal_updates",
            )
            if key in quant
        }
        if record.status == QuantRunStatus.COMPLETED:
            patch["yolo_last_completed_at"] = completed_at.isoformat()
        self._patcher.apply(self._session_dir, patch)

    def _permission_snapshots(self, plan) -> dict[AgentName, dict[str, list[str]]]:
        baseline_run_id = self._selection.baseline_run_id if self._selection else None
        contracts = load_quant_contracts()
        snapshots: dict[AgentName, dict[str, list[str]]] = {}
        for item in plan.active_agents:
            permissions = contracts.for_agent(item.agent).permissions.model_dump(mode="json")
            resolved: dict[str, list[str]] = {}
            for operation, templates in permissions.items():
                paths: list[str] = []
                selected_run = baseline_run_id if operation.startswith("baseline_") else plan.run_id
                for template in templates:
                    if not selected_run and "{baseline_run_id}" in template:
                        continue
                    path = template.replace("{run_id}", selected_run or "").replace(
                        "{baseline_run_id}", selected_run or "",
                    )
                    if "{ticker}" in path:
                        paths.extend(
                            path.replace("{ticker}", ticker)
                            for ticker in item.effective_ticker_scope
                        )
                    else:
                        paths.append(path)
                resolved[operation] = paths
            snapshots[item.agent] = resolved
        return snapshots

    def _permission_templates(self, plan) -> dict[AgentName, dict[str, list[str]]]:
        contracts = load_quant_contracts()
        return {
            item.agent: contracts.for_agent(item.agent).permissions.model_dump(mode="json")
            for item in plan.active_agents
        }

    async def _report_blocked(self, execution: DependencyExecutionResult) -> None:
        for record in execution.records:
            if record.state == AgentExecutionState.BLOCKED:
                await self._notify(
                    record.agent,
                    record.state,
                    ", ".join(record.blocked_by),
                )

    async def _notify(self, agent: AgentName, state: AgentExecutionState, detail: Any) -> None:
        if self._progress is not None:
            await self._progress(agent, state, str(detail or ""))


def _agent_output(loaded, producer: str) -> BaseModel | None:
    for item in loaded:
        if item.reference.producer != producer:
            continue
        if producer == "DataFetcher" and isinstance(item.value, MarketDataArtifact):
            continue
        if producer != "Runtime" and isinstance(item.value, quant_artifact_schema(producer)):
            return item.value
    return None


def _runtime_collection(loaded) -> AssetAnalysisCollection | None:
    return next(
        (item.value for item in loaded if isinstance(item.value, AssetAnalysisCollection)),
        None,
    )


def _token_log(state: dict[str, Any]) -> list[dict[str, Any]]:
    quant = state.get("quant_state")
    if not isinstance(quant, dict):
        quant = {}
        state["quant_state"] = quant
    return quant.setdefault("token_log", [])


def _unique_additional_artifacts(
    execution: DependencyExecutionResult,
    references,
) -> list:
    completed = {
        record.artifact_ref.path
        for record in execution.records
        if record.artifact_ref is not None
    }
    seen = set(completed)
    additional = []
    for reference in references:
        if reference.path in seen:
            continue
        seen.add(reference.path)
        additional.append(reference)
    return additional