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
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
//! Tests for foreach execution functionality

use super::foreach::*;
use crate::config::command::{
    ForeachConfig, ForeachInput, ParallelConfig, TestCommand, WorkflowStepCommand,
};
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use tempfile::TempDir;

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

    /// Test basic foreach with list input
    #[tokio::test]
    async fn test_foreach_with_list_input() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec!["item1".to_string(), "item2".to_string()]),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![Box::new(WorkflowStepCommand {
                shell: Some("echo Processing ${item}".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false,
            max_items: None,
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 2);
        assert_eq!(result.successful_items, 2);
        assert_eq!(result.failed_items, 0);
        assert!(result.errors.is_empty());
    }

    /// Test foreach with command input
    #[tokio::test]
    async fn test_foreach_with_command_input() {
        let config = ForeachConfig {
            input: ForeachInput::Command("echo -e 'item1\\nitem2\\nitem3'".to_string()),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![Box::new(WorkflowStepCommand {
                shell: Some("echo Processing ${item}".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false,
            max_items: None,
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 3);
        assert_eq!(result.successful_items, 3);
        assert_eq!(result.failed_items, 0);
    }

    /// Test parallel execution
    #[tokio::test]
    async fn test_foreach_parallel_execution() {
        // Create a counter to track concurrent executions
        let _counter = Arc::new(AtomicUsize::new(0));
        let _max_concurrent = Arc::new(AtomicUsize::new(0));

        let config = ForeachConfig {
            input: ForeachInput::List(vec![
                "item1".to_string(),
                "item2".to_string(),
                "item3".to_string(),
                "item4".to_string(),
            ]),
            parallel: ParallelConfig::Count(2), // Limit to 2 parallel
            do_block: vec![Box::new(WorkflowStepCommand {
                // This command will help us test parallelism
                shell: Some("sleep 0.1 && echo Processing ${item}".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false,
            max_items: None,
        };

        let start = std::time::Instant::now();
        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();
        let duration = start.elapsed();

        assert_eq!(result.total_items, 4);
        assert_eq!(result.successful_items, 4);
        assert_eq!(result.failed_items, 0);

        // With parallelism of 2, 4 items with 0.1s sleep each should take ~0.2s, not 0.4s
        // Allow more time on CI systems which may have resource constraints
        assert!(
            duration.as_secs_f32() < 0.6,
            "Parallel execution took {:.2}s, expected < 0.6s",
            duration.as_secs_f32()
        );
    }

    /// Test max_items limit
    #[tokio::test]
    async fn test_foreach_max_items() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec![
                "item1".to_string(),
                "item2".to_string(),
                "item3".to_string(),
                "item4".to_string(),
                "item5".to_string(),
            ]),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![Box::new(WorkflowStepCommand {
                shell: Some("echo Processing ${item}".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false,
            max_items: Some(3), // Limit to 3 items
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 3); // Should process only 3 items
        assert_eq!(result.successful_items, 3);
        assert_eq!(result.failed_items, 0);
    }

    /// Test continue_on_error behavior
    #[tokio::test]
    async fn test_foreach_continue_on_error() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec![
                "item1".to_string(),
                "fail".to_string(),
                "item3".to_string(),
            ]),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![Box::new(WorkflowStepCommand {
                // This will fail for "fail" item
                shell: Some("test \"${item}\" != \"fail\" && echo Success || exit 1".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: true, // Continue despite failures
            max_items: None,
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 3);
        assert_eq!(result.successful_items, 2);
        assert_eq!(result.failed_items, 1);
        assert_eq!(result.errors.len(), 1);
    }

    /// Test fail-fast behavior (stop on first error)
    #[tokio::test]
    async fn test_foreach_fail_fast() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec![
                "item1".to_string(),
                "fail".to_string(),
                "item3".to_string(),
            ]),
            parallel: ParallelConfig::Boolean(false), // Sequential to ensure order
            do_block: vec![Box::new(WorkflowStepCommand {
                // This will fail for "fail" item
                shell: Some("test \"${item}\" != \"fail\" && echo Success || exit 1".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false, // Stop on first error
            max_items: None,
        };

        let result = execute_foreach(&config).await;

        // Should fail with error
        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Foreach execution failed"));
    }

    /// Test variable interpolation with index and total
    #[tokio::test]
    async fn test_foreach_variable_interpolation() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec!["item1".to_string(), "item2".to_string()]),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![Box::new(WorkflowStepCommand {
                shell: Some("echo 'Processing ${item} (${index}/${total})'".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false,
            max_items: None,
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 2);
        assert_eq!(result.successful_items, 2);
        assert_eq!(result.failed_items, 0);
    }

    /// Test empty input list
    #[tokio::test]
    async fn test_foreach_empty_input() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec![]),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![Box::new(WorkflowStepCommand {
                shell: Some("echo Processing ${item}".to_string()),
                claude: None,
                analyze: None,
                test: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false,
            max_items: None,
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 0);
        assert_eq!(result.successful_items, 0);
        assert_eq!(result.failed_items, 0);
        assert!(result.errors.is_empty());
    }

    /// Test multiple commands in do block
    #[tokio::test]
    async fn test_foreach_multiple_commands() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec!["item1".to_string(), "item2".to_string()]),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![
                Box::new(WorkflowStepCommand {
                    shell: Some("echo 'Starting ${item}'".to_string()),
                    claude: None,
                    analyze: None,
                    test: None,
                    foreach: None,
                    write_file: None,
                    id: None,
                    commit_required: false,
                    analysis: None,
                    outputs: None,
                    validate: None,
                    timeout: None,
                    when: None,
                    capture_format: None,
                    capture_streams: None,
                    output_file: None,
                    capture_output: None,
                    on_failure: None,
                    on_success: None,
                }),
                Box::new(WorkflowStepCommand {
                    shell: Some("echo 'Processing ${item}'".to_string()),
                    claude: None,
                    analyze: None,
                    test: None,
                    foreach: None,
                    write_file: None,
                    id: None,
                    commit_required: false,
                    analysis: None,
                    outputs: None,
                    validate: None,
                    timeout: None,
                    when: None,
                    capture_format: None,
                    capture_streams: None,
                    output_file: None,
                    capture_output: None,
                    on_failure: None,
                    on_success: None,
                }),
                Box::new(WorkflowStepCommand {
                    shell: Some("echo 'Finished ${item}'".to_string()),
                    claude: None,
                    analyze: None,
                    test: None,
                    foreach: None,
                    write_file: None,
                    id: None,
                    commit_required: false,
                    analysis: None,
                    outputs: None,
                    validate: None,
                    timeout: None,
                    when: None,
                    capture_format: None,
                    capture_streams: None,
                    output_file: None,
                    capture_output: None,
                    on_failure: None,
                    on_success: None,
                }),
            ],
            continue_on_error: false,
            max_items: None,
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 2);
        assert_eq!(result.successful_items, 2);
        assert_eq!(result.failed_items, 0);
    }

    /// Test deprecated test command type
    #[tokio::test]
    async fn test_foreach_deprecated_test_command() {
        let config = ForeachConfig {
            input: ForeachInput::List(vec!["item1".to_string()]),
            parallel: ParallelConfig::Boolean(false),
            do_block: vec![Box::new(WorkflowStepCommand {
                test: Some(TestCommand {
                    command: "echo 'Testing ${item}'".to_string(),
                    on_failure: None,
                }),
                shell: None,
                claude: None,
                analyze: None,
                foreach: None,
                write_file: None,
                id: None,
                commit_required: false,
                analysis: None,
                outputs: None,
                validate: None,
                timeout: None,
                when: None,
                capture_format: None,
                capture_streams: None,
                output_file: None,
                capture_output: None,
                on_failure: None,
                on_success: None,
            })],
            continue_on_error: false,
            max_items: None,
        };

        let temp_dir = TempDir::new().unwrap();
        let result = execute_foreach_with_dir(&config, Some(temp_dir.path().to_path_buf()))
            .await
            .unwrap();

        assert_eq!(result.total_items, 1);
        assert_eq!(result.successful_items, 1);
        assert_eq!(result.failed_items, 0);
    }
}

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

    /// Test command that produces no output
    #[tokio::test]
    async fn test_command_input_empty_output() {
        let input = ForeachInput::Command("echo ''".to_string());
        let items = get_items(&input).await.unwrap();
        assert_eq!(items.len(), 0);
    }

    /// Test command that produces items with spaces
    #[tokio::test]
    async fn test_command_input_with_spaces() {
        let input = ForeachInput::Command("printf 'item with spaces\\nanother item'".to_string());
        let items = get_items(&input).await.unwrap();
        assert_eq!(items.len(), 2);
        assert_eq!(items[0], "item with spaces");
        assert_eq!(items[1], "another item");
    }

    /// Test command that fails
    #[tokio::test]
    async fn test_command_input_failure() {
        let input = ForeachInput::Command("exit 1".to_string());
        let result = get_items(&input).await;
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Foreach command failed"));
    }

    /// Test list input preservation
    #[tokio::test]
    async fn test_list_input_preservation() {
        let original_items = vec![
            "item1".to_string(),
            "item with spaces".to_string(),
            "item-with-dashes".to_string(),
            "item_with_underscores".to_string(),
        ];
        let input = ForeachInput::List(original_items.clone());
        let items = get_items(&input).await.unwrap();
        assert_eq!(items, original_items);
    }
}