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
//! Integration tests for write_file command in MapReduce workflows
//! Verifies that write_file command works correctly in map and reduce phases

use anyhow::Result;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Helper to create a test MapReduce workflow with write_file commands
fn create_test_workflow(temp_dir: &TempDir) -> PathBuf {
    let workflow_path = temp_dir.path().join("test-write-file.yml");
    let workflow_content = r#"
name: test-write-file-mapreduce
mode: mapreduce

setup:
  - shell: "echo '{\"items\": [{\"id\": 1, \"name\": \"alice\"}, {\"id\": 2, \"name\": \"bob\"}]}' > items.json"

map:
  input: "items.json"
  json_path: "$.items[*]"
  agent_template:
    - write_file:
        path: "output/${item.name}.json"
        content: '{"user_id": ${item.id}, "user_name": "${item.name}", "processed": true}'
        format: json
        create_dirs: true
  max_parallel: 2

reduce:
  - write_file:
      path: "summary.txt"
      content: "Processed ${map.total} users successfully"
      format: text
"#;

    fs::write(&workflow_path, workflow_content).expect("Failed to write workflow");
    workflow_path
}

/// Helper to create workflow with YAML output
fn create_yaml_workflow(temp_dir: &TempDir) -> PathBuf {
    let workflow_path = temp_dir.path().join("test-write-yaml.yml");
    let workflow_content = r#"
name: test-write-yaml
mode: mapreduce

setup:
  - shell: "echo '{\"configs\": [{\"env\": \"dev\", \"port\": 3000}, {\"env\": \"prod\", \"port\": 8080}]}' > configs.json"

map:
  input: "configs.json"
  json_path: "$.configs[*]"
  agent_template:
    - write_file:
        path: "configs/${item.env}.yml"
        content: |
          environment: ${item.env}
          server:
            port: ${item.port}
            host: localhost
        format: yaml
        create_dirs: true
  max_parallel: 2

reduce:
  - shell: "echo 'Generated ${map.total} config files'"
"#;

    fs::write(&workflow_path, workflow_content).expect("Failed to write workflow");
    workflow_path
}

#[test]
fn test_mapreduce_write_file_json_in_map_phase() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_test_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify map phase has write_file command
    let map = config.get("map").expect("Should have map phase");
    let agent_template = map
        .get("agent_template")
        .expect("Should have agent_template")
        .as_sequence()
        .expect("agent_template should be sequence");

    let write_cmd = &agent_template[0];
    assert!(
        write_cmd.get("write_file").is_some(),
        "First command should be write_file"
    );

    // Verify write_file configuration
    let write_config = write_cmd.get("write_file").unwrap();
    assert_eq!(
        write_config.get("path").and_then(|v| v.as_str()),
        Some("output/${item.name}.json")
    );
    assert_eq!(
        write_config.get("format").and_then(|v| v.as_str()),
        Some("json")
    );
    assert_eq!(
        write_config.get("create_dirs").and_then(|v| v.as_bool()),
        Some(true)
    );

    Ok(())
}

#[test]
fn test_mapreduce_write_file_text_in_reduce_phase() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_test_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify reduce phase has write_file command
    let reduce = config
        .get("reduce")
        .expect("Should have reduce phase")
        .as_sequence()
        .expect("reduce should be sequence");

    let write_cmd = &reduce[0];
    assert!(
        write_cmd.get("write_file").is_some(),
        "Reduce command should be write_file"
    );

    // Verify write_file configuration for summary
    let write_config = write_cmd.get("write_file").unwrap();
    assert_eq!(
        write_config.get("path").and_then(|v| v.as_str()),
        Some("summary.txt")
    );
    assert_eq!(
        write_config.get("format").and_then(|v| v.as_str()),
        Some("text")
    );

    // Verify it uses map variables
    let content = write_config
        .get("content")
        .and_then(|v| v.as_str())
        .unwrap();
    assert!(
        content.contains("${map.total}"),
        "Should use map.total variable"
    );

    Ok(())
}

#[test]
fn test_mapreduce_write_file_yaml_format() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_yaml_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify map phase has write_file with YAML format
    let map = config.get("map").expect("Should have map phase");
    let agent_template = map
        .get("agent_template")
        .expect("Should have agent_template")
        .as_sequence()
        .expect("agent_template should be sequence");

    let write_cmd = &agent_template[0];
    let write_config = write_cmd.get("write_file").unwrap();

    assert_eq!(
        write_config.get("format").and_then(|v| v.as_str()),
        Some("yaml")
    );

    // Verify YAML content structure
    let content = write_config
        .get("content")
        .and_then(|v| v.as_str())
        .unwrap();
    assert!(content.contains("environment:"));
    assert!(content.contains("server:"));
    assert!(content.contains("port:"));

    Ok(())
}

#[test]
fn test_write_file_with_create_dirs_option() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_test_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify create_dirs is properly configured
    let map = config.get("map").unwrap();
    let agent_template = map.get("agent_template").unwrap().as_sequence().unwrap();
    let write_cmd = &agent_template[0];
    let write_config = write_cmd.get("write_file").unwrap();

    // Verify create_dirs is true
    assert_eq!(
        write_config.get("create_dirs").and_then(|v| v.as_bool()),
        Some(true),
        "create_dirs should be enabled for nested paths"
    );

    // Verify path uses nested directory
    let path = write_config.get("path").and_then(|v| v.as_str()).unwrap();
    assert!(
        path.contains("/"),
        "Path should contain directory separator"
    );

    Ok(())
}

#[test]
fn test_write_file_variable_interpolation() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_test_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify variable interpolation in path
    let map = config.get("map").unwrap();
    let agent_template = map.get("agent_template").unwrap().as_sequence().unwrap();
    let write_cmd = &agent_template[0];
    let write_config = write_cmd.get("write_file").unwrap();

    let path = write_config.get("path").and_then(|v| v.as_str()).unwrap();
    assert!(
        path.contains("${item.name}"),
        "Path should use item variable"
    );

    // Verify variable interpolation in content
    let content = write_config
        .get("content")
        .and_then(|v| v.as_str())
        .unwrap();
    assert!(
        content.contains("${item.id}"),
        "Content should use item.id variable"
    );
    assert!(
        content.contains("${item.name}"),
        "Content should use item.name variable"
    );

    Ok(())
}

#[test]
fn test_write_file_workflow_structure() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_test_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify workflow mode
    assert_eq!(
        config.get("mode").and_then(|v| v.as_str()),
        Some("mapreduce")
    );

    // Verify all required phases exist
    assert!(config.get("setup").is_some(), "Should have setup phase");
    assert!(config.get("map").is_some(), "Should have map phase");
    assert!(config.get("reduce").is_some(), "Should have reduce phase");

    // Verify setup prepares input file
    let setup = config.get("setup").unwrap().as_sequence().unwrap();
    let setup_cmd = setup[0].get("shell").unwrap().as_str().unwrap();
    assert!(
        setup_cmd.contains("items.json"),
        "Setup should create input"
    );

    Ok(())
}

/// Helper to create workflow using map.results in reduce phase
fn create_map_results_workflow(temp_dir: &TempDir) -> PathBuf {
    let workflow_path = temp_dir.path().join("test-map-results.yml");
    let workflow_content = r#"
name: test-map-results
mode: mapreduce

setup:
  - shell: "echo '{\"tasks\": [{\"id\": \"task-1\", \"priority\": 1}, {\"id\": \"task-2\", \"priority\": 2}]}' > tasks.json"

map:
  input: "tasks.json"
  json_path: "$.tasks[*]"
  agent_template:
    - shell: "echo 'Processed ${item.id} with priority ${item.priority}'"
  max_parallel: 2

reduce:
  - write_file:
      path: "results.json"
      content: "${map.results}"
      format: json
  - write_file:
      path: "summary.txt"
      content: "Total: ${map.total}, Successful: ${map.successful}, Failed: ${map.failed}"
      format: text
"#;

    fs::write(&workflow_path, workflow_content).expect("Failed to write workflow");
    workflow_path
}

#[test]
fn test_mapreduce_write_file_map_results() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_map_results_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify reduce phase has write_file commands
    let reduce = config
        .get("reduce")
        .expect("Should have reduce phase")
        .as_sequence()
        .expect("reduce should be sequence");

    assert_eq!(reduce.len(), 2, "Should have 2 reduce commands");

    // First reduce command: write map.results to JSON file
    let results_write = &reduce[0];
    assert!(
        results_write.get("write_file").is_some(),
        "First reduce command should be write_file"
    );

    let results_config = results_write.get("write_file").unwrap();
    assert_eq!(
        results_config.get("path").and_then(|v| v.as_str()),
        Some("results.json")
    );
    assert_eq!(
        results_config.get("format").and_then(|v| v.as_str()),
        Some("json")
    );

    // Verify content uses ${map.results} variable
    let content = results_config
        .get("content")
        .and_then(|v| v.as_str())
        .unwrap();
    assert_eq!(
        content, "${map.results}",
        "Content should use map.results variable"
    );

    // Second reduce command: write summary with scalar variables
    let summary_write = &reduce[1];
    assert!(
        summary_write.get("write_file").is_some(),
        "Second reduce command should be write_file"
    );

    let summary_config = summary_write.get("write_file").unwrap();
    assert_eq!(
        summary_config.get("path").and_then(|v| v.as_str()),
        Some("summary.txt")
    );
    assert_eq!(
        summary_config.get("format").and_then(|v| v.as_str()),
        Some("text")
    );

    // Verify it uses map scalar variables
    let summary_content = summary_config
        .get("content")
        .and_then(|v| v.as_str())
        .unwrap();
    assert!(
        summary_content.contains("${map.total}"),
        "Should use map.total variable"
    );
    assert!(
        summary_content.contains("${map.successful}"),
        "Should use map.successful variable"
    );
    assert!(
        summary_content.contains("${map.failed}"),
        "Should use map.failed variable"
    );

    Ok(())
}

#[test]
fn test_write_file_supports_large_variables() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let workflow_path = create_map_results_workflow(&temp_dir);

    // Parse the workflow
    let workflow_yaml = fs::read_to_string(&workflow_path)?;
    let config: serde_yaml::Value = serde_yaml::from_str(&workflow_yaml)?;

    // Verify workflow structure supports large variables like map.results
    // This test documents that write_file is the correct command for large variables
    // (as opposed to shell commands which would hit E2BIG errors)

    let reduce = config.get("reduce").unwrap().as_sequence().unwrap();
    let results_write = &reduce[0];
    let results_config = results_write.get("write_file").unwrap();

    // Verify using write_file for map.results (not shell)
    assert!(
        results_write.get("write_file").is_some(),
        "Should use write_file for large variables, not shell"
    );

    // Verify JSON format for structured data
    assert_eq!(
        results_config.get("format").and_then(|v| v.as_str()),
        Some("json"),
        "Should use JSON format for structured map.results"
    );

    Ok(())
}