Repository navigation
Expand file tree
/
Copy pathtest_core_parity.py
More file actions
201 lines (163 loc) · 6.71 KB
/
Copy pathtest_core_parity.py
File metadata and controls
201 lines (163 loc) · 6.71 KB
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
import asyncio
import pytest
from a2a.types import Part
from workflow_engine import (
BusinessInput, ControlPoint, JumpCondition, MessageContent, RouteDecision,
StepType, StubWorkflowEngineClient, Task, TaskResult, Workflow, WorkflowExecutor,
WorkflowStep,
)
class RecordingControlPoint(ControlPoint):
def __init__(self):
self.task_requests = []
self.route_requests = []
async def on_task(self, request):
self.task_requests.append(request)
return MessageContent.text(request.instruction)
async def on_self_task(self, request):
self.task_requests.append(request)
return TaskResult.succeeded(("first", {"second": 2}))
async def on_route(self, request):
self.route_requests.append(request)
return RouteDecision.allow(request.condition)
def workflow_with_routes(edges):
targets = {edge.step for edge in edges}
return Workflow(name="routes", steps=[
WorkflowStep(
name="source", layer=0,
subtasks=[Task(agent="source-agent", description="source task")],
next=edges,
),
*[
WorkflowStep(
name=name, layer=1,
subtasks=[Task(agent=f"agent-{name}", description=f"task-{name}")],
)
for name in ("a", "b", "c") if name in targets
],
])
@pytest.mark.asyncio
async def test_mixed_routes_allow_unconditional_and_each_allowed_condition():
control = RecordingControlPoint()
workflow = workflow_with_routes([
JumpCondition("a", ""),
JumpCondition("b", "eligible"),
JumpCondition("c", "healthy"),
])
result = await WorkflowExecutor(
workflow, control, StubWorkflowEngineClient()
).run()
assert result.success
assert set(result.step_outputs) == {"source", "a", "b", "c"}
assert [(item.next_step, item.condition) for item in control.route_requests] == [
("b", "eligible"), ("c", "healthy")
]
@pytest.mark.asyncio
async def test_all_denied_conditions_end_branch_normally():
class Deny(RecordingControlPoint):
async def on_route(self, request):
self.route_requests.append(request)
return RouteDecision.deny("not selected")
control = Deny()
result = await WorkflowExecutor(
workflow_with_routes([JumpCondition("a", "x"), JumpCondition("b", "y")]),
control, StubWorkflowEngineClient(),
).run()
assert result.success
assert set(result.step_outputs) == {"source"}
assert len(control.route_requests) == 2
@pytest.mark.asyncio
async def test_one_route_failure_suppresses_every_successor():
class Failure(RecordingControlPoint):
async def on_route(self, request):
if request.next_step == "b":
raise ValueError("broken condition")
return RouteDecision.allow()
result = await WorkflowExecutor(
workflow_with_routes([JumpCondition("a", "x"), JumpCondition("b", "y")]),
Failure(), StubWorkflowEngineClient(),
).run()
assert not result.success
assert "source -> b" in result.error
assert set(result.step_outputs) == {"source"}
@pytest.mark.asyncio
async def test_structured_upstream_results_are_not_rendered_into_instruction():
control = RecordingControlPoint()
workflow = Workflow(name="context", steps=[
WorkflowStep(
name="first", layer=0,
subtasks=[Task(agent="a", description="produce")],
next=[JumpCondition("second")],
),
WorkflowStep(
name="second", layer=1,
subtasks=[Task(agent="b", description="consume")],
),
])
result = await WorkflowExecutor(
workflow, control, StubWorkflowEngineClient(), runtime_intent="diagnose"
).run()
assert result.success
second = next(item for item in control.task_requests if item.step_name == "second")
assert second.instruction == "consume"
assert second.workflow_input.runtime_intent == "diagnose"
assert second.workflow_input.upstream_results[0].step_name == "first"
assert second.workflow_input.upstream_results[0].task_results[0].outputs == ("OK from a",)
@pytest.mark.asyncio
async def test_self_task_supports_multiple_unrestricted_outputs():
control = RecordingControlPoint()
workflow = Workflow(name="self", steps=[WorkflowStep(
name="local", layer=0, step_type=StepType.SELF_LOOP,
subtasks=[Task(agent="host", description="aggregate")],
)])
result = await WorkflowExecutor(
workflow, control, StubWorkflowEngineClient()
).run()
assert result.success
assert result.step_outputs["local"]["aggregate"] == ("first", {"second": 2})
@pytest.mark.asyncio
async def test_duplicate_descriptions_do_not_overwrite_parallel_results():
control = RecordingControlPoint()
workflow = Workflow(name="duplicates", steps=[WorkflowStep(
name="parallel", layer=0,
subtasks=[
Task(agent="a", description="same"),
Task(agent="b", description="same"),
],
)])
result = await WorkflowExecutor(
workflow, control, StubWorkflowEngineClient()
).run()
assert result.success
assert set(result.step_outputs["parallel"]) == {"same [a#0]", "same [b#1]"}
def test_business_input_and_content_snapshot_mutable_values():
source = {"items": ["a"]}
value = BusinessInput.from_data(source)
source["items"].append("b")
assert value.data["items"] == ("a",)
metadata = {"extension": {"items": [1]}}
content = MessageContent((Part(text="task"),), metadata)
metadata["extension"]["items"].append(2)
assert content.metadata["extension"]["items"] == (1,)
def test_business_values_and_message_parts_fail_fast_on_invalid_types():
with pytest.raises(TypeError, match="JSON-compatible"):
BusinessInput.from_data(object())
with pytest.raises(TypeError, match="a2a.types.Part"):
MessageContent(parts=("not-a-part",))
with pytest.raises(ValueError, match="must contain"):
MessageContent(parts=(Part(),))
def test_validator_rejects_duplicate_edges_and_invalid_context_source():
from workflow_engine.core.workflow_validator import validate_workflow
duplicate = workflow_with_routes([
JumpCondition("a"), JumpCondition("a", "condition")
])
with pytest.raises(ValueError, match="duplicate outgoing target"):
validate_workflow(duplicate)
invalid_context = Workflow(name="bad-context", steps=[
WorkflowStep(name="left", layer=0, subtasks=[Task(agent="a")]),
WorkflowStep(
name="right", layer=0, subtasks=[Task(agent="b")],
context_from=["left"],
),
])
with pytest.raises(ValueError, match="not an upstream dependency"):
validate_workflow(invalid_context)