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
//! Variable checkpoint state management for workflow resume
//!
//! This module handles persisting and restoring variable state across workflow interruptions,
//! ensuring that all variable types maintain their values correctly during resume operations.

use crate::cook::workflow::variables::VariableStore;
use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::PathBuf;

/// Type alias for restored variables (global, captured outputs, iteration vars)
type RestoredVariables = (
    HashMap<String, String>,
    HashMap<String, String>,
    HashMap<String, String>,
);

/// Complete variable state for checkpoint persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableCheckpointState {
    /// Global workflow variables
    pub global_variables: HashMap<String, Value>,

    /// Phase-specific variables (setup, map, reduce)
    pub phase_variables: HashMap<String, HashMap<String, Value>>,

    /// Cached computed values
    pub computed_cache: HashMap<String, CachedValue>,

    /// Environment snapshot at checkpoint time
    pub environment_snapshot: EnvironmentSnapshot,

    /// History of variable interpolations for validation
    pub interpolation_history: Vec<InterpolationRecord>,

    /// Metadata about variable state
    pub variable_metadata: VariableMetadata,

    /// Captured outputs from commands
    pub captured_outputs: HashMap<String, String>,

    /// Iteration variables for loops
    pub iteration_vars: HashMap<String, String>,
}

/// Cached computed value with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedValue {
    /// The computed value
    pub value: Value,

    /// When this value was computed
    pub computed_at: DateTime<Utc>,

    /// Cache key for invalidation
    pub cache_key: String,

    /// Variable dependencies
    pub dependencies: Vec<String>,

    /// Whether this is an expensive computation
    pub is_expensive: bool,
}

/// Environment snapshot for compatibility checking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvironmentSnapshot {
    /// Environment variables at checkpoint time
    pub variables: HashMap<String, String>,

    /// When the snapshot was taken
    pub captured_at: DateTime<Utc>,

    /// Hostname for validation
    pub hostname: String,

    /// Working directory
    pub working_directory: PathBuf,
}

/// Record of a variable interpolation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterpolationRecord {
    /// Template that was interpolated
    pub template: String,

    /// Result of interpolation
    pub result: String,

    /// When interpolation occurred
    pub interpolated_at: DateTime<Utc>,

    /// Variables that were referenced
    pub variable_dependencies: Vec<String>,

    /// Phase context (if any)
    pub phase: Option<String>,
}

/// Metadata about variable state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableMetadata {
    /// Total number of variables
    pub total_variables: usize,

    /// Number of computed variables
    pub computed_variables: usize,

    /// Number of interpolations performed
    pub total_interpolations: usize,

    /// Checkpoint version for compatibility
    pub checkpoint_version: String,
}

/// Environment compatibility check results
#[derive(Debug, Clone)]
pub struct EnvironmentCompatibility {
    /// Variables missing in current environment
    pub missing_variables: HashMap<String, String>,

    /// Variables that changed values
    pub changed_variables: HashMap<String, (String, String)>, // (old, new)

    /// New variables in current environment
    pub new_variables: HashMap<String, String>,

    /// Whether resume is safe
    pub is_compatible: bool,
}

impl Default for EnvironmentCompatibility {
    fn default() -> Self {
        Self::new()
    }
}

impl EnvironmentCompatibility {
    /// Create new compatibility check
    pub fn new() -> Self {
        Self {
            missing_variables: HashMap::new(),
            changed_variables: HashMap::new(),
            new_variables: HashMap::new(),
            is_compatible: true,
        }
    }

    /// Add a missing variable
    pub fn add_missing_variable(&mut self, key: String, value: String) {
        self.missing_variables.insert(key, value);
        self.is_compatible = false; // Missing variables make the environment incompatible
    }

    /// Add a changed variable
    pub fn add_changed_variable(&mut self, key: String, old_value: String, new_value: String) {
        self.changed_variables.insert(key, (old_value, new_value));
        self.is_compatible = false; // Changed variables make the environment incompatible
    }

    /// Add a new variable
    pub fn add_new_variable(&mut self, key: String, value: String) {
        self.new_variables.insert(key, value);
    }

    /// Check if there are critical changes
    pub fn has_critical_changes(&self) -> bool {
        // Critical if required variables are missing or changed
        !self.missing_variables.is_empty() || !self.changed_variables.is_empty()
    }
}

/// Test results for variable interpolation validation
#[derive(Debug, Clone)]
pub struct InterpolationTestResults {
    /// Individual test results
    pub tests: Vec<InterpolationTest>,

    /// Total tests run
    pub total_tests: usize,

    /// Tests that passed
    pub passed_tests: usize,

    /// Tests that failed
    pub failed_tests: usize,

    /// Duration of testing
    pub test_duration: std::time::Duration,
}

impl Default for InterpolationTestResults {
    fn default() -> Self {
        Self::new()
    }
}

impl InterpolationTestResults {
    /// Create new test results
    pub fn new() -> Self {
        Self {
            tests: Vec::new(),
            total_tests: 0,
            passed_tests: 0,
            failed_tests: 0,
            test_duration: std::time::Duration::default(),
        }
    }

    /// Add a test result
    pub fn add_test(&mut self, test: InterpolationTest) {
        self.total_tests += 1;
        if test.matches {
            self.passed_tests += 1;
        } else {
            self.failed_tests += 1;
        }
        self.tests.push(test);
    }

    /// Check if all tests passed
    pub fn all_passed(&self) -> bool {
        self.failed_tests == 0
    }
}

/// Individual interpolation test result
#[derive(Debug, Clone)]
pub struct InterpolationTest {
    /// Template that was tested
    pub template: String,

    /// Original interpolation result
    pub original_result: String,

    /// Current interpolation result
    pub current_result: String,

    /// Whether results match
    pub matches: bool,

    /// When the test was performed
    pub interpolated_at: DateTime<Utc>,
}

/// Manager for variable resume operations
pub struct VariableResumeManager {}

impl Default for VariableResumeManager {
    fn default() -> Self {
        Self::new()
    }
}

impl VariableResumeManager {
    /// Create new variable resume manager
    pub fn new() -> Self {
        Self {}
    }

    /// Create variable checkpoint state from current context
    pub fn create_checkpoint(
        &self,
        variables: &HashMap<String, String>,
        captured_outputs: &HashMap<String, String>,
        iteration_vars: &HashMap<String, String>,
        _variable_store: &VariableStore,
    ) -> Result<VariableCheckpointState> {
        // Convert string variables to Value
        let mut global_variables = HashMap::new();
        for (key, value) in variables {
            global_variables.insert(key.clone(), Value::String(value.clone()));
        }

        // Add captured outputs
        for (key, value) in captured_outputs {
            global_variables.insert(key.clone(), Value::String(value.clone()));
        }

        // Create environment snapshot
        let environment_snapshot = EnvironmentSnapshot {
            variables: std::env::vars().collect(),
            captured_at: Utc::now(),
            hostname: hostname::get()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string(),
            working_directory: std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/")),
        };

        // Create metadata
        let variable_metadata = VariableMetadata {
            total_variables: global_variables.len(),
            computed_variables: 0,
            total_interpolations: 0,
            checkpoint_version: "1.0.0".to_string(),
        };

        Ok(VariableCheckpointState {
            global_variables,
            phase_variables: HashMap::new(),
            computed_cache: HashMap::new(),
            environment_snapshot,
            interpolation_history: Vec::new(),
            variable_metadata,
            captured_outputs: captured_outputs.clone(),
            iteration_vars: iteration_vars.clone(),
        })
    }

    /// Restore variables from checkpoint state
    pub fn restore_from_checkpoint(
        &self,
        state: &VariableCheckpointState,
    ) -> Result<RestoredVariables> {
        let mut variables = HashMap::new();
        let mut captured_outputs = HashMap::new();
        let iteration_vars = state.iteration_vars.clone();

        // Restore global variables
        for (key, value) in &state.global_variables {
            if let Value::String(s) = value {
                variables.insert(key.clone(), s.clone());
            } else {
                variables.insert(key.clone(), value.to_string());
            }
        }

        // Restore captured outputs
        for (key, value) in &state.captured_outputs {
            captured_outputs.insert(key.clone(), value.clone());
        }

        Ok((variables, captured_outputs, iteration_vars))
    }

    /// Validate environment compatibility
    pub fn validate_environment(
        &self,
        saved_snapshot: &EnvironmentSnapshot,
    ) -> Result<EnvironmentCompatibility> {
        let mut compatibility = EnvironmentCompatibility::new();
        let current_env: HashMap<String, String> = std::env::vars().collect();

        // Check for missing or changed variables
        for (key, saved_value) in &saved_snapshot.variables {
            // Skip internal/system variables
            if key.starts_with("PRODIGY_") || key.starts_with("_") {
                continue;
            }

            match current_env.get(key) {
                Some(current_value) if current_value != saved_value => {
                    compatibility.add_changed_variable(
                        key.clone(),
                        saved_value.clone(),
                        current_value.clone(),
                    );
                }
                None => {
                    // Only warn about missing variables that seem important
                    if !key.starts_with("RUST_") && !key.contains("TEMP") && !key.contains("TMP") {
                        compatibility.add_missing_variable(key.clone(), saved_value.clone());
                    }
                }
                _ => {} // Variable matches
            }
        }

        // Check for new variables
        for (key, current_value) in &current_env {
            if !saved_snapshot.variables.contains_key(key) && !key.starts_with("PRODIGY_") {
                compatibility.add_new_variable(key.clone(), current_value.clone());
            }
        }

        // Determine compatibility
        compatibility.is_compatible = !compatibility.has_critical_changes();

        Ok(compatibility)
    }

    /// Recalculate MapReduce variables from job state
    pub fn recalculate_mapreduce_variables(
        &self,
        total_items: usize,
        successful_items: usize,
        failed_items: usize,
    ) -> HashMap<String, String> {
        let mut variables = HashMap::new();

        // Set aggregate variables
        variables.insert("map.total".to_string(), total_items.to_string());
        variables.insert("map.successful".to_string(), successful_items.to_string());
        variables.insert("map.failed".to_string(), failed_items.to_string());
        variables.insert(
            "map.completed".to_string(),
            (successful_items + failed_items).to_string(),
        );

        // Calculate success rate
        let success_rate = if total_items > 0 {
            (successful_items as f64 / total_items as f64) * 100.0
        } else {
            0.0
        };
        variables.insert(
            "map.success_rate".to_string(),
            format!("{:.2}", success_rate),
        );

        variables
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_environment_compatibility() {
        let mut compatibility = EnvironmentCompatibility::new();

        // Add some changes
        compatibility.add_missing_variable("API_KEY".to_string(), "secret".to_string());
        compatibility.add_changed_variable(
            "ENV".to_string(),
            "production".to_string(),
            "development".to_string(),
        );

        // Should have critical changes
        assert!(compatibility.has_critical_changes());
        assert!(!compatibility.is_compatible);
    }

    #[test]
    fn test_mapreduce_variable_recalculation() {
        let manager = VariableResumeManager::new();
        let vars = manager.recalculate_mapreduce_variables(10, 7, 3);

        assert_eq!(vars.get("map.total").unwrap(), "10");
        assert_eq!(vars.get("map.successful").unwrap(), "7");
        assert_eq!(vars.get("map.failed").unwrap(), "3");
        assert_eq!(vars.get("map.completed").unwrap(), "10");
        assert_eq!(vars.get("map.success_rate").unwrap(), "70.00");
    }

    #[test]
    fn test_interpolation_test_results() {
        let mut results = InterpolationTestResults::new();

        results.add_test(InterpolationTest {
            template: "${item}".to_string(),
            original_result: "test.txt".to_string(),
            current_result: "test.txt".to_string(),
            matches: true,
            interpolated_at: Utc::now(),
        });

        results.add_test(InterpolationTest {
            template: "${map.total}".to_string(),
            original_result: "10".to_string(),
            current_result: "0".to_string(),
            matches: false,
            interpolated_at: Utc::now(),
        });

        assert_eq!(results.total_tests, 2);
        assert_eq!(results.passed_tests, 1);
        assert_eq!(results.failed_tests, 1);
        assert!(!results.all_passed());
    }
}