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
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
//! Comprehensive tests for the agent module

use super::*;
use crate::worktree::WorktreeManager;
use serde_json::json;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

// Test AgentResult creation and status checking
#[test]
fn test_agent_result_creation() {
    let result = AgentResult::success(
        "item-1".to_string(),
        Some("output".to_string()),
        Duration::from_secs(5),
    );

    assert!(result.is_success());
    assert!(!result.is_failure());
    assert_eq!(result.item_id, "item-1");
    assert_eq!(result.output, Some("output".to_string()));
    assert!(matches!(result.status, AgentStatus::Success));
}

#[test]
fn test_agent_result_failure() {
    let result = AgentResult::failed(
        "item-2".to_string(),
        "error occurred".to_string(),
        Duration::from_secs(2),
    );

    assert!(!result.is_success());
    assert!(result.is_failure());
    assert_eq!(result.item_id, "item-2");
    assert_eq!(result.error, Some("error occurred".to_string()));
    assert!(matches!(result.status, AgentStatus::Failed(_)));
}

#[test]
fn test_agent_result_with_worktree_info() {
    let mut result = AgentResult::success("item-1".to_string(), None, Duration::from_secs(5));

    result.worktree_path = Some(PathBuf::from("/tmp/worktree"));
    result.branch_name = Some("feature-branch".to_string());
    result.worktree_session_id = Some("session-123".to_string());
    result.files_modified = vec!["file1.rs".to_string(), "file2.rs".to_string()];
    result.commits = vec!["commit1".to_string(), "commit2".to_string()];

    assert_eq!(result.worktree_path, Some(PathBuf::from("/tmp/worktree")));
    assert_eq!(result.branch_name, Some("feature-branch".to_string()));
    assert_eq!(result.worktree_session_id, Some("session-123".to_string()));
    assert_eq!(result.files_modified.len(), 2);
    assert_eq!(result.commits.len(), 2);
}

// Test AggregatedResults functionality
#[test]
fn test_aggregated_results() {
    let results = vec![
        AgentResult::success("item-1".to_string(), None, Duration::from_secs(5)),
        AgentResult::failed(
            "item-2".to_string(),
            "error".to_string(),
            Duration::from_secs(3),
        ),
        AgentResult::success("item-3".to_string(), None, Duration::from_secs(4)),
    ];

    let aggregated = AggregatedResults::from_results(results);

    assert_eq!(aggregated.success_count, 2);
    assert_eq!(aggregated.failure_count, 1);
    assert_eq!(aggregated.total, 3);
    assert_eq!(aggregated.successful.len(), 2);
    assert_eq!(aggregated.failed.len(), 1);
}

#[test]
fn test_aggregated_results_json_conversion() {
    let mut results = vec![
        AgentResult::success(
            "item-1".to_string(),
            Some("output1".to_string()),
            Duration::from_secs(5),
        ),
        AgentResult::failed(
            "item-2".to_string(),
            "error".to_string(),
            Duration::from_secs(3),
        ),
    ];

    // Add commits to first result
    results[0].commits = vec!["commit1".to_string()];

    let aggregated = AggregatedResults::from_results(results);
    let json_value = aggregated.to_json_value();

    assert_eq!(json_value["total"], json!(2));
    assert_eq!(json_value["success_count"], json!(1));
    assert_eq!(json_value["failure_count"], json!(1));
    assert!(json_value["successful"].is_array());
    assert!(json_value["failed"].is_array());
}

#[test]
fn test_aggregated_results_empty() {
    let aggregated = AggregatedResults::from_results(vec![]);

    assert_eq!(aggregated.total, 0);
    assert_eq!(aggregated.success_count, 0);
    assert_eq!(aggregated.failure_count, 0);
    assert!(aggregated.successful.is_empty());
    assert!(aggregated.failed.is_empty());
}

// Test AgentResultAggregator
#[tokio::test]
async fn test_default_result_aggregator() {
    let aggregator = DefaultResultAggregator::new();

    let results = vec![
        AgentResult::success("item-1".to_string(), None, Duration::from_secs(1)),
        AgentResult::failed(
            "item-2".to_string(),
            "error".to_string(),
            Duration::from_secs(1),
        ),
        AgentResult::success(
            "item-3".to_string(),
            Some("output".to_string()),
            Duration::from_secs(2),
        ),
    ];

    let aggregated = aggregator.aggregate(results.clone());

    assert_eq!(aggregated.success_count, 2);
    assert_eq!(aggregated.failure_count, 1);
    assert_eq!(aggregated.total, 3);
}

#[tokio::test]
async fn test_aggregator_to_interpolation_context() {
    let aggregator = DefaultResultAggregator::new();

    let results = vec![
        AgentResult::success(
            "item-1".to_string(),
            Some("output1".to_string()),
            Duration::from_secs(1),
        ),
        AgentResult::failed(
            "item-2".to_string(),
            "error".to_string(),
            Duration::from_secs(1),
        ),
    ];

    let aggregated = aggregator.aggregate(results.clone());
    let context = aggregator.to_interpolation_context(&aggregated);

    // Check that variables were set correctly
    assert!(context.variables.contains_key("map.successful"));
    assert!(context.variables.contains_key("map.failed"));
    assert!(context.variables.contains_key("map.total"));
    assert!(context.variables.contains_key("map.results"));
}

#[tokio::test]
async fn test_aggregator_to_variable_context() {
    let aggregator = DefaultResultAggregator::new();

    let results = vec![
        AgentResult::success("item-1".to_string(), None, Duration::from_secs(1)),
        AgentResult::failed(
            "item-2".to_string(),
            "error".to_string(),
            Duration::from_secs(1),
        ),
    ];

    let aggregated = aggregator.aggregate(results.clone());
    let context = aggregator.to_variable_context(&aggregated).await;

    // Verify that the context has the expected global variables
    // Note: We can't easily check the actual values without exposing internal state,
    // but we can verify the context was created
    assert!(std::mem::size_of_val(&context) > 0);
}

// Test AgentState and status transitions
#[test]
fn test_agent_state_transitions() {
    let state = AgentState::default();
    assert_eq!(state.status, AgentStateStatus::Idle);

    let state = AgentState {
        status: AgentStateStatus::Executing,
        ..Default::default()
    };
    assert!(matches!(state.status, AgentStateStatus::Executing));

    let state = AgentState {
        status: AgentStateStatus::Completed,
        ..Default::default()
    };
    assert!(matches!(state.status, AgentStateStatus::Completed));
}

#[test]
fn test_agent_state_with_error() {
    let state = AgentState {
        status: AgentStateStatus::Failed("Test error".to_string()),
        retry_count: 2,
        ..Default::default()
    };

    assert!(matches!(state.status, AgentStateStatus::Failed(_)));
    assert_eq!(state.retry_count, 2);
}

// Test AgentOperation enum
#[test]
fn test_agent_operation_variants() {
    let op1 = AgentOperation::Idle;
    assert!(matches!(op1, AgentOperation::Idle));

    let op2 = AgentOperation::Claude("test command".to_string());
    if let AgentOperation::Claude(item) = op2 {
        assert_eq!(item, "test command");
    } else {
        panic!("Expected Processing variant");
    }

    let op3 = AgentOperation::Retrying("item-2".to_string(), 2);
    if let AgentOperation::Retrying(item, attempt) = op3 {
        assert_eq!(item, "item-2");
        assert_eq!(attempt, 2);
    } else {
        panic!("Expected Retrying variant");
    }

    let op4 = AgentOperation::Complete;
    assert!(matches!(op4, AgentOperation::Complete));
}

// Test lifecycle manager
#[tokio::test]
async fn test_default_lifecycle_manager() {
    let temp_dir = tempfile::tempdir().unwrap();
    let project_root = temp_dir.path().to_path_buf();

    // Initialize git repository in temp directory
    std::process::Command::new("git")
        .arg("init")
        .current_dir(&project_root)
        .output()
        .expect("Failed to initialize git repository");

    // Configure git user for the test
    std::process::Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(&project_root)
        .output()
        .expect("Failed to configure git email");

    std::process::Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(&project_root)
        .output()
        .expect("Failed to configure git name");

    // Create initial commit
    std::fs::write(project_root.join("README.md"), "Test").unwrap();
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&project_root)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(["commit", "-m", "Initial commit"])
        .current_dir(&project_root)
        .output()
        .unwrap();

    let subprocess = crate::subprocess::SubprocessManager::production();
    let worktree_manager = Arc::new(WorktreeManager::new(project_root, subprocess).unwrap());

    let lifecycle_manager = DefaultLifecycleManager::new(worktree_manager);

    let config = AgentConfig::new(
        "agent-1".to_string(),
        "item-1".to_string(),
        "test-branch".to_string(),
        3,
        Duration::from_secs(60),
        0,
        10,
    );

    // Test initialization
    let commands = vec![];
    let handle = lifecycle_manager
        .create_agent(config, commands)
        .await
        .unwrap();
    assert_eq!(handle.id(), "agent-1");

    // Test cleanup (should not fail even if worktree doesn't exist)
    lifecycle_manager.cleanup_agent(handle).await.unwrap();
}

// Test helper functions for creating test data
fn create_test_agent_result(id: &str, success: bool) -> AgentResult {
    if success {
        AgentResult::success(
            id.to_string(),
            Some(format!("Output for {}", id)),
            Duration::from_secs(1),
        )
    } else {
        AgentResult::failed(
            id.to_string(),
            format!("Error for {}", id),
            Duration::from_secs(1),
        )
    }
}

#[test]
fn test_batch_agent_results() {
    let results: Vec<AgentResult> = (0..10)
        .map(|i| create_test_agent_result(&format!("item-{}", i), i % 3 != 0))
        .collect();

    let success_count = results.iter().filter(|r| r.is_success()).count();
    let failure_count = results.iter().filter(|r| r.is_failure()).count();

    assert_eq!(success_count, 6); // items 1,2,4,5,7,8
    assert_eq!(failure_count, 4); // items 0,3,6,9
    assert_eq!(success_count + failure_count, 10);
}

// Test JSON serialization/deserialization
#[test]
fn test_agent_result_serialization() {
    let result = AgentResult::success(
        "item-1".to_string(),
        Some("output".to_string()),
        Duration::from_secs(5),
    );

    let json = serde_json::to_string(&result).unwrap();
    let deserialized: AgentResult = serde_json::from_str(&json).unwrap();

    assert_eq!(result.item_id, deserialized.item_id);
    assert_eq!(result.output, deserialized.output);
    assert!(matches!(deserialized.status, AgentStatus::Success));
}

#[test]
fn test_aggregated_results_serialization() {
    let results = vec![
        AgentResult::success("item-1".to_string(), None, Duration::from_secs(1)),
        AgentResult::failed(
            "item-2".to_string(),
            "error".to_string(),
            Duration::from_secs(1),
        ),
    ];

    let aggregated = AggregatedResults::from_results(results);

    let json = serde_json::to_string(&aggregated).unwrap();
    let deserialized: AggregatedResults = serde_json::from_str(&json).unwrap();

    assert_eq!(aggregated.total, deserialized.total);
    assert_eq!(aggregated.success_count, deserialized.success_count);
    assert_eq!(aggregated.failure_count, deserialized.failure_count);
}

// Test edge cases
#[test]
fn test_agent_result_timeout_status() {
    let mut result = AgentResult::success("item-1".to_string(), None, Duration::from_secs(5));

    result.status = AgentStatus::Timeout;

    assert!(!result.is_success());
    assert!(result.is_failure()); // Timeout is considered a failure
    assert!(matches!(result.status, AgentStatus::Timeout));
}

#[test]
fn test_agent_result_retrying_status() {
    let mut result = AgentResult::success("item-1".to_string(), None, Duration::from_secs(5));

    result.status = AgentStatus::Retrying(2);

    assert!(!result.is_success());
    assert!(!result.is_failure());
    if let AgentStatus::Retrying(count) = result.status {
        assert_eq!(count, 2);
    } else {
        panic!("Expected Retrying status");
    }
}

// Performance tests
#[test]
fn test_large_aggregation_performance() {
    let start = std::time::Instant::now();

    let results: Vec<AgentResult> = (0..1000)
        .map(|i| create_test_agent_result(&format!("item-{}", i), i % 2 == 0))
        .collect();

    let aggregated = AggregatedResults::from_results(results);

    let elapsed = start.elapsed();

    assert_eq!(aggregated.total, 1000);
    assert_eq!(aggregated.success_count, 500);
    assert_eq!(aggregated.failure_count, 500);

    // Should complete in under 100ms
    assert!(elapsed.as_millis() < 100);
}

// Integration test for the complete flow
#[tokio::test]
async fn test_agent_workflow_integration() {
    let temp_dir = tempfile::tempdir().unwrap();
    let project_root = temp_dir.path().to_path_buf();

    // Initialize git repository in temp directory
    std::process::Command::new("git")
        .arg("init")
        .current_dir(&project_root)
        .output()
        .expect("Failed to initialize git repository");

    // Configure git user for the test
    std::process::Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(&project_root)
        .output()
        .expect("Failed to configure git email");

    std::process::Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(&project_root)
        .output()
        .expect("Failed to configure git name");

    // Create initial commit
    std::fs::write(project_root.join("README.md"), "Test").unwrap();
    std::process::Command::new("git")
        .args(["add", "."])
        .current_dir(&project_root)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(["commit", "-m", "Initial commit"])
        .current_dir(&project_root)
        .output()
        .unwrap();

    let subprocess = crate::subprocess::SubprocessManager::production();
    let worktree_manager = Arc::new(WorktreeManager::new(project_root, subprocess).unwrap());

    let lifecycle_manager = DefaultLifecycleManager::new(worktree_manager);
    let aggregator = DefaultResultAggregator::new();

    // Create multiple agent configs
    let configs: Vec<AgentConfig> = (0..3)
        .map(|i| {
            AgentConfig::new(
                format!("agent-{}", i),
                format!("item-{}", i),
                format!("branch-{}", i),
                3,
                Duration::from_secs(60),
                i,
                3,
            )
        })
        .collect();

    // Initialize agents
    let mut handles = vec![];
    for config in configs {
        let commands = vec![];
        let handle = lifecycle_manager
            .create_agent(config, commands)
            .await
            .unwrap();
        handles.push(handle);
    }

    // Simulate results
    let results: Vec<AgentResult> = handles
        .iter()
        .enumerate()
        .map(|(i, handle)| {
            if i % 2 == 0 {
                AgentResult::success(
                    handle.config.item_id.clone(),
                    Some(format!("Output from {}", handle.id())),
                    Duration::from_secs(i as u64 + 1),
                )
            } else {
                AgentResult::failed(
                    handle.config.item_id.clone(),
                    format!("Error from {}", handle.id()),
                    Duration::from_secs(i as u64 + 1),
                )
            }
        })
        .collect();

    // Aggregate results
    let aggregated = aggregator.aggregate(results.clone());
    assert_eq!(aggregated.total, 3);
    assert_eq!(aggregated.success_count, 2);
    assert_eq!(aggregated.failure_count, 1);

    // Convert to contexts
    let interp_context = aggregator.to_interpolation_context(&aggregated);
    assert!(interp_context.variables.contains_key("map.total"));

    let var_context = aggregator.to_variable_context(&aggregated).await;
    // Context should be created successfully
    assert!(std::mem::size_of_val(&var_context) > 0);

    // Cleanup
    for handle in handles {
        lifecycle_manager.cleanup_agent(handle).await.unwrap();
    }
}

// Test concurrent agent operations
#[tokio::test]
async fn test_concurrent_agent_operations() {
    use std::sync::Arc;
    use tokio::sync::Mutex;

    let results = Arc::new(Mutex::new(Vec::new()));

    let mut handles = vec![];
    for i in 0..10 {
        let results_clone = results.clone();
        let handle = tokio::spawn(async move {
            // Simulate some async work
            tokio::time::sleep(Duration::from_millis(10)).await;

            let result = if i % 3 == 0 {
                AgentResult::failed(
                    format!("item-{}", i),
                    "error".to_string(),
                    Duration::from_millis(10),
                )
            } else {
                AgentResult::success(
                    format!("item-{}", i),
                    Some(format!("output-{}", i)),
                    Duration::from_millis(10),
                )
            };

            let mut results = results_clone.lock().await;
            results.push(result);
        });
        handles.push(handle);
    }

    // Wait for all tasks
    for handle in handles {
        handle.await.unwrap();
    }

    let results = results.lock().await;
    assert_eq!(results.len(), 10);

    let success_count = results.iter().filter(|r| r.is_success()).count();
    let failure_count = results.iter().filter(|r| r.is_failure()).count();

    assert_eq!(success_count, 6); // items 1,2,4,5,7,8
    assert_eq!(failure_count, 4); // items 0,3,6,9
}