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
//! Integration tests for MapReduce worktree architecture (Spec 134)
//!
//! These tests verify that the MapReduce workflow correctly executes in a single
//! parent worktree without creating intermediate session-mapreduce-xxx worktrees,
//! and that the user is prompted to merge (not automatic merge).

use anyhow::Result;
use prodigy::subprocess::{ProcessCommandBuilder, SubprocessManager};
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
use tokio::process::Command;

/// Helper to initialize a git repository with initial commit
async fn setup_git_repo(path: &PathBuf) -> Result<()> {
    Command::new("git")
        .args(["init"])
        .current_dir(path)
        .output()
        .await?;

    Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(path)
        .output()
        .await?;

    Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(path)
        .output()
        .await?;

    // Create initial commit
    fs::write(path.join("README.md"), "# Test Project")?;
    Command::new("git")
        .args(["add", "."])
        .current_dir(path)
        .output()
        .await?;

    Command::new("git")
        .args(["commit", "-m", "Initial commit"])
        .current_dir(path)
        .output()
        .await?;

    Ok(())
}

/// Test that MapReduce workflow executes in parent worktree without creating
/// an intermediate session-mapreduce-xxx worktree
///
/// Architecture (Spec 134):
/// ```
/// original_branch (e.g., master)
////// parent worktree (session-xxx) ← All MapReduce phases execute here
///     ├→ Setup phase executes here
///     ├→ Agent worktrees branch from parent
///     │  ├→ agent-1 → processes item, merges back to parent
///     │  ├→ agent-2 → processes item, merges back to parent
///     │  └→ agent-N → processes item, merges back to parent
///     ├→ Reduce phase executes here
///     └→ User prompt: Merge to {original_branch}? [Y/n]
/// ```
#[tokio::test]
#[ignore] // Requires full Prodigy CLI and worktree setup
async fn test_mapreduce_executes_in_parent_worktree_only() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();

    // Setup main git repository
    setup_git_repo(&repo_path).await?;

    // Create a minimal MapReduce workflow YAML
    let workflow_yaml = r#"
name: test-mapreduce-architecture
mode: mapreduce

setup:
  - shell: "echo 'setup phase' > setup-output.txt"

map:
  input: '[{"id": 1, "name": "item1"}, {"id": 2, "name": "item2"}]'
  json_path: "$[*]"
  agent_template:
    - shell: "echo 'Processing ${item.name}' > ${item.name}.txt"
  max_parallel: 2

reduce:
  - shell: "echo 'reduce phase' > reduce-output.txt"
"#;

    let workflow_path = repo_path.join("test-workflow.yml");
    fs::write(&workflow_path, workflow_yaml)?;

    // Run the MapReduce workflow using Prodigy CLI
    // This simulates what happens when a user runs: prodigy run test-workflow.yml
    let subprocess = SubprocessManager::production();

    // Check if prodigy CLI is available
    let prodigy_check = Command::new("prodigy").arg("--version").output().await;
    if prodigy_check.is_err() || !prodigy_check.unwrap().status.success() {
        eprintln!("⚠️  Prodigy CLI not available, skipping integration test");
        return Ok(());
    }

    // Execute the workflow with auto-accept to avoid prompts
    let run_cmd = ProcessCommandBuilder::new("prodigy")
        .args(["run", workflow_path.to_str().unwrap(), "-y"])
        .current_dir(&repo_path)
        .env("PRODIGY_AUTOMATION", "true")
        .build();

    let result = subprocess.runner().run(run_cmd).await?;

    if !result.status.success() {
        eprintln!("Prodigy run output:");
        eprintln!("{}", result.stdout);
        eprintln!("{}", result.stderr);
        anyhow::bail!("Prodigy run failed");
    }

    // CRITICAL VALIDATION: Verify NO intermediate session-mapreduce-xxx worktree was created
    // List all worktrees
    let worktree_list_output = Command::new("git")
        .args(["worktree", "list"])
        .current_dir(&repo_path)
        .output()
        .await?;

    let worktree_list = String::from_utf8_lossy(&worktree_list_output.stdout);

    // Verify:
    // 1. Parent worktree exists (session-xxx or prodigy-session-xxx)
    // 2. NO session-mapreduce-xxx worktree exists
    // 3. Agent worktrees may exist (or may have been cleaned up)

    assert!(
        !worktree_list.contains("session-mapreduce-"),
        "MapReduce should NOT create intermediate session-mapreduce-xxx worktree. Found worktrees:\n{}",
        worktree_list
    );

    // The parent worktree should exist and contain setup/reduce phase outputs
    // (finding the exact worktree path requires parsing git worktree list)

    Ok(())
}

/// Test that verifies setup and reduce phases execute in the parent worktree
///
/// This test creates a mock scenario where we can verify the execution context
/// of each phase without requiring full Prodigy CLI integration.
#[tokio::test]
async fn test_setup_and_reduce_execute_in_parent_worktree() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();

    setup_git_repo(&repo_path).await?;

    // Create a parent worktree (simulating what Prodigy orchestrator does)
    let subprocess = SubprocessManager::production();
    let worktree_name = "prodigy-session-test";
    let worktree_path = repo_path.join(".prodigy-worktrees").join(worktree_name);

    // Create worktrees directory
    fs::create_dir_all(worktree_path.parent().unwrap())?;

    let worktree_cmd = ProcessCommandBuilder::new("git")
        .current_dir(&repo_path)
        .args([
            "worktree",
            "add",
            "-b",
            &format!("prodigy-{}", worktree_name),
            worktree_path.to_str().unwrap(),
        ])
        .build();

    subprocess.runner().run(worktree_cmd).await?;

    // Simulate setup phase execution in parent worktree
    let setup_cmd = ProcessCommandBuilder::new("sh")
        .current_dir(&worktree_path)
        .args(["-c", "echo 'setup' > setup-output.txt"])
        .build();

    let setup_result = subprocess.runner().run(setup_cmd).await?;
    assert!(setup_result.status.success(), "Setup phase should succeed");

    // Verify setup output exists in parent worktree
    assert!(
        worktree_path.join("setup-output.txt").exists(),
        "Setup phase output should exist in parent worktree"
    );

    // Simulate reduce phase execution in parent worktree
    let reduce_cmd = ProcessCommandBuilder::new("sh")
        .current_dir(&worktree_path)
        .args(["-c", "echo 'reduce' > reduce-output.txt"])
        .build();

    let reduce_result = subprocess.runner().run(reduce_cmd).await?;
    assert!(
        reduce_result.status.success(),
        "Reduce phase should succeed"
    );

    // Verify reduce output exists in parent worktree
    assert!(
        worktree_path.join("reduce-output.txt").exists(),
        "Reduce phase output should exist in parent worktree"
    );

    // CRITICAL: Verify we're working in ONE worktree, not multiple
    let worktree_list_output = Command::new("git")
        .args(["worktree", "list"])
        .current_dir(&repo_path)
        .output()
        .await?;

    let worktree_list = String::from_utf8_lossy(&worktree_list_output.stdout);

    // Should see: main repo + parent worktree (2 total)
    // Should NOT see: main repo + parent worktree + mapreduce worktree (3+ would be wrong)
    let worktree_count = worktree_list.lines().count();
    assert_eq!(
        worktree_count, 2,
        "Should have exactly 2 worktrees (main + parent), found {}:\n{}",
        worktree_count, worktree_list
    );

    Ok(())
}

/// Test that agent worktrees branch from parent worktree, not from a separate
/// MapReduce worktree
#[tokio::test]
async fn test_agent_worktrees_branch_from_parent() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();

    setup_git_repo(&repo_path).await?;

    // Create parent worktree
    let subprocess = SubprocessManager::production();
    let parent_worktree_name = "prodigy-session-parent";
    let parent_path = repo_path
        .join(".prodigy-worktrees")
        .join(parent_worktree_name);

    fs::create_dir_all(parent_path.parent().unwrap())?;

    let parent_cmd = ProcessCommandBuilder::new("git")
        .current_dir(&repo_path)
        .args([
            "worktree",
            "add",
            "-b",
            &format!("prodigy-{}", parent_worktree_name),
            parent_path.to_str().unwrap(),
        ])
        .build();

    subprocess.runner().run(parent_cmd).await?;

    // Simulate agent worktree creation FROM parent worktree
    // (This is what MapReduce coordination does for each agent)
    let agent_worktree_name = "agent-1";
    let agent_path = parent_path.join(agent_worktree_name);

    let agent_cmd = ProcessCommandBuilder::new("git")
        .current_dir(&parent_path) // Create from parent, not from main repo
        .args([
            "worktree",
            "add",
            "-b",
            &format!("prodigy-{}", agent_worktree_name),
            agent_path.to_str().unwrap(),
        ])
        .build();

    subprocess.runner().run(agent_cmd).await?;

    // Verify agent worktree exists and is a child of parent
    assert!(
        agent_path.exists(),
        "Agent worktree should exist as child of parent worktree"
    );

    // Verify git worktree list shows the correct structure
    let worktree_list_output = Command::new("git")
        .args(["worktree", "list", "--porcelain"])
        .current_dir(&repo_path)
        .output()
        .await?;

    let worktree_list = String::from_utf8_lossy(&worktree_list_output.stdout);

    // Should have: main repo + parent worktree + agent worktree = 3 total
    // Should NOT have: main repo + parent + mapreduce + agent = 4+
    let worktree_count = worktree_list.matches("worktree ").count();
    assert!(
        worktree_count <= 3,
        "Should have at most 3 worktrees (main + parent + agent), found {}:\n{}",
        worktree_count,
        worktree_list
    );

    Ok(())
}

/// Test that user is prompted to merge after workflow completion (not automatic)
///
/// This is a documentation test since we can't easily test CLI prompts in integration tests.
/// The actual prompt logic is in the orchestrator completion handler.
#[test]
fn test_user_prompted_for_merge_not_automatic() {
    // Spec 134 requirement:
    // After MapReduce workflow completes, the user should be prompted:
    // "Merge session-xxx to {original_branch}? [Y/n]"
    //
    // The workflow should NOT automatically merge back to the original branch.
    //
    // Implementation location: src/cook/orchestrator/completion.rs
    // The orchestrator checks if auto_accept is enabled:
    //   - If auto_accept (-y flag): Auto-merge without prompt
    //   - If not auto_accept: Prompt user for confirmation
    //
    // This test documents the expected behavior.
    // Manual test:
    //   1. Run: prodigy run mapreduce-workflow.yml (without -y)
    //   2. Wait for completion
    //   3. Verify prompt appears: "Merge session-xxx to master? [Y/n]"
    //   4. User must respond Y/n
    //
    // Automated test would require mocking user interaction, which is complex.
    // Instead, we document the requirement here as a specification test.
}

/// Test that original branch tracking works correctly
///
/// The parent worktree should track whatever branch the user was on when starting
/// the workflow (not hardcoded to "master" or "main").
#[tokio::test]
async fn test_original_branch_tracking() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();

    setup_git_repo(&repo_path).await?;

    // Create a feature branch and check it out
    Command::new("git")
        .args(["checkout", "-b", "feature/my-feature"])
        .current_dir(&repo_path)
        .output()
        .await?;

    // Create parent worktree FROM the feature branch
    let subprocess = SubprocessManager::production();
    let worktree_name = "prodigy-session-feature";
    let worktree_path = repo_path.join(".prodigy-worktrees").join(worktree_name);

    fs::create_dir_all(worktree_path.parent().unwrap())?;

    let worktree_cmd = ProcessCommandBuilder::new("git")
        .current_dir(&repo_path)
        .args([
            "worktree",
            "add",
            "-b",
            &format!("prodigy-{}", worktree_name),
            worktree_path.to_str().unwrap(),
        ])
        .build();

    subprocess.runner().run(worktree_cmd).await?;

    // Verify the parent worktree knows it came from feature/my-feature
    // (This information would be stored in WorktreeState by the orchestrator)

    // The merge prompt should show: "Merge session-feature to feature/my-feature? [Y/n]"
    // NOT: "Merge session-feature to master? [Y/n]"

    // We verify by checking current branch in main repo
    let branch_output = Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .current_dir(&repo_path)
        .output()
        .await?;

    let current_branch = String::from_utf8_lossy(&branch_output.stdout)
        .trim()
        .to_string();
    assert_eq!(
        current_branch, "feature/my-feature",
        "Original branch should be tracked as feature/my-feature"
    );

    Ok(())
}

/// Test cleanup behavior - worktrees should be cleaned up properly
///
/// After a successful merge, the parent worktree and any agent worktrees
/// should be cleaned up.
#[tokio::test]
async fn test_worktree_cleanup_after_completion() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();

    setup_git_repo(&repo_path).await?;

    // Create parent worktree
    let subprocess = SubprocessManager::production();
    let worktree_name = "prodigy-session-cleanup-test";
    let worktree_path = repo_path.join(".prodigy-worktrees").join(worktree_name);

    fs::create_dir_all(worktree_path.parent().unwrap())?;

    let worktree_cmd = ProcessCommandBuilder::new("git")
        .current_dir(&repo_path)
        .args([
            "worktree",
            "add",
            "-b",
            &format!("prodigy-{}", worktree_name),
            worktree_path.to_str().unwrap(),
        ])
        .build();

    subprocess.runner().run(worktree_cmd).await?;

    // Verify worktree was created
    assert!(worktree_path.exists(), "Worktree should exist");

    // Simulate cleanup
    let cleanup_cmd = ProcessCommandBuilder::new("git")
        .current_dir(&repo_path)
        .args(["worktree", "remove", worktree_path.to_str().unwrap()])
        .build();

    let cleanup_result = subprocess.runner().run(cleanup_cmd).await?;
    assert!(
        cleanup_result.status.success(),
        "Worktree cleanup should succeed"
    );

    // Verify worktree was removed
    assert!(
        !worktree_path.exists(),
        "Worktree should be removed after cleanup"
    );

    Ok(())
}