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
//! Performance benchmarks for checkpoint operations
//! Verifies <5% overhead requirement for checkpoint save/load

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use prodigy::cook::workflow::checkpoint::{
    CheckpointManager, CompletedStep, ExecutionState, WorkflowCheckpoint, WorkflowStatus,
    CHECKPOINT_VERSION,
};
use serde_json::json;
use std::collections::HashMap;
use std::hint::black_box;
use std::time::Duration;
use tempfile::TempDir;
use tokio::runtime::Runtime;

/// Create a test checkpoint with varying complexity
fn create_test_checkpoint(num_variables: usize, num_completed_steps: usize) -> WorkflowCheckpoint {
    let mut variables = HashMap::new();

    // Add variables of varying sizes
    for i in 0..num_variables {
        variables.insert(
            format!("var_{}", i),
            json!(format!(
                "value_{}_with_some_content_to_make_it_realistic",
                i
            )),
        );
    }

    // Add some nested structures
    variables.insert(
        "shell".to_string(),
        json!({
            "output": "Sample command output with multiple lines\n".repeat(10),
            "exit_code": 0,
            "duration_ms": 1234
        }),
    );

    // Create completed steps
    let completed_steps = (0..num_completed_steps)
        .map(|i| CompletedStep {
            step_index: i,
            command: format!("shell: command_{}.sh", i),
            success: true,
            output: Some(format!("Output from command {}", i)),
            captured_variables: HashMap::new(),
            duration: Duration::from_secs(1),
            completed_at: chrono::Utc::now(),
            retry_state: None,
        })
        .collect();

    let now = chrono::Utc::now();

    WorkflowCheckpoint {
        workflow_id: "bench-workflow-123".to_string(),
        execution_state: ExecutionState {
            current_step_index: num_completed_steps,
            total_steps: num_completed_steps + 10,
            status: WorkflowStatus::Running,
            start_time: now,
            last_checkpoint: now,
            current_iteration: None,
            total_iterations: None,
        },
        completed_steps,
        variable_state: variables,
        mapreduce_state: None,
        timestamp: now,
        version: CHECKPOINT_VERSION,
        workflow_hash: "test-hash".to_string(),
        total_steps: num_completed_steps + 10,
        workflow_name: Some("benchmark-workflow".to_string()),
        workflow_path: None,
        error_recovery_state: None,
        retry_checkpoint_state: None,
        variable_checkpoint_state: None,
    }
}

fn bench_checkpoint_save(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();

    c.benchmark_group("checkpoint_save")
        .warm_up_time(Duration::from_secs(1))
        .measurement_time(Duration::from_secs(5))
        .bench_function("small_checkpoint", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    #[allow(deprecated)]
                    let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                    let checkpoint = create_test_checkpoint(10, 5);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                },
                BatchSize::SmallInput,
            );
        })
        .bench_function("medium_checkpoint", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    #[allow(deprecated)]
                    let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                    let checkpoint = create_test_checkpoint(100, 50);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                },
                BatchSize::SmallInput,
            );
        })
        .bench_function("large_checkpoint", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    #[allow(deprecated)]
                    let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                    let checkpoint = create_test_checkpoint(1000, 200);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                },
                BatchSize::SmallInput,
            );
        });
}

fn bench_checkpoint_load(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();

    c.benchmark_group("checkpoint_load")
        .warm_up_time(Duration::from_secs(1))
        .measurement_time(Duration::from_secs(5))
        .bench_function("small_checkpoint", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    #[allow(deprecated)]
                    let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                    let checkpoint = create_test_checkpoint(10, 5);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    // Save the checkpoint first
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                    // Now benchmark the load
                    black_box(
                        manager
                            .load_checkpoint(&checkpoint.workflow_id)
                            .await
                            .unwrap(),
                    );
                },
                BatchSize::SmallInput,
            );
        })
        .bench_function("medium_checkpoint", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    #[allow(deprecated)]
                    let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                    let checkpoint = create_test_checkpoint(100, 50);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    // Save the checkpoint first
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                    // Now benchmark the load
                    black_box(
                        manager
                            .load_checkpoint(&checkpoint.workflow_id)
                            .await
                            .unwrap(),
                    );
                },
                BatchSize::SmallInput,
            );
        })
        .bench_function("large_checkpoint", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    #[allow(deprecated)]
                    let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                    let checkpoint = create_test_checkpoint(1000, 200);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    // Save the checkpoint first
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                    // Now benchmark the load
                    black_box(
                        manager
                            .load_checkpoint(&checkpoint.workflow_id)
                            .await
                            .unwrap(),
                    );
                },
                BatchSize::SmallInput,
            );
        });
}

fn bench_checkpoint_atomic_write(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();

    c.bench_function("checkpoint_atomic_write", |b| {
        b.to_async(&rt).iter_batched(
            || {
                let temp_dir = TempDir::new().unwrap();
                #[allow(deprecated)]
                let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                let checkpoint = create_test_checkpoint(100, 50);
                (manager, checkpoint, temp_dir)
            },
            |(manager, checkpoint, _temp_dir)| async move {
                // Test atomic write with potential concurrent access
                manager.save_checkpoint(&checkpoint).await.unwrap();
            },
            BatchSize::SmallInput,
        );
    });
}

fn bench_checkpoint_list(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();

    c.bench_function("checkpoint_list_10_checkpoints", |b| {
        b.to_async(&rt).iter_batched(
            || {
                let temp_dir = TempDir::new().unwrap();
                #[allow(deprecated)]
                let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                let checkpoints: Vec<_> = (0..10)
                    .map(|i| {
                        let mut checkpoint = create_test_checkpoint(50, 25);
                        checkpoint.workflow_id = format!("workflow-{}", i);
                        checkpoint
                    })
                    .collect();
                (manager, checkpoints, temp_dir)
            },
            |(manager, checkpoints, _temp_dir)| async move {
                // Create the checkpoints first
                for checkpoint in checkpoints {
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                }
                // Now benchmark the list operation
                black_box(manager.list_checkpoints().await.unwrap());
            },
            BatchSize::SmallInput,
        );
    });
}

fn bench_checkpoint_delete(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();

    c.bench_function("checkpoint_delete", |b| {
        b.to_async(&rt).iter_batched(
            || {
                let temp_dir = TempDir::new().unwrap();
                #[allow(deprecated)]
                let manager = CheckpointManager::new(temp_dir.path().to_path_buf());
                let checkpoints: Vec<_> = (0..5)
                    .map(|i| {
                        let mut checkpoint = create_test_checkpoint(50, 25);
                        checkpoint.workflow_id = format!("workflow-to-delete-{}", i);
                        checkpoint
                    })
                    .collect();
                (manager, checkpoints, temp_dir)
            },
            |(manager, checkpoints, _temp_dir)| async move {
                // Create the checkpoints first
                let workflow_ids: Vec<_> =
                    checkpoints.iter().map(|c| c.workflow_id.clone()).collect();
                for checkpoint in checkpoints {
                    manager.save_checkpoint(&checkpoint).await.unwrap();
                }
                // Now benchmark the delete operations
                for workflow_id in workflow_ids {
                    manager.delete_checkpoint(&workflow_id).await.unwrap();
                }
            },
            BatchSize::SmallInput,
        );
    });
}

fn bench_mapreduce_checkpoint_overhead(c: &mut Criterion) {
    use prodigy::cook::execution::mapreduce::checkpoint::{
        AgentState, CheckpointConfig, CheckpointManager as MapReduceCheckpointManager,
        CheckpointMetadata, CheckpointReason, CompressionAlgorithm, ErrorState,
        ExecutionState as MapReduceExecutionState, FileCheckpointStorage, MapReduceCheckpoint,
        PhaseType, ResourceState, VariableState, WorkItemState,
    };
    use std::collections::HashMap;

    let rt = Runtime::new().unwrap();

    // Helper function to create a MapReduce checkpoint
    let create_mr_checkpoint = |num_items: usize| -> MapReduceCheckpoint {
        MapReduceCheckpoint {
            metadata: CheckpointMetadata {
                checkpoint_id: "test-mr-checkpoint".to_string(),
                job_id: "test-job".to_string(),
                version: 1,
                created_at: chrono::Utc::now(),
                phase: PhaseType::Map,
                total_work_items: num_items,
                completed_items: num_items / 2,
                checkpoint_reason: CheckpointReason::Interval,
                integrity_hash: String::new(),
            },
            execution_state: MapReduceExecutionState {
                current_phase: PhaseType::Map,
                phase_start_time: chrono::Utc::now(),
                setup_results: None,
                map_results: None,
                reduce_results: None,
                workflow_variables: HashMap::new(),
            },
            work_item_state: WorkItemState {
                pending_items: vec![],
                in_progress_items: HashMap::new(),
                completed_items: vec![],
                failed_items: vec![],
                current_batch: None,
            },
            agent_state: AgentState {
                active_agents: HashMap::new(),
                agent_assignments: HashMap::new(),
                agent_results: HashMap::new(),
                resource_allocation: HashMap::new(),
            },
            variable_state: VariableState {
                workflow_variables: HashMap::new(),
                captured_outputs: HashMap::new(),
                environment_variables: HashMap::new(),
                item_variables: HashMap::new(),
            },
            resource_state: ResourceState {
                total_agents_allowed: 10,
                current_agents_active: 0,
                worktrees_created: vec![],
                worktrees_cleaned: vec![],
                disk_usage_bytes: None,
            },
            error_state: ErrorState {
                error_count: 0,
                dlq_items: vec![],
                error_threshold_reached: false,
                last_error: None,
            },
        }
    };

    // Benchmark checkpoint creation overhead
    c.benchmark_group("mapreduce_checkpoint_overhead")
        .warm_up_time(Duration::from_secs(1))
        .measurement_time(Duration::from_secs(5))
        .bench_function("no_compression", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    let storage = Box::new(FileCheckpointStorage::with_compression(
                        temp_dir.path().to_path_buf(),
                        CompressionAlgorithm::None,
                    ));
                    let config = CheckpointConfig::default();
                    let manager =
                        MapReduceCheckpointManager::new(storage, config, "test-job".to_string());
                    let checkpoint = create_mr_checkpoint(1000);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    manager
                        .create_checkpoint(&checkpoint, CheckpointReason::Interval)
                        .await
                        .unwrap();
                },
                BatchSize::SmallInput,
            );
        })
        .bench_function("gzip_compression", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    let storage = Box::new(FileCheckpointStorage::with_compression(
                        temp_dir.path().to_path_buf(),
                        CompressionAlgorithm::Gzip,
                    ));
                    let config = CheckpointConfig::default();
                    let manager =
                        MapReduceCheckpointManager::new(storage, config, "test-job".to_string());
                    let checkpoint = create_mr_checkpoint(1000);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    manager
                        .create_checkpoint(&checkpoint, CheckpointReason::Interval)
                        .await
                        .unwrap();
                },
                BatchSize::SmallInput,
            );
        })
        .bench_function("zstd_compression", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    let storage = Box::new(FileCheckpointStorage::with_compression(
                        temp_dir.path().to_path_buf(),
                        CompressionAlgorithm::Zstd,
                    ));
                    let config = CheckpointConfig::default();
                    let manager =
                        MapReduceCheckpointManager::new(storage, config, "test-job".to_string());
                    let checkpoint = create_mr_checkpoint(1000);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    manager
                        .create_checkpoint(&checkpoint, CheckpointReason::Interval)
                        .await
                        .unwrap();
                },
                BatchSize::SmallInput,
            );
        })
        .bench_function("lz4_compression", |b| {
            b.to_async(&rt).iter_batched(
                || {
                    let temp_dir = TempDir::new().unwrap();
                    let storage = Box::new(FileCheckpointStorage::with_compression(
                        temp_dir.path().to_path_buf(),
                        CompressionAlgorithm::Lz4,
                    ));
                    let config = CheckpointConfig::default();
                    let manager =
                        MapReduceCheckpointManager::new(storage, config, "test-job".to_string());
                    let checkpoint = create_mr_checkpoint(1000);
                    (manager, checkpoint, temp_dir)
                },
                |(manager, checkpoint, _temp_dir)| async move {
                    manager
                        .create_checkpoint(&checkpoint, CheckpointReason::Interval)
                        .await
                        .unwrap();
                },
                BatchSize::SmallInput,
            );
        });
}

criterion_group!(
    benches,
    bench_checkpoint_save,
    bench_checkpoint_load,
    bench_checkpoint_atomic_write,
    bench_checkpoint_list,
    bench_checkpoint_delete,
    bench_mapreduce_checkpoint_overhead
);

criterion_main!(benches);