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
//! Effect-based orchestration for composable I/O operations
//!
//! This module uses Stillwater's Effect pattern to separate pure business logic
//! from I/O operations, enabling testable workflow orchestration.
//!
//! # Architecture
//!
//! Following the spec 174e pattern, effects are composed in three stages:
//! 1. `setup_environment_effect` - Session creation and worktree setup
//! 2. `execute_plan_effect` - Execute the plan based on mode
//! 3. `finalize_session_effect` - Cleanup and session finalization
//!
//! # Example
//!
//! ```ignore
//! use prodigy::core::orchestration::plan_execution;
//! use prodigy::cook::orchestrator::effects::*;
//!
//! let plan = plan_execution(&config);
//!
//! let effect = setup_environment_effect(&plan)
//!     .and_then(|env| execute_plan_effect(&plan, env))
//!     .and_then(|result| finalize_session_effect(result));
//!
//! effect.run_async(&deps).await
//! ```

use crate::config::WorkflowConfig;
use crate::cook::orchestrator::environment::OrchestratorEnv;
use crate::cook::orchestrator::pure;
use crate::core::orchestration::{ExecutionMode, ExecutionPlan, Phase};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use stillwater::{fail, from_async, pure as stillwater_pure, BoxedEffect, EffectExt};

/// Effect type for orchestrator operations
pub type OrchEffect<T> = BoxedEffect<T, anyhow::Error, OrchestratorEnv>;

/// Workflow session state
#[derive(Debug, Clone)]
pub struct WorkflowSession {
    /// Session ID
    pub session_id: String,
    /// Worktree path (if using worktree)
    pub worktree_path: Option<PathBuf>,
    /// Workflow configuration
    pub config: WorkflowConfig,
}

/// Step execution context
#[derive(Debug, Clone)]
pub struct StepContext {
    /// Session ID
    pub session_id: String,
    /// Step index in workflow
    pub step_index: usize,
    /// Working directory
    pub working_dir: PathBuf,
    /// Previous step results
    pub previous_results: Vec<StepResult>,
}

/// Result from executing a workflow step
#[derive(Debug, Clone)]
pub struct StepResult {
    /// Step index
    pub step_index: usize,
    /// Whether step succeeded
    pub success: bool,
    /// Output from step
    pub output: String,
    /// Error message if failed
    pub error: Option<String>,
}

/// Complete workflow execution result
#[derive(Debug, Clone)]
pub struct WorkflowResult {
    /// Session ID
    pub session_id: String,
    /// Results from all steps
    pub step_results: Vec<StepResult>,
    /// Overall success
    pub success: bool,
}

/// Execution environment created during setup
#[derive(Debug, Clone)]
pub struct ExecutionEnvironment {
    /// Working directory (may be worktree)
    pub working_dir: Arc<PathBuf>,
    /// Original project directory
    pub project_dir: Arc<PathBuf>,
    /// Worktree name if using worktree
    pub worktree_name: Option<Arc<str>>,
    /// Session ID
    pub session_id: Arc<str>,
    /// Variables for interpolation
    pub variables: HashMap<String, String>,
}

/// Setup environment effect (I/O)
///
/// Creates session and worktree based on the execution plan.
///
/// # Arguments
///
/// * `plan` - The execution plan from pure planning
/// * `project_path` - Project root path
/// * `dry_run` - Whether this is a dry run
///
/// # Returns
///
/// An Effect that creates the execution environment
pub fn setup_environment_effect(
    plan: ExecutionPlan,
    project_path: Arc<PathBuf>,
    dry_run: bool,
) -> OrchEffect<ExecutionEnvironment> {
    from_async(move |env: &OrchestratorEnv| {
        let plan = plan.clone();
        let project_path = project_path.clone();
        let session_manager = env.session_manager.clone();

        async move {
            // Generate session ID
            let session_id = crate::unified_session::SessionId::new().to_string();
            let session_id_arc: Arc<str> = Arc::from(session_id.as_str());

            // Start session
            session_manager
                .start_session(&session_id)
                .await
                .map_err(|e| anyhow::anyhow!("Failed to start session: {}", e))?;

            // Create worktree if needed (based on pure plan)
            let (working_dir, worktree_name) = if plan.requires_worktrees() && !dry_run {
                // Worktree creation delegated to external manager
                // For now, we use project path - full implementation
                // would use WorktreeManager here
                (project_path.clone(), None)
            } else {
                (project_path.clone(), None)
            };

            Ok(ExecutionEnvironment {
                working_dir,
                project_dir: project_path,
                worktree_name,
                session_id: session_id_arc,
                variables: HashMap::new(),
            })
        }
    })
    .boxed()
}

/// Execute plan effect (I/O)
///
/// Executes the plan based on detected mode.
///
/// # Arguments
///
/// * `plan` - The execution plan
/// * `env` - The execution environment from setup
///
/// # Returns
///
/// An Effect that executes the plan
pub fn execute_plan_effect(
    plan: ExecutionPlan,
    exec_env: ExecutionEnvironment,
) -> OrchEffect<WorkflowResult> {
    from_async(move |_env: &OrchestratorEnv| {
        let plan = plan.clone();
        let exec_env = exec_env.clone();

        async move {
            // Dispatch based on execution mode (determined by pure planning)
            let step_results = match plan.mode {
                ExecutionMode::MapReduce => {
                    // MapReduce execution would be delegated to mapreduce executor
                    vec![]
                }
                ExecutionMode::Standard | ExecutionMode::Iterative => {
                    // Standard/Iterative execution would execute phases
                    execute_phases(&plan.phases, &exec_env).await?
                }
                ExecutionMode::DryRun => {
                    // Dry run - no actual execution
                    vec![StepResult {
                        step_index: 0,
                        success: true,
                        output: "Dry run completed".to_string(),
                        error: None,
                    }]
                }
            };

            let success = step_results.iter().all(|r| r.success);

            Ok(WorkflowResult {
                session_id: exec_env.session_id.to_string(),
                step_results,
                success,
            })
        }
    })
    .boxed()
}

/// Helper: Execute phases sequentially
async fn execute_phases(
    phases: &[Phase],
    _exec_env: &ExecutionEnvironment,
) -> anyhow::Result<Vec<StepResult>> {
    let mut results = Vec::new();

    for (idx, phase) in phases.iter().enumerate() {
        // Each phase would execute its commands
        // For now, return placeholder results
        let result = StepResult {
            step_index: idx,
            success: true,
            output: format!("Phase {} completed", phase),
            error: None,
        };
        results.push(result);
    }

    Ok(results)
}

/// Finalize session effect (I/O)
///
/// Cleans up session and worktree after execution.
///
/// # Arguments
///
/// * `result` - The execution result
/// * `exec_env` - The execution environment
///
/// # Returns
///
/// An Effect that finalizes the session
pub fn finalize_session_effect(
    result: WorkflowResult,
    exec_env: ExecutionEnvironment,
) -> OrchEffect<WorkflowResult> {
    from_async(move |env: &OrchestratorEnv| {
        let result = result.clone();
        let exec_env = exec_env.clone();
        let session_manager = env.session_manager.clone();

        async move {
            // Complete session
            let _ = session_manager.complete_session().await;

            // Cleanup worktree if present
            if let Some(_worktree) = exec_env.worktree_name {
                // Worktree cleanup would be delegated to WorktreeManager
                // Full implementation would prompt for merge or cleanup
            }

            Ok(result)
        }
    })
    .boxed()
}

/// Compose all effects into complete workflow execution
///
/// This is the main entry point for effect-based workflow execution.
/// It composes setup, execution, and finalization into a single effect.
///
/// # Arguments
///
/// * `plan` - Pure execution plan
/// * `project_path` - Project root path
/// * `dry_run` - Whether this is a dry run
///
/// # Returns
///
/// A composed effect that executes the complete workflow
pub fn run_workflow_effect(
    plan: ExecutionPlan,
    project_path: Arc<PathBuf>,
    dry_run: bool,
) -> OrchEffect<WorkflowResult> {
    let plan_for_exec = plan.clone();
    let _plan_for_finalize = plan.clone(); // Reserved for future phase integration

    setup_environment_effect(plan, project_path, dry_run)
        .and_then_auto(move |exec_env| {
            let env_for_finalize = exec_env.clone();
            execute_plan_effect(plan_for_exec.clone(), exec_env)
                .and_then_auto(move |result| finalize_session_effect(result, env_for_finalize))
        })
        .boxed()
}

/// Validate workflow configuration (Effect wrapping pure validation)
///
/// This demonstrates how to lift pure validations into Effects.
pub fn validate_workflow(config: WorkflowConfig) -> OrchEffect<WorkflowConfig> {
    use stillwater::Validation;

    match pure::validate_workflow(&config) {
        Validation::Success(_) => stillwater_pure(config).boxed(),
        Validation::Failure(errors) => {
            let error_msg = errors
                .iter()
                .map(|e| e.to_string())
                .collect::<Vec<_>>()
                .join(", ");
            fail(anyhow::anyhow!("Workflow validation failed: {}", error_msg)).boxed()
        }
    }
}

/// Setup workflow environment (Effect)
///
/// Creates session and prepares execution environment.
/// This is a simplified implementation demonstrating Effect composition.
pub fn setup_workflow(config: WorkflowConfig) -> OrchEffect<WorkflowSession> {
    // First validate
    validate_workflow(config.clone())
        .and_then_auto(|validated_config| {
            // Then setup session
            from_async(move |env: &OrchestratorEnv| {
                let session_manager = env.session_manager.clone();
                async move {
                    // Generate session ID
                    let session_id = uuid::Uuid::new_v4().to_string();

                    // Start session
                    session_manager
                        .start_session(&session_id)
                        .await
                        .map_err(|e| anyhow::anyhow!("Failed to start session: {}", e))?;

                    // For now, we don't create worktree here - that's handled elsewhere
                    // This is a simplified implementation focusing on the Effect pattern

                    Ok::<WorkflowSession, anyhow::Error>(WorkflowSession {
                        session_id,
                        worktree_path: None,
                        config: validated_config,
                    })
                }
            })
        })
        .boxed()
}

/// Execute complete workflow (placeholder)
///
/// This demonstrates the Effect composition pattern.
/// Full workflow execution will be integrated incrementally.
pub fn execute_workflow(config: WorkflowConfig) -> OrchEffect<WorkflowResult> {
    setup_workflow(config)
        .and_then(|session| {
            let result = WorkflowResult {
                session_id: session.session_id,
                step_results: vec![],
                success: true,
            };
            stillwater_pure::<_, anyhow::Error, OrchestratorEnv>(result).boxed()
        })
        .boxed()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::command::WorkflowCommand;

    // Test fixtures
    fn simple_workflow_config() -> WorkflowConfig {
        WorkflowConfig {
            name: Some("test".to_string()),
            commands: vec![WorkflowCommand::Simple("echo test".to_string())],
            env: None,
            secrets: None,
            env_files: None,
            profiles: None,
            merge: None,
        }
    }

    fn empty_workflow_config() -> WorkflowConfig {
        WorkflowConfig {
            name: Some("empty".to_string()),
            commands: vec![],
            env: None,
            secrets: None,
            env_files: None,
            profiles: None,
            merge: None,
        }
    }

    #[test]
    fn test_workflow_session_creation() {
        let config = simple_workflow_config();
        let session = WorkflowSession {
            session_id: "test-123".to_string(),
            worktree_path: Some(PathBuf::from("/tmp/test")),
            config,
        };

        assert_eq!(session.session_id, "test-123");
        assert!(session.worktree_path.is_some());
    }

    #[test]
    fn test_workflow_session_without_worktree() {
        let config = simple_workflow_config();
        let session = WorkflowSession {
            session_id: "test-456".to_string(),
            worktree_path: None,
            config,
        };

        assert_eq!(session.session_id, "test-456");
        assert!(session.worktree_path.is_none());
    }

    #[test]
    fn test_step_context_creation() {
        let context = StepContext {
            session_id: "test-123".to_string(),
            step_index: 0,
            working_dir: PathBuf::from("/tmp"),
            previous_results: vec![],
        };

        assert_eq!(context.session_id, "test-123");
        assert_eq!(context.step_index, 0);
        assert!(context.previous_results.is_empty());
    }

    #[test]
    fn test_step_context_with_previous_results() {
        let prev_result = StepResult {
            step_index: 0,
            success: true,
            output: "previous output".to_string(),
            error: None,
        };

        let context = StepContext {
            session_id: "test-123".to_string(),
            step_index: 1,
            working_dir: PathBuf::from("/tmp"),
            previous_results: vec![prev_result],
        };

        assert_eq!(context.step_index, 1);
        assert_eq!(context.previous_results.len(), 1);
    }

    #[test]
    fn test_step_result_success() {
        let result = StepResult {
            step_index: 0,
            success: true,
            output: "test output".to_string(),
            error: None,
        };

        assert!(result.success);
        assert!(result.error.is_none());
        assert_eq!(result.output, "test output");
    }

    #[test]
    fn test_step_result_failure() {
        let result = StepResult {
            step_index: 0,
            success: false,
            output: String::new(),
            error: Some("test error".to_string()),
        };

        assert!(!result.success);
        assert!(result.error.is_some());
        assert_eq!(result.error.unwrap(), "test error");
    }

    #[test]
    fn test_workflow_result_success() {
        let result = WorkflowResult {
            session_id: "test-789".to_string(),
            step_results: vec![StepResult {
                step_index: 0,
                success: true,
                output: "done".to_string(),
                error: None,
            }],
            success: true,
        };

        assert!(result.success);
        assert_eq!(result.step_results.len(), 1);
    }

    #[test]
    fn test_workflow_result_failure() {
        let result = WorkflowResult {
            session_id: "test-999".to_string(),
            step_results: vec![
                StepResult {
                    step_index: 0,
                    success: true,
                    output: "step 1 ok".to_string(),
                    error: None,
                },
                StepResult {
                    step_index: 1,
                    success: false,
                    output: "".to_string(),
                    error: Some("step 2 failed".to_string()),
                },
            ],
            success: false,
        };

        assert!(!result.success);
        assert_eq!(result.step_results.len(), 2);
        assert!(!result.step_results[1].success);
    }

    #[test]
    fn test_validate_workflow_pure_success() {
        let config = simple_workflow_config();
        let result = pure::validate_workflow(&config);
        use stillwater::Validation;
        assert!(matches!(result, Validation::Success(_)));
    }

    #[test]
    fn test_validate_workflow_pure_failure() {
        let config = empty_workflow_config();
        let result = pure::validate_workflow(&config);
        use stillwater::Validation;
        assert!(matches!(result, Validation::Failure(_)));
    }

    #[test]
    fn test_validate_workflow_effect_pure_transform() {
        let config = simple_workflow_config();
        // The validate_workflow effect wraps pure validation
        // We test the pure part here; Effect execution requires runtime
        let pure_result = pure::validate_workflow(&config);
        use stillwater::Validation;
        assert!(matches!(pure_result, Validation::Success(_)));
    }

    // NOTE: Integration tests with full mock environment deferred
    // Current tests focus on pure logic and data structures
    // Full Effect execution tests require complete mock implementations
    // of all traits (SessionManager, CommandExecutor, ClaudeExecutor, etc.)
    //
    // For now, Effect-based code is tested through:
    // 1. Pure function tests (above)
    // 2. Data structure tests (above)
    // 3. Integration with actual orchestrator in production code
}