"""Typed, atomic V8 run records stored beside legacy replay records.""" from __future__ import annotations from datetime import datetime from enum import StrEnum from pathlib import Path from typing import Self from pydantic import AwareDatetime, Field, model_validator from polyhedra.core.active_routing import ActiveRoutingPlan from polyhedra.core.artifacts import ( ArtifactRef, Identifier, RunMode, SchemaVersion, TickerScope, TypedModel, ) from polyhedra.core.dependency_executor import ( AgentExecutionState, DependencyExecutionResult, ) from polyhedra.core.quant_contracts import AgentName, ExecutionContext from polyhedra.services.file_writer import FileWriter class QuantRunStatus(StrEnum): COMPLETED = "completed" FAILED = "failed" BLOCKED = "blocked" class QuantRunRecord(TypedModel): schema_version: SchemaVersion = "8.0" run_id: Identifier session_id: Identifier execution_context: ExecutionContext status: QuantRunStatus run_mode: RunMode baseline_run_id: Identifier | None ticker_scope: TickerScope started_at: AwareDatetime completed_at: AwareDatetime routing_plan: ActiveRoutingPlan execution: DependencyExecutionResult input_snapshot_paths: dict[AgentName, str] permission_templates: dict[AgentName, dict[str, list[str]]] = Field(default_factory=dict) permission_snapshots: dict[AgentName, dict[str, list[str]]] = Field(default_factory=dict) artifact_inventory: list[ArtifactRef] @model_validator(mode="after") def fixed_runtime_identity(self) -> Self: if self.execution_context != self.routing_plan.execution_context: raise ValueError("run record context contradicts the frozen plan") if self.ticker_scope != self.routing_plan.requested_ticker_scope: raise ValueError("run record scope contradicts the frozen plan") if (self.routing_plan.run_id, self.execution.run_id, self.execution.session_id) != ( self.run_id, self.run_id, self.session_id, ): raise ValueError("run record plan or execution identity mismatch") if (self.run_mode == RunMode.DELTA) != (self.baseline_run_id is not None): raise ValueError("delta run record requires one frozen baseline ID") if self.baseline_run_id == self.run_id: raise ValueError("run cannot use itself as baseline") if self.completed_at < self.started_at: raise ValueError("run completion precedes start") active = {item.agent for item in self.routing_plan.active_agents} executed = {item.agent for item in self.execution.records} if executed != active: raise ValueError("execution records must cover the active plan") if any(item.state in {AgentExecutionState.WAITING, AgentExecutionState.RUNNING} for item in self.execution.records): raise ValueError("final run record cannot contain nonterminal agents") if self.status != _run_status(self.execution): raise ValueError("run status contradicts agent execution records") for agent, path in self.input_snapshot_paths.items(): if agent not in active: raise ValueError("input snapshot belongs to an inactive agent") if path != f"quant/runs/{self.run_id}/inputs/{agent}.json": raise ValueError("input snapshot path contradicts run or agent") operations = {"read", "create", "edit", "list", "baseline_read", "baseline_list"} for label, snapshots in ( ("permission template", self.permission_templates), ("permission", self.permission_snapshots), ): if snapshots and set(snapshots) != active: raise ValueError(f"{label} snapshots must cover every active agent") for agent, snapshot in snapshots.items(): if set(snapshot) != operations: raise ValueError(f"{label} snapshot for {agent} is incomplete") for paths in snapshot.values(): if len(paths) != len(set(paths)) or any( not path or path.startswith("/") or "\\" in path or ".." in path.split("/") for path in paths ): raise ValueError(f"{label} snapshot contains an invalid filename") paths: set[str] = set() identities: set[str] = set() for artifact in self.artifact_inventory: if (artifact.run_id, artifact.session_id) != (self.run_id, self.session_id): raise ValueError("artifact inventory crosses run or session") if artifact.path in paths or artifact.artifact_id in identities: raise ValueError("artifact inventory contains duplicate identity") paths.add(artifact.path) identities.add(artifact.artifact_id) completed_paths = { item.artifact_ref.path for item in self.execution.records if item.state == AgentExecutionState.COMPLETED and item.artifact_ref is not None } if not completed_paths.issubset(paths): raise ValueError("artifact inventory omits a completed agent output") return self def build_quant_run_record( *, plan: ActiveRoutingPlan, execution: DependencyExecutionResult, session_id: str, run_mode: RunMode, baseline_run_id: str | None, started_at: datetime, completed_at: datetime, input_snapshot_paths: dict[AgentName, str], permission_templates: dict[AgentName, dict[str, list[str]]] | None = None, permission_snapshots: dict[AgentName, dict[str, list[str]]] | None = None, additional_artifacts: list[ArtifactRef] | None = None, ) -> QuantRunRecord: inventory = [ item.artifact_ref for item in execution.records if item.state == AgentExecutionState.COMPLETED and item.artifact_ref is not None ] inventory.extend(additional_artifacts or []) return QuantRunRecord( run_id=plan.run_id, session_id=session_id, execution_context=plan.execution_context, status=_run_status(execution), run_mode=run_mode, baseline_run_id=baseline_run_id, ticker_scope=list(plan.requested_ticker_scope), started_at=started_at, completed_at=completed_at, routing_plan=plan, execution=execution, input_snapshot_paths=input_snapshot_paths, permission_templates=permission_templates or {}, permission_snapshots=permission_snapshots or {}, artifact_inventory=inventory, ) class QuantRunRecordStore: """Write immutable V8 records without altering legacy replay files.""" def __init__(self, session_dir: Path, *, session_id: str) -> None: self._session_dir = session_dir self._session_id = session_id self._writer = FileWriter(session_dir, session_id=session_id) def save(self, record: QuantRunRecord) -> str: if record.session_id != self._session_id: raise ValueError("run record belongs to another session") path = f"yolo_runs/{record.run_id}.json" if self._writer.exists(path): raise FileExistsError(path) self._writer.write_atomic(path, record.model_dump_json(indent=2), agent_role="Runtime") return path def load(self, run_id: str) -> QuantRunRecord: path = f"yolo_runs/{run_id}.json" content = self._writer.read(path) if content is None: raise FileNotFoundError(path) record = QuantRunRecord.model_validate_json(content, strict=True) if record.session_id != self._session_id or record.run_id != run_id: raise ValueError("stored run record identity mismatch") return record def baseline_candidates(self) -> list[dict]: """Return valid V8 records; legacy and malformed replay files stay untouched.""" directory = self._session_dir / "yolo_runs" candidates: list[dict] = [] if not directory.is_dir(): return candidates for path in directory.glob("*.json"): try: record = QuantRunRecord.model_validate_json( path.read_text(encoding="utf-8"), strict=True, ) except Exception: continue if record.session_id == self._session_id: candidates.append(record.model_dump(mode="json")) return candidates class QuantSupportRunRecordStore: """Persist visualization and monitor records outside YOLO replay history.""" def __init__(self, session_dir: Path, *, session_id: str) -> None: self._session_dir = session_dir self._session_id = session_id self._writer = FileWriter(session_dir, session_id=session_id) def save(self, record: QuantRunRecord) -> str: if record.session_id != self._session_id: raise ValueError("support run record belongs to another session") if record.execution_context == ExecutionContext.YOLO: raise ValueError("YOLO records belong to QuantRunRecordStore") path = f"support_runs/{record.run_id}.json" if self._writer.exists(path): raise FileExistsError(path) self._writer.write_atomic(path, record.model_dump_json(indent=2), agent_role="Runtime") return path def load(self, run_id: str) -> QuantRunRecord: path = f"support_runs/{run_id}.json" content = self._writer.read(path) if content is None: raise FileNotFoundError(path) record = QuantRunRecord.model_validate_json(content, strict=True) if record.session_id != self._session_id or record.run_id != run_id: raise ValueError("stored support run record identity mismatch") if record.execution_context == ExecutionContext.YOLO: raise ValueError("support run store contains a YOLO record") return record def records(self, *, context: ExecutionContext | None = None) -> list[QuantRunRecord]: directory = self._session_dir / "support_runs" records: list[QuantRunRecord] = [] if not directory.is_dir(): return records for path in directory.glob("*.json"): try: record = QuantRunRecord.model_validate_json( path.read_text(encoding="utf-8"), strict=True, ) except Exception: continue if record.session_id != self._session_id: continue if context is None or record.execution_context == context: records.append(record) return records def _run_status(execution: DependencyExecutionResult) -> QuantRunStatus: states = {item.state for item in execution.records} if states == {AgentExecutionState.COMPLETED}: return QuantRunStatus.COMPLETED if AgentExecutionState.FAILED in states: return QuantRunStatus.FAILED return QuantRunStatus.BLOCKED