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
//! Edge case tests for agent chain.
//!
//! Tests for wrapping behavior, single-agent chains, timeout handling,
//! and integration-style event loop simulations.

use crate::agents::AgentRole;
use crate::common::domain_types::AgentName;
use crate::reducer::event::{AgentErrorKind, PipelinePhase, TimeoutOutputKind};
use crate::reducer::io_tests::{create_test_state, reduce, PipelineEvent, PipelineState};

#[test]
fn test_agent_invocation_failed_retriable_network_does_not_wrap_model() {
    let base_state = create_test_state();
    let state = PipelineState {
        agent_chain: base_state
            .agent_chain
            .with_agents(
                vec!["agent1".to_string()],
                vec![vec!["model1".to_string(), "model2".to_string()]],
                AgentRole::Developer,
            )
            .advance_to_next_model(), // Move to model index 1 (last model)
        ..base_state
    };

    // Verify we're on the last model
    assert_eq!(state.agent_chain.current_model_index, 1);

    let new_state = reduce(
        state,
        PipelineEvent::agent_invocation_failed(
            AgentRole::Developer,
            AgentName::from("agent1"),
            1,
            AgentErrorKind::Network,
            true,
        ),
    );

    // Network error should NOT wrap model - it sets check_pending instead
    assert_eq!(new_state.agent_chain.current_agent_index, 0);
    assert_eq!(
        new_state.agent_chain.current_model_index, 1,
        "Network error should NOT change model (check_pending takes priority)"
    );
    assert!(
        new_state.connectivity.check_pending,
        "Network error should set check_pending for connectivity verification"
    );
}

#[test]
fn test_agent_fallback_from_last_agent_wraps_and_increments_cycle() {
    let base_state = create_test_state();
    let state = PipelineState {
        agent_chain: base_state
            .agent_chain
            .with_agents(
                vec!["agent1".to_string(), "agent2".to_string()],
                vec![vec!["model1".to_string()], vec!["model2".to_string()]],
                AgentRole::Developer,
            )
            .switch_to_next_agent(), // Move to agent index 1 (last agent)
        ..base_state
    };

    // Verify we're on the last agent and cycle is 0
    assert_eq!(state.agent_chain.current_agent_index, 1);
    assert_eq!(state.agent_chain.retry_cycle, 0);

    let new_state = reduce(
        state,
        PipelineEvent::agent_fallback_triggered(
            AgentRole::Developer,
            AgentName::from("agent2"),
            AgentName::from("agent1"),
        ),
    );

    // Should wrap back to first agent (1 -> 0) and increment retry_cycle (0 -> 1)
    assert_eq!(new_state.agent_chain.current_agent_index, 0);
    assert_eq!(new_state.agent_chain.current_model_index, 0);
    assert_eq!(new_state.agent_chain.retry_cycle, 1);
}

#[test]
fn test_agent_invocation_failed_retriable_network_on_single_model_wraps() {
    let base_state = create_test_state();
    let state = PipelineState {
        agent_chain: base_state.agent_chain.with_agents(
            vec!["agent1".to_string()],
            vec![vec!["model1".to_string()]], // Only one model
            AgentRole::Developer,
        ),
        ..base_state
    };

    let new_state = reduce(
        state,
        PipelineEvent::agent_invocation_failed(
            AgentRole::Developer,
            AgentName::from("agent1"),
            1,
            AgentErrorKind::Network,
            true,
        ),
    );

    // With only one model, should wrap back to index 0
    assert_eq!(new_state.agent_chain.current_agent_index, 0);
    assert_eq!(new_state.agent_chain.current_model_index, 0);
}

// ============================================================================
// Timeout Fallback Tests
// ============================================================================

#[test]
fn test_timed_out_retries_same_agent_before_fallback() {
    // Setup: two agents, each with two models. Same-agent retry budget 2 means:
    // - First timeout retries same agent
    // - Second timeout falls back to next agent
    let base_state = create_test_state();
    let state = PipelineState {
        continuation: crate::reducer::state::ContinuationState::with_limits(2, 3, 2),
        agent_chain: base_state.agent_chain.with_agents(
            vec!["agent-a".to_string(), "agent-b".to_string()],
            vec![
                vec!["model-a1".to_string(), "model-a2".to_string()],
                vec!["model-b1".to_string()],
            ],
            AgentRole::Developer,
        ),
        ..base_state
    };

    assert_eq!(
        state.agent_chain.current_agent().map(String::as_str),
        Some("agent-a")
    );
    assert_eq!(state.agent_chain.current_model_index, 0);

    let after_first_timeout = reduce(
        state,
        PipelineEvent::agent_timed_out(
            AgentRole::Developer,
            AgentName::from("agent-a"),
            TimeoutOutputKind::PartialResult,
            Some(".agent/logs/developer_0.log".to_string()),
            None,
        ),
    );

    // Timeout retries MUST NOT reuse XSD retry mechanism.
    assert!(
        !after_first_timeout.continuation.xsd_retry_pending,
        "Timeout retry should not set xsd_retry_pending (XSD retry is only for invalid XML)"
    );
    assert_eq!(
        after_first_timeout.continuation.xsd_retry_count, 0,
        "Timeout retry should not increment xsd_retry_count (XSD retry is only for invalid XML)"
    );

    assert_eq!(
        after_first_timeout
            .agent_chain
            .current_agent()
            .map(String::as_str),
        Some("agent-a"),
        "Timeout should retry same agent first"
    );
    assert_eq!(
        after_first_timeout.agent_chain.current_model_index, 0,
        "Timeout retry should not advance model"
    );

    let after_second_timeout = reduce(
        after_first_timeout,
        PipelineEvent::agent_timed_out(
            AgentRole::Developer,
            AgentName::from("agent-a"),
            TimeoutOutputKind::PartialResult,
            Some(".agent/logs/developer_0.log".to_string()),
            None,
        ),
    );

    assert_eq!(
        after_second_timeout
            .agent_chain
            .current_agent()
            .map(String::as_str),
        Some("agent-b"),
        "After retry budget exhaustion, timeout should fall back to next agent"
    );
    assert_eq!(
        after_second_timeout.agent_chain.current_model_index, 0,
        "Model index should reset to 0 when switching agents"
    );
}

#[test]
fn test_internal_error_retries_same_agent_before_fallback_without_xsd_retry() {
    use crate::reducer::event::AgentErrorKind;

    let base_state = create_test_state();
    let state = PipelineState {
        continuation: crate::reducer::state::ContinuationState::with_limits(2, 3, 2),
        agent_chain: base_state.agent_chain.with_agents(
            vec!["agent-a".to_string(), "agent-b".to_string()],
            vec![vec![], vec![]],
            AgentRole::Developer,
        ),
        ..base_state
    };

    let after_first_failure = reduce(
        state,
        PipelineEvent::agent_invocation_failed(
            AgentRole::Developer,
            AgentName::from("agent-a"),
            1,
            AgentErrorKind::InternalError,
            false,
        ),
    );

    assert_eq!(
        after_first_failure
            .agent_chain
            .current_agent()
            .map(String::as_str),
        Some("agent-a"),
        "Internal error should retry same agent first"
    );
    assert!(
        !after_first_failure.continuation.xsd_retry_pending,
        "Internal error retry should not set xsd_retry_pending (XSD retry is only for invalid XML)"
    );
    assert_eq!(
        after_first_failure.continuation.xsd_retry_count, 0,
        "Internal error retry should not increment xsd_retry_count (XSD retry is only for invalid XML)"
    );
}

#[test]
fn test_timed_out_partial_output_preserves_session_id_for_context_retry() {
    // AC-1: PartialResult timeout should preserve session ID for context reuse
    let base_state = create_test_state();
    let state = PipelineState {
        agent_chain: base_state
            .agent_chain
            .with_agents(
                vec!["agent-a".to_string(), "agent-b".to_string()],
                vec![vec![], vec![]],
                AgentRole::Developer,
            )
            .with_session_id(Some("session-123".to_string())),
        ..base_state
    };

    // Verify session ID is set
    assert_eq!(
        state.agent_chain.last_session_id,
        Some("session-123".to_string())
    );

    // Apply PartialResult timeout - should preserve session for context reuse
    let new_state = reduce(
        state,
        PipelineEvent::agent_timed_out(
            AgentRole::Developer,
            AgentName::from("agent-a"),
            TimeoutOutputKind::PartialResult,
            Some(".agent/logs/developer_0.log".to_string()),
            None,
        ),
    );

    assert!(
        !new_state.continuation.xsd_retry_pending,
        "Timeout retry should not set xsd_retry_pending (XSD retry is only for invalid XML)"
    );

    // Session ID should be PRESERVED for TimeoutWithContext (PartialResult)
    assert_eq!(
        new_state.agent_chain.last_session_id,
        Some("session-123".to_string()),
        "PartialResult timeout should preserve session ID for context reuse"
    );

    // Should set session reuse pending flag
    assert!(
        new_state.continuation.xsd_retry_session_reuse_pending,
        "PartialResult timeout should set xsd_retry_session_reuse_pending"
    );
}

#[test]
fn test_timed_out_no_output_clears_session_id_for_immediate_switch() {
    // AC-2: NoResult timeout should clear session ID (immediate agent switch)
    let base_state = create_test_state();
    let state = PipelineState {
        agent_chain: base_state
            .agent_chain
            .with_agents(
                vec!["agent-a".to_string(), "agent-b".to_string()],
                vec![vec![], vec![]],
                AgentRole::Developer,
            )
            .with_session_id(Some("session-123".to_string())),
        ..base_state
    };

    // Verify session ID is set
    assert_eq!(
        state.agent_chain.last_session_id,
        Some("session-123".to_string())
    );

    // Apply NoResult timeout - should clear session and switch agents immediately
    let new_state = reduce(
        state,
        PipelineEvent::agent_timed_out(
            AgentRole::Developer,
            AgentName::from("agent-a"),
            TimeoutOutputKind::NoResult,
            None,
            None,
        ),
    );

    // Session ID should be CLEARED for NoOutput (immediate agent switch)
    assert_eq!(
        new_state.agent_chain.last_session_id, None,
        "NoResult timeout should clear session ID (immediate agent switch)"
    );

    // Should NOT set session reuse pending flag
    assert!(
        !new_state.continuation.xsd_retry_session_reuse_pending,
        "NoResult timeout should not set xsd_retry_session_reuse_pending"
    );
}

#[test]
fn test_timed_out_from_last_agent_increments_retry_cycle_when_budget_exhausted() {
    // This test verifies behavior when same-agent retry budget is exhausted and we're on the
    // last agent in the chain.
    //
    // With the "retry same agent first" policy:
    // - First timeout => retry same agent (same_agent_retry_count=1)
    // - Second timeout => retry budget exhausted (count=2 >= max=2), fall back
    // - Falling back from last agent => wrap to first agent and increment retry_cycle
    let base_state = create_test_state();
    let state = PipelineState {
        continuation: crate::reducer::state::ContinuationState::with_limits(1, 3, 2)
            .with_max_same_agent_retry(2), // Fallback on the 2nd timeout when max=2
        agent_chain: base_state
            .agent_chain
            .with_agents(
                vec!["agent-a".to_string(), "agent-b".to_string()],
                vec![vec![], vec![]],
                AgentRole::Developer,
            )
            .switch_to_next_agent(), // Move to last agent (agent-b)
        ..base_state
    };

    // Verify we're on the last agent
    assert_eq!(
        state.agent_chain.current_agent().map(String::as_str),
        Some("agent-b")
    );
    assert_eq!(state.agent_chain.retry_cycle, 0);
    assert_eq!(state.continuation.same_agent_retry_count, 0);

    // First timeout: should retry same agent, not fall back yet
    let after_first_timeout = reduce(
        state,
        PipelineEvent::agent_timed_out(
            AgentRole::Developer,
            AgentName::from("agent-b"),
            TimeoutOutputKind::PartialResult,
            Some(".agent/logs/developer_0.log".to_string()),
            None,
        ),
    );

    assert!(
        !after_first_timeout.continuation.xsd_retry_pending,
        "Timeout retry should not set xsd_retry_pending (XSD retry is only for invalid XML)"
    );
    assert_eq!(
        after_first_timeout
            .agent_chain
            .current_agent()
            .map(String::as_str),
        Some("agent-b"),
        "First timeout should retry same agent, not fall back"
    );
    assert_eq!(after_first_timeout.continuation.same_agent_retry_count, 1);
    assert!(after_first_timeout.continuation.same_agent_retry_pending);

    // Second timeout: same-agent retry budget exhausted (count=2 >= max=2), fall back
    // Since we're on the last agent, this should wrap to first agent and increment retry_cycle
    let after_second_timeout = reduce(
        after_first_timeout,
        PipelineEvent::agent_timed_out(
            AgentRole::Developer,
            AgentName::from("agent-b"),
            TimeoutOutputKind::PartialResult,
            Some(".agent/logs/developer_0.log".to_string()),
            None,
        ),
    );

    // Should wrap back to first agent and increment retry cycle
    assert_eq!(
        after_second_timeout
            .agent_chain
            .current_agent()
            .map(String::as_str),
        Some("agent-a"),
        "Should wrap back to first agent after exhausting retry budget on last agent"
    );
    assert_eq!(
        after_second_timeout.agent_chain.retry_cycle, 1,
        "Should increment retry cycle when wrapping"
    );
    // Retry counters should be reset after agent switch
    assert_eq!(after_second_timeout.continuation.same_agent_retry_count, 0);
    assert!(!after_second_timeout.continuation.same_agent_retry_pending);
}

// ============================================================================
// Integration-Style Tests (Event Loop Simulation)
// ============================================================================

/// Simulates running the event loop to verify backoff wait does not cause infinite loops.
///
/// This test starts with a state that has `backoff_pending_ms=Some(...)` and runs
/// through the effect/reduce cycle to verify the pipeline progresses correctly
/// without getting stuck repeating `BackoffWait` effects.
#[test]
fn test_backoff_wait_does_not_cause_infinite_loop_in_event_loop_simulation() {
    use crate::reducer::effect::Effect;
    use crate::reducer::orchestration::determine_next_effect;
    use crate::reducer::state::{AgentChainState, ContinuationState};

    let state = PipelineState {
        phase: PipelinePhase::Development,
        iteration: 1,
        agent_chain: AgentChainState::initial()
            .with_agents(
                vec!["claude".to_string()],
                vec![vec![]],
                AgentRole::Developer,
            )
            .with_max_cycles(2),
        continuation: ContinuationState::default(),
        development_context_prepared_iteration: Some(1),
        development_prompt_prepared_iteration: Some(1),
        development_required_files_cleaned_iteration: Some(1),
        ..create_test_state()
    };
    let state = PipelineState {
        agent_chain: AgentChainState {
            backoff_pending_ms: Some(100),
            ..state.agent_chain
        },
        ..state
    };

    let max_iterations = 20;
    let mut current_state = state;
    let mut backoff_cycles = 0u32;
    let mut iterations = 0;

    let final_state = loop {
        if iterations >= max_iterations {
            panic!("exceeded max iterations waiting for a non-backoff effect");
        }
        iterations += 1;

        let effect = determine_next_effect(&current_state);
        match effect {
            Effect::BackoffWait { role, cycle, .. } => {
                assert!(backoff_cycles < 2, "backoff wait repeated more than twice");
                backoff_cycles += 1;
                current_state = reduce(
                    current_state,
                    PipelineEvent::agent_retry_cycle_started(role, cycle),
                );
            }
            Effect::InvokeDevelopmentAgent { .. } => break current_state,
            other => panic!("unexpected effect during backoff simulation: {other:?}"),
        }
    };

    // Verify backoff_pending_ms was cleared
    assert!(
        final_state.agent_chain.backoff_pending_ms.is_none(),
        "backoff_pending_ms should be cleared after RetryCycleStarted event"
    );
}