ccswarm 0.4.2

AI-powered multi-agent orchestration system with proactive intelligence, security monitoring, and session management
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Workflow graph definition and validation

use super::node::{NodeId, NodeType, WorkflowNode};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};

/// Error types for workflow operations
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorkflowError {
    /// Duplicate workflow/node ID
    DuplicateId(String),
    /// Node not found
    NodeNotFound(String),
    /// Workflow not found
    NotFound(String),
    /// Invalid edge
    InvalidEdge { from: String, to: String },
    /// Cycle detected
    CycleDetected(Vec<String>),
    /// No start node
    NoStartNode,
    /// No end node
    NoEndNode,
    /// Multiple start nodes
    MultipleStartNodes,
    /// Unreachable nodes
    UnreachableNodes(Vec<String>),
    /// Validation error
    ValidationError(String),
}

impl std::fmt::Display for WorkflowError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WorkflowError::DuplicateId(id) => write!(f, "Duplicate ID: {}", id),
            WorkflowError::NodeNotFound(id) => write!(f, "Node not found: {}", id),
            WorkflowError::NotFound(id) => write!(f, "Workflow not found: {}", id),
            WorkflowError::InvalidEdge { from, to } => {
                write!(f, "Invalid edge: {} -> {}", from, to)
            }
            WorkflowError::CycleDetected(path) => write!(f, "Cycle detected: {:?}", path),
            WorkflowError::NoStartNode => write!(f, "No start node defined"),
            WorkflowError::NoEndNode => write!(f, "No end node defined"),
            WorkflowError::MultipleStartNodes => write!(f, "Multiple start nodes defined"),
            WorkflowError::UnreachableNodes(nodes) => write!(f, "Unreachable nodes: {:?}", nodes),
            WorkflowError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
        }
    }
}

impl std::error::Error for WorkflowError {}

/// A workflow graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workflow {
    /// Unique workflow ID
    pub id: String,
    /// Workflow name
    pub name: String,
    /// Description
    pub description: Option<String>,
    /// Nodes in the workflow
    pub nodes: Vec<WorkflowNode>,
    /// Version
    pub version: String,
    /// Tags
    pub tags: Vec<String>,
    /// Metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

impl Workflow {
    /// Get a node by ID
    pub fn get_node(&self, id: &NodeId) -> Option<&WorkflowNode> {
        self.nodes.iter().find(|n| &n.id == id)
    }

    /// Get mutable node by ID
    pub fn get_node_mut(&mut self, id: &NodeId) -> Option<&mut WorkflowNode> {
        self.nodes.iter_mut().find(|n| &n.id == id)
    }

    /// Find start nodes
    pub fn start_nodes(&self) -> Vec<&WorkflowNode> {
        self.nodes.iter().filter(|n| n.is_start()).collect()
    }

    /// Find end nodes
    pub fn end_nodes(&self) -> Vec<&WorkflowNode> {
        self.nodes.iter().filter(|n| n.is_end()).collect()
    }

    /// Get entry nodes (no inputs)
    pub fn entry_nodes(&self) -> Vec<&WorkflowNode> {
        self.nodes.iter().filter(|n| n.has_no_inputs()).collect()
    }

    /// Get exit nodes (no outputs)
    pub fn exit_nodes(&self) -> Vec<&WorkflowNode> {
        self.nodes.iter().filter(|n| n.has_no_outputs()).collect()
    }

    /// Get successors of a node
    pub fn successors(&self, node_id: &NodeId) -> Vec<&WorkflowNode> {
        if let Some(node) = self.get_node(node_id) {
            node.outputs
                .iter()
                .filter_map(|id| self.get_node(id))
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Get predecessors of a node
    pub fn predecessors(&self, node_id: &NodeId) -> Vec<&WorkflowNode> {
        if let Some(node) = self.get_node(node_id) {
            node.inputs
                .iter()
                .filter_map(|id| self.get_node(id))
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Validate the workflow
    pub fn validate(&self) -> Result<(), WorkflowError> {
        // Check for duplicate node IDs
        let mut seen_ids: HashSet<&str> = HashSet::new();
        for node in &self.nodes {
            if !seen_ids.insert(&node.id.0) {
                return Err(WorkflowError::DuplicateId(node.id.0.clone()));
            }
        }

        // Check all edges reference valid nodes
        for node in &self.nodes {
            for input in &node.inputs {
                if self.get_node(input).is_none() {
                    return Err(WorkflowError::NodeNotFound(input.0.clone()));
                }
            }
            for output in &node.outputs {
                if self.get_node(output).is_none() {
                    return Err(WorkflowError::NodeNotFound(output.0.clone()));
                }
            }
        }

        // Check for cycles using DFS
        if let Some(cycle) = self.find_cycle() {
            return Err(WorkflowError::CycleDetected(cycle));
        }

        Ok(())
    }

    /// Find a cycle in the graph using DFS
    fn find_cycle(&self) -> Option<Vec<String>> {
        let mut visited: HashSet<String> = HashSet::new();
        let mut rec_stack: HashSet<String> = HashSet::new();
        let mut path: Vec<String> = Vec::new();

        for node in &self.nodes {
            if self.dfs_find_cycle(&node.id.0, &mut visited, &mut rec_stack, &mut path) {
                return Some(path);
            }
        }

        None
    }

    fn dfs_find_cycle(
        &self,
        node_id: &str,
        visited: &mut HashSet<String>,
        rec_stack: &mut HashSet<String>,
        path: &mut Vec<String>,
    ) -> bool {
        if rec_stack.contains(node_id) {
            path.push(node_id.to_string());
            return true;
        }

        if visited.contains(node_id) {
            return false;
        }

        visited.insert(node_id.to_string());
        rec_stack.insert(node_id.to_string());
        path.push(node_id.to_string());

        if let Some(node) = self.nodes.iter().find(|n| n.id.0 == node_id) {
            for output_id in &node.outputs {
                if self.dfs_find_cycle(&output_id.0, visited, rec_stack, path) {
                    return true;
                }
            }
        }

        rec_stack.remove(node_id);
        path.pop();
        false
    }

    /// Get topological ordering of nodes
    pub fn topological_sort(&self) -> Result<Vec<&WorkflowNode>, WorkflowError> {
        let mut in_degree: HashMap<&str, usize> = HashMap::new();
        for node in &self.nodes {
            in_degree.entry(&node.id.0).or_insert(0);
            for output in &node.outputs {
                *in_degree.entry(&output.0).or_insert(0) += 1;
            }
        }

        // Adjust: we need to count inputs, not outputs
        in_degree.clear();
        for node in &self.nodes {
            in_degree.insert(&node.id.0, node.inputs.len());
        }

        let mut queue: VecDeque<&WorkflowNode> = VecDeque::new();
        for node in &self.nodes {
            if node.inputs.is_empty() {
                queue.push_back(node);
            }
        }

        let mut result: Vec<&WorkflowNode> = Vec::new();

        while let Some(node) = queue.pop_front() {
            result.push(node);

            for output_id in &node.outputs {
                if let Some(count) = in_degree.get_mut(output_id.0.as_str()) {
                    *count = count.saturating_sub(1);
                    if *count == 0 {
                        if let Some(next_node) = self.get_node(output_id) {
                            queue.push_back(next_node);
                        }
                    }
                }
            }
        }

        if result.len() != self.nodes.len() {
            return Err(WorkflowError::CycleDetected(vec![
                "Cycle in graph".to_string(),
            ]));
        }

        Ok(result)
    }

    /// Get ready nodes (all predecessors completed)
    pub fn get_ready_nodes(&self, completed: &HashSet<NodeId>) -> Vec<&WorkflowNode> {
        self.nodes
            .iter()
            .filter(|node| {
                !completed.contains(&node.id)
                    && node.inputs.iter().all(|input| completed.contains(input))
            })
            .collect()
    }
}

/// Builder for constructing workflows
pub struct WorkflowBuilder {
    id: String,
    name: String,
    description: Option<String>,
    nodes: Vec<WorkflowNode>,
    version: String,
    tags: Vec<String>,
    metadata: HashMap<String, serde_json::Value>,
}

impl WorkflowBuilder {
    /// Create a new workflow builder
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: String::new(),
            description: None,
            nodes: Vec::new(),
            version: "1.0.0".to_string(),
            tags: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Set workflow name
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// Set description
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Set version
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.version = version.into();
        self
    }

    /// Add a tag
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Add metadata
    pub fn metadata(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Add a node
    pub fn node(mut self, node: WorkflowNode) -> Self {
        self.nodes.push(node);
        self
    }

    /// Add multiple nodes
    pub fn nodes(mut self, nodes: Vec<WorkflowNode>) -> Self {
        self.nodes.extend(nodes);
        self
    }

    /// Add a start node
    pub fn start_node(self, id: impl Into<NodeId>) -> Self {
        let id = id.into();
        self.node(WorkflowNode::new(
            id.clone(),
            format!("Start: {}", id.0),
            NodeType::Start,
        ))
    }

    /// Add an end node
    pub fn end_node(self, id: impl Into<NodeId>) -> Self {
        let id = id.into();
        self.node(WorkflowNode::new(
            id.clone(),
            format!("End: {}", id.0),
            NodeType::End,
        ))
    }

    /// Add a task node
    pub fn task_node(
        self,
        id: impl Into<NodeId>,
        description: impl Into<String>,
        agent_role: Option<String>,
    ) -> Self {
        let id = id.into();
        let desc = description.into();
        self.node(WorkflowNode::new(
            id.clone(),
            desc.clone(),
            NodeType::Task {
                description: desc,
                agent_role,
                timeout_secs: None,
            },
        ))
    }

    /// Connect two nodes
    pub fn connect(mut self, from: impl Into<NodeId>, to: impl Into<NodeId>) -> Self {
        let from_id = from.into();
        let to_id = to.into();

        // Find and update the source node
        for node in &mut self.nodes {
            if node.id == from_id {
                if !node.outputs.contains(&to_id) {
                    node.outputs.push(to_id.clone());
                }
            }
            if node.id == to_id {
                if !node.inputs.contains(&from_id) {
                    node.inputs.push(from_id.clone());
                }
            }
        }

        self
    }

    /// Build the workflow
    pub fn build(self) -> Result<Workflow, WorkflowError> {
        if self.name.is_empty() {
            return Err(WorkflowError::ValidationError(
                "Workflow name is required".to_string(),
            ));
        }

        let workflow = Workflow {
            id: self.id,
            name: self.name,
            description: self.description,
            nodes: self.nodes,
            version: self.version,
            tags: self.tags,
            metadata: self.metadata,
        };

        workflow.validate()?;
        Ok(workflow)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_workflow_builder_basic() {
        let workflow = WorkflowBuilder::new("test-wf")
            .name("Test Workflow")
            .description("A test workflow")
            .version("2.0.0")
            .tag("test")
            .build()
            .unwrap();

        assert_eq!(workflow.id, "test-wf");
        assert_eq!(workflow.name, "Test Workflow");
        assert_eq!(workflow.description, Some("A test workflow".to_string()));
        assert_eq!(workflow.version, "2.0.0");
        assert!(workflow.tags.contains(&"test".to_string()));
    }

    #[test]
    fn test_workflow_with_nodes() {
        let workflow = WorkflowBuilder::new("node-wf")
            .name("Node Workflow")
            .start_node("start")
            .end_node("end")
            .connect("start", "end")
            .build()
            .unwrap();

        assert_eq!(workflow.nodes.len(), 2);
        assert_eq!(workflow.start_nodes().len(), 1);
        assert_eq!(workflow.end_nodes().len(), 1);
    }

    #[test]
    fn test_workflow_successors_predecessors() {
        let workflow = WorkflowBuilder::new("succ-pred")
            .name("Successor/Predecessor Test")
            .start_node("start")
            .task_node("middle", "Middle Task", None)
            .end_node("end")
            .connect("start", "middle")
            .connect("middle", "end")
            .build()
            .unwrap();

        let middle_id = NodeId::new("middle");
        let successors = workflow.successors(&middle_id);
        assert_eq!(successors.len(), 1);
        assert!(successors[0].is_end());

        let predecessors = workflow.predecessors(&middle_id);
        assert_eq!(predecessors.len(), 1);
        assert!(predecessors[0].is_start());
    }

    #[test]
    fn test_workflow_validation_duplicate_id() {
        let node1 = WorkflowNode::new("dup", "First", NodeType::Start);
        let node2 = WorkflowNode::new("dup", "Second", NodeType::End);

        let result = WorkflowBuilder::new("dup-test")
            .name("Duplicate Test")
            .node(node1)
            .node(node2)
            .build();

        assert!(matches!(result, Err(WorkflowError::DuplicateId(_))));
    }

    #[test]
    fn test_workflow_validation_invalid_edge() {
        let mut node = WorkflowNode::new("start", "Start", NodeType::Start);
        node.outputs.push(NodeId::new("nonexistent"));

        let result = WorkflowBuilder::new("invalid-edge")
            .name("Invalid Edge Test")
            .node(node)
            .build();

        assert!(matches!(result, Err(WorkflowError::NodeNotFound(_))));
    }

    #[test]
    fn test_workflow_topological_sort() {
        let workflow = WorkflowBuilder::new("topo-sort")
            .name("Topological Sort Test")
            .start_node("a")
            .task_node("b", "Task B", None)
            .task_node("c", "Task C", None)
            .end_node("d")
            .connect("a", "b")
            .connect("a", "c")
            .connect("b", "d")
            .connect("c", "d")
            .build()
            .unwrap();

        let sorted = workflow.topological_sort().unwrap();
        assert_eq!(sorted.len(), 4);

        // Start should be first
        assert_eq!(sorted[0].id.0, "a");
        // End should be last
        assert_eq!(sorted[3].id.0, "d");
    }

    #[test]
    fn test_workflow_get_ready_nodes() {
        let workflow = WorkflowBuilder::new("ready-test")
            .name("Ready Nodes Test")
            .start_node("start")
            .task_node("task1", "Task 1", None)
            .task_node("task2", "Task 2", None)
            .end_node("end")
            .connect("start", "task1")
            .connect("start", "task2")
            .connect("task1", "end")
            .connect("task2", "end")
            .build()
            .unwrap();

        // Initially, only start should be ready
        let ready = workflow.get_ready_nodes(&HashSet::new());
        assert_eq!(ready.len(), 1);
        assert_eq!(ready[0].id.0, "start");

        // After start is done, task1 and task2 should be ready
        let mut completed = HashSet::new();
        completed.insert(NodeId::new("start"));
        let ready = workflow.get_ready_nodes(&completed);
        assert_eq!(ready.len(), 2);

        // After task1 and task2 are done, end should be ready
        completed.insert(NodeId::new("task1"));
        completed.insert(NodeId::new("task2"));
        let ready = workflow.get_ready_nodes(&completed);
        assert_eq!(ready.len(), 1);
        assert_eq!(ready[0].id.0, "end");
    }

    #[test]
    fn test_workflow_entry_exit_nodes() {
        let workflow = WorkflowBuilder::new("entry-exit")
            .name("Entry/Exit Test")
            .start_node("entry1")
            .start_node("entry2")
            .end_node("exit")
            .connect("entry1", "exit")
            .connect("entry2", "exit")
            .build()
            .unwrap();

        assert_eq!(workflow.entry_nodes().len(), 2);
        assert_eq!(workflow.exit_nodes().len(), 1);
    }

    #[test]
    fn test_workflow_error_display() {
        let err = WorkflowError::DuplicateId("test-id".to_string());
        assert!(err.to_string().contains("test-id"));

        let err = WorkflowError::CycleDetected(vec!["a".to_string(), "b".to_string()]);
        assert!(err.to_string().contains("Cycle"));
    }

    #[test]
    fn test_workflow_missing_name() {
        let result = WorkflowBuilder::new("no-name").build();
        assert!(matches!(result, Err(WorkflowError::ValidationError(_))));
    }

    #[test]
    fn test_workflow_with_task_and_agent() {
        let workflow = WorkflowBuilder::new("agent-wf")
            .name("Agent Workflow")
            .start_node("start")
            .task_node("frontend-task", "Create UI", Some("frontend".to_string()))
            .task_node("backend-task", "Create API", Some("backend".to_string()))
            .end_node("end")
            .connect("start", "frontend-task")
            .connect("start", "backend-task")
            .connect("frontend-task", "end")
            .connect("backend-task", "end")
            .build()
            .unwrap();

        assert_eq!(workflow.nodes.len(), 4);
    }
}