-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_workflow_grammar.py
More file actions
193 lines (140 loc) · 4.98 KB
/
Copy pathtest_workflow_grammar.py
File metadata and controls
193 lines (140 loc) · 4.98 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
"""Tests for intellagent_runtime/workflow_grammar.py."""
from __future__ import annotations
import pytest
from intellagent_runtime.workflow_grammar import (
Stage,
Workflow,
WorkflowError,
parse_workflow,
parse_workflow_from_text,
self_check,
validate_stages,
)
# ---------------------------------------------------------------------------
# Stage enum
# ---------------------------------------------------------------------------
def test_stage_values_are_lowercase_strings():
for s in Stage:
assert s.value == s.value.lower()
assert isinstance(s, str)
def test_stage_str_subclass_works_in_json():
import json
payload = {"stages": [Stage.REVIEW, Stage.EXECUTE]}
encoded = json.dumps(payload)
assert "review" in encoded
assert "execute" in encoded
# ---------------------------------------------------------------------------
# Validation rules
# ---------------------------------------------------------------------------
def test_canonical_workflow_is_valid():
w = Workflow.from_stage_names(
["inspect", "propose", "review", "execute", "verify", "report", "stop"]
)
assert w.is_valid()
def test_execute_before_review_is_rejected():
w = Workflow.from_stage_names(["propose", "execute", "review", "report", "stop"])
assert not w.is_valid()
assert any("execute" in v and "review" in v for v in w.validate())
def test_verify_before_execute_rejected_without_flag():
w = Workflow.from_stage_names(["inspect", "verify", "report", "stop"])
assert not w.is_valid()
def test_verify_before_execute_allowed_with_flag():
w = Workflow.from_stage_names(
["inspect", "verify", "report", "stop"],
verify_over_existing_artifact=True,
)
assert w.is_valid()
def test_report_required_before_stop():
w = Workflow.from_stage_names(["inspect", "propose", "review", "execute", "stop"])
assert not w.is_valid()
assert any("stop" in v and "report" in v for v in w.validate())
def test_stop_is_terminal():
w = Workflow.from_stage_names(
["inspect", "propose", "review", "execute", "report", "stop", "report"]
)
assert not w.is_valid()
def test_refuse_is_terminal():
w = Workflow.from_stage_names(
["inspect", "propose", "review", "refuse", "report"]
)
assert not w.is_valid()
def test_refusal_terminal_without_execute_is_valid():
"""A refusal that never reaches execute is a successful protocol outcome
and MUST NOT be flagged as missing-execute."""
w = Workflow.from_stage_names(["inspect", "propose", "review", "refuse"])
assert w.is_valid()
def test_empty_workflow_is_invalid():
w = Workflow(stages=[])
assert not w.is_valid()
def test_propose_without_review_is_rejected():
w = Workflow.from_stage_names(["inspect", "propose", "report", "stop"])
assert not w.is_valid()
# ---------------------------------------------------------------------------
# Parsing
# ---------------------------------------------------------------------------
def test_parse_workflow_recognizes_inline_stages():
text = """## Stages
- inspect the world
- propose a patch
- review the patch
- execute the admitted patch
- verify post-execute
- report results
- stop
"""
w = parse_workflow(text)
assert w.stages[0] is Stage.INSPECT
assert w.stages[-1] is Stage.STOP
def test_parse_workflow_skips_code_fences():
text = """```bash
make ci
```
- inspect
- propose
- review
- execute
- verify
- report
- stop
"""
stages = parse_workflow_from_text(text)
assert Stage.INSPECT in stages
assert Stage.STOP in stages
def test_parse_workflow_handles_numbered_items():
text = """## Stages
1. inspect the repo
2. propose a change
3. review the change
4. execute
5. verify
6. report
7. stop
"""
w = parse_workflow(text)
assert len(w.stages) == 7
def test_from_stage_names_rejects_unknown_token():
with pytest.raises(WorkflowError):
Workflow.from_stage_names(["inspect", "wat", "review", "execute", "report", "stop"])
# ---------------------------------------------------------------------------
# Misc
# ---------------------------------------------------------------------------
def test_validate_stages_returns_empty_for_valid():
assert validate_stages(
[Stage.INSPECT, Stage.PROPOSE, Stage.REVIEW, Stage.EXECUTE, Stage.VERIFY, Stage.REPORT, Stage.STOP]
) == []
def test_terminal_kind_reports_stop():
w = Workflow.from_stage_names(
["inspect", "propose", "review", "execute", "verify", "report", "stop"]
)
assert w.terminal_kind() is Stage.STOP
def test_terminal_kind_reports_refuse():
w = Workflow.from_stage_names(["inspect", "propose", "review", "refuse"])
assert w.terminal_kind() is Stage.REFUSE
def test_terminal_kind_returns_none_when_open():
w = Workflow.from_stage_names(["inspect", "propose"])
assert w.terminal_kind() is None
def test_self_check_returns_zero(capsys):
rc = self_check()
out = capsys.readouterr().out
assert rc == 0
assert "PASS" in out