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
//! Builder and configuration for WorkflowExecutor
//!
//! Handles construction, configuration, and initialization logic.

use super::super::checkpoint::{
    create_checkpoint_with_total_steps, CheckpointManager,
    CompletedStep as CheckpointCompletedStep, ResumeContext, RetryState,
};
use super::super::error_recovery::ErrorRecoveryState;
use super::super::normalized;
use super::super::normalized::NormalizedWorkflow;
use super::super::validation::OnIncompleteConfig;
use super::{
    CaptureOutput, ExtendedWorkflowConfig, SensitivePatternConfig, StepResult, WorkflowContext,
    WorkflowExecutor, WorkflowStep,
};
use crate::abstractions::git::RealGitOperations;
use crate::commands::CommandRegistry;
use crate::cook::execution::ClaudeExecutor;
use crate::cook::interaction::UserInteraction;
use crate::cook::retry_state::RetryStateManager;
use crate::cook::session::SessionManager;
use crate::testing::config::TestConfiguration;
use crate::unified_session::TimingTracker;
use anyhow::Result;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

impl WorkflowExecutor {
    // Constructor and builder methods

    /// Create a new workflow executor
    pub fn new(
        claude_executor: Arc<dyn ClaudeExecutor>,
        session_manager: Arc<dyn SessionManager>,
        user_interaction: Arc<dyn UserInteraction>,
    ) -> Self {
        use std::sync::atomic::AtomicBool;
        Self {
            claude_executor,
            session_manager,
            user_interaction,
            timing_tracker: TimingTracker::new(),
            test_config: None,
            command_registry: None,
            subprocess: crate::subprocess::SubprocessManager::production(),
            sensitive_config: SensitivePatternConfig::default(),
            completed_steps: Vec::new(),
            checkpoint_manager: None,
            workflow_id: None,
            checkpoint_completed_steps: Vec::new(),
            environment_manager: None,
            global_environment_config: None,
            current_workflow: None,
            current_step_index: None,
            git_operations: Arc::new(RealGitOperations::new()),
            resume_context: None,
            retry_state_manager: Arc::new(RetryStateManager::new()),
            workflow_path: None,
            dry_run: false,
            assumed_commits: Vec::new(),
            dry_run_commands: Vec::new(),
            dry_run_validations: Vec::new(),
            dry_run_potential_handlers: Vec::new(),
            positional_args: None,
            shutdown_signal: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Sets the command registry for modular command handlers
    pub async fn with_command_registry(mut self) -> Self {
        self.command_registry = Some(CommandRegistry::with_defaults().await);
        self
    }

    /// Set the resume context for handling interrupted workflows
    pub fn with_resume_context(mut self, context: ResumeContext) -> Self {
        // Restore retry state if available in checkpoint
        if let Some(ref checkpoint) = context.checkpoint {
            if let Some(retry_checkpoint_state) = checkpoint.retry_checkpoint_state.clone() {
                // Clone the retry state manager Arc to avoid borrowing issues
                let retry_manager = self.retry_state_manager.clone();

                // Spawn a task to restore retry state asynchronously
                tokio::spawn(async move {
                    if let Err(e) = retry_manager
                        .restore_from_checkpoint(&retry_checkpoint_state)
                        .await
                    {
                        tracing::warn!("Failed to restore retry state from checkpoint: {}", e);
                    } else {
                        tracing::info!("Successfully restored retry state from checkpoint");
                    }
                });
            }
        }

        self.resume_context = Some(context);
        self
    }

    /// Set the workflow file path (for checkpoint resume)
    pub fn with_workflow_path(mut self, path: PathBuf) -> Self {
        self.workflow_path = Some(path);
        self
    }

    /// Enable dry-run mode for preview without execution
    pub fn with_dry_run(mut self, dry_run: bool) -> Self {
        self.dry_run = dry_run;
        self
    }

    /// Set the environment configuration for the workflow
    pub fn with_environment_config(
        mut self,
        config: crate::cook::environment::EnvironmentConfig,
    ) -> Result<Self> {
        // Initialize environment manager with current directory
        let current_dir = std::env::current_dir()?;
        self.environment_manager = Some(crate::cook::environment::EnvironmentManager::new(
            current_dir,
        )?);
        self.global_environment_config = Some(config);
        Ok(self)
    }

    /// Set positional arguments for workflow execution (Spec 163)
    ///
    /// These arguments will be automatically injected as ARG_N environment
    /// variables, making them available across all workflow phases.
    pub fn with_positional_args(mut self, args: Vec<String>) -> Self {
        self.positional_args = Some(args);
        self
    }

    /// Setup signal handler for graceful shutdown (Spec 184)
    ///
    /// Installs SIGINT/SIGTERM handlers that set the shutdown flag,
    /// allowing the executor to save a checkpoint before exiting.
    pub fn setup_signal_handler(&self) {
        use std::sync::atomic::Ordering;

        let shutdown_flag = Arc::clone(&self.shutdown_signal);

        #[cfg(unix)]
        {
            use signal_hook::consts::{SIGINT, SIGTERM};
            use signal_hook::iterator::Signals;
            use std::thread;

            // Spawn signal handler thread
            thread::spawn(move || {
                let mut signals =
                    Signals::new([SIGINT, SIGTERM]).expect("Failed to create signal handler");

                for sig in signals.forever() {
                    match sig {
                        SIGINT | SIGTERM => {
                            tracing::info!(
                                "Received shutdown signal, initiating graceful shutdown"
                            );
                            shutdown_flag.store(true, Ordering::Release);
                            break;
                        }
                        _ => {}
                    }
                }
            });
        }

        #[cfg(not(unix))]
        {
            // Windows Ctrl+C handler
            std::thread::spawn(move || {
                use std::sync::mpsc::channel;
                let (tx, rx) = channel();

                ctrlc::set_handler(move || {
                    let _ = tx.send(());
                })
                .expect("Failed to set Ctrl+C handler");

                let _ = rx.recv();
                tracing::info!("Received Ctrl+C, initiating graceful shutdown");
                shutdown_flag.store(true, Ordering::Release);
            });
        }
    }

    /// Check if shutdown was requested (Spec 184)
    pub fn is_shutdown_requested(&self) -> bool {
        use std::sync::atomic::Ordering;
        self.shutdown_signal.load(Ordering::Acquire)
    }

    /// Set the checkpoint manager for workflow resumption
    pub fn with_checkpoint_manager(
        mut self,
        manager: Arc<CheckpointManager>,
        workflow_id: String,
    ) -> Self {
        self.checkpoint_manager = Some(manager);
        self.workflow_id = Some(workflow_id);
        self
    }

    /// Configure sensitive pattern detection
    pub fn with_sensitive_patterns(mut self, config: SensitivePatternConfig) -> Self {
        self.sensitive_config = config;
        self
    }

    /// Create a new workflow executor with test configuration
    pub fn with_test_config(
        claude_executor: Arc<dyn ClaudeExecutor>,
        session_manager: Arc<dyn SessionManager>,
        user_interaction: Arc<dyn UserInteraction>,
        test_config: Arc<TestConfiguration>,
    ) -> Self {
        use std::sync::atomic::AtomicBool;
        Self {
            claude_executor,
            session_manager,
            user_interaction,
            timing_tracker: TimingTracker::new(),
            test_config: Some(test_config),
            command_registry: None,
            subprocess: crate::subprocess::SubprocessManager::production(),
            sensitive_config: SensitivePatternConfig::default(),
            completed_steps: Vec::new(),
            checkpoint_manager: None,
            workflow_id: None,
            checkpoint_completed_steps: Vec::new(),
            environment_manager: None,
            global_environment_config: None,
            current_workflow: None,
            current_step_index: None,
            git_operations: Arc::new(RealGitOperations::new()),
            resume_context: None,
            retry_state_manager: Arc::new(RetryStateManager::new()),
            workflow_path: None,
            dry_run: false,
            assumed_commits: Vec::new(),
            dry_run_commands: Vec::new(),
            dry_run_validations: Vec::new(),
            dry_run_potential_handlers: Vec::new(),
            positional_args: None,
            shutdown_signal: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Create executor with test configuration and custom git operations
    #[cfg(test)]
    pub fn with_test_config_and_git(
        claude_executor: Arc<dyn ClaudeExecutor>,
        session_manager: Arc<dyn SessionManager>,
        user_interaction: Arc<dyn UserInteraction>,
        test_config: Arc<TestConfiguration>,
        git_operations: Arc<dyn crate::abstractions::git::GitOperations>,
    ) -> Self {
        use std::sync::atomic::AtomicBool;
        Self {
            claude_executor,
            session_manager,
            user_interaction,
            timing_tracker: TimingTracker::new(),
            test_config: Some(test_config),
            command_registry: None,
            subprocess: crate::subprocess::SubprocessManager::production(),
            sensitive_config: SensitivePatternConfig::default(),
            completed_steps: Vec::new(),
            checkpoint_manager: None,
            workflow_id: None,
            checkpoint_completed_steps: Vec::new(),
            environment_manager: None,
            global_environment_config: None,
            current_workflow: None,
            current_step_index: None,
            git_operations,
            resume_context: None,
            retry_state_manager: Arc::new(RetryStateManager::new()),
            workflow_path: None,
            dry_run: false,
            assumed_commits: Vec::new(),
            dry_run_commands: Vec::new(),
            dry_run_validations: Vec::new(),
            dry_run_potential_handlers: Vec::new(),
            positional_args: None,
            shutdown_signal: Arc::new(AtomicBool::new(false)),
        }
    }

    // Configuration helpers

    /// Create a validation handler from on_incomplete configuration
    pub(super) fn create_validation_handler(
        &self,
        on_incomplete: &OnIncompleteConfig,
        _ctx: &WorkflowContext,
    ) -> Option<WorkflowStep> {
        // Create a step based on the handler configuration
        if on_incomplete.claude.is_some() || on_incomplete.shell.is_some() {
            Some(WorkflowStep {
                name: None,
                claude: on_incomplete.claude.clone(),
                shell: on_incomplete.shell.clone(),
                test: None,
                foreach: None,
                write_file: None,
                command: None,
                handler: None,
                capture: None,
                capture_format: None,
                capture_streams: Default::default(),
                output_file: None,
                timeout: None,
                capture_output: CaptureOutput::Disabled,
                on_failure: None,
                retry: None,
                on_success: None,
                on_exit_code: Default::default(),
                commit_required: on_incomplete.commit_required,
                auto_commit: false,
                commit_config: None,
                working_dir: None,
                env: Default::default(),
                validate: None,
                step_validate: None,
                skip_validation: false,
                validation_timeout: None,
                ignore_validation_failure: false,
                when: None,
            })
        } else {
            None
        }
    }

    /// Restore error recovery state from resume context
    pub(super) fn restore_error_recovery_state(
        &self,
        step_index: usize,
        workflow_context: &mut WorkflowContext,
    ) {
        if let Some(ref resume_ctx) = self.resume_context {
            if let Some(recovery_state_value) =
                resume_ctx.variable_state.get("__error_recovery_state")
            {
                if let Ok(error_recovery_state) =
                    serde_json::from_value::<ErrorRecoveryState>(recovery_state_value.clone())
                {
                    if !error_recovery_state.active_handlers.is_empty() {
                        tracing::info!(
                            "Restored {} error handlers for step {}",
                            error_recovery_state.active_handlers.len(),
                            step_index
                        );
                        for (key, value) in error_recovery_state.error_context {
                            workflow_context
                                .variables
                                .insert(format!("error.{}", key), value.to_string());
                        }
                    }
                }
            }
        }
    }

    // Checkpoint functions

    /// Save a checkpoint during step execution (e.g., during retries)
    pub(super) async fn save_retry_checkpoint(
        &self,
        workflow: &NormalizedWorkflow,
        current_step_index: usize,
        retry_state: Option<RetryState>,
        ctx: &WorkflowContext,
    ) {
        if let Some(ref checkpoint_manager) = self.checkpoint_manager {
            if let Some(ref workflow_id) = self.workflow_id {
                let workflow_hash = format!("{:?}", workflow.steps.len());

                // Create a checkpoint with current retry state
                let mut checkpoint_steps = self.checkpoint_completed_steps.clone();

                // Add or update the current step with retry state
                if let Some(retry_state) = retry_state {
                    let step_name = if current_step_index < workflow.steps.len() {
                        match &workflow.steps[current_step_index].command {
                            normalized::StepCommand::Claude(cmd) => format!("claude: {}", cmd),
                            normalized::StepCommand::Shell(cmd) => format!("shell: {}", cmd),
                            normalized::StepCommand::Test { command, .. } => {
                                format!("test: {}", command)
                            }
                            normalized::StepCommand::Simple(cmd) => cmd.to_string(),
                            _ => "complex command".to_string(),
                        }
                    } else {
                        "unknown step".to_string()
                    };

                    let retry_step = CheckpointCompletedStep {
                        step_index: current_step_index,
                        command: step_name,
                        success: false,
                        output: None,
                        captured_variables: HashMap::new(),
                        duration: Duration::from_secs(0),
                        completed_at: chrono::Utc::now(),
                        retry_state: Some(retry_state),
                    };

                    // Remove any existing entry for this step and add the new one
                    checkpoint_steps.retain(|s| s.step_index != current_step_index);
                    checkpoint_steps.push(retry_step);
                }

                let mut checkpoint = create_checkpoint_with_total_steps(
                    workflow_id.clone(),
                    workflow,
                    ctx,
                    checkpoint_steps,
                    current_step_index,
                    workflow_hash,
                    workflow.steps.len(),
                );

                // Set workflow path if available
                if let Some(ref path) = self.workflow_path {
                    checkpoint.workflow_path = Some(path.clone());
                }

                // Add retry state from RetryStateManager
                if let Ok(retry_checkpoint_state) =
                    self.retry_state_manager.create_checkpoint_state().await
                {
                    checkpoint.retry_checkpoint_state = Some(retry_checkpoint_state);
                }

                if let Err(e) = checkpoint_manager.save_checkpoint(&checkpoint).await {
                    tracing::warn!("Failed to save retry checkpoint: {}", e);
                } else {
                    tracing::debug!(
                        "Saved retry checkpoint at step {} attempt",
                        current_step_index
                    );
                }
            }
        }
    }

    // Dry-run display

    /// Display dry-run information before workflow execution
    pub fn display_dry_run_info(&self, workflow: &ExtendedWorkflowConfig) {
        if self.dry_run {
            println!("[DRY RUN] Workflow execution simulation mode");
            println!("[DRY RUN] No commands will be executed");
            if workflow.max_iterations > 1 {
                println!("[DRY RUN] Would run {} iterations", workflow.max_iterations);
            }
        }
    }

    /// Display dry-run summary at the end of execution
    pub fn display_dry_run_summary(&self) {
        if !self.dry_run {
            return;
        }

        println!("\n[DRY RUN] Summary:");
        println!("==================");

        // Show main commands
        println!(
            "Main commands that would execute: {}",
            self.dry_run_commands.len()
        );
        if !self.dry_run_commands.is_empty() {
            for cmd in &self.dry_run_commands {
                println!("  - {}", cmd);
            }
        }

        // Show validation commands
        if !self.dry_run_validations.is_empty() {
            println!(
                "\nValidation commands that would execute: {}",
                self.dry_run_validations.len()
            );
            for val in &self.dry_run_validations {
                println!("  - {}", val);
            }
        }

        // Show potential failure handlers
        if !self.dry_run_potential_handlers.is_empty() {
            println!("\nPotential failure handlers (if needed):");
            for handler in &self.dry_run_potential_handlers {
                println!("  - {}", handler);
            }
        }

        // Show assumed commits
        if !self.assumed_commits.is_empty() {
            println!("\nAssumed commits: {}", self.assumed_commits.len());
            for commit in &self.assumed_commits {
                println!("  - From: {}", commit);
            }
        }

        println!("\nNo actual commands executed or files changed.");
        println!("To run for real, remove the --dry-run flag.");
    }

    // Message builders

    /// Build error message for a failed step
    pub(super) fn build_step_error_message(step: &WorkflowStep, result: &StepResult) -> String {
        super::pure::build_step_error_message(step, result)
    }

    /// Generate a commit message from template or default (delegated to commit_handler module)
    pub fn generate_commit_message(
        &self,
        step: &WorkflowStep,
        context: &WorkflowContext,
    ) -> String {
        super::commit_handler::generate_commit_message(
            step,
            context,
            &self.get_step_display_name(step),
        )
    }
}