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
//! Integration tests for MapReduce environment variable execution
//!
//! These tests verify that environment variables are correctly resolved
//! and passed during actual workflow execution, not just parsing.

use anyhow::Result;
use prodigy::config::parse_mapreduce_workflow;

#[test]
fn test_numeric_field_env_interpolation() -> Result<()> {
    let workflow_yaml = r#"
name: test-numeric-env
mode: mapreduce

env:
  MAX_PARALLEL: "5"
  TIMEOUT_SECONDS: "900"

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: ${MAX_PARALLEL}
  agent_timeout_secs: ${TIMEOUT_SECONDS}

  agent_template:
    - shell: "echo test"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Verify env block contains our variables
    assert!(config.env.is_some());
    let env = config.env.as_ref().unwrap();
    assert_eq!(env.get("MAX_PARALLEL"), Some(&"5".to_string()));
    assert_eq!(env.get("TIMEOUT_SECONDS"), Some(&"900".to_string()));

    // Verify numeric fields accept string values (for env var references)
    assert_eq!(config.map.max_parallel, "${MAX_PARALLEL}");
    assert_eq!(
        config.map.agent_timeout_secs,
        Some("${TIMEOUT_SECONDS}".to_string())
    );

    // Verify we can convert to MapPhase and resolve variables
    let map_phase = config.to_map_phase()?;

    // After resolution, should be numeric values
    assert_eq!(map_phase.config.max_parallel, 5);
    assert_eq!(map_phase.config.agent_timeout_secs, Some(900));

    Ok(())
}

#[test]
fn test_numeric_field_direct_values() -> Result<()> {
    let workflow_yaml = r#"
name: test-numeric-direct
mode: mapreduce

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: 3
  agent_timeout_secs: 600

  agent_template:
    - shell: "echo test"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Verify numeric fields accept direct numeric values too
    assert_eq!(config.map.max_parallel, "3");
    assert_eq!(config.map.agent_timeout_secs, Some("600".to_string()));

    // Verify we can convert to MapPhase
    let map_phase = config.to_map_phase()?;

    // Should parse to numeric values
    assert_eq!(map_phase.config.max_parallel, 3);
    assert_eq!(map_phase.config.agent_timeout_secs, Some(600));

    Ok(())
}

#[test]
fn test_env_var_resolution_error_handling() -> Result<()> {
    let workflow_yaml = r#"
name: test-missing-var
mode: mapreduce

env:
  EXISTING_VAR: "10"

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: ${MISSING_VAR}  # This variable is not defined

  agent_template:
    - shell: "echo test"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Should parse successfully
    assert_eq!(config.map.max_parallel, "${MISSING_VAR}");

    // But conversion to MapPhase should fail with clear error
    let result = config.to_map_phase();
    assert!(result.is_err());

    let error = result.unwrap_err();
    let error_msg = format!("{:#}", error);
    assert!(error_msg.contains("MISSING_VAR"));
    assert!(error_msg.contains("not found"));

    Ok(())
}

#[test]
fn test_env_var_invalid_numeric_value() -> Result<()> {
    let workflow_yaml = r#"
name: test-invalid-numeric
mode: mapreduce

env:
  MAX_PARALLEL: "not-a-number"

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: ${MAX_PARALLEL}

  agent_template:
    - shell: "echo test"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Conversion to MapPhase should fail with parse error
    let result = config.to_map_phase();
    assert!(result.is_err());

    let error = result.unwrap_err();
    let error_msg = format!("{:#}", error);
    assert!(error_msg.contains("MAX_PARALLEL"));
    assert!(error_msg.contains("not-a-number") || error_msg.contains("parse"));

    Ok(())
}

#[test]
fn test_both_env_var_syntaxes() -> Result<()> {
    let workflow_yaml = r#"
name: test-syntax-variants
mode: mapreduce

env:
  VAR1: "5"
  VAR2: "600"

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: $VAR1           # Shell-style without braces
  agent_timeout_secs: ${VAR2}   # Bracketed style

  agent_template:
    - shell: "echo test"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Both syntaxes should be accepted
    assert_eq!(config.map.max_parallel, "$VAR1");
    assert_eq!(config.map.agent_timeout_secs, Some("${VAR2}".to_string()));

    // Both should resolve correctly
    let map_phase = config.to_map_phase()?;
    assert_eq!(map_phase.config.max_parallel, 5);
    assert_eq!(map_phase.config.agent_timeout_secs, Some(600));

    Ok(())
}

// Note: test_system_env_fallback and test_workflow_env_overrides_system were removed.
// These tests manipulated global environment variables which is not thread-safe.
// The workflow env variable resolution is already tested through workflow-defined
// env blocks (without system env fallback). If system env fallback needs testing,
// the production code should be refactored to accept a ConfigEnv abstraction.

#[test]
fn test_optional_timeout_field() -> Result<()> {
    let workflow_yaml = r#"
name: test-optional-timeout
mode: mapreduce

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: 3
  # agent_timeout_secs is optional - not specified

  agent_template:
    - shell: "echo test"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Optional field should be None
    assert_eq!(config.map.agent_timeout_secs, None);

    // Should convert successfully
    let map_phase = config.to_map_phase()?;
    assert_eq!(map_phase.config.agent_timeout_secs, None);

    Ok(())
}

/// Test that environment variables work correctly in shell commands
#[test]
fn test_env_vars_in_setup_commands() -> Result<()> {
    let workflow_yaml = r#"
name: test-setup-env
mode: mapreduce

env:
  PROJECT_NAME: "my-project"
  OUTPUT_DIR: "output"

setup:
  - shell: "echo $PROJECT_NAME"
  - shell: "mkdir -p $OUTPUT_DIR"

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: 1

  agent_template:
    - shell: "echo test"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Verify setup commands contain env var references
    assert!(config.setup.is_some());
    let setup = config.setup.as_ref().unwrap();
    assert_eq!(setup.commands.len(), 2);

    // Verify the shell commands contain variable references
    // (actual execution will substitute them)
    let cmd1 = &setup.commands[0];
    assert!(cmd1.shell.is_some());
    assert!(cmd1.shell.as_ref().unwrap().contains("$PROJECT_NAME"));

    let cmd2 = &setup.commands[1];
    assert!(cmd2.shell.is_some());
    assert!(cmd2.shell.as_ref().unwrap().contains("$OUTPUT_DIR"));

    Ok(())
}

/// Integration test combining numeric field interpolation with command env vars
#[test]
fn test_complete_env_workflow() -> Result<()> {
    let workflow_yaml = r#"
name: test-complete-env
mode: mapreduce

env:
  PROJECT_NAME: "complete-test"
  MAX_PARALLEL: "4"
  TIMEOUT_SECONDS: "300"
  OUTPUT_DIR: "results"

setup:
  - shell: "mkdir -p $OUTPUT_DIR"
  - shell: "echo Starting $PROJECT_NAME"

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: ${MAX_PARALLEL}
  agent_timeout_secs: ${TIMEOUT_SECONDS}

  agent_template:
    - shell: "echo Processing ${item.name} for $PROJECT_NAME"
    - shell: "echo ${item.name} > $OUTPUT_DIR/${item.name}.txt"

reduce:
  - shell: "echo Completed $PROJECT_NAME"
  - shell: "cat $OUTPUT_DIR/*.txt > $OUTPUT_DIR/summary.txt"

merge:
  commands:
    - shell: "echo Merging $PROJECT_NAME results"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Verify environment variables
    assert!(config.env.is_some());
    let env = config.env.as_ref().unwrap();
    assert_eq!(env.len(), 4);

    // Verify numeric fields can be resolved
    let map_phase = config.to_map_phase()?;
    assert_eq!(map_phase.config.max_parallel, 4);
    assert_eq!(map_phase.config.agent_timeout_secs, Some(300));

    // Verify all phases have commands with env var references
    assert!(config.setup.is_some());
    assert_eq!(config.setup.as_ref().unwrap().commands.len(), 2);
    assert_eq!(map_phase.agent_template.len(), 2);
    assert!(config.reduce.is_some());
    assert_eq!(config.reduce.as_ref().unwrap().commands.len(), 2);
    assert!(config.merge.is_some());
    assert_eq!(config.merge.as_ref().unwrap().commands.len(), 1);

    Ok(())
}

/// Test that environment variables are interpolated in map.input field
#[test]
fn test_map_input_env_interpolation() -> Result<()> {
    let workflow_yaml = r#"
name: test-map-input-env
mode: mapreduce

env:
  DATA_FILE: "workflows/data/items.json"
  PROJECT_NAME: "test-project"

map:
  input: ${DATA_FILE}  # Should be interpolated to actual file path
  json_path: "$.items[*]"
  max_parallel: 1

  agent_template:
    - shell: "echo ${item.name}"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Verify env variables are defined
    assert!(config.env.is_some());
    let env = config.env.as_ref().unwrap();
    assert_eq!(
        env.get("DATA_FILE"),
        Some(&"workflows/data/items.json".to_string())
    );

    // Verify map.input contains the variable reference (before interpolation)
    assert_eq!(config.map.input, "${DATA_FILE}");

    // NOTE: The actual interpolation happens at runtime in WorkflowExecutor
    // when it populates workflow_context.variables and interpolates the input.
    // This test verifies the workflow parses correctly with env var references.

    Ok(())
}

/// Test that environment variables work in shell commands
#[test]
fn test_shell_command_env_vars() -> Result<()> {
    let workflow_yaml = r#"
name: test-shell-env
mode: mapreduce

env:
  OUTPUT_DIR: "test-output"
  PROJECT_NAME: "my-project"

setup:
  - shell: "mkdir -p $OUTPUT_DIR"
  - shell: "echo Starting $PROJECT_NAME"

map:
  input: "items.json"
  json_path: "$.items[*]"
  max_parallel: 1

  agent_template:
    - shell: "echo Processing for $PROJECT_NAME"
"#;

    // Parse the workflow
    let config = parse_mapreduce_workflow(workflow_yaml)?;

    // Verify environment variables
    assert!(config.env.is_some());
    let env = config.env.as_ref().unwrap();
    assert_eq!(env.get("OUTPUT_DIR"), Some(&"test-output".to_string()));
    assert_eq!(env.get("PROJECT_NAME"), Some(&"my-project".to_string()));

    // Verify setup commands contain variable references
    // (the shell will expand these when environment variables are set)
    assert!(config.setup.is_some());
    let setup = config.setup.as_ref().unwrap();

    let cmd1 = &setup.commands[0];
    assert!(cmd1.shell.as_ref().unwrap().contains("$OUTPUT_DIR"));

    let cmd2 = &setup.commands[1];
    assert!(cmd2.shell.as_ref().unwrap().contains("$PROJECT_NAME"));

    Ok(())
}