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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Declarative workflow DSL — YAML definitions that map to existing coordination APIs.
//!
//! WorkflowDefinition parses a YAML file describing a multi-step workflow.
//! The execution plan maps each step to the appropriate coordination module call:
//! - Parallel → AgentGroup::parallel()
//! - Chain → AgentGroup::sequential()
//! - ForEach → CoordinatedGroup::map_reduce()
//! - Vote → Consensus::start() + cast_vote()
//! - SetState → SharedMemory::write()
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// A complete workflow definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowDefinition {
/// Workflow name
pub name: String,
/// Human-readable description
#[serde(default)]
pub description: String,
/// Ordered list of steps
pub steps: Vec<WorkflowStepDef>,
}
/// A single step in a workflow.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WorkflowStepDef {
/// Run a single agent with a task.
Run {
/// Name of the agent to run.
agent: String,
/// Task prompt to send to the agent.
task: String,
/// Optional SharedMemory key to store the agent's result under.
#[serde(default)]
output: Option<String>,
},
/// Run multiple agents in parallel with the same task.
Parallel {
/// Names of the agents to run concurrently.
agents: Vec<String>,
/// Task prompt sent to every agent.
task: String,
/// Maximum number of agents to run at once (defaults to all).
#[serde(default)]
concurrency: Option<usize>,
},
/// Run agents sequentially, passing results forward.
Chain {
/// Ordered sub-steps executed one after another.
steps: Vec<WorkflowStepDef>,
},
/// Fan-out: run an agent for each item in a SharedMemory key.
ForEach {
/// SharedMemory key holding the list of items to iterate over.
items_key: String,
/// Optional SharedMemory namespace to read the items from.
#[serde(default)]
namespace: Option<String>,
/// Name of the agent to run for each item.
agent: String,
/// Task template with `{item}` replaced by each item's value.
task_template: String,
/// Maximum number of agents to run at once (defaults to all).
#[serde(default)]
concurrency: Option<usize>,
},
/// Vote: ask multiple agents and aggregate by threshold.
Vote {
/// Names of the agents whose responses form the vote.
agents: Vec<String>,
/// Question posed to each agent.
question: String,
/// Fraction of matching responses required to reach consensus (0.0–1.0).
#[serde(default)]
threshold: Option<f32>,
},
/// Set a value in SharedMemory.
SetState {
/// SharedMemory key to write.
key: String,
/// Optional SharedMemory namespace to write into.
#[serde(default)]
namespace: Option<String>,
/// JSON value to store at the key.
value: Value,
},
}
impl WorkflowDefinition {
/// Load a workflow definition from a YAML file.
pub fn from_yaml_file(path: &str) -> Result<Self> {
let content = std::fs::read_to_string(path)
.with_context(|| format!("Failed to read workflow file: {}", path))?;
Self::from_yaml_str(&content)
}
/// Parse a workflow definition from a YAML string.
pub fn from_yaml_str(yaml: &str) -> Result<Self> {
let def: WorkflowDefinition =
serde_yaml::from_str(yaml).with_context(|| "Failed to parse workflow YAML")?;
def.validate()?;
Ok(def)
}
/// Validate the workflow definition.
fn validate(&self) -> Result<()> {
if self.name.is_empty() {
anyhow::bail!("Workflow name must not be empty");
}
if self.steps.is_empty() {
anyhow::bail!("Workflow must have at least one step");
}
// Recursively validate steps
for (i, step) in self.steps.iter().enumerate() {
Self::validate_step(step, i)?;
}
Ok(())
}
fn validate_step(step: &WorkflowStepDef, index: usize) -> Result<()> {
match step {
WorkflowStepDef::Run { agent, task, .. } => {
if agent.is_empty() {
anyhow::bail!("Step {}: agent name must not be empty", index);
}
if task.is_empty() {
anyhow::bail!("Step {}: task must not be empty", index);
}
}
WorkflowStepDef::Parallel {
agents,
task,
concurrency,
..
} => {
if agents.is_empty() {
anyhow::bail!("Step {}: parallel must have at least one agent", index);
}
if task.is_empty() {
anyhow::bail!("Step {}: task must not be empty", index);
}
if let Some(c) = concurrency
&& *c == 0
{
anyhow::bail!("Step {}: concurrency must be > 0", index);
}
}
WorkflowStepDef::Chain { steps } => {
if steps.is_empty() {
anyhow::bail!("Step {}: chain must have at least one sub-step", index);
}
for (j, sub) in steps.iter().enumerate() {
Self::validate_step(sub, j)?;
}
}
WorkflowStepDef::ForEach {
agent,
task_template,
..
} => {
if agent.is_empty() {
anyhow::bail!("Step {}: agent must not be empty", index);
}
if task_template.is_empty() {
anyhow::bail!("Step {}: task_template must not be empty", index);
}
}
WorkflowStepDef::Vote {
agents, question, ..
} => {
if agents.is_empty() {
anyhow::bail!("Step {}: vote must have at least one agent", index);
}
if question.is_empty() {
anyhow::bail!("Step {}: question must not be empty", index);
}
}
WorkflowStepDef::SetState { key, .. } => {
if key.is_empty() {
anyhow::bail!("Step {}: key must not be empty", index);
}
}
}
Ok(())
}
/// Count total steps (including nested).
pub fn step_count(&self) -> usize {
self.steps.iter().map(Self::count_step).sum()
}
fn count_step(step: &WorkflowStepDef) -> usize {
match step {
WorkflowStepDef::Chain { steps } => {
1 + steps.iter().map(Self::count_step).sum::<usize>()
}
_ => 1,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_simple_workflow() {
let yaml = r#"
name: code-review
steps:
- type: run
agent: reviewer
task: "Review the code changes"
- type: set_state
key: review_result
value: "pending"
"#;
let wf = WorkflowDefinition::from_yaml_str(yaml).unwrap();
assert_eq!(wf.name, "code-review");
assert_eq!(wf.steps.len(), 2);
assert_eq!(wf.step_count(), 2);
}
#[test]
fn test_parse_parallel_workflow() {
let yaml = r#"
name: parallel-test
steps:
- type: parallel
agents: [agent-a, agent-b, agent-c]
task: "Analyze the codebase"
concurrency: 2
"#;
let wf = WorkflowDefinition::from_yaml_str(yaml).unwrap();
assert_eq!(wf.name, "parallel-test");
match &wf.steps[0] {
WorkflowStepDef::Parallel {
agents,
concurrency,
..
} => {
assert_eq!(agents.len(), 3);
assert_eq!(*concurrency, Some(2));
}
_ => panic!("Expected Parallel step"),
}
}
#[test]
fn test_parse_chain_workflow() {
let yaml = r#"
name: pipeline
steps:
- type: chain
steps:
- type: run
agent: designer
task: "Design the API"
- type: run
agent: implementer
task: "Implement the design"
"#;
let wf = WorkflowDefinition::from_yaml_str(yaml).unwrap();
assert_eq!(wf.step_count(), 3); // 1 chain + 2 inner
}
#[test]
fn test_parse_vote_workflow() {
let yaml = r#"
name: consensus
steps:
- type: vote
agents: [agent-a, agent-b, agent-c]
question: "Which approach is best?"
threshold: 0.66
"#;
let wf = WorkflowDefinition::from_yaml_str(yaml).unwrap();
match &wf.steps[0] {
WorkflowStepDef::Vote {
agents,
question,
threshold,
} => {
assert_eq!(agents.len(), 3);
assert_eq!(question, "Which approach is best?");
assert_eq!(*threshold, Some(0.66));
}
_ => panic!("Expected Vote step"),
}
}
#[test]
fn test_validation_empty_name() {
let yaml = r#"
name: ""
steps:
- type: run
agent: a
task: t
"#;
assert!(WorkflowDefinition::from_yaml_str(yaml).is_err());
}
#[test]
fn test_validation_empty_steps() {
let yaml = r#"
name: empty
steps: []
"#;
assert!(WorkflowDefinition::from_yaml_str(yaml).is_err());
}
#[test]
fn test_foreach_workflow() {
let yaml = r#"
name: batch-process
steps:
- type: for_each
items_key: file_list
agent: file-processor
task_template: "Process file: {item}"
concurrency: 4
"#;
let wf = WorkflowDefinition::from_yaml_str(yaml).unwrap();
match &wf.steps[0] {
WorkflowStepDef::ForEach {
items_key,
agent,
task_template,
concurrency,
..
} => {
assert_eq!(items_key, "file_list");
assert_eq!(agent, "file-processor");
assert_eq!(task_template, "Process file: {item}");
assert_eq!(*concurrency, Some(4));
}
_ => panic!("Expected ForEach step"),
}
}
}