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
# Stillwater Validation Migration Guide

This document provides before/after examples demonstrating the migration of Prodigy's validation code to use the stillwater library's error accumulation patterns.

## Overview

The migration to stillwater validation brings several key improvements:

- **Error Accumulation**: Collect all validation errors before failing, instead of stopping at the first error
- **Composable Validators**: Build complex validators from simple, reusable functions
- **Pure Functions**: Separate validation logic from I/O operations for better testability
- **Consistent Error Handling**: Uniform error types and severity classification across the codebase

## Migration Patterns

### Pattern 1: Single Field Validation with Early Return (Before)

**Before (Imperative Style)**:
```rust
fn validate_path(path: &Path) -> Result<PathBuf, ValidationError> {
    if !path.exists() {
        return Err(ValidationError::PathNotFound(path.to_path_buf()));
    }

    if path.starts_with("..") {
        return Err(ValidationError::PathInParentDir(path.to_path_buf()));
    }

    Ok(path.to_path_buf())
}

// Only reports FIRST error
fn validate_all_paths(paths: &[&Path]) -> Result<Vec<PathBuf>, ValidationError> {
    let mut results = Vec::new();
    for path in paths {
        results.push(validate_path(path)?); // Stops at first error
    }
    Ok(results)
}
```

**After (Stillwater Functional Style)**:
```rust
use stillwater::Validation;

fn validate_path(
    path: &Path,
    exists_check: FileExistsCheck,
) -> Validation<PathBuf, Vec<ValidationError>> {
    let mut errors = Vec::new();

    if !exists_check(path) {
        errors.push(ValidationError::PathNotFound(path.to_path_buf()));
    }

    if path.starts_with("..") {
        errors.push(ValidationError::PathInParentDir(path.to_path_buf()));
    }

    if errors.is_empty() {
        Validation::success(path.to_path_buf())
    } else {
        Validation::failure(errors)
    }
}

// Accumulates ALL errors from ALL paths
pub fn validate_paths(paths: &[&Path], exists_check: FileExistsCheck) -> ValidationResult {
    let mut all_errors = Vec::new();
    let mut all_paths = Vec::new();

    for &path in paths {
        match validate_path(path, exists_check).into_result() {
            Ok(p) => all_paths.push(p),
            Err(errors) => all_errors.extend(errors), // Collect ALL errors
        }
    }

    let validation = if all_errors.is_empty() {
        Validation::success(all_paths)
    } else {
        Validation::failure(all_errors)
    };

    ValidationResult::from_validation(validation)
}
```

**Benefits**:
- Reports ALL path errors at once instead of just the first one
- Separates I/O (file existence check) from pure validation logic
- Allows testing without touching the filesystem

### Pattern 2: Multiple Independent Validations (Before)

**Before (Sequential with Early Returns)**:
```rust
fn validate_environment(required_vars: &[&str]) -> Result<(), Vec<String>> {
    let mut errors = Vec::new();

    for var in required_vars {
        if std::env::var(var).is_err() {
            errors.push(format!("Missing: {}", var));
        }
    }

    if !errors.is_empty() {
        return Err(errors);
    }

    Ok(())
}
```

**After (Stillwater Accumulation)**:
```rust
use stillwater::Validation;

fn validate_env_var(
    var_name: &str,
    env_vars: &HashMap<String, String>,
) -> Validation<(String, String), Vec<ValidationError>> {
    if let Some(value) = env_vars.get(var_name) {
        if (var_name.contains("KEY") || var_name.contains("SECRET"))
            && value.trim().is_empty()
        {
            Validation::failure(vec![ValidationError::EnvVarEmpty(var_name.to_string())])
        } else {
            Validation::success((var_name.to_string(), value.clone()))
        }
    } else {
        Validation::failure(vec![ValidationError::EnvVarMissing(var_name.to_string())])
    }
}

pub fn validate_environment(
    required_vars: &[&str],
    env_vars: &HashMap<String, String>,
) -> ValidationResult {
    let mut all_errors = Vec::new();
    let mut all_vars = Vec::new();

    for &var in required_vars {
        match validate_env_var(var, env_vars).into_result() {
            Ok(pair) => all_vars.push(pair),
            Err(errors) => all_errors.extend(errors),
        }
    }

    let validation = if all_errors.is_empty() {
        Validation::success(all_vars)
    } else {
        Validation::failure(all_errors)
    };

    ValidationResult::from_validation(validation)
}
```

**Benefits**:
- Validates ALL environment variables, not just until first failure
- Separates env var access from validation logic (accepts HashMap parameter)
- Distinguishes between missing vars (errors) and empty sensitive vars (warnings)

### Pattern 3: Nested Validation with Multiple Error Types

**Before (Mixed Error Types)**:
```rust
fn validate_command(command: &str) -> Result<(), String> {
    if command.trim().is_empty() {
        return Err("Command is empty".to_string());
    }

    if command.contains("rm -rf /") {
        return Err("Dangerous command pattern".to_string());
    }

    if command.contains("sudo") {
        eprintln!("Warning: Command uses sudo");
    }

    Ok(())
}
```

**After (Unified Error Handling)**:
```rust
use stillwater::Validation;

fn check_dangerous_patterns(command: &str) -> Validation<(), Vec<ValidationError>> {
    let dangerous_patterns = [
        "rm -rf /",
        "dd if=/dev/zero",
        ":(){ :|:& };:", // Fork bomb
    ];

    let errors: Vec<ValidationError> = dangerous_patterns
        .iter()
        .filter(|&&pattern| command.contains(pattern))
        .map(|&pattern| ValidationError::CommandDangerous {
            cmd: command.to_string(),
            pattern: pattern.to_string(),
        })
        .collect();

    if errors.is_empty() {
        Validation::success(())
    } else {
        Validation::failure(errors)
    }
}

fn check_suspicious_patterns(command: &str) -> Validation<(), Vec<ValidationError>> {
    let mut warnings = Vec::new();

    if command.contains("sudo") && !command.contains("sudo -n") {
        warnings.push(ValidationError::CommandSuspicious {
            cmd: command.to_string(),
            reason: "Command uses sudo which may require password".to_string(),
        });
    }

    if warnings.is_empty() {
        Validation::success(())
    } else {
        Validation::failure(warnings)
    }
}

pub fn validate_command(command: &str) -> ValidationResult {
    if command.trim().is_empty() {
        return ValidationResult::from_validation(
            Validation::<(), Vec<ValidationError>>::failure(vec![
                ValidationError::CommandEmpty,
            ])
        );
    }

    let mut all_errors = Vec::new();

    // Accumulate errors from multiple checks
    if let Err(errors) = check_dangerous_patterns(command).into_result() {
        all_errors.extend(errors);
    }

    if let Err(errors) = check_suspicious_patterns(command).into_result() {
        all_errors.extend(errors);
    }

    let validation = if all_errors.is_empty() {
        Validation::success(())
    } else {
        Validation::failure(all_errors)
    };

    ValidationResult::from_validation(validation)
}
```

**Benefits**:
- Finds ALL dangerous and suspicious patterns in a single pass
- Separates error severity (errors vs warnings) through error classification
- Composable validators can be tested independently
- Consistent error types instead of ad-hoc strings

### Pattern 4: Resource Limit Validation

**Before (Separate Functions)**:
```rust
fn validate_memory(memory_mb: usize) -> Result<(), String> {
    if memory_mb == 0 {
        return Err("Memory cannot be zero".to_string());
    }
    if memory_mb < 128 {
        eprintln!("Warning: Low memory limit");
    }
    Ok(())
}

fn validate_cpu(cpu_cores: usize) -> Result<(), String> {
    if cpu_cores == 0 {
        return Err("CPU cores cannot be zero".to_string());
    }
    Ok(())
}

fn validate_limits(limits: &ResourceLimits) -> Result<(), Vec<String>> {
    let mut errors = Vec::new();

    if let Err(e) = validate_memory(limits.memory_mb) {
        errors.push(e);
    }

    if let Err(e) = validate_cpu(limits.cpu_cores) {
        errors.push(e);
    }

    if !errors.is_empty() {
        Err(errors)
    } else {
        Ok(())
    }
}
```

**After (Composed Validation)**:
```rust
use stillwater::Validation;

fn validate_memory_limit(memory_mb: usize) -> Validation<usize, Vec<ValidationError>> {
    let mut errors = Vec::new();

    if memory_mb == 0 {
        errors.push(ValidationError::MemoryLimitZero);
    } else if memory_mb < 128 {
        errors.push(ValidationError::MemoryLimitLow(memory_mb));
    } else if memory_mb > 32768 {
        errors.push(ValidationError::MemoryLimitHigh(memory_mb));
    }

    if errors.is_empty() {
        Validation::success(memory_mb)
    } else {
        Validation::failure(errors)
    }
}

fn validate_cpu_cores(cpu_cores: usize) -> Validation<usize, Vec<ValidationError>> {
    let mut errors = Vec::new();

    if cpu_cores == 0 {
        errors.push(ValidationError::CpuCoresZero);
    } else if cpu_cores > 64 {
        errors.push(ValidationError::CpuCoresHigh(cpu_cores));
    }

    if errors.is_empty() {
        Validation::success(cpu_cores)
    } else {
        Validation::failure(errors)
    }
}

pub fn validate_resource_limits(limits: &ResourceLimits) -> ValidationResult {
    let mut all_errors = Vec::new();

    // Accumulate errors from all validators
    if let Err(errors) = validate_memory_limit(limits.memory_mb).into_result() {
        all_errors.extend(errors);
    }

    if let Err(errors) = validate_cpu_cores(limits.cpu_cores).into_result() {
        all_errors.extend(errors);
    }

    if let Err(errors) = validate_timeout(limits.timeout_seconds).into_result() {
        all_errors.extend(errors);
    }

    let validation = if all_errors.is_empty() {
        Validation::success(())
    } else {
        Validation::failure(all_errors)
    };

    ValidationResult::from_validation(validation)
}
```

**Benefits**:
- Reports ALL resource limit issues at once
- Structured error types (not strings) enable programmatic handling
- Warnings and errors handled through error severity classification
- Each validator is independently testable

## Error Severity Classification

Stillwater validation introduces error severity classification:

```rust
#[derive(Debug, Clone, PartialEq)]
pub enum ErrorSeverity {
    Error,   // Must be fixed
    Warning, // Should review but not blocking
}

impl ValidationError {
    pub fn severity(&self) -> ErrorSeverity {
        match self {
            // Warnings
            Self::PathInParentDir(_)
            | Self::PathInTempDir(_)
            | Self::EnvVarEmpty(_)
            | Self::CommandSuspicious { .. }
            | Self::IterationCountHigh(_)
            | Self::MemoryLimitLow(_) => ErrorSeverity::Warning,

            // Errors
            _ => ErrorSeverity::Error,
        }
    }
}
```

**Usage Example**:
```rust
let result = validate_paths(&paths, exists_check);

// Separate errors and warnings
for error in result.errors {
    eprintln!("Error: {}", error);
}

for warning in result.warnings {
    eprintln!("Warning: {}", warning);
}

// Can proceed with warnings, but not errors
if !result.errors.is_empty() {
    return Err("Validation failed");
}
```

## Testing Benefits

**Before (Requires I/O)**:
```rust
#[test]
fn test_validate_path() {
    let temp_file = create_temp_file(); // Requires file system
    let result = validate_path(&temp_file);
    assert!(result.is_ok());
}
```

**After (Pure Function)**:
```rust
#[test]
fn test_validate_path() {
    // Mock exists check - no I/O required
    fn mock_exists_check(path: &Path) -> bool {
        path.to_str().map(|s| s.contains("exists")).unwrap_or(false)
    }

    let path = Path::new("/tmp/exists.txt");
    let result = validate_path(path, mock_exists_check);
    assert!(result.is_success());
}

#[test]
fn test_validate_multiple_errors() {
    let paths = vec![
        Path::new("/missing1"),
        Path::new("/missing2"),
        Path::new("/missing3"),
    ];

    let result = validate_paths(&paths, |_| false);

    // Accumulates ALL errors
    assert_eq!(result.errors.len(), 3);
}
```

## Performance Considerations

The stillwater validation migration maintains zero performance regression:

- **No allocation overhead**: Error accumulation uses `Vec` which is already allocated
- **No additional traversals**: Validation happens in a single pass over the data
- **Inline-friendly**: Small validators are optimized by the compiler
- **Benchmark verification**: See `benches/execution_benchmarks.rs::bench_validation_performance`

Run benchmarks to verify:
```bash
cargo bench --bench execution_benchmarks -- validation_performance
```

## Migration Checklist

When migrating validation code to stillwater:

1. **Identify validation functions** that return early on first error
2. **Extract I/O operations** as function parameters (e.g., `FileExistsCheck`)
3. **Replace early returns** with error accumulation in `Vec<ValidationError>`
4. **Use `Validation<T, Vec<ValidationError>>`** as return type
5. **Compose validators** by collecting errors from multiple validators
6. **Classify errors** using `ErrorSeverity` for warnings vs errors
7. **Convert to `ValidationResult`** for backward compatibility
8. **Add tests** that verify error accumulation (not just first error)

## Key Takeaways

The stillwater validation migration provides:

- **Better UX**: Users see ALL errors at once, not just the first one
- **Better testing**: Pure functions with injected dependencies
- **Better composition**: Build complex validators from simple ones
- **Better consistency**: Uniform error types across the codebase
- **Zero regression**: Maintains performance while improving functionality

This migration is part of Prodigy's broader adoption of functional programming patterns to improve code quality, testability, and maintainability.