ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// Review phase tests.
//
// Tests for review phase effect determination, agent chain states,
// fix triggering, and pass counting.

use super::*;
use crate::reducer::state::ReviewValidatedOutcome;

#[test]
fn test_determine_effect_review_phase_empty_chain() {
    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 1,
        total_reviewer_passes: 2,
        agent_chain: AgentChainState::initial(),
        ..create_test_state()
    };
    let effect = determine_next_effect(&state);
    assert!(matches!(
        effect,
        Effect::InitializeAgentChain {
            drain: crate::agents::AgentDrain::Review,
            ..
        }
    ));
}

#[test]
fn test_determine_effect_review_phase_exhausted_chain() {
    let mut chain = AgentChainState::initial()
        .with_agents(
            vec!["claude".to_string()],
            vec![vec![]],
            AgentRole::Reviewer,
        )
        .with_drain(crate::agents::AgentDrain::Review)
        .with_max_cycles(3);
    chain = chain.start_retry_cycle();
    chain = chain.start_retry_cycle();
    chain = chain.start_retry_cycle();

    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 1,
        total_reviewer_passes: 2,
        agent_chain: chain,
        ..create_test_state()
    };
    let effect = determine_next_effect(&state);
    assert!(matches!(effect, Effect::SaveCheckpoint { .. }));
}

#[test]
fn test_determine_effect_review_exhausted_chain_after_checkpoint_aborts() {
    let mut chain = AgentChainState::initial()
        .with_agents(
            vec!["claude".to_string()],
            vec![vec![]],
            AgentRole::Reviewer,
        )
        .with_drain(crate::agents::AgentDrain::Review)
        .with_max_cycles(3);
    chain = chain.start_retry_cycle();
    chain = chain.start_retry_cycle();
    chain = chain.start_retry_cycle();

    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 1,
        total_reviewer_passes: 2,
        checkpoint_saved_count: 1,
        agent_chain: chain,
        ..create_test_state()
    };
    let effect = determine_next_effect(&state);
    assert!(matches!(effect, Effect::ReportAgentChainExhausted { .. }));
}

#[test]
fn test_determine_effect_review_phase_with_chain() {
    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 1,
        total_reviewer_passes: 2,
        agent_chain: PipelineState::initial(5, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        ..create_test_state()
    };
    let effect = determine_next_effect(&state);
    assert!(matches!(effect, Effect::PrepareReviewContext { pass: 1 }));
}

#[test]
fn test_resume_scenario_at_final_review_pass_runs_work() {
    // Test RESUME-ONLY scenario: reviewer_pass == total_reviewer_passes with no progress flags.
    // This simulates resuming from a checkpoint saved at the final review pass.
    // Orchestration should re-run the review work (resume behavior),
    // not skip to SaveCheckpoint.
    //
    // This is distinct from fresh-run behavior where if all progress flags indicate
    // completion (archived == Some(pass)), orchestration should ApplyReviewOutcome
    // instead of re-running the work.
    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 2,
        total_reviewer_passes: 2,
        agent_chain: PipelineState::initial(5, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        // Explicitly set all progress flags to None to simulate resume state
        review_context_prepared_pass: None,
        review_prompt_prepared_pass: None,
        review_required_files_cleaned_pass: None,
        review_agent_invoked_pass: None,
        review_issues_xml_extracted_pass: None,
        review_validated_outcome: None,
        review_issues_markdown_written_pass: None,
        review_issue_snippets_extracted_pass: None,
        review_issues_xml_archived_pass: None,
        fix_prompt_prepared_pass: None,
        fix_required_files_cleaned_pass: None,
        fix_agent_invoked_pass: None,
        fix_result_xml_extracted_pass: None,
        fix_validated_outcome: None,
        fix_result_xml_archived_pass: None,
        ..create_test_state()
    };
    let effect = determine_next_effect(&state);
    // Should derive review work, not SaveCheckpoint
    assert!(matches!(effect, Effect::PrepareReviewContext { .. }));
}

#[test]
fn test_review_triggers_fix_when_issues_found() {
    // Create state in Review phase with issues found
    let mut state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 0,
        total_reviewer_passes: 2,
        review_issues_found: false,
        agent_chain: PipelineState::initial(5, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        ..create_test_state()
    };

    // Initially should begin review chain
    let effect = determine_next_effect(&state);
    assert!(
        matches!(effect, Effect::PrepareReviewContext { pass: 0 }),
        "Expected PrepareReviewContext, got {effect:?}"
    );

    // Simulate review completing with issues found
    state = reduce(state, PipelineEvent::review_completed(0, true));

    // State should now have issues_found flag set
    assert!(
        state.review_issues_found,
        "review_issues_found should be true"
    );

    // With explicit drains, review completion with issues should initialize the fix drain first.
    let effect = determine_next_effect(&state);
    assert!(
        matches!(
            effect,
            Effect::InitializeAgentChain {
                drain: crate::agents::AgentDrain::Fix,
                ..
            }
        ),
        "Expected fix drain initialization after issues found, got {effect:?}"
    );

    // After fix completes, goes to CommitMessage phase
    state = reduce(state, PipelineEvent::fix_attempt_completed(0, true));

    assert!(
        !state.review_issues_found,
        "review_issues_found should be reset after fix"
    );
    // After fix, goes to CommitMessage phase (pass increment happens after commit)
    assert_eq!(
        state.reviewer_pass, 0,
        "Pass stays at 0 until CommitCreated"
    );
    assert_eq!(
        state.phase,
        PipelinePhase::CommitMessage,
        "Should go to CommitMessage phase after fix"
    );

    // After commit is created, pass is incremented
    state = reduce(state, PipelineEvent::commit_generation_started());
    state = reduce(
        state,
        PipelineEvent::commit_created("abc123".to_string(), "fix commit".to_string()),
    );

    assert_eq!(
        state.reviewer_pass, 1,
        "Should increment to next pass after commit"
    );
    assert_eq!(
        state.phase,
        PipelinePhase::Review,
        "Should return to Review phase after commit"
    );
}

#[test]
fn test_review_runs_exactly_n_passes() {
    // Similar to development iteration test, verify review passes count
    let mut state = PipelineState::initial(0, 3); // 0 dev, 3 review passes
    state.agent_chain = state
        .agent_chain
        .with_agents(
            vec!["claude".to_string()],
            vec![vec![]],
            AgentRole::Reviewer,
        )
        .with_drain(crate::agents::AgentDrain::Review);

    let mut passes_run = Vec::new();
    let max_steps = 30;

    for _ in 0..max_steps {
        let effect = determine_next_effect(&state);

        match effect {
            Effect::LockPromptPermissions => {
                state = reduce(state, PipelineEvent::prompt_permissions_locked(None));
            }
            Effect::RestorePromptPermissions => {
                state = reduce(state, PipelineEvent::prompt_permissions_restored());
            }
            Effect::InitializeAgentChain { drain, .. } => {
                state = reduce(
                    state,
                    PipelineEvent::agent_chain_initialized(
                        drain,
                        vec![AgentName::from("claude")],
                        vec![],
                        3,
                        1000,
                        2.0,
                        60000,
                    ),
                );
            }
            Effect::PrepareReviewContext { pass } => {
                passes_run.push(pass);
                state = reduce(state, PipelineEvent::review_context_prepared(pass));
                state = reduce(state, PipelineEvent::review_prompt_prepared(pass));
                state = reduce(state, PipelineEvent::review_issues_xml_cleaned(pass));
                state = reduce(state, PipelineEvent::review_agent_invoked(pass));
                state = reduce(state, PipelineEvent::review_issues_xml_extracted(pass));
                state = reduce(
                    state,
                    PipelineEvent::review_issues_xml_validated(
                        pass,
                        false,
                        true,
                        Vec::new(),
                        Some("ok".to_string()),
                    ),
                );
                state = reduce(state, PipelineEvent::review_issues_markdown_written(pass));
                state = reduce(state, PipelineEvent::review_issue_snippets_extracted(pass));
                state = reduce(state, PipelineEvent::review_issues_xml_archived(pass));
                state = reduce(state, PipelineEvent::review_pass_completed_clean(pass));
            }
            Effect::SaveCheckpoint { .. } => {
                // Review complete
                break;
            }
            _ => break,
        }
    }

    assert_eq!(
        passes_run.len(),
        3,
        "Should run exactly 3 review passes, ran: {passes_run:?}"
    );
    assert_eq!(passes_run, vec![0, 1, 2], "Should run passes 0-2");
    assert_eq!(
        state.phase,
        PipelinePhase::CommitMessage,
        "Should transition to CommitMessage after reviews"
    );
}

#[test]
fn test_review_skips_fix_when_no_issues() {
    // Create state in Review phase
    let mut state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 0,
        total_reviewer_passes: 2,
        review_issues_found: false,
        agent_chain: PipelineState::initial(5, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        ..create_test_state()
    };

    // Begin review chain
    let effect = determine_next_effect(&state);
    assert!(matches!(effect, Effect::PrepareReviewContext { pass: 0 }));

    // Review completes with NO issues
    state = reduce(state, PipelineEvent::review_completed(0, false));

    assert!(
        !state.review_issues_found,
        "review_issues_found should be false"
    );

    assert_eq!(
        state.reviewer_pass, 1,
        "Should increment to next pass when no issues"
    );

    // Should begin next review chain (pass 1), NOT fix chain
    let effect = determine_next_effect(&state);
    assert!(
        matches!(effect, Effect::PrepareReviewContext { pass: 1 }),
        "Expected PrepareReviewContext pass 1 when no issues, got {effect:?}"
    );
}

#[test]
fn test_same_agent_retry_in_fix_drain_uses_fix_prompt_without_review_flags() {
    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 1,
        total_reviewer_passes: 2,
        review_issues_found: false,
        agent_chain: PipelineState::initial(5, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Fix)
            .with_mode(crate::agents::DrainMode::SameAgentRetry),
        continuation: crate::reducer::state::ContinuationState {
            same_agent_retry_pending: true,
            ..crate::reducer::state::ContinuationState::default()
        },
        ..create_test_state()
    };

    let effect = determine_next_effect(&state);

    assert!(
        matches!(effect, Effect::PrepareFixPrompt { pass: 1, .. }),
        "expected fix retry prompt for fix drain, got {effect:?}"
    );

    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 1,
        total_reviewer_passes: 2,
        review_issues_found: false,
        agent_chain: PipelineState::initial(5, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        continuation: crate::reducer::state::ContinuationState {
            fix_continue_pending: true,
            fix_continuation_attempt: 1,
            ..crate::reducer::state::ContinuationState::default()
        },
        ..create_test_state()
    };

    let effect = determine_next_effect(&state);

    assert!(
        matches!(
            effect,
            Effect::InitializeAgentChain {
                drain: crate::agents::AgentDrain::Fix,
            }
        ),
        "fix continuation markers should reinitialize the fix drain when review is still loaded, got {effect:?}"
    );
}

#[test]
fn test_determine_effect_review_phase_with_wrong_role_chain() {
    // Scenario: Review phase with a non-empty chain, but the role is Commit
    // This simulates the bug where we transition from CommitMessage back to Review
    // and the chain was left as AgentRole::Commit
    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 1,
        total_reviewer_passes: 2,
        agent_chain: PipelineState::initial(5, 2).agent_chain.with_agents(
            vec!["commit-agent".to_string()],
            vec![vec![]],
            AgentRole::Commit, // Wrong role!
        ),
        ..create_test_state()
    };

    // Should initialize a new chain for Reviewer role, not use the Commit chain
    let effect = determine_next_effect(&state);
    assert!(
        matches!(
            effect,
            Effect::InitializeAgentChain {
                drain: crate::agents::AgentDrain::Review,
                ..
            }
        ),
        "Expected InitializeAgentChain for the review drain, got {effect:?}"
    );
}

#[test]
fn test_resume_at_final_review_pass_should_run_review_not_skip() {
    // BUG REPRODUCTION: When checkpoint saved at reviewer_pass=2, total=2
    // and all progress flags are None (reset on resume),
    // orchestration should derive review work effects,
    // NOT SaveCheckpoint (which would skip to next phase).

    let state = PipelineState {
        phase: PipelinePhase::Review,
        iteration: 3,
        total_iterations: 3,
        reviewer_pass: 2,
        total_reviewer_passes: 2,
        review_issues_found: false,
        agent_chain: PipelineState::initial(3, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        // All progress flags None - simulating resume state
        review_context_prepared_pass: None,
        review_prompt_prepared_pass: None,
        review_required_files_cleaned_pass: None,
        review_agent_invoked_pass: None,
        review_issues_xml_extracted_pass: None,
        review_validated_outcome: None,
        review_issues_markdown_written_pass: None,
        review_issue_snippets_extracted_pass: None,
        review_issues_xml_archived_pass: None,
        ..create_test_state()
    };

    let effect = determine_next_effect(&state);

    // CRITICAL: Should derive review work, NOT phase transition
    // This test verifies the fix: previously would fail, now passes
    assert!(
        matches!(effect, Effect::PrepareReviewContext { .. }),
        "Expected PrepareReviewContext, got {effect:?}"
    );
}

#[test]
fn test_resume_at_final_review_pass_with_no_progress_should_run_review() {
    // Bug scenario: checkpoint saved at reviewer_pass=2, total=2
    // On resume, all progress flags are None (reset)
    // Expected: Should re-run review pass
    // Actual (bug): Skips to SaveCheckpoint, then transitions to next phase

    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 2,
        total_reviewer_passes: 2,
        review_issues_found: false,
        agent_chain: AgentChainState::initial()
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        // All progress flags are None (simulating resume state)
        review_context_prepared_pass: None,
        review_prompt_prepared_pass: None,
        review_agent_invoked_pass: None,
        review_issues_xml_archived_pass: None,
        ..create_test_state()
    };

    let effect = determine_next_effect(&state);

    // Should prepare review context (start pass), NOT save checkpoint
    assert!(
        matches!(effect, Effect::PrepareReviewContext { pass: 2 }),
        "Expected PrepareReviewContext but got {effect:?}"
    );
}

#[test]
fn test_resume_at_review_pass_zero_with_total_one_runs_work() {
    // Boundary case: reviewer_pass=0, total_reviewer_passes=1

    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 0,
        total_reviewer_passes: 1,
        review_issues_found: false,
        agent_chain: AgentChainState::initial()
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        review_agent_invoked_pass: None,
        ..create_test_state()
    };

    let effect = determine_next_effect(&state);

    assert!(
        matches!(effect, Effect::PrepareReviewContext { pass: 0 }),
        "Expected PrepareReviewContext but got {effect:?}"
    );
}

#[test]
fn test_review_pass_completed_applies_outcome_not_reruns() {
    // Verify that when reviewer_pass == total_reviewer_passes AND the work is
    // actually complete (review_issues_xml_archived_pass is Some), orchestration
    // should apply the review outcome (to transition to the next phase), not re-run the work.
    //
    // This is the "truly complete" scenario, distinct from the resume scenario
    // where all progress flags are None.
    //
    // When the review pass is complete:
    // - Orchestration derives ApplyReviewOutcome (to process the outcome)
    // - The outcome handler may then trigger a phase transition

    let state = PipelineState {
        phase: PipelinePhase::Review,
        reviewer_pass: 2,
        total_reviewer_passes: 2,
        review_issues_found: false,
        agent_chain: PipelineState::initial(3, 2)
            .agent_chain
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Reviewer,
            )
            .with_drain(crate::agents::AgentDrain::Review),
        // Work is complete - archived flag is set
        review_issues_xml_archived_pass: Some(2),
        // Other progress flags set to indicate completion
        review_context_prepared_pass: Some(2),
        review_prompt_prepared_pass: Some(2),
        review_required_files_cleaned_pass: Some(2),
        review_agent_invoked_pass: Some(2),
        review_issues_xml_extracted_pass: Some(2),
        review_validated_outcome: Some(ReviewValidatedOutcome {
            pass: 2,
            issues_found: false,
            clean_no_issues: true,
            issues: Vec::new().into_boxed_slice(),
            no_issues_found: Some("ok".to_string()),
        }),
        review_issues_markdown_written_pass: Some(2),
        review_issue_snippets_extracted_pass: Some(2),
        ..create_test_state()
    };

    let effect = determine_next_effect(&state);

    // Should apply the outcome (which will trigger transition), NOT re-run the review work
    assert!(
        matches!(
            effect,
            Effect::ApplyReviewOutcome {
                pass: 2,
                issues_found: false,
                clean_no_issues: true
            }
        ),
        "Expected ApplyReviewOutcome for completed review, got {effect:?}"
    );
}