File size: 11,108 Bytes
3d027e0
 
 
 
 
 
 
 
 
30cc2cc
3d027e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30cc2cc
 
3d027e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30cc2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d027e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30cc2cc
 
3d027e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30cc2cc
 
3d027e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1a28b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d027e0
 
 
 
 
 
 
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
"""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