yf176 commited on
Commit
ea2c230
·
1 Parent(s): a9b533e

feat: execute ready quant agents concurrently

Browse files
src/polyhedra/core/dependency_executor.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Asynchronous execution of one immutable V8 active Routing Plan."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from collections.abc import Awaitable, Callable
7
+ from datetime import datetime, timezone
8
+ from enum import StrEnum
9
+ from typing import Self
10
+
11
+ from pydantic import AwareDatetime, model_validator
12
+
13
+ from polyhedra.core.active_routing import ActiveAgentPlan, ActiveRoutingPlan
14
+ from polyhedra.core.artifacts import Availability, JsonArtifactRef, TypedModel
15
+ from polyhedra.core.quant_contracts import AgentName
16
+
17
+
18
+ class AgentExecutionState(StrEnum):
19
+ WAITING = "WAITING"
20
+ RUNNING = "RUNNING"
21
+ COMPLETED = "COMPLETED"
22
+ FAILED = "FAILED"
23
+ BLOCKED = "BLOCKED"
24
+
25
+
26
+ class AgentCompletion(TypedModel):
27
+ artifact_ref: JsonArtifactRef
28
+ result_status: Availability
29
+
30
+
31
+ class AgentExecutionRecord(TypedModel):
32
+ agent: AgentName
33
+ state: AgentExecutionState
34
+ result_status: Availability | None = None
35
+ artifact_ref: JsonArtifactRef | None = None
36
+ started_at: AwareDatetime | None = None
37
+ finished_at: AwareDatetime | None = None
38
+ error: str | None = None
39
+ blocked_by: tuple[AgentName, ...] = ()
40
+
41
+ @model_validator(mode="after")
42
+ def coherent_state(self) -> Self:
43
+ if self.state == AgentExecutionState.COMPLETED:
44
+ if self.result_status is None or self.artifact_ref is None or self.finished_at is None:
45
+ raise ValueError("completed execution requires a published typed result")
46
+ elif self.result_status is not None or self.artifact_ref is not None:
47
+ raise ValueError("only completed execution can carry a result")
48
+ if self.state == AgentExecutionState.FAILED and (not self.error or self.finished_at is None):
49
+ raise ValueError("failed execution requires an error and finish time")
50
+ if self.state == AgentExecutionState.BLOCKED and not self.blocked_by:
51
+ raise ValueError("blocked execution requires failed dependencies")
52
+ return self
53
+
54
+
55
+ class DependencyExecutionResult(TypedModel):
56
+ run_id: str
57
+ session_id: str
58
+ records: tuple[AgentExecutionRecord, ...]
59
+
60
+ def for_agent(self, agent: AgentName) -> AgentExecutionRecord:
61
+ for record in self.records:
62
+ if record.agent == agent:
63
+ return record
64
+ raise KeyError(agent)
65
+
66
+
67
+ AgentRunner = Callable[[ActiveAgentPlan], Awaitable[AgentCompletion]]
68
+
69
+
70
+ class DependencyExecutor:
71
+ """Activate ready agents concurrently and derive execution state from results."""
72
+
73
+ def __init__(
74
+ self,
75
+ plan: ActiveRoutingPlan,
76
+ *,
77
+ session_id: str,
78
+ runner: AgentRunner,
79
+ now: Callable[[], datetime] | None = None,
80
+ ) -> None:
81
+ if not session_id:
82
+ raise ValueError("dependency executor requires a session identity")
83
+ self.plan = plan
84
+ self.session_id = session_id
85
+ self._runner = runner
86
+ self._now = now or (lambda: datetime.now(tz=timezone.utc))
87
+ self._records: dict[AgentName, AgentExecutionRecord] = {
88
+ item.agent: AgentExecutionRecord(agent=item.agent, state=AgentExecutionState.WAITING)
89
+ for item in plan.active_agents
90
+ }
91
+ self._published_paths: set[str] = set()
92
+ self._published_ids: set[str] = set()
93
+
94
+ async def run(self) -> DependencyExecutionResult:
95
+ pending = {item.agent: item for item in self.plan.active_agents}
96
+ running: dict[asyncio.Task[AgentCompletion], ActiveAgentPlan] = {}
97
+
98
+ while pending or running:
99
+ self._block_failed_dependents(pending)
100
+ ready = [
101
+ item for item in pending.values()
102
+ if all(
103
+ self._records[dependency.agent].state == AgentExecutionState.COMPLETED
104
+ for dependency in item.required_dependencies
105
+ )
106
+ ]
107
+ for item in ready:
108
+ pending.pop(item.agent)
109
+ started = self._now()
110
+ self._records[item.agent] = AgentExecutionRecord(
111
+ agent=item.agent,
112
+ state=AgentExecutionState.RUNNING,
113
+ started_at=started,
114
+ )
115
+ running[asyncio.create_task(self._runner(item))] = item
116
+
117
+ if not running:
118
+ if pending:
119
+ raise RuntimeError("active routing plan cannot make dependency progress")
120
+ break
121
+
122
+ done, _ = await asyncio.wait(running, return_when=asyncio.FIRST_COMPLETED)
123
+ for task in done:
124
+ item = running.pop(task)
125
+ await self._record_completion(item, task)
126
+
127
+ ordered = tuple(self._records[item.agent] for item in self.plan.active_agents)
128
+ return DependencyExecutionResult(
129
+ run_id=self.plan.run_id,
130
+ session_id=self.session_id,
131
+ records=ordered,
132
+ )
133
+
134
+ async def _record_completion(
135
+ self,
136
+ item: ActiveAgentPlan,
137
+ task: asyncio.Task[AgentCompletion],
138
+ ) -> None:
139
+ started = self._records[item.agent].started_at
140
+ finished = self._now()
141
+ try:
142
+ completion = task.result()
143
+ reference = completion.artifact_ref
144
+ if (reference.run_id, reference.session_id, reference.producer) != (
145
+ self.plan.run_id, self.session_id, item.agent,
146
+ ):
147
+ raise ValueError("published artifact identity contradicts the activation")
148
+ if reference.path in self._published_paths or reference.artifact_id in self._published_ids:
149
+ raise ValueError("artifact was published more than once in this run")
150
+ self._published_paths.add(reference.path)
151
+ self._published_ids.add(reference.artifact_id)
152
+ self._records[item.agent] = AgentExecutionRecord(
153
+ agent=item.agent,
154
+ state=AgentExecutionState.COMPLETED,
155
+ result_status=completion.result_status,
156
+ artifact_ref=reference,
157
+ started_at=started,
158
+ finished_at=finished,
159
+ )
160
+ except BaseException as exc:
161
+ if isinstance(exc, asyncio.CancelledError):
162
+ raise
163
+ self._records[item.agent] = AgentExecutionRecord(
164
+ agent=item.agent,
165
+ state=AgentExecutionState.FAILED,
166
+ started_at=started,
167
+ finished_at=finished,
168
+ error=f"{type(exc).__name__}: {exc}",
169
+ )
170
+
171
+ def _block_failed_dependents(self, pending: dict[AgentName, ActiveAgentPlan]) -> None:
172
+ changed = True
173
+ while changed:
174
+ changed = False
175
+ for agent, item in list(pending.items()):
176
+ blocked_by = tuple(
177
+ dependency.agent
178
+ for dependency in item.required_dependencies
179
+ if self._records[dependency.agent].state in {
180
+ AgentExecutionState.FAILED, AgentExecutionState.BLOCKED,
181
+ }
182
+ )
183
+ if not blocked_by:
184
+ continue
185
+ pending.pop(agent)
186
+ self._records[agent] = AgentExecutionRecord(
187
+ agent=agent,
188
+ state=AgentExecutionState.BLOCKED,
189
+ blocked_by=blocked_by,
190
+ )
191
+ changed = True
tests/test_dependency_executor.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Dependency execution uses publication state, not linear mention routing."""
2
+
3
+ import asyncio
4
+
5
+ import pytest
6
+
7
+ from polyhedra.core.active_routing import build_active_routing_plan
8
+ from polyhedra.core.artifacts import JsonArtifactRef
9
+ from polyhedra.core.dependency_executor import AgentCompletion, DependencyExecutor
10
+ from polyhedra.core.quant_contracts import load_quant_contracts
11
+
12
+
13
+ def plan(tickers):
14
+ return build_active_routing_plan(run_id="run1", execution_context="YOLO", tickers=list(tickers))
15
+
16
+
17
+ def completion(agent, status="COMPLETE", *, run="run1", session="session1"):
18
+ contract = load_quant_contracts().for_agent(agent)
19
+ path = next(path for path in contract.permissions.create if path.endswith(".json"))
20
+ path = path.replace("{run_id}", run)
21
+ return AgentCompletion(
22
+ result_status=status,
23
+ artifact_ref=JsonArtifactRef(
24
+ schema_version="8.0",
25
+ artifact_id=f"{agent}-{run}",
26
+ run_id=run,
27
+ session_id=session,
28
+ producer=agent,
29
+ path=path,
30
+ ),
31
+ )
32
+
33
+
34
+ @pytest.mark.asyncio
35
+ async def test_ready_agents_overlap_and_join_waits_for_all_applicable_dependencies():
36
+ active = plan(["MSFT"])
37
+ data_done = asyncio.Event()
38
+ branch_started = {"TrendAnalyst": asyncio.Event(), "SentimentAnalyst": asyncio.Event()}
39
+ branch_release = asyncio.Event()
40
+ starts = []
41
+
42
+ async def runner(item):
43
+ starts.append(item.agent)
44
+ if item.agent == "DataFetcher":
45
+ data_done.set()
46
+ elif item.agent in branch_started:
47
+ assert data_done.is_set()
48
+ branch_started[item.agent].set()
49
+ await branch_release.wait()
50
+ elif item.agent == "QuantResearcher":
51
+ assert all(event.is_set() for event in branch_started.values())
52
+ return completion(item.agent)
53
+
54
+ task = asyncio.create_task(DependencyExecutor(
55
+ active, session_id="session1", runner=runner,
56
+ ).run())
57
+ await asyncio.wait_for(data_done.wait(), timeout=1)
58
+ await asyncio.wait_for(branch_started["TrendAnalyst"].wait(), timeout=1)
59
+ await asyncio.wait_for(branch_started["SentimentAnalyst"].wait(), timeout=1)
60
+ assert "QuantResearcher" not in starts
61
+ branch_release.set()
62
+ result = await asyncio.wait_for(task, timeout=1)
63
+ assert all(record.state == "COMPLETED" for record in result.records)
64
+ assert starts.index("TrendAnalyst") < starts.index("QuantResearcher")
65
+ assert starts.index("SentimentAnalyst") < starts.index("QuantResearcher")
66
+
67
+
68
+ @pytest.mark.asyncio
69
+ async def test_unavailable_typed_result_satisfies_dependency_completion():
70
+ async def runner(item):
71
+ status = "UNAVAILABLE" if item.agent == "TrendAnalyst" else "COMPLETE"
72
+ return completion(item.agent, status)
73
+
74
+ result = await DependencyExecutor(
75
+ plan(["MSFT"]), session_id="session1", runner=runner,
76
+ ).run()
77
+ assert result.for_agent("TrendAnalyst").state == "COMPLETED"
78
+ assert result.for_agent("TrendAnalyst").result_status == "UNAVAILABLE"
79
+ assert result.for_agent("QuantResearcher").state == "COMPLETED"
80
+
81
+
82
+ @pytest.mark.asyncio
83
+ async def test_failure_blocks_dependents_but_unrelated_root_finishes():
84
+ completed = []
85
+
86
+ async def runner(item):
87
+ if item.agent == "DataFetcher":
88
+ raise RuntimeError("provider normalization failed")
89
+ completed.append(item.agent)
90
+ return completion(item.agent)
91
+
92
+ result = await DependencyExecutor(
93
+ plan(["SPY"]), session_id="session1", runner=runner,
94
+ ).run()
95
+ assert result.for_agent("DataFetcher").state == "FAILED"
96
+ assert result.for_agent("OptionsFlowWatcher").state == "COMPLETED"
97
+ assert completed == ["OptionsFlowWatcher"]
98
+ assert result.for_agent("TrendAnalyst").blocked_by == ("DataFetcher",)
99
+ assert result.for_agent("Publisher").state == "BLOCKED"
100
+
101
+
102
+ @pytest.mark.asyncio
103
+ async def test_wrong_publication_identity_is_execution_failure():
104
+ async def runner(item):
105
+ if item.agent == "DataFetcher":
106
+ return completion(item.agent, run="other")
107
+ return completion(item.agent)
108
+
109
+ result = await DependencyExecutor(
110
+ plan(["MSFT"]), session_id="session1", runner=runner,
111
+ ).run()
112
+ record = result.for_agent("DataFetcher")
113
+ assert record.state == "FAILED"
114
+ assert "identity contradicts" in record.error
115
+ assert result.for_agent("TrendAnalyst").state == "BLOCKED"
116
+
117
+
118
+ @pytest.mark.asyncio
119
+ async def test_each_active_agent_is_invoked_once():
120
+ calls = {}
121
+
122
+ async def runner(item):
123
+ calls[item.agent] = calls.get(item.agent, 0) + 1
124
+ return completion(item.agent)
125
+
126
+ active = plan(["SPY", "BTC"])
127
+ result = await DependencyExecutor(
128
+ active, session_id="session1", runner=runner,
129
+ ).run()
130
+ assert set(calls) == {item.agent for item in active.active_agents}
131
+ assert set(calls.values()) == {1}
132
+ assert len(result.records) == len(active.active_agents)