Download src/polyhedra/services/quant_runtime.py from polyHedra/PolyHedra: direct link, hf CLI and curl.
- Browser
- Download file 28.2 kB
-
https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/src/polyhedra/services/quant_runtime.py
- Command line
-
hf download hf://spaces/polyHedra/PolyHedra/src/polyhedra/services/quant_runtime.py
-
curl -L -o quant_runtime.py https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/src/polyhedra/services/quant_runtime.py
28.2 kB
| """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]] | |
| class QuantRuntimeResult: | |
| record: QuantRunRecord | |
| finley: PublisherOutput | None | |
| baseline: BaselineSelection | |
| 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 | |