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
//! Reduce phase executor for MapReduce workflows
//!
//! This module handles the execution of reduce commands that aggregate
//! and process results from the map phase. It extracts the complex
//! reduce logic from the main module into a focused orchestrator.

use super::{PhaseContext, PhaseError, PhaseExecutor, PhaseMetrics, PhaseResult, PhaseType};
use crate::cook::execution::mapreduce::{
    utils::{build_agent_context_variables, calculate_map_result_summary},
    AgentResult, ReducePhase,
};
use crate::cook::workflow::variables::CapturedValue;
use crate::cook::workflow::WorkflowStep;
use async_trait::async_trait;
use serde_json::json;
use std::collections::HashMap;
use std::time::Instant;
use tracing::{debug, info, warn};

/// Executor for the reduce phase of MapReduce workflows
pub struct ReducePhaseExecutor {
    /// The reduce phase configuration
    reduce_phase: ReducePhase,
}

impl ReducePhaseExecutor {
    /// Create a new reduce phase executor
    pub fn new(reduce_phase: ReducePhase) -> Self {
        Self { reduce_phase }
    }

    /// Prepare the reduce context with map results
    async fn prepare_reduce_context(
        &self,
        map_results: &[AgentResult],
        context: &mut PhaseContext,
    ) -> Result<(), PhaseError> {
        // Calculate summary statistics using pure functions
        let summary_stats = calculate_map_result_summary(map_results);

        // Build and add variables using pure functions
        let context_variables = build_agent_context_variables(map_results, &summary_stats)
            .map_err(|e| PhaseError::ExecutionFailed {
                message: format!("Failed to build agent context variables: {}", e),
            })?;

        // Transfer variables to reduce context
        for (key, value) in context_variables {
            context.variables.insert(key, value);
        }

        // Add map results to variable store as structured data
        self.add_map_results_to_store(map_results, &summary_stats, context)
            .await;

        Ok(())
    }

    /// Add map results to the variable store for access in reduce commands
    async fn add_map_results_to_store(
        &self,
        map_results: &[AgentResult],
        summary_stats: &crate::cook::execution::mapreduce::utils::MapResultSummary,
        context: &mut PhaseContext,
    ) {
        // Add summary statistics
        context
            .variable_store
            .set(
                "map.successful",
                CapturedValue::Number(summary_stats.successful as f64),
            )
            .await;
        context
            .variable_store
            .set(
                "map.failed",
                CapturedValue::Number(summary_stats.failed as f64),
            )
            .await;
        context
            .variable_store
            .set(
                "map.total",
                CapturedValue::Number(summary_stats.total as f64),
            )
            .await;

        // Add the full results as a structured JSON value
        if let Ok(results_value) = serde_json::to_value(map_results) {
            context
                .variable_store
                .set("map.results", CapturedValue::from(results_value))
                .await;
        }

        // Also add individual results for easier access
        let results_array: Vec<CapturedValue> = map_results
            .iter()
            .map(|result| {
                if let Ok(result_json) = serde_json::to_value(result) {
                    CapturedValue::from(result_json)
                } else {
                    CapturedValue::String(format!("{:?}", result))
                }
            })
            .collect();
        context
            .variable_store
            .set("map.results_array", CapturedValue::Array(results_array))
            .await;
    }

    /// Execute reduce commands sequentially
    async fn execute_reduce_commands(&self, context: &mut PhaseContext) -> Result<(), PhaseError> {
        for (step_index, step) in self.reduce_phase.commands.iter().enumerate() {
            debug!(
                "Executing reduce step {}/{}",
                step_index + 1,
                self.reduce_phase.commands.len()
            );

            // Execute the step
            let step_result = self.execute_single_step(step, context).await.map_err(|e| {
                PhaseError::ExecutionFailed {
                    message: format!("Reduce step {} failed: {}", step_index + 1, e),
                }
            })?;

            if !step_result.success {
                // Handle step failure with on_failure handler if present
                if let Some(on_failure) = &step.on_failure {
                    info!(
                        "Step {} failed, executing on_failure handler",
                        step_index + 1
                    );

                    // Store error context for handler
                    context.variables.insert(
                        "error.message".to_string(),
                        step_result
                            .error
                            .unwrap_or_else(|| "Unknown error".to_string()),
                    );
                    context.variables.insert(
                        "error.step".to_string(),
                        format!("reduce_step_{}", step_index + 1),
                    );

                    // Execute the on_failure handler
                    let handler_result = self.handle_on_failure(on_failure, step, context).await;

                    match handler_result {
                        Ok(handled) if handled => {
                            // Handler succeeded, continue to next step
                            info!("on_failure handler succeeded, continuing");
                        }
                        Ok(_) => {
                            // Handler says we should fail
                            return Err(PhaseError::ExecutionFailed {
                                message: format!(
                                    "Reduce step {} failed and fail_workflow is true",
                                    step_index + 1
                                ),
                            });
                        }
                        Err(e) => {
                            // Handler itself failed
                            if on_failure.should_fail_workflow() {
                                return Err(PhaseError::ExecutionFailed {
                                    message: format!(
                                        "Reduce step {} on_failure handler failed: {}",
                                        step_index + 1,
                                        e
                                    ),
                                });
                            }
                            // Log but continue
                            warn!("on_failure handler failed but continuing: {}", e);
                        }
                    }
                } else {
                    // No on_failure handler, fail immediately
                    return Err(PhaseError::ExecutionFailed {
                        message: format!(
                            "Reduce step {} failed: {}",
                            step_index + 1,
                            step_result
                                .error
                                .unwrap_or_else(|| "Unknown error".to_string())
                        ),
                    });
                }
            } else if let Some(on_success) = &step.on_success {
                // Execute on_success handler for successful step
                info!(
                    "Step {} succeeded, executing on_success handler",
                    step_index + 1
                );

                // Store success context
                context.variables.insert(
                    "shell.output".to_string(),
                    step_result.output.clone().unwrap_or_default(),
                );

                // Execute the on_success handler
                let success_result = self.execute_single_step(on_success, context).await;

                if let Err(e) = success_result {
                    warn!(
                        "on_success handler failed for step {}: {}",
                        step_index + 1,
                        e
                    );
                    // Note: We don't fail the workflow when on_success handler fails
                }
            }

            // Make captured outputs available for subsequent commands
            if let Some(output) = step_result.output {
                context.variables.insert("shell.output".to_string(), output);
            }
        }

        Ok(())
    }

    /// Execute a single reduce step
    async fn execute_single_step(
        &self,
        step: &WorkflowStep,
        context: &mut PhaseContext,
    ) -> Result<StepResult, PhaseError> {
        // This is a simplified execution model
        // In the full implementation, this would delegate to the appropriate executor
        if let Some(cmd) = &step.shell {
            // Execute shell command
            use crate::subprocess::ProcessCommandBuilder;
            let command = ProcessCommandBuilder::new("sh")
                .args(["-c", cmd])
                .current_dir(&context.environment.working_dir)
                .build();

            let result = context
                .subprocess_manager
                .runner()
                .run(command)
                .await
                .map_err(|e| PhaseError::ExecutionFailed {
                    message: format!("Shell command failed: {}", e),
                })?;

            Ok(StepResult {
                success: result.status.success(),
                output: Some(result.stdout),
                error: if !result.status.success() {
                    Some(result.stderr)
                } else {
                    None
                },
            })
        } else {
            // Handle other command types
            Ok(StepResult {
                success: true,
                output: Some("Command executed".to_string()),
                error: None,
            })
        }
    }

    /// Handle on_failure configuration
    async fn handle_on_failure(
        &self,
        on_failure: &crate::cook::workflow::OnFailureConfig,
        _original_step: &WorkflowStep,
        context: &mut PhaseContext,
    ) -> Result<bool, PhaseError> {
        // Handle on_failure based on its variant
        match on_failure {
            crate::cook::workflow::OnFailureConfig::IgnoreErrors(_) => {
                // Just continue, errors are ignored
                Ok(true)
            }
            crate::cook::workflow::OnFailureConfig::SingleCommand(cmd) => {
                // Execute the single command
                let handler_step = crate::cook::workflow::WorkflowStep {
                    command: Some(cmd.clone()),
                    name: None,
                    claude: None,
                    shell: None,
                    test: None,
                    foreach: None,
                    write_file: None,
                    handler: None,
                    on_failure: None,
                    on_success: None,
                    timeout: None,
                    commit_required: false,
                    auto_commit: false,
                    capture: None,
                    capture_format: None,
                    capture_streams: Default::default(),
                    output_file: None,
                    capture_output: Default::default(),
                    retry: None,
                    working_dir: None,
                    env: HashMap::new(),
                    on_exit_code: HashMap::new(),
                    commit_config: None,
                    validate: None,
                    step_validate: None,
                    skip_validation: false,
                    validation_timeout: None,
                    ignore_validation_failure: false,
                    when: None,
                };
                let result = self.execute_single_step(&handler_step, context).await?;
                Ok(result.success)
            }
            crate::cook::workflow::OnFailureConfig::MultipleCommands(cmds) => {
                // Execute multiple commands
                for cmd in cmds {
                    let handler_step = crate::cook::workflow::WorkflowStep {
                        command: Some(cmd.clone()),
                        name: None,
                        claude: None,
                        shell: None,
                        test: None,
                        foreach: None,
                        write_file: None,
                        handler: None,
                        on_failure: None,
                        on_success: None,
                        timeout: None,
                        commit_required: false,
                        auto_commit: false,
                        capture: None,
                        capture_format: None,
                        capture_streams: Default::default(),
                        output_file: None,
                        capture_output: Default::default(),
                        retry: None,
                        working_dir: None,
                        env: HashMap::new(),
                        on_exit_code: HashMap::new(),
                        commit_config: None,
                        validate: None,
                        step_validate: None,
                        skip_validation: false,
                        validation_timeout: None,
                        ignore_validation_failure: false,
                        when: None,
                    };
                    let result = self.execute_single_step(&handler_step, context).await?;
                    if !result.success {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            _ => {
                // For other variants, just continue
                Ok(true)
            }
        }
    }
}

/// Result from executing a single step
struct StepResult {
    success: bool,
    output: Option<String>,
    error: Option<String>,
}

#[async_trait]
impl PhaseExecutor for ReducePhaseExecutor {
    async fn execute(&self, context: &mut PhaseContext) -> Result<PhaseResult, PhaseError> {
        info!("Starting reduce phase execution");
        let start_time = Instant::now();

        // Get map results from context
        let map_results = context
            .map_results
            .as_ref()
            .ok_or_else(|| PhaseError::ValidationError {
                message: "No map results available for reduce phase".to_string(),
            })?
            .clone();

        // Calculate summary for metrics
        let summary = calculate_map_result_summary(&map_results);

        info!(
            "Processing reduce phase with {} successful and {} failed map results",
            summary.successful, summary.failed
        );

        // Prepare the reduce context with map results
        self.prepare_reduce_context(&map_results, context).await?;

        // Execute reduce commands
        self.execute_reduce_commands(context).await?;

        let duration = start_time.elapsed();
        let metrics = PhaseMetrics {
            duration_secs: duration.as_secs_f64(),
            items_processed: self.reduce_phase.commands.len(),
            items_successful: self.reduce_phase.commands.len(),
            items_failed: 0,
        };

        info!("Reduce phase completed successfully");

        Ok(PhaseResult {
            phase_type: PhaseType::Reduce,
            success: true,
            data: Some(json!({
                "commands_executed": self.reduce_phase.commands.len(),
                "map_results_processed": map_results.len(),
                "successful_agents": summary.successful,
                "failed_agents": summary.failed,
            })),
            error_message: None,
            metrics,
        })
    }

    fn phase_type(&self) -> PhaseType {
        PhaseType::Reduce
    }

    fn can_skip(&self, context: &PhaseContext) -> bool {
        // Skip reduce phase if there are no map results or no commands
        context.map_results.is_none() || self.reduce_phase.commands.is_empty()
    }

    fn validate_context(&self, context: &PhaseContext) -> Result<(), PhaseError> {
        // Validate that we have map results
        if context.map_results.is_none() {
            return Err(PhaseError::ValidationError {
                message: "No map results available for reduce phase".to_string(),
            });
        }

        // Validate that we have commands to execute
        if self.reduce_phase.commands.is_empty() {
            return Err(PhaseError::ValidationError {
                message: "No reduce commands to execute".to_string(),
            });
        }

        // Validate that required variables are available
        // This would check for any ${map.*} references in commands
        // For now, we'll assume validation passes
        Ok(())
    }
}