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
//! Recovery and resume logic for MapReduce jobs
//!
//! Handles job recovery from checkpoints and calculating pending work.

use super::{JobState, RecoveryPlan, StateError, StateEvent, StateEventType, StateManager};
use chrono::Utc;
use serde_json::Value;
use tracing::{debug, info};

impl StateManager {
    /// Create a recovery plan from a checkpoint
    pub async fn recover_from_checkpoint(
        &self,
        job_id: &str,
        checkpoint_version: Option<u32>,
    ) -> Result<RecoveryPlan, StateError> {
        let state = self
            .get_state(job_id)
            .await?
            .ok_or_else(|| StateError::NotFound(job_id.to_string()))?;

        // Validate checkpoint if specified
        if let Some(version) = checkpoint_version {
            if let Some(ref checkpoint) = state.checkpoint {
                if checkpoint.version != version {
                    return Err(StateError::ValidationError(format!(
                        "Requested checkpoint version {} but found {}",
                        version, checkpoint.version
                    )));
                }
            } else {
                return Err(StateError::ValidationError(format!(
                    "No checkpoint found for job {}",
                    job_id
                )));
            }
        }

        // Validate state before recovery
        self.validate_checkpoint(&state)?;

        // Calculate pending items
        let pending_items = self.calculate_pending_items(&state, 0)?;

        // Log recovery start
        self.log_event(StateEvent {
            timestamp: Utc::now(),
            event_type: StateEventType::RecoveryStarted {
                checkpoint_version: state.checkpoint.as_ref().map(|c| c.version).unwrap_or(0),
            },
            job_id: job_id.to_string(),
            details: Some(format!("Recovering {} pending items", pending_items.len())),
        })
        .await;

        let plan = RecoveryPlan {
            resume_phase: state.phase,
            pending_items,
            skip_items: state.processed_items.clone(),
            variables: state.variables.clone(),
            agent_results: state.agent_results.clone(),
        };

        info!(
            "Created recovery plan for job {}: {} items to process, {} to skip",
            job_id,
            plan.pending_items.len(),
            plan.skip_items.len()
        );

        Ok(plan)
    }

    /// Calculate pending items for resumption
    pub fn calculate_pending_items(
        &self,
        state: &JobState,
        max_additional_retries: u32,
    ) -> Result<Vec<Value>, StateError> {
        let mut pending_items = Vec::new();
        let work_items = self.get_work_items_from_state(state)?;

        // Add never-attempted items
        for (i, item) in work_items.iter().enumerate() {
            let item_id = format!("item_{}", i);
            if !state.processed_items.contains(&item_id) && !state.failed_items.contains(&item_id) {
                pending_items.push(item.clone());
                debug!("Adding never-attempted item: {}", item_id);
            }
        }

        // Add retriable failed items
        let _max_retries = max_additional_retries;
        for failed_item_id in &state.failed_items {
            // Extract item index from ID
            if let Some(idx) = failed_item_id
                .strip_prefix("item_")
                .and_then(|s| s.parse::<usize>().ok())
            {
                if idx < work_items.len() {
                    // Check if we should retry this item
                    // In the current implementation, we assume all failed items should be retried
                    // up to the max retry limit
                    pending_items.push(work_items[idx].clone());
                    debug!("Adding failed item for retry: {}", failed_item_id);
                }
            }
        }

        info!(
            "Calculated {} pending items for recovery",
            pending_items.len()
        );
        Ok(pending_items)
    }

    /// Get work items from state or reconstruct them
    fn get_work_items_from_state(&self, state: &JobState) -> Result<Vec<Value>, StateError> {
        // In a real implementation, we would store work items in the state
        // For now, we'll reconstruct them from the total count
        let mut items = Vec::new();
        for i in 0..state.total_items {
            items.push(Value::String(format!("item_{}", i)));
        }
        Ok(items)
    }

    /// Check if a job can be resumed
    pub async fn can_resume_job(&self, job_id: &str) -> bool {
        match self.get_state(job_id).await {
            Ok(Some(state)) => !state.is_complete && state.checkpoint.is_some(),
            _ => false,
        }
    }

    /// Apply a recovery plan to resume job execution
    pub async fn apply_recovery_plan(
        &self,
        job_id: &str,
        plan: &RecoveryPlan,
    ) -> Result<(), StateError> {
        self.update_state(job_id, |state| {
            // Restore variables
            state.variables = plan.variables.clone();

            // Restore agent results
            state.agent_results = plan.agent_results.clone();

            // Restore processed items
            state.processed_items = plan.skip_items.clone();

            // Update phase if needed
            if state.phase != plan.resume_phase {
                debug!(
                    "Updating phase from {:?} to {:?}",
                    state.phase, plan.resume_phase
                );
                state.phase = plan.resume_phase;
            }

            Ok(())
        })
        .await?;

        info!(
            "Applied recovery plan to job {}: resuming from phase {:?}",
            job_id, plan.resume_phase
        );

        Ok(())
    }

    /// Mark items as processed during recovery
    pub async fn mark_items_processed(
        &self,
        job_id: &str,
        item_ids: Vec<String>,
    ) -> Result<(), StateError> {
        let count = item_ids.len();

        self.update_state(job_id, |state| {
            for item_id in item_ids {
                state.processed_items.insert(item_id);
            }
            Ok(())
        })
        .await?;

        // Log progress
        self.log_event(StateEvent {
            timestamp: Utc::now(),
            event_type: StateEventType::ItemsProcessed { count },
            job_id: job_id.to_string(),
            details: None,
        })
        .await;

        Ok(())
    }

    /// Mark items as failed during recovery
    pub async fn mark_items_failed(
        &self,
        job_id: &str,
        item_ids: Vec<String>,
    ) -> Result<(), StateError> {
        let count = item_ids.len();

        self.update_state(job_id, |state| {
            for item_id in item_ids {
                if !state.failed_items.contains(&item_id) {
                    state.failed_items.push(item_id);
                }
            }
            Ok(())
        })
        .await?;

        // Log failures
        self.log_event(StateEvent {
            timestamp: Utc::now(),
            event_type: StateEventType::ItemsFailed { count },
            job_id: job_id.to_string(),
            details: None,
        })
        .await;

        Ok(())
    }
}

/// Options for job resumption
#[derive(Debug, Clone)]
pub struct ResumeOptions {
    /// Force resume even if job appears complete
    pub force_resume: bool,
    /// Maximum additional retries for failed items
    pub max_additional_retries: u32,
    /// Skip validation of checkpoint integrity
    pub skip_validation: bool,
    /// Specific checkpoint version to resume from
    pub from_checkpoint: Option<u32>,
}

impl Default for ResumeOptions {
    fn default() -> Self {
        Self {
            force_resume: false,
            max_additional_retries: 2,
            skip_validation: false,
            from_checkpoint: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cook::execution::mapreduce::state::persistence::InMemoryStateStore;
    use crate::cook::execution::mapreduce::state::PhaseType;
    use crate::cook::execution::mapreduce::{AgentResult, AgentStatus, MapReduceConfig};
    use std::collections::HashSet;
    use std::sync::Arc;
    use std::time::Duration;

    #[tokio::test]
    async fn test_recovery_plan_creation() {
        let store = Arc::new(InMemoryStateStore::new());
        let manager = StateManager::new(store);

        let config = MapReduceConfig::default();
        let job_id = "test-job-recovery".to_string();

        // Create job with some progress
        let _state = manager.create_job(&config, job_id.clone()).await.unwrap();

        // Simulate some processing
        manager
            .update_state(&job_id, |state| {
                state.total_items = 5;
                state.processed_items.insert("item_0".to_string());
                state.processed_items.insert("item_1".to_string());

                state.agent_results.insert(
                    "item_0".to_string(),
                    AgentResult {
                        item_id: "item_0".to_string(),
                        status: AgentStatus::Success,
                        output: Some("output".to_string()),
                        commits: vec![],
                        duration: Duration::from_secs(1),
                        error: None,
                        worktree_path: None,
                        branch_name: None,
                        worktree_session_id: None,
                        files_modified: vec![],
                        json_log_location: None,
                        cleanup_status: None,
                    },
                );

                state.agent_results.insert(
                    "item_1".to_string(),
                    AgentResult {
                        item_id: "item_1".to_string(),
                        status: AgentStatus::Success,
                        output: Some("output".to_string()),
                        commits: vec![],
                        duration: Duration::from_secs(1),
                        error: None,
                        worktree_path: None,
                        branch_name: None,
                        worktree_session_id: None,
                        files_modified: vec![],
                        json_log_location: None,
                        cleanup_status: None,
                    },
                );

                state.failed_items.push("item_2".to_string());

                Ok(())
            })
            .await
            .unwrap();

        // Create checkpoint
        manager.create_checkpoint(&job_id).await.unwrap();

        // Create recovery plan
        let plan = manager
            .recover_from_checkpoint(&job_id, None)
            .await
            .unwrap();

        assert_eq!(plan.skip_items.len(), 2); // item_0 and item_1
        assert!(plan.skip_items.contains("item_0"));
        assert!(plan.skip_items.contains("item_1"));

        // Should have pending items: never attempted (item_3, item_4) + failed (item_2)
        assert_eq!(plan.pending_items.len(), 3);
    }

    #[tokio::test]
    async fn test_calculate_pending_items() {
        let store = Arc::new(InMemoryStateStore::new());
        let manager = StateManager::new(store);

        let mut state = JobState {
            id: "test-job".to_string(),
            phase: PhaseType::Map,
            checkpoint: None,
            processed_items: HashSet::new(),
            failed_items: Vec::new(),
            variables: Default::default(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            config: MapReduceConfig::default(),
            agent_results: Default::default(),
            is_complete: false,
            total_items: 5,
        };

        // Mark some items as processed
        state.processed_items.insert("item_0".to_string());
        state.processed_items.insert("item_1".to_string());

        // Mark some items as failed
        state.failed_items.push("item_2".to_string());

        let pending = manager.calculate_pending_items(&state, 0).unwrap();

        // Should have item_3, item_4 (never attempted) and item_2 (failed)
        assert_eq!(pending.len(), 3);
    }

    #[tokio::test]
    async fn test_mark_items_processed() {
        let store = Arc::new(InMemoryStateStore::new());
        let manager = StateManager::new(store);

        let config = MapReduceConfig::default();
        let job_id = "test-job-mark".to_string();

        manager.create_job(&config, job_id.clone()).await.unwrap();

        // Mark items as processed
        manager
            .mark_items_processed(&job_id, vec!["item_0".to_string(), "item_1".to_string()])
            .await
            .unwrap();

        let state = manager.get_state(&job_id).await.unwrap().unwrap();
        assert_eq!(state.processed_items.len(), 2);
        assert!(state.processed_items.contains("item_0"));
        assert!(state.processed_items.contains("item_1"));
    }

    #[tokio::test]
    async fn test_can_resume_job() {
        let store = Arc::new(InMemoryStateStore::new());
        let manager = StateManager::new(store);

        let config = MapReduceConfig::default();
        let job_id = "test-job-resume".to_string();

        // Create job
        manager.create_job(&config, job_id.clone()).await.unwrap();

        // Job without checkpoint cannot be resumed
        assert!(!manager.can_resume_job(&job_id).await);

        // Create checkpoint
        manager.create_checkpoint(&job_id).await.unwrap();

        // Now job can be resumed
        assert!(manager.can_resume_job(&job_id).await);

        // Mark job as complete
        manager
            .update_state(&job_id, |state| {
                state.is_complete = true;
                Ok(())
            })
            .await
            .unwrap();

        // Complete job cannot be resumed
        assert!(!manager.can_resume_job(&job_id).await);
    }
}