Download src/polyhedra/services/published_inputs.py from polyHedra/PolyHedra: direct link, hf CLI and curl.
- Browser
- Download file 4.69 kB
-
https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/src/polyhedra/services/published_inputs.py
- Command line
-
hf download hf://spaces/polyHedra/PolyHedra/src/polyhedra/services/published_inputs.py
-
curl -L -o published_inputs.py https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/src/polyhedra/services/published_inputs.py
4.69 kB
| """Freeze validated published artifacts for one V8 agent activation.""" | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from pydantic import BaseModel | |
| from polyhedra.agents.quant.asset_contracts import AssetAnalysisCollection | |
| from polyhedra.agents.quant.io_registry import quant_artifact_schema | |
| from polyhedra.agents.quant.market_data_contracts import MarketDataArtifact | |
| from polyhedra.core.artifacts import JsonArtifactRef | |
| from polyhedra.services.file_writer import ArtifactAccessError, ScopedFileWriter | |
| class LoadedArtifact: | |
| reference: JsonArtifactRef | |
| value: BaseModel | |
| source_json: str | |
| class ActivationArtifactSnapshot: | |
| required: tuple[LoadedArtifact, ...] | |
| optional: tuple[LoadedArtifact, ...] | |
| baseline: tuple[LoadedArtifact, ...] | |
| def references(self) -> tuple[JsonArtifactRef, ...]: | |
| return tuple( | |
| item.reference for group in (self.required, self.optional, self.baseline) | |
| for item in group | |
| ) | |
| class PublishedInputResolver: | |
| """Read and validate the exact published JSON visible at activation time.""" | |
| def __init__(self, files: ScopedFileWriter) -> None: | |
| self._files = files | |
| def capture( | |
| self, | |
| *, | |
| required_refs: tuple[JsonArtifactRef, ...] = (), | |
| baseline_refs: tuple[JsonArtifactRef, ...] = (), | |
| ) -> ActivationArtifactSnapshot: | |
| explicit = (*required_refs, *baseline_refs) | |
| paths = [reference.path for reference in explicit] | |
| if len(paths) != len(set(paths)): | |
| raise ValueError("activation references contain duplicate filenames") | |
| for reference in required_refs: | |
| if reference.run_id != self._files.run_id: | |
| raise ValueError("required dependency reference is not from the current run") | |
| for reference in baseline_refs: | |
| if reference.run_id != self._files.baseline_run_id: | |
| raise ValueError("baseline reference is not from the selected baseline run") | |
| required = tuple(self._load(reference) for reference in required_refs) | |
| baseline = tuple(self._load(reference) for reference in baseline_refs) | |
| explicit_paths = set(paths) | |
| optional_current: list[LoadedArtifact] = [] | |
| optional_baseline: list[LoadedArtifact] = [] | |
| for reference in self._files.readable_published_json_refs(): | |
| if reference.path in explicit_paths: | |
| continue | |
| loaded = self._load(reference) | |
| if reference.run_id == self._files.run_id: | |
| optional_current.append(loaded) | |
| elif reference.run_id == self._files.baseline_run_id: | |
| optional_baseline.append(loaded) | |
| return ActivationArtifactSnapshot( | |
| required=required, | |
| optional=tuple(optional_current), | |
| baseline=(*baseline, *optional_baseline), | |
| ) | |
| def _load(self, reference: JsonArtifactRef) -> LoadedArtifact: | |
| if not self._files.is_published(reference.path): | |
| raise ArtifactAccessError("agent input requires a published artifact") | |
| registered = self._files.published_reference(reference.path) | |
| if registered != reference: | |
| raise ValueError("published artifact identity does not match requested reference") | |
| content = self._files.read(reference.path) | |
| if content is None: | |
| raise FileNotFoundError(reference.path) | |
| schema = _schema_for(reference) | |
| value = schema.model_validate_json(content, strict=True) | |
| identity = tuple(getattr(value, field, None) for field in ( | |
| "artifact_id", "run_id", "session_id", "producer", | |
| )) | |
| expected = ( | |
| reference.artifact_id, reference.run_id, reference.session_id, reference.producer, | |
| ) | |
| if identity != expected: | |
| raise ValueError("artifact contents contradict the published reference") | |
| if getattr(value, "schema_version", None) != reference.schema_version: | |
| raise ValueError("artifact schema version contradicts the published reference") | |
| return LoadedArtifact(reference=reference, value=value, source_json=content) | |
| def _schema_for(reference: JsonArtifactRef) -> type[BaseModel]: | |
| if reference.producer == "Runtime": | |
| if reference.path.endswith("/asset_analysis.json"): | |
| return AssetAnalysisCollection | |
| raise ValueError("unsupported runtime-owned activation artifact") | |
| if reference.producer == "DataFetcher" and "/raw/" in reference.path: | |
| return MarketDataArtifact | |
| return quant_artifact_schema(reference.producer) | |