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
// Tests for dry-run functionality across commands

use super::test_utils::*;
use std::fs;
use std::path::Path;
use tempfile::TempDir;

/// Test that run --dry-run doesn't execute commands
#[test]
fn test_run_dry_run_no_execution() {
    let test = CliTest::new();

    // Create a workflow that would create a file if executed
    let workflow_content = r#"
name: dry-run-test
commands:
  - shell: "touch /tmp/test_dry_run_marker.txt"
  - shell: "echo 'This should not be executed'"
"#;
    let (test, workflow_path) = test.with_workflow("dry_run", workflow_content);

    // Ensure the marker file doesn't exist
    let marker_path = "/tmp/test_dry_run_marker.txt";
    let _ = fs::remove_file(marker_path);

    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("[DRY RUN]"));

    // Verify the file was not created
    assert!(
        !Path::new(marker_path).exists(),
        "Commands should not execute in dry-run mode"
    );
}

/// Test that run --dry-run shows what would be executed
#[test]
fn test_run_dry_run_output() {
    let test = CliTest::new();

    let workflow_content = r#"
name: dry-run-output-test
commands:
  - shell: "echo 'First command'"
  - shell: "echo 'Second command'"
  - claude: "/test-command"
"#;
    let (test, workflow_path) = test.with_workflow("dry_run_output", workflow_content);

    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("[DRY RUN]"));
    assert!(output.stdout_contains("Would execute"));
    assert!(output.stdout_contains("echo 'First command'"));
    assert!(output.stdout_contains("echo 'Second command'"));
    assert!(output.stdout_contains("/test-command"));
}

/// Test run --dry-run with iterations
#[test]
fn test_run_dry_run_with_iterations() {
    let test = CliTest::new();

    let workflow_content = r#"
name: dry-run-iterations
commands:
  - shell: "echo 'Iteration test'"
"#;
    let (test, workflow_path) = test.with_workflow("iterations", workflow_content);

    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .arg("-n")
        .arg("3")
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("[DRY RUN]"));
    assert!(output.stdout_contains("Would run 3 iterations"));
}

/// Test events clean --dry-run functionality
#[test]
fn test_events_clean_dry_run() {
    let test = CliTest::new();
    let temp_dir = TempDir::new().unwrap();
    let events_file = temp_dir.path().join("events.jsonl");

    // Create a test events file with old entries
    let old_timestamp = "2020-01-01T00:00:00Z";
    let recent_timestamp = chrono::Utc::now().to_rfc3339();

    let events_content = format!(
        r#"{{"timestamp":"{}","event":"old_event"}}
{{"timestamp":"{}","event":"recent_event"}}
"#,
        old_timestamp, recent_timestamp
    );

    fs::write(&events_file, events_content).unwrap();

    // Get initial file size
    let initial_size = fs::metadata(&events_file).unwrap().len();

    let output = test
        .arg("events")
        .arg("clean")
        .arg("--older-than")
        .arg("365d")
        .arg("--dry-run")
        .arg("--file")
        .arg(events_file.to_str().unwrap())
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("DRY RUN") || output.stdout_contains("dry run"));
    assert!(output.stdout_contains("Would"));

    // Verify file was not modified
    let current_size = fs::metadata(&events_file).unwrap().len();
    assert_eq!(
        initial_size, current_size,
        "File should not be modified in dry-run mode"
    );

    // Verify original content is intact
    let content = fs::read_to_string(&events_file).unwrap();
    assert!(content.contains("old_event"));
    assert!(content.contains("recent_event"));
}

/// Test events clean --dry-run with max events
#[test]
fn test_events_clean_dry_run_max_events() {
    let test = CliTest::new();
    let temp_dir = TempDir::new().unwrap();
    let events_file = temp_dir.path().join("events.jsonl");

    // Create a test events file with multiple entries
    let mut events_content = String::new();
    for i in 0..10 {
        let timestamp = chrono::Utc::now().to_rfc3339();
        events_content.push_str(&format!(
            r#"{{"timestamp":"{}","event":"event_{}"}}"#,
            timestamp, i
        ));
        events_content.push('\n');
    }

    fs::write(&events_file, &events_content).unwrap();

    let output = test
        .arg("events")
        .arg("clean")
        .arg("--max-events")
        .arg("5")
        .arg("--dry-run")
        .arg("--file")
        .arg(events_file.to_str().unwrap())
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("DRY RUN") || output.stdout_contains("dry run"));
    assert!(output.stdout_contains("events"));

    // Verify file was not modified
    let content_after = fs::read_to_string(&events_file).unwrap();
    assert_eq!(
        events_content, content_after,
        "File should not be modified in dry-run mode"
    );
}

/// Test events clean --dry-run with size limit
#[test]
fn test_events_clean_dry_run_max_size() {
    let test = CliTest::new();
    let temp_dir = TempDir::new().unwrap();
    let events_file = temp_dir.path().join("events.jsonl");

    // Create a test events file
    let mut events_content = String::new();
    for i in 0..100 {
        let timestamp = chrono::Utc::now().to_rfc3339();
        events_content.push_str(&format!(
            r#"{{"timestamp":"{}","event":"event_{}","data":"padding_data_to_increase_size"}}"#,
            timestamp, i
        ));
        events_content.push('\n');
    }

    fs::write(&events_file, &events_content).unwrap();
    let file_size = fs::metadata(&events_file).unwrap().len();

    let output = test
        .arg("events")
        .arg("clean")
        .arg("--max-size")
        .arg("1KB") // Very small limit to trigger cleanup
        .arg("--dry-run")
        .arg("--file")
        .arg(events_file.to_str().unwrap())
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("DRY RUN") || output.stdout_contains("dry run"));

    // Verify file size unchanged
    let current_size = fs::metadata(&events_file).unwrap().len();
    assert_eq!(
        file_size, current_size,
        "File size should not change in dry-run mode"
    );
}

/// Test run --dry-run with MapReduce workflow
#[test]
fn test_run_dry_run_mapreduce() {
    let test = CliTest::new();
    let temp_dir = TempDir::new().unwrap();
    let items_file = temp_dir.path().join("items.json");

    // Create test input file
    let items_content = r#"{"items": ["item1", "item2", "item3"]}"#;
    fs::write(&items_file, items_content).unwrap();

    let workflow_content = format!(
        r#"
name: mapreduce-dry-run
mode: mapreduce
map:
  input: "{}"
  json_path: "$.items[*]"
  agent_template:
    - shell: "echo 'Processing ${{item}}'"
  max_parallel: 2
reduce:
  - shell: "echo 'Reduce phase'"
"#,
        items_file.to_str().unwrap()
    );

    let (test, workflow_path) = test.with_workflow("mapreduce_dry", &workflow_content);

    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("[DRY RUN]"));
    assert!(output.stdout_contains("MapReduce"));
    assert!(output.stdout_contains("3 items") || output.stdout_contains("items: 3"));
}

/// Test run --dry-run doesn't create worktrees
#[test]
fn test_run_dry_run_no_worktree() {
    let test = CliTest::new();

    let workflow_content = r#"
name: worktree-dry-run
commands:
  - shell: "echo 'Test'"
"#;
    let (test, workflow_path) = test.with_workflow("worktree_dry", workflow_content);

    // Run with dry-run flag (worktrees are now always created by default)
    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .run();

    // In dry-run mode, the command should succeed but not create any worktrees
    if output.exit_code != exit_codes::SUCCESS {
        eprintln!("stdout: {}", output.stdout);
        eprintln!("stderr: {}", output.stderr);
        eprintln!("exit_code: {}", output.exit_code);
    }
    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("[DRY RUN]"));

    // Verify the dry run output shows worktree would be created but isn't actually created
    assert!(!output.stdout_contains("Created worktree"));
}

/// Test run --dry-run with validation steps
#[test]
fn test_run_dry_run_with_validation() {
    let test = CliTest::new();

    let workflow_content = r#"
name: validation-dry-run
commands:
  - shell: "echo 'Main command'"
    validate:
      command: "test -f /tmp/nonexistent"
      on_failure: "echo 'Validation would fail'"
"#;
    let (test, workflow_path) = test.with_workflow("validation_dry", workflow_content);

    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("[DRY RUN]"));
    assert!(output.stdout_contains("Main command"));
    assert!(output.stdout_contains("validate") || output.stdout_contains("Validation"));
}

/// Test events clean --dry-run with archiving
#[test]
fn test_events_clean_dry_run_archive() {
    let test = CliTest::new();
    let temp_dir = TempDir::new().unwrap();
    let events_file = temp_dir.path().join("events.jsonl");
    let archive_dir = temp_dir.path().join("archive");

    // Create test events
    let old_timestamp = "2020-01-01T00:00:00Z";
    let events_content = format!(r#"{{"timestamp":"{}","event":"old_event"}}"#, old_timestamp);
    fs::write(&events_file, events_content).unwrap();

    let output = test
        .arg("events")
        .arg("clean")
        .arg("--older-than")
        .arg("365d")
        .arg("--archive")
        .arg("--archive-path")
        .arg(archive_dir.to_str().unwrap())
        .arg("--dry-run")
        .arg("--file")
        .arg(events_file.to_str().unwrap())
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("DRY RUN") || output.stdout_contains("dry run"));
    assert!(output.stdout_contains("archive") || output.stdout_contains("Archive"));

    // Verify archive directory was not created
    assert!(
        !archive_dir.exists(),
        "Archive directory should not be created in dry-run mode"
    );
}

/// Test run --dry-run shows correct command count
#[test]
fn test_run_dry_run_command_count() {
    let test = CliTest::new();

    let workflow_content = r#"
name: command-count-test
commands:
  - shell: "echo 'Command 1'"
  - shell: "echo 'Command 2'"
  - shell: "echo 'Command 3'"
  - foreach:
      foreach: ["a", "b"]
      do:
        - shell: "echo 'Item ${item}'"
"#;
    let (test, workflow_path) = test.with_workflow("count_dry", workflow_content);

    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .run();

    assert_eq!(output.exit_code, exit_codes::SUCCESS);
    assert!(output.stdout_contains("[DRY RUN]"));
    // Should show 3 direct commands + 1 foreach block
    assert!(output.stdout_contains("commands") || output.stdout_contains("steps"));
}

/// Test that --dry-run conflicts with commit_required
#[test]
fn test_run_dry_run_commit_required_conflict() {
    let test = CliTest::new();

    let workflow_content = r#"
name: commit-required-test
commands:
  - shell: "echo 'test'"
    commit_required: true
"#;
    let (test, workflow_path) = test.with_workflow("commit_dry", workflow_content);

    let output = test
        .arg("run")
        .arg(workflow_path.to_str().unwrap())
        .arg("--dry-run")
        .run();

    // Should either skip the command or show a warning
    assert!(
        output.exit_code == exit_codes::SUCCESS
            || output.stdout_contains("skip")
            || output.stdout_contains("warning")
            || output.stdout_contains("commit_required")
    );
}