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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Tests for workflow checkpoint and resume functionality

#[cfg(test)]
mod tests {
    use super::super::checkpoint::*;
    use crate::cook::workflow::executor::WorkflowContext;
    use crate::cook::workflow::normalized::{NormalizedStep, NormalizedWorkflow, StepCommand};
    use std::collections::HashMap;
    use std::sync::Arc;
    use tempfile::TempDir;

    /// Create a test checkpoint manager with temp directory
    #[allow(deprecated)]
    fn create_test_checkpoint_manager() -> (CheckpointManager, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
        (manager, temp_dir)
    }

    /// Create a sample normalized workflow for testing
    fn create_test_workflow() -> NormalizedWorkflow {
        use std::time::Duration;

        NormalizedWorkflow {
            name: Arc::from("test-workflow"),
            steps: Arc::from(vec![
                NormalizedStep {
                    id: Arc::from("step-1"),
                    command: StepCommand::Shell(Arc::from("echo 'Step 1'")),
                    validation: None,
                    handlers: Default::default(),
                    timeout: Some(Duration::from_secs(30)),
                    working_dir: None,
                    env: Arc::new(HashMap::new()),
                    outputs: None,
                    commit_required: false,
                    when: None,
                },
                NormalizedStep {
                    id: Arc::from("step-2"),
                    command: StepCommand::Shell(Arc::from("echo 'Step 2'")),
                    validation: None,
                    handlers: Default::default(),
                    timeout: Some(Duration::from_secs(30)),
                    working_dir: None,
                    env: Arc::new(HashMap::new()),
                    outputs: None,
                    commit_required: false,
                    when: None,
                },
                NormalizedStep {
                    id: Arc::from("step-3"),
                    command: StepCommand::Shell(Arc::from("echo 'Step 3'")),
                    validation: None,
                    handlers: Default::default(),
                    timeout: Some(Duration::from_secs(30)),
                    working_dir: None,
                    env: Arc::new(HashMap::new()),
                    outputs: None,
                    commit_required: false,
                    when: None,
                },
            ]),
            execution_mode: crate::cook::workflow::normalized::ExecutionMode::Sequential,
            variables: Arc::new(HashMap::new()),
        }
    }

    #[tokio::test]
    async fn test_checkpoint_save_and_load() {
        let (manager, _temp_dir) = create_test_checkpoint_manager();
        let workflow = create_test_workflow();
        let mut context = WorkflowContext::default();
        context
            .variables
            .insert("test_var".to_string(), "test_value".to_string());

        // Create a checkpoint
        let checkpoint = create_checkpoint(
            "test-workflow-123".to_string(),
            &workflow,
            &context,
            vec![CompletedStep {
                step_index: 0,
                command: "echo 'Step 1'".to_string(),
                success: true,
                output: Some("Step 1".to_string()),
                captured_variables: HashMap::new(),
                duration: std::time::Duration::from_secs(1),
                completed_at: chrono::Utc::now(),
                retry_state: None,
            }],
            1, // Current step
            "workflow_hash_123".to_string(),
        );

        // Save checkpoint
        manager.save_checkpoint(&checkpoint).await.unwrap();

        // Load checkpoint
        let loaded = manager.load_checkpoint("test-workflow-123").await.unwrap();

        // Verify checkpoint
        assert_eq!(loaded.workflow_id, "test-workflow-123");
        assert_eq!(loaded.execution_state.current_step_index, 1);
        assert_eq!(loaded.completed_steps.len(), 1);
        assert_eq!(loaded.workflow_hash, "workflow_hash_123");
    }

    #[tokio::test]
    async fn test_checkpoint_list() {
        let (manager, _temp_dir) = create_test_checkpoint_manager();
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();

        // Create and save multiple checkpoints
        for i in 1..=3 {
            let checkpoint = create_checkpoint(
                format!("workflow-{}", i),
                &workflow,
                &context,
                vec![],
                0,
                "hash".to_string(),
            );
            manager.save_checkpoint(&checkpoint).await.unwrap();
        }

        // List checkpoints
        let mut checkpoints = manager.list_checkpoints().await.unwrap();
        checkpoints.sort();

        assert_eq!(checkpoints.len(), 3);
        assert_eq!(checkpoints[0], "workflow-1");
        assert_eq!(checkpoints[1], "workflow-2");
        assert_eq!(checkpoints[2], "workflow-3");
    }

    #[tokio::test]
    async fn test_checkpoint_delete() {
        let (manager, _temp_dir) = create_test_checkpoint_manager();
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();

        // Create and save checkpoint
        let checkpoint = create_checkpoint(
            "workflow-to-delete".to_string(),
            &workflow,
            &context,
            vec![],
            0,
            "hash".to_string(),
        );
        manager.save_checkpoint(&checkpoint).await.unwrap();

        // Verify it exists
        assert!(manager.load_checkpoint("workflow-to-delete").await.is_ok());

        // Delete checkpoint
        manager
            .delete_checkpoint("workflow-to-delete")
            .await
            .unwrap();

        // Verify it's gone
        assert!(manager.load_checkpoint("workflow-to-delete").await.is_err());
    }

    #[tokio::test]
    async fn test_checkpoint_validation() {
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();

        // Create checkpoint with valid state
        let checkpoint = create_checkpoint(
            "test-workflow".to_string(),
            &workflow,
            &context,
            vec![],
            0,
            "original_hash".to_string(),
        );

        // Validate with matching hash
        assert!(
            CheckpointManager::validate_checkpoint(&checkpoint, "original_hash", None, 3).is_ok()
        );

        // Validate with different hash (should fail without workflow path)
        assert!(
            CheckpointManager::validate_checkpoint(&checkpoint, "different_hash", None, 3).is_ok()
        );

        // Create invalid checkpoint (step index out of bounds)
        let mut invalid_checkpoint = checkpoint.clone();
        invalid_checkpoint.execution_state.current_step_index = 100;
        invalid_checkpoint.execution_state.total_steps = 3;

        // Should fail validation
        assert!(CheckpointManager::validate_checkpoint(
            &invalid_checkpoint,
            "original_hash",
            None,
            3
        )
        .is_err());
    }

    #[tokio::test]
    async fn test_build_resume_context() {
        let workflow = create_test_workflow();
        let mut context = WorkflowContext::default();
        context
            .variables
            .insert("var1".to_string(), "value1".to_string());

        let completed_steps = vec![CompletedStep {
            step_index: 0,
            command: "echo 'Step 1'".to_string(),
            success: true,
            output: Some("Step 1 output".to_string()),
            captured_variables: HashMap::from([(
                "step1_var".to_string(),
                "step1_value".to_string(),
            )]),
            duration: std::time::Duration::from_secs(1),
            completed_at: chrono::Utc::now(),
            retry_state: None,
        }];

        let checkpoint = create_checkpoint(
            "test-workflow".to_string(),
            &workflow,
            &context,
            completed_steps.clone(),
            1,
            "hash".to_string(),
        );

        let resume_context = build_resume_context(checkpoint);

        assert_eq!(resume_context.skip_steps.len(), 1);
        assert_eq!(resume_context.start_from_step, 1);
        assert!(resume_context.variable_state.contains_key("var1"));
        assert_eq!(
            resume_context.variable_state.get("var1").unwrap(),
            &serde_json::Value::String("value1".to_string())
        );
    }

    #[tokio::test]
    #[allow(deprecated)]
    async fn test_checkpoint_auto_interval() {
        let (mut manager, _temp_dir) = create_test_checkpoint_manager();

        // Configure with 1 second interval for testing (to avoid timing issues)
        manager.configure(std::time::Duration::from_secs(1), true);

        // Create fixed timestamps relative to a baseline
        let baseline = chrono::Utc::now();

        // Check with last checkpoint 10 seconds ago (should checkpoint)
        let old_checkpoint = baseline - chrono::Duration::seconds(10);
        assert!(manager.should_checkpoint(old_checkpoint).await);

        // Check with last checkpoint 500ms ago (should not checkpoint yet with 1s interval)
        let recent_checkpoint = baseline - chrono::Duration::milliseconds(500);
        assert!(!manager.should_checkpoint(recent_checkpoint).await);

        // Check with last checkpoint 2 seconds ago (should checkpoint since interval is 1s)
        let old_enough_checkpoint = baseline - chrono::Duration::seconds(2);
        assert!(manager.should_checkpoint(old_enough_checkpoint).await);
    }

    #[test]
    fn test_workflow_status_equality() {
        assert_eq!(WorkflowStatus::Running, WorkflowStatus::Running);
        assert_ne!(WorkflowStatus::Running, WorkflowStatus::Completed);
        assert_ne!(WorkflowStatus::Failed, WorkflowStatus::Interrupted);
    }

    #[tokio::test]
    async fn test_checkpoint_workflow_path_persistence() {
        let (manager, _temp_dir) = create_test_checkpoint_manager();
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();

        // Create a checkpoint with workflow path
        let mut checkpoint = create_checkpoint(
            "test-workflow-path".to_string(),
            &workflow,
            &context,
            vec![],
            0,
            "workflow_hash".to_string(),
        );

        // Set the workflow path
        let test_path = std::path::PathBuf::from("/test/workflows/implement.yml");
        checkpoint.workflow_path = Some(test_path.clone());

        // Save checkpoint
        manager.save_checkpoint(&checkpoint).await.unwrap();

        // Load checkpoint
        let loaded = manager.load_checkpoint("test-workflow-path").await.unwrap();

        // Verify workflow path was persisted
        assert_eq!(loaded.workflow_path, Some(test_path));
    }

    #[test]
    fn test_create_error_checkpoint_includes_context() {
        // Setup test data
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();
        let error = anyhow::anyhow!("Test error: commit required but no commits were created");
        let failed_step_index = 5;

        // Create error checkpoint
        let checkpoint = create_error_checkpoint(
            "test-workflow".to_string(),
            &workflow,
            &context,
            vec![],
            "hash123".to_string(),
            &error,
            failed_step_index,
        )
        .expect("Failed to create error checkpoint");

        // Verify status is Failed
        assert_eq!(checkpoint.execution_state.status, WorkflowStatus::Failed);

        // Verify error message is captured
        let error_msg = checkpoint
            .variable_state
            .get("__error_message")
            .expect("Missing __error_message");
        assert!(
            error_msg.as_str().unwrap().contains("commit required"),
            "Error message should contain 'commit required', got: {}",
            error_msg
        );

        // Verify failed step index is captured
        let failed_step = checkpoint
            .variable_state
            .get("__failed_step_index")
            .expect("Missing __failed_step_index");
        assert_eq!(
            failed_step.as_u64().unwrap(),
            failed_step_index as u64,
            "Failed step index should be {}",
            failed_step_index
        );

        // Verify timestamp is present
        let timestamp = checkpoint
            .variable_state
            .get("__error_timestamp")
            .expect("Missing __error_timestamp");
        assert!(
            timestamp.is_string(),
            "Timestamp should be a string ISO 8601 format"
        );
    }

    #[test]
    fn test_create_completion_checkpoint_status() {
        // Setup test data
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();
        let current_step_index = 3;

        // Create completion checkpoint
        let checkpoint = create_completion_checkpoint(
            "test-workflow".to_string(),
            &workflow,
            &context,
            vec![],
            current_step_index,
            "hash123".to_string(),
        )
        .expect("Failed to create completion checkpoint");

        // Verify status is Completed
        assert_eq!(checkpoint.execution_state.status, WorkflowStatus::Completed);

        // Verify no error context variables
        assert!(
            !checkpoint.variable_state.contains_key("__error_message"),
            "Completion checkpoint should not have error message"
        );
        assert!(
            !checkpoint
                .variable_state
                .contains_key("__failed_step_index"),
            "Completion checkpoint should not have failed step index"
        );
    }

    #[test]
    fn test_error_checkpoint_preserves_workflow_context() {
        // Setup test data with variables
        let workflow = create_test_workflow();
        let mut context = WorkflowContext::default();
        context
            .variables
            .insert("user_var".to_string(), "user_value".to_string());
        context
            .captured_outputs
            .insert("output_var".to_string(), "output_value".to_string());

        let error = anyhow::anyhow!("Test error");

        // Create error checkpoint
        let checkpoint = create_error_checkpoint(
            "test-workflow".to_string(),
            &workflow,
            &context,
            vec![],
            "hash123".to_string(),
            &error,
            2,
        )
        .expect("Failed to create error checkpoint");

        // Verify user variables are preserved
        assert!(
            checkpoint.variable_state.contains_key("user_var"),
            "User variables should be preserved in error checkpoint"
        );
        assert!(
            checkpoint.variable_state.contains_key("output_var"),
            "Captured outputs should be preserved in error checkpoint"
        );

        // Verify error context variables are added
        assert!(
            checkpoint.variable_state.contains_key("__error_message"),
            "Error message should be added"
        );
        assert!(
            checkpoint
                .variable_state
                .contains_key("__failed_step_index"),
            "Failed step index should be added"
        );
    }

    // ============================================================================
    // Tests for builder pattern and immutability (Spec 124)
    // ============================================================================

    #[tokio::test]
    async fn test_checkpoint_manager_builder_pattern() {
        use crate::cook::workflow::checkpoint_path::CheckpointStorage;
        use std::time::Duration;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let storage = CheckpointStorage::Local(temp_dir.path().to_path_buf());

        // Test builder pattern chaining
        let manager = CheckpointManager::with_storage(storage)
            .with_interval(Duration::from_secs(30))
            .with_enabled(true);

        // Verify manager works correctly by testing behavior
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();
        let checkpoint = create_checkpoint(
            "test-builder".to_string(),
            &workflow,
            &context,
            vec![],
            0,
            "hash".to_string(),
        );

        // Should successfully save checkpoint (verifies enabled=true)
        manager.save_checkpoint(&checkpoint).await.unwrap();
        let checkpoint_path = temp_dir.path().join("test-builder.checkpoint.json");
        assert!(
            checkpoint_path.exists(),
            "Checkpoint should be saved when enabled"
        );
    }

    #[test]
    fn test_builder_pattern_with_disabled_checkpointing() {
        use crate::cook::workflow::checkpoint_path::CheckpointStorage;
        use std::time::Duration;

        let storage = CheckpointStorage::Local(std::path::PathBuf::from("/tmp"));

        // Test that builder pattern allows chaining
        let _manager = CheckpointManager::with_storage(storage)
            .with_interval(Duration::from_secs(60))
            .with_enabled(false);

        // Manager is created successfully (behavior verified in other tests)
    }

    #[tokio::test]
    async fn test_builder_pattern_default_values() {
        use crate::cook::workflow::checkpoint_path::CheckpointStorage;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let storage = CheckpointStorage::Local(temp_dir.path().to_path_buf());

        // Create manager with only storage (should use defaults)
        let manager = CheckpointManager::with_storage(storage);

        // Verify defaults by testing behavior
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();
        let checkpoint = create_checkpoint(
            "test-defaults".to_string(),
            &workflow,
            &context,
            vec![],
            0,
            "hash".to_string(),
        );

        // Should save checkpoint (verifies enabled=true by default)
        manager.save_checkpoint(&checkpoint).await.unwrap();
        let checkpoint_path = temp_dir.path().join("test-defaults.checkpoint.json");
        assert!(checkpoint_path.exists(), "Default should be enabled");
    }

    #[tokio::test]
    async fn test_checkpoint_save_respects_enabled_flag() {
        use crate::cook::workflow::checkpoint_path::CheckpointStorage;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let storage = CheckpointStorage::Local(temp_dir.path().to_path_buf());

        // Create manager with checkpointing disabled
        let manager = CheckpointManager::with_storage(storage).with_enabled(false);

        let workflow = create_test_workflow();
        let context = WorkflowContext::default();
        let checkpoint = create_checkpoint(
            "test-disabled".to_string(),
            &workflow,
            &context,
            vec![],
            0,
            "hash".to_string(),
        );

        // Save should succeed but not actually write file
        manager.save_checkpoint(&checkpoint).await.unwrap();

        // Verify file was not created
        let checkpoint_path = temp_dir.path().join("test-disabled.checkpoint.json");
        assert!(
            !checkpoint_path.exists(),
            "Checkpoint file should not exist when disabled"
        );
    }

    #[tokio::test]
    #[allow(deprecated)]
    async fn test_deprecated_api_backwards_compatibility() {
        use std::time::Duration;
        use tempfile::TempDir;

        // Test that deprecated API still works
        let temp_dir = TempDir::new().unwrap();
        let mut manager = CheckpointManager::new(temp_dir.path().to_path_buf());
        manager.configure(Duration::from_secs(30), true);

        // Verify old API works by testing behavior
        let workflow = create_test_workflow();
        let context = WorkflowContext::default();
        let checkpoint = create_checkpoint(
            "test-deprecated".to_string(),
            &workflow,
            &context,
            vec![],
            0,
            "hash".to_string(),
        );

        // Should save checkpoint (verifies configure worked)
        manager.save_checkpoint(&checkpoint).await.unwrap();
        let checkpoint_path = temp_dir.path().join("test-deprecated.checkpoint.json");
        assert!(checkpoint_path.exists(), "Deprecated API should still work");
    }

    #[tokio::test]
    async fn test_builder_pattern_interval_configuration() {
        use crate::cook::workflow::checkpoint_path::CheckpointStorage;
        use std::time::Duration;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let storage = CheckpointStorage::Local(temp_dir.path().to_path_buf());

        let custom_interval = Duration::from_secs(120);
        let manager = CheckpointManager::with_storage(storage).with_interval(custom_interval);

        // Verify interval configuration by testing should_checkpoint behavior
        let baseline = chrono::Utc::now();

        // With 120s interval, should not checkpoint after 60s
        let recent = baseline - chrono::Duration::seconds(60);
        assert!(!manager.should_checkpoint(recent).await);

        // With 120s interval, should checkpoint after 130s
        let old = baseline - chrono::Duration::seconds(130);
        assert!(manager.should_checkpoint(old).await);
    }
}