prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
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
//! Workflow coordinator for high-level workflow orchestration

use crate::config::command::WorkflowStepCommand;
use crate::config::WorkflowCommand;
use crate::cook::interaction::UserInteraction;
use crate::cook::workflow::{CaptureOutput, WorkflowStep};
use anyhow::Result;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;

/// Workflow execution context
#[derive(Debug, Clone)]
pub struct WorkflowContext {
    /// Current iteration
    pub iteration: usize,
    /// Maximum iterations
    pub max_iterations: usize,
    /// Variables for command substitution
    pub variables: HashMap<String, String>,
}

/// Trait for workflow coordination
#[async_trait]
pub trait WorkflowCoordinator: Send + Sync {
    /// Execute a workflow step
    async fn execute_step(
        &self,
        step: &WorkflowStep,
        context: &WorkflowContext,
    ) -> Result<HashMap<String, String>>;

    /// Execute complete workflow
    async fn execute_workflow(
        &self,
        commands: &[WorkflowCommand],
        context: &mut WorkflowContext,
    ) -> Result<()>;

    /// Check if workflow should continue
    async fn should_continue(&self, context: &WorkflowContext) -> Result<bool>;

    /// Handle user interaction
    async fn prompt_user(&self, message: &str, default: bool) -> Result<bool>;

    /// Display progress
    fn display_progress(&self, message: &str);
}

/// Default implementation of workflow coordinator
pub struct DefaultWorkflowCoordinator {
    #[allow(dead_code)]
    workflow_executor: Arc<dyn crate::cook::workflow::WorkflowExecutor>,
    user_interaction: Arc<dyn UserInteraction>,
}

impl DefaultWorkflowCoordinator {
    /// Create new workflow coordinator
    pub fn new(
        workflow_executor: Arc<dyn crate::cook::workflow::WorkflowExecutor>,
        user_interaction: Arc<dyn UserInteraction>,
    ) -> Self {
        Self {
            workflow_executor,
            user_interaction,
        }
    }

    fn extract_command_string(command: &WorkflowCommand) -> String {
        match command {
            crate::config::WorkflowCommand::Simple(s) => s.clone(),
            crate::config::WorkflowCommand::Structured(c) => c.name.clone(),
            crate::config::WorkflowCommand::WorkflowStep(step) => {
                Self::extract_workflow_step_command(step)
            }
            crate::config::WorkflowCommand::SimpleObject(obj) => obj.name.clone(),
        }
    }

    fn extract_workflow_step_command(step: &WorkflowStepCommand) -> String {
        if let Some(claude_cmd) = &step.claude {
            claude_cmd.clone()
        } else if let Some(shell_cmd) = &step.shell {
            format!("shell {shell_cmd}")
        } else if let Some(test_cmd) = &step.test {
            format!("test {}", test_cmd.command)
        } else {
            String::new()
        }
    }

    fn normalize_command_string(command_str: String) -> String {
        if command_str.starts_with('/') {
            command_str
        } else {
            format!("/{command_str}")
        }
    }

    fn create_default_workflow_step(command: Option<String>) -> WorkflowStep {
        WorkflowStep {
            name: None,
            command,
            claude: None,
            shell: None,
            test: None,
            foreach: None,
            write_file: None,
            handler: None,
            capture: None,
            capture_format: None,
            capture_streams: Default::default(),
            output_file: None,
            capture_output: CaptureOutput::Disabled,
            timeout: None,
            working_dir: None,
            env: HashMap::new(),
            on_failure: None,
            retry: None,
            on_success: None,
            on_exit_code: HashMap::new(),
            commit_required: true,
            auto_commit: false,
            commit_config: None,
            validate: None,
            step_validate: None,
            skip_validation: false,
            validation_timeout: None,
            ignore_validation_failure: false,
            when: None,
        }
    }

    fn convert_to_workflow_step(command: &WorkflowCommand) -> WorkflowStep {
        let command_str = Self::extract_command_string(command);
        let normalized_command = Self::normalize_command_string(command_str);
        Self::create_default_workflow_step(Some(normalized_command))
    }
}

#[async_trait]
impl WorkflowCoordinator for DefaultWorkflowCoordinator {
    async fn execute_step(
        &self,
        step: &WorkflowStep,
        context: &WorkflowContext,
    ) -> Result<HashMap<String, String>> {
        // Display progress
        let step_display = step.name.as_deref().unwrap_or("unnamed step");
        self.display_progress(&format!(
            "Executing step: {} (iteration {}/{})",
            step_display, context.iteration, context.max_iterations
        ));

        // For now, return empty outputs as actual execution would be done elsewhere
        // The coordinator just coordinates, doesn't execute
        Ok(HashMap::new())
    }

    async fn execute_workflow(
        &self,
        commands: &[WorkflowCommand],
        context: &mut WorkflowContext,
    ) -> Result<()> {
        // Execute workflow
        loop {
            context.iteration += 1;

            // Check if we should continue
            if !self.should_continue(context).await? {
                break;
            }

            // Execute all commands in the workflow
            for command in commands.iter() {
                let step = Self::convert_to_workflow_step(command);
                let _outputs = self.execute_step(&step, context).await?;
            }
        }

        Ok(())
    }

    async fn should_continue(&self, context: &WorkflowContext) -> Result<bool> {
        // Check iteration limit
        if context.iteration > context.max_iterations {
            self.display_progress(&format!(
                "Reached maximum iterations ({})",
                context.max_iterations
            ));
            return Ok(false);
        }

        // Could add more conditions here (e.g., check if improvements were made)
        Ok(true)
    }

    async fn prompt_user(&self, message: &str, _default: bool) -> Result<bool> {
        self.user_interaction.prompt_yes_no(message).await
    }

    fn display_progress(&self, message: &str) {
        self.user_interaction.display_progress(message);
    }
}

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

    #[tokio::test]
    async fn test_workflow_should_continue() {
        let context = WorkflowContext {
            iteration: 3,
            max_iterations: 5,
            variables: HashMap::new(),
        };

        // Test with a simple mock coordinator
        struct TestCoordinator;

        #[async_trait]
        impl WorkflowCoordinator for TestCoordinator {
            async fn execute_step(
                &self,
                _step: &WorkflowStep,
                _context: &WorkflowContext,
            ) -> Result<HashMap<String, String>> {
                Ok(HashMap::new())
            }

            async fn execute_workflow(
                &self,
                _commands: &[WorkflowCommand],
                _context: &mut WorkflowContext,
            ) -> Result<()> {
                Ok(())
            }

            async fn should_continue(&self, context: &WorkflowContext) -> Result<bool> {
                Ok(context.iteration <= context.max_iterations)
            }

            async fn prompt_user(&self, _message: &str, _default: bool) -> Result<bool> {
                Ok(true)
            }

            fn display_progress(&self, _message: &str) {}
        }

        let coordinator = TestCoordinator;
        let should_continue = coordinator.should_continue(&context).await.unwrap();
        assert!(should_continue);
    }

    #[tokio::test]
    async fn test_workflow_max_iterations_reached() {
        let context = WorkflowContext {
            iteration: 6,
            max_iterations: 5,
            variables: HashMap::new(),
        };

        struct TestCoordinator;

        #[async_trait]
        impl WorkflowCoordinator for TestCoordinator {
            async fn execute_step(
                &self,
                _step: &WorkflowStep,
                _context: &WorkflowContext,
            ) -> Result<HashMap<String, String>> {
                Ok(HashMap::new())
            }

            async fn execute_workflow(
                &self,
                _commands: &[WorkflowCommand],
                _context: &mut WorkflowContext,
            ) -> Result<()> {
                Ok(())
            }

            async fn should_continue(&self, context: &WorkflowContext) -> Result<bool> {
                Ok(context.iteration <= context.max_iterations)
            }

            async fn prompt_user(&self, _message: &str, _default: bool) -> Result<bool> {
                Ok(true)
            }

            fn display_progress(&self, _message: &str) {}
        }

        let coordinator = TestCoordinator;
        let should_continue = coordinator.should_continue(&context).await.unwrap();
        assert!(!should_continue);
    }

    #[test]
    fn test_extract_command_string_simple() {
        let command = WorkflowCommand::Simple("test-command".to_string());
        let result = DefaultWorkflowCoordinator::extract_command_string(&command);
        assert_eq!(result, "test-command");
    }

    #[test]
    fn test_extract_command_string_structured() {
        use crate::config::command::Command;
        let command = WorkflowCommand::Structured(Box::new(Command {
            name: "structured-command".to_string(),
            args: vec![],
            options: HashMap::new(),
            metadata: Default::default(),
            id: None,
            outputs: None,
            analysis: None,
        }));
        let result = DefaultWorkflowCoordinator::extract_command_string(&command);
        assert_eq!(result, "structured-command");
    }

    #[test]
    fn test_extract_command_string_simple_object() {
        use crate::config::command::SimpleCommand;
        let command = WorkflowCommand::SimpleObject(SimpleCommand {
            name: "simple-object".to_string(),
            commit_required: None,
            args: None,
            analysis: None,
        });
        let result = DefaultWorkflowCoordinator::extract_command_string(&command);
        assert_eq!(result, "simple-object");
    }

    #[test]
    fn test_extract_workflow_step_command_claude() {
        let step = WorkflowStepCommand {
            claude: Some("claude-command".to_string()),
            shell: None,
            analyze: None,
            test: None,
            foreach: None,
            write_file: None,
            id: None,
            capture_output: None,
            on_failure: None,
            on_success: None,
            commit_required: true,
            analysis: None,
            outputs: None,
            validate: None,
            timeout: None,
            when: None,
            capture_format: None,
            capture_streams: None,
            output_file: None,
        };
        let result = DefaultWorkflowCoordinator::extract_workflow_step_command(&step);
        assert_eq!(result, "claude-command");
    }

    #[test]
    fn test_extract_workflow_step_command_shell() {
        let step = WorkflowStepCommand {
            claude: None,
            shell: Some("ls -la".to_string()),
            analyze: None,
            test: None,
            foreach: None,
            write_file: None,
            id: None,
            capture_output: None,
            on_failure: None,
            on_success: None,
            commit_required: true,
            analysis: None,
            outputs: None,
            validate: None,
            timeout: None,
            when: None,
            capture_format: None,
            capture_streams: None,
            output_file: None,
        };
        let result = DefaultWorkflowCoordinator::extract_workflow_step_command(&step);
        assert_eq!(result, "shell ls -la");
    }

    #[test]
    fn test_extract_workflow_step_command_test() {
        use crate::config::command::TestCommand;
        let step = WorkflowStepCommand {
            claude: None,
            shell: None,
            analyze: None,
            test: Some(TestCommand {
                command: "cargo test".to_string(),
                on_failure: None,
            }),
            foreach: None,
            write_file: None,
            id: None,
            capture_output: None,
            on_failure: None,
            on_success: None,
            commit_required: true,
            analysis: None,
            outputs: None,
            validate: None,
            timeout: None,
            when: None,
            capture_format: None,
            capture_streams: None,
            output_file: None,
        };
        let result = DefaultWorkflowCoordinator::extract_workflow_step_command(&step);
        assert_eq!(result, "test cargo test");
    }

    #[test]
    fn test_extract_workflow_step_command_empty() {
        let step = WorkflowStepCommand {
            claude: None,
            shell: None,
            analyze: None,
            test: None,
            foreach: None,
            write_file: None,
            id: None,
            capture_output: None,
            on_failure: None,
            on_success: None,
            commit_required: true,
            analysis: None,
            outputs: None,
            validate: None,
            timeout: None,
            when: None,
            capture_format: None,
            capture_streams: None,
            output_file: None,
        };
        let result = DefaultWorkflowCoordinator::extract_workflow_step_command(&step);
        assert_eq!(result, "");
    }

    #[test]
    fn test_normalize_command_string_with_slash() {
        let result = DefaultWorkflowCoordinator::normalize_command_string("/command".to_string());
        assert_eq!(result, "/command");
    }

    #[test]
    fn test_normalize_command_string_without_slash() {
        let result = DefaultWorkflowCoordinator::normalize_command_string("command".to_string());
        assert_eq!(result, "/command");
    }

    #[test]
    fn test_create_default_workflow_step() {
        let step =
            DefaultWorkflowCoordinator::create_default_workflow_step(Some("/test".to_string()));
        assert_eq!(step.command, Some("/test".to_string()));
        assert!(step.commit_required);
        assert!(step.env.is_empty());
        assert!(step.on_exit_code.is_empty());
        assert!(step.name.is_none());
        assert!(step.claude.is_none());
        assert!(step.shell.is_none());
        assert!(step.test.is_none());
        assert!(step.handler.is_none());
        assert!(!step.capture_output.is_enabled());
        assert!(step.timeout.is_none());
        assert!(step.working_dir.is_none());
        assert!(step.on_failure.is_none());
        assert!(step.on_success.is_none());
    }

    #[test]
    fn test_convert_to_workflow_step_integration() {
        // Test with a simple command
        let command = WorkflowCommand::Simple("test".to_string());
        let step = DefaultWorkflowCoordinator::convert_to_workflow_step(&command);
        assert_eq!(step.command, Some("/test".to_string()));

        // Test with a command already having slash
        let command = WorkflowCommand::Simple("/test".to_string());
        let step = DefaultWorkflowCoordinator::convert_to_workflow_step(&command);
        assert_eq!(step.command, Some("/test".to_string()));
    }

    #[test]
    fn test_extract_workflow_step_command_priority() {
        // Test priority: claude > shell > test
        let step = WorkflowStepCommand {
            claude: Some("claude".to_string()),
            shell: Some("shell".to_string()),
            analyze: None,
            test: Some(crate::config::command::TestCommand {
                command: "test".to_string(),
                on_failure: None,
            }),
            foreach: None,
            write_file: None,
            id: None,
            capture_output: None,
            on_failure: None,
            on_success: None,
            commit_required: true,
            analysis: None,
            outputs: None,
            validate: None,
            timeout: None,
            when: None,
            capture_format: None,
            capture_streams: None,
            output_file: None,
        };
        let result = DefaultWorkflowCoordinator::extract_workflow_step_command(&step);
        assert_eq!(
            result, "claude",
            "claude should take priority over shell and test"
        );
    }
}