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
//! Comprehensive unit tests for ResumeExecutor::execute_from_checkpoint
//!
//! These tests verify:
//! 1. Happy path execution from checkpoint
//! 2. Error handling for missing executors
//! 3. Checkpoint validation
//! 4. Workflow file parsing (YAML/JSON)
//! 5. Context restoration
//! 6. Completed workflow handling

use anyhow::Result;
use async_trait::async_trait;
use prodigy::cook::execution::{ClaudeExecutor, ExecutionResult};
use prodigy::cook::interaction::{SpinnerHandle, UserInteraction, VerbosityLevel};
use prodigy::cook::session::state::SessionState;
use prodigy::cook::session::summary::SessionSummary;
use prodigy::cook::session::{SessionInfo, SessionManager, SessionUpdate};
use prodigy::cook::workflow::checkpoint::{
    CheckpointManager, CompletedStep, ExecutionState, ResumeOptions, WorkflowCheckpoint,
    WorkflowStatus, CHECKPOINT_VERSION,
};
use prodigy::cook::workflow::resume::ResumeExecutor;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tempfile::TempDir;

// ============================================================================
// Mock Implementations
// ============================================================================

struct TestClaudeExecutor {
    responses: Arc<Mutex<Vec<ExecutionResult>>>,
}

impl TestClaudeExecutor {
    fn new() -> Self {
        Self {
            responses: Arc::new(Mutex::new(Vec::new())),
        }
    }

    #[allow(dead_code)]
    fn add_response(&self, response: ExecutionResult) {
        self.responses.lock().unwrap().push(response);
    }
}

#[async_trait]
impl ClaudeExecutor for TestClaudeExecutor {
    async fn execute_claude_command(
        &self,
        _command: &str,
        _working_dir: &Path,
        _env_vars: HashMap<String, String>,
    ) -> Result<ExecutionResult> {
        self.responses
            .lock()
            .unwrap()
            .pop()
            .ok_or_else(|| anyhow::anyhow!("No mock response configured"))
    }

    async fn check_claude_cli(&self) -> Result<bool> {
        Ok(true)
    }

    async fn get_claude_version(&self) -> Result<String> {
        Ok("mock-1.0.0".to_string())
    }
}

struct TestSessionManager {
    updates: Arc<Mutex<Vec<SessionUpdate>>>,
}

impl TestSessionManager {
    fn new() -> Self {
        Self {
            updates: Arc::new(Mutex::new(Vec::new())),
        }
    }
}

#[async_trait]
impl SessionManager for TestSessionManager {
    async fn update_session(&self, update: SessionUpdate) -> Result<()> {
        self.updates.lock().unwrap().push(update);
        Ok(())
    }

    async fn start_session(&self, _session_id: &str) -> Result<()> {
        Ok(())
    }

    async fn complete_session(&self) -> Result<SessionSummary> {
        Ok(SessionSummary {
            iterations: 1,
            files_changed: 0,
        })
    }

    fn get_state(&self) -> Result<SessionState> {
        Ok(SessionState::new(
            "test-session".to_string(),
            PathBuf::from("/tmp"),
        ))
    }

    async fn save_state(&self, _path: &Path) -> Result<()> {
        Ok(())
    }

    async fn load_state(&self, _path: &Path) -> Result<()> {
        Ok(())
    }

    async fn load_session(&self, _session_id: &str) -> Result<SessionState> {
        Ok(SessionState::new(
            "test-session".to_string(),
            PathBuf::from("/tmp"),
        ))
    }

    async fn save_checkpoint(&self, _state: &SessionState) -> Result<()> {
        Ok(())
    }

    async fn list_resumable(&self) -> Result<Vec<SessionInfo>> {
        Ok(vec![])
    }

    async fn get_last_interrupted(&self) -> Result<Option<String>> {
        Ok(None)
    }
}

struct TestSpinnerHandle;

impl SpinnerHandle for TestSpinnerHandle {
    fn update_message(&mut self, _message: &str) {}
    fn success(&mut self, _message: &str) {}
    fn fail(&mut self, _message: &str) {}
}

struct TestUserInteraction;

#[async_trait]
impl UserInteraction for TestUserInteraction {
    async fn prompt_yes_no(&self, _message: &str) -> Result<bool> {
        Ok(true)
    }

    async fn prompt_text(&self, _message: &str, _default: Option<&str>) -> Result<String> {
        Ok(String::new())
    }

    fn display_info(&self, _message: &str) {}
    fn display_warning(&self, _message: &str) {}
    fn display_error(&self, _message: &str) {}
    fn display_progress(&self, _message: &str) {}
    fn start_spinner(&self, _message: &str) -> Box<dyn SpinnerHandle> {
        Box::new(TestSpinnerHandle)
    }
    fn display_success(&self, _message: &str) {}
    fn display_action(&self, _message: &str) {}
    fn display_metric(&self, _label: &str, _value: &str) {}
    fn display_status(&self, _message: &str) {}
    fn iteration_start(&self, _current: u32, _total: u32) {}
    fn iteration_end(&self, _current: u32, _duration: Duration, _success: bool) {}
    fn step_start(&self, _step: u32, _total: u32, _description: &str) {}
    fn step_end(&self, _step: u32, _success: bool) {}
    fn command_output(&self, _output: &str, _verbosity: VerbosityLevel) {}
    fn debug_output(&self, _message: &str, _min_verbosity: VerbosityLevel) {}
    fn verbosity(&self) -> VerbosityLevel {
        VerbosityLevel::Normal
    }
}

// ============================================================================
// Test Helpers
// ============================================================================

/// Compute SHA256 hash of workflow content
fn compute_workflow_hash(content: &str) -> String {
    use sha2::{Digest, Sha256};
    format!("{:x}", Sha256::digest(content.as_bytes()))
}

fn create_test_checkpoint(workflow_id: &str, workflow_path: PathBuf) -> WorkflowCheckpoint {
    // Compute hash from actual workflow content if file exists
    let workflow_hash = if workflow_path.exists() {
        std::fs::read_to_string(&workflow_path)
            .map(|content| compute_workflow_hash(&content))
            .unwrap_or_else(|_| "test-hash".to_string())
    } else {
        "test-hash".to_string()
    };

    WorkflowCheckpoint {
        workflow_id: workflow_id.to_string(),
        workflow_path: Some(workflow_path),
        execution_state: ExecutionState {
            current_step_index: 0,
            total_steps: 2,
            status: WorkflowStatus::Running,
            start_time: chrono::Utc::now(),
            last_checkpoint: chrono::Utc::now(),
            current_iteration: Some(1),
            total_iterations: Some(1),
        },
        completed_steps: vec![],
        variable_state: HashMap::new(),
        mapreduce_state: None,
        timestamp: chrono::Utc::now(),
        variable_checkpoint_state: None,
        version: CHECKPOINT_VERSION,
        workflow_hash,
        total_steps: 2,
        workflow_name: Some("test-workflow".to_string()),
        error_recovery_state: None,
        retry_checkpoint_state: None,
    }
}

async fn setup_test_environment() -> Result<(TempDir, PathBuf, PathBuf)> {
    let temp_dir = TempDir::new()?;
    let checkpoint_dir = temp_dir.path().join("checkpoints");
    let workflow_path = temp_dir.path().join("test.yml");

    tokio::fs::create_dir_all(&checkpoint_dir).await?;

    Ok((temp_dir, checkpoint_dir, workflow_path))
}

async fn create_yaml_workflow(path: &Path) -> Result<()> {
    let workflow_content = r#"
name: test-workflow
commands:
  - shell: echo "step1"
  - shell: echo "step2"
"#;
    tokio::fs::write(path, workflow_content).await?;
    Ok(())
}

async fn create_json_workflow(path: &Path) -> Result<()> {
    let workflow_content = r#"{
  "name": "test-workflow",
  "commands": [
    {"shell": "echo step1"},
    {"shell": "echo step2"}
  ]
}"#;
    tokio::fs::write(path, workflow_content).await?;
    Ok(())
}

// ============================================================================
// Phase 1 Tests: Foundation Testing - Critical Paths
// ============================================================================

#[tokio::test]
async fn test_execute_from_checkpoint_missing_claude_executor() -> Result<()> {
    let (_temp_dir, checkpoint_dir, workflow_path) = setup_test_environment().await?;
    create_yaml_workflow(&workflow_path).await?;

    let checkpoint = create_test_checkpoint("test-workflow", workflow_path.clone());

    #[allow(deprecated)]
    let checkpoint_manager = Arc::new(CheckpointManager::new(checkpoint_dir));
    checkpoint_manager.save_checkpoint(&checkpoint).await?;

    // Create executor WITHOUT setting executors
    let mut executor = ResumeExecutor::new(checkpoint_manager);

    let options = ResumeOptions {
        skip_validation: false,
        force: false,
        from_step: None,
        reset_failures: false,
    };

    let result = executor
        .execute_from_checkpoint("test-workflow", &workflow_path, options)
        .await;

    assert!(result.is_err());
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("Claude executor not configured"),
        "Expected error about missing Claude executor, got: {}",
        err_msg
    );

    Ok(())
}

// Note: We cannot test missing session_manager or user_interaction independently
// because with_executors() sets all three at once. The missing_claude_executor
// test above covers the case where no executors are set.

#[tokio::test]
async fn test_execute_from_checkpoint_already_completed() -> Result<()> {
    let (_temp_dir, checkpoint_dir, workflow_path) = setup_test_environment().await?;
    create_yaml_workflow(&workflow_path).await?;

    // Create checkpoint with completed status
    let mut checkpoint = create_test_checkpoint("test-workflow", workflow_path.clone());
    checkpoint.execution_state.status = WorkflowStatus::Completed;
    checkpoint.execution_state.current_step_index = 2;

    #[allow(deprecated)]
    let checkpoint_manager = Arc::new(CheckpointManager::new(checkpoint_dir));
    checkpoint_manager.save_checkpoint(&checkpoint).await?;

    let claude_executor = Arc::new(TestClaudeExecutor::new());
    let session_manager = Arc::new(TestSessionManager::new());
    let user_interaction = Arc::new(TestUserInteraction);

    let mut executor = ResumeExecutor::new(checkpoint_manager).with_executors(
        claude_executor,
        session_manager,
        user_interaction,
    );

    let options = ResumeOptions {
        skip_validation: false,
        force: false,
        from_step: None,
        reset_failures: false,
    };

    let result = executor
        .execute_from_checkpoint("test-workflow", &workflow_path, options)
        .await?;

    assert!(result.success);
    assert_eq!(result.new_steps_executed, 0);
    assert_eq!(result.total_steps_executed, 2);
    assert_eq!(result.skipped_steps, 2);

    Ok(())
}

#[tokio::test]
async fn test_execute_from_checkpoint_workflow_file_yaml_parsing() -> Result<()> {
    let (_temp_dir, checkpoint_dir, workflow_path) = setup_test_environment().await?;
    create_yaml_workflow(&workflow_path).await?;

    let checkpoint = create_test_checkpoint("test-workflow", workflow_path.clone());

    #[allow(deprecated)]
    let checkpoint_manager = Arc::new(CheckpointManager::new(checkpoint_dir));
    checkpoint_manager.save_checkpoint(&checkpoint).await?;

    let claude_executor = Arc::new(TestClaudeExecutor::new());
    let session_manager = Arc::new(TestSessionManager::new());
    let user_interaction = Arc::new(TestUserInteraction);

    // Add mock responses for shell commands
    claude_executor.add_response(ExecutionResult {
        success: true,
        stdout: "step2".to_string(),
        stderr: String::new(),
        exit_code: Some(0),
        metadata: HashMap::new(),
    });
    claude_executor.add_response(ExecutionResult {
        success: true,
        stdout: "step1".to_string(),
        stderr: String::new(),
        exit_code: Some(0),
        metadata: HashMap::new(),
    });

    let mut executor = ResumeExecutor::new(checkpoint_manager).with_executors(
        claude_executor,
        session_manager,
        user_interaction,
    );

    let options = ResumeOptions {
        skip_validation: false,
        force: false,
        from_step: None,
        reset_failures: false,
    };

    // This should parse the YAML file without error
    // The test passes if it doesn't panic during parsing
    let result = executor
        .execute_from_checkpoint("test-workflow", &workflow_path, options)
        .await;

    // We expect this to succeed or fail gracefully, not panic
    assert!(
        result.is_ok() || result.is_err(),
        "Execution should complete (success or controlled failure)"
    );

    Ok(())
}

#[tokio::test]
async fn test_execute_from_checkpoint_workflow_file_json_parsing() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let checkpoint_dir = temp_dir.path().join("checkpoints");
    let workflow_path = temp_dir.path().join("test.json");

    tokio::fs::create_dir_all(&checkpoint_dir).await?;
    create_json_workflow(&workflow_path).await?;

    let checkpoint = create_test_checkpoint("test-workflow", workflow_path.clone());

    #[allow(deprecated)]
    let checkpoint_manager = Arc::new(CheckpointManager::new(checkpoint_dir));
    checkpoint_manager.save_checkpoint(&checkpoint).await?;

    let claude_executor = Arc::new(TestClaudeExecutor::new());
    let session_manager = Arc::new(TestSessionManager::new());
    let user_interaction = Arc::new(TestUserInteraction);

    // Add mock responses
    claude_executor.add_response(ExecutionResult {
        success: true,
        stdout: "step2".to_string(),
        stderr: String::new(),
        exit_code: Some(0),
        metadata: HashMap::new(),
    });
    claude_executor.add_response(ExecutionResult {
        success: true,
        stdout: "step1".to_string(),
        stderr: String::new(),
        exit_code: Some(0),
        metadata: HashMap::new(),
    });

    let mut executor = ResumeExecutor::new(checkpoint_manager).with_executors(
        claude_executor,
        session_manager,
        user_interaction,
    );

    let options = ResumeOptions {
        skip_validation: false,
        force: false,
        from_step: None,
        reset_failures: false,
    };

    // This should parse the JSON file without error
    let result = executor
        .execute_from_checkpoint("test-workflow", &workflow_path, options)
        .await;

    assert!(
        result.is_ok() || result.is_err(),
        "Execution should complete (success or controlled failure)"
    );

    Ok(())
}

#[tokio::test]
async fn test_execute_from_checkpoint_invalid_workflow_format() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let checkpoint_dir = temp_dir.path().join("checkpoints");
    let workflow_path = temp_dir.path().join("test.txt");

    tokio::fs::create_dir_all(&checkpoint_dir).await?;
    tokio::fs::write(&workflow_path, "invalid format").await?;

    let checkpoint = create_test_checkpoint("test-workflow", workflow_path.clone());

    #[allow(deprecated)]
    let checkpoint_manager = Arc::new(CheckpointManager::new(checkpoint_dir));
    checkpoint_manager.save_checkpoint(&checkpoint).await?;

    let claude_executor = Arc::new(TestClaudeExecutor::new());
    let session_manager = Arc::new(TestSessionManager::new());
    let user_interaction = Arc::new(TestUserInteraction);

    let mut executor = ResumeExecutor::new(checkpoint_manager).with_executors(
        claude_executor,
        session_manager,
        user_interaction,
    );

    let options = ResumeOptions {
        skip_validation: false,
        force: false,
        from_step: None,
        reset_failures: false,
    };

    let result = executor
        .execute_from_checkpoint("test-workflow", &workflow_path, options)
        .await;

    assert!(result.is_err());
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("Unsupported workflow file format"),
        "Expected unsupported format error, got: {}",
        err_msg
    );

    Ok(())
}

#[tokio::test]
async fn test_execute_from_checkpoint_restore_context() -> Result<()> {
    let (_temp_dir, checkpoint_dir, workflow_path) = setup_test_environment().await?;
    create_yaml_workflow(&workflow_path).await?;

    // Create checkpoint with some variable state
    let mut checkpoint = create_test_checkpoint("test-workflow", workflow_path.clone());
    checkpoint
        .variable_state
        .insert("test_var".to_string(), serde_json::json!("test_value"));
    checkpoint.completed_steps.push(CompletedStep {
        step_index: 0,
        command: "shell: echo step1".to_string(),
        success: true,
        output: Some("step1".to_string()),
        captured_variables: HashMap::new(),
        duration: std::time::Duration::from_secs(1),
        completed_at: chrono::Utc::now(),
        retry_state: None,
    });
    checkpoint.execution_state.current_step_index = 1;

    #[allow(deprecated)]
    let checkpoint_manager = Arc::new(CheckpointManager::new(checkpoint_dir));
    checkpoint_manager.save_checkpoint(&checkpoint).await?;

    let claude_executor = Arc::new(TestClaudeExecutor::new());
    let session_manager = Arc::new(TestSessionManager::new());
    let user_interaction = Arc::new(TestUserInteraction);

    // Add mock response for remaining step
    claude_executor.add_response(ExecutionResult {
        success: true,
        stdout: "step2".to_string(),
        stderr: String::new(),
        exit_code: Some(0),
        metadata: HashMap::new(),
    });

    let mut executor = ResumeExecutor::new(checkpoint_manager).with_executors(
        claude_executor,
        session_manager,
        user_interaction,
    );

    let options = ResumeOptions {
        skip_validation: false,
        force: false,
        from_step: None,
        reset_failures: false,
    };

    let result = executor
        .execute_from_checkpoint("test-workflow", &workflow_path, options)
        .await;

    // Check that context was restored (test passes if execution proceeds)
    assert!(
        result.is_ok() || result.is_err(),
        "Execution should handle context restoration"
    );

    if let Ok(resume_result) = result {
        // Verify that we skipped the first step
        assert_eq!(resume_result.skipped_steps, 1);
    }

    Ok(())
}