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
//! Phase transition metric tests
//!
//! Tests for phase-specific metric updates:
//! - Planning phase metrics
//! - Development phase metrics
//! - Review phase metrics
//! - Commit phase metrics

use super::*;

#[test]
fn test_same_agent_retry_within_budget_does_increment() {
    let mut state = PipelineState::initial(3, 0);
    state.continuation.max_same_agent_retry_count = 3;
    state.continuation.same_agent_retry_count = 0;

    // First retry (count becomes 1, which is < max) should increment
    let event = PipelineEvent::agent_timed_out(
        AgentRole::Developer,
        AgentName::from("claude"),
        TimeoutOutputKind::PartialResult,
        Some(".agent/logs/developer_0.log".to_string()),
        None,
    );
    let state = reduce(state, event);

    assert_eq!(state.metrics.same_agent_retry_attempts_total, 1);
    assert_eq!(state.continuation.same_agent_retry_count, 1);
    assert!(state.continuation.same_agent_retry_pending);
}

// ==============================================================================
// New tests for per-iteration/pass continuation attempt tracking
// ==============================================================================

#[test]
fn test_dev_continuation_attempt_increments_on_continuation_triggered() {
    let mut state = PipelineState::initial(1, 0);
    state = reduce(state, PipelineEvent::development_iteration_started(0));

    assert_eq!(state.metrics.dev_continuation_attempt, 0);

    // Trigger continuation
    let event = PipelineEvent::Development(DevelopmentEvent::ContinuationTriggered {
        iteration: 0,
        status: DevelopmentStatus::Partial,
        summary: "Partial work".to_string(),
        files_changed: None,
        next_steps: None,
    });
    state = reduce(state, event);

    assert_eq!(state.metrics.dev_continuation_attempt, 1);
    assert_eq!(state.metrics.dev_iterations_completed, 0); // Not completed yet
}

#[test]
fn test_dev_continuation_attempt_resets_on_new_iteration() {
    let mut state = PipelineState::initial(2, 0);

    // Iteration 0 with continuation
    state = reduce(state, PipelineEvent::development_iteration_started(0));
    state = reduce(
        state,
        PipelineEvent::Development(DevelopmentEvent::ContinuationTriggered {
            iteration: 0,
            status: DevelopmentStatus::Partial,
            summary: "Partial".to_string(),
            files_changed: None,
            next_steps: None,
        }),
    );
    assert_eq!(state.metrics.dev_continuation_attempt, 1);

    // New iteration should reset
    state = reduce(state, PipelineEvent::development_iteration_started(1));
    assert_eq!(state.metrics.dev_continuation_attempt, 0);
}

#[test]
fn test_dev_iterations_completed_increments_on_continuation_succeeded() {
    let mut state = PipelineState::initial(1, 0);
    state = reduce(state, PipelineEvent::development_iteration_started(0));

    assert_eq!(state.metrics.dev_iterations_completed, 0);

    // Continuation succeeded
    let event = PipelineEvent::Development(DevelopmentEvent::ContinuationSucceeded {
        iteration: 0,
        total_continuation_attempts: 2,
    });
    state = reduce(state, event);

    assert_eq!(state.metrics.dev_iterations_completed, 1);
    assert_eq!(
        state.phase,
        crate::reducer::event::PipelinePhase::CommitMessage
    );
}

#[test]
fn test_dev_iterations_completed_not_incremented_on_continuation_triggered() {
    let mut state = PipelineState::initial(1, 0);
    state = reduce(state, PipelineEvent::development_iteration_started(0));

    // Trigger continuation - should NOT increment completed counter
    let event = PipelineEvent::Development(DevelopmentEvent::ContinuationTriggered {
        iteration: 0,
        status: DevelopmentStatus::Partial,
        summary: "Partial".to_string(),
        files_changed: None,
        next_steps: None,
    });
    state = reduce(state, event);

    assert_eq!(state.metrics.dev_iterations_completed, 0);
    assert_eq!(state.metrics.dev_continuation_attempt, 1);
}

#[test]
fn test_review_pass_increments_current_pass_on_pass_started() {
    let state = PipelineState::initial(0, 3);

    let event = PipelineEvent::review_pass_started(1);
    let state = reduce(state, event);

    assert_eq!(state.metrics.current_review_pass, 1);
    assert_eq!(state.metrics.review_passes_started, 1);
}

#[test]
fn test_pass_started_retry_does_not_reset_fix_continuation_attempt_metric() {
    use crate::reducer::state::FixStatus;

    let mut state = PipelineState::initial(0, 1);
    state = reduce(state, PipelineEvent::review_pass_started(1));

    // Drive fix continuation attempt within pass.
    state = reduce(
        state,
        PipelineEvent::Review(ReviewEvent::FixContinuationTriggered {
            pass: 1,
            status: FixStatus::IssuesRemain,
            summary: None,
        }),
    );
    assert_eq!(state.metrics.fix_continuation_attempt, 1);

    // Orchestration may re-emit PassStarted for the same pass on retry.
    state = reduce(state, PipelineEvent::review_pass_started(1));
    assert_eq!(state.metrics.fix_continuation_attempt, 1);
}

#[test]
fn test_fix_continuation_attempt_increments_on_continuation_triggered() {
    use crate::reducer::state::FixStatus;

    let mut state = PipelineState::initial(0, 1);
    state = reduce(state, PipelineEvent::review_pass_started(1));

    assert_eq!(state.metrics.fix_continuation_attempt, 0);

    // Trigger fix continuation
    let event = PipelineEvent::Review(ReviewEvent::FixContinuationTriggered {
        pass: 1,
        status: FixStatus::IssuesRemain,
        summary: Some("Fixed some issues".to_string()),
    });
    state = reduce(state, event);

    assert_eq!(state.metrics.fix_continuation_attempt, 1);
    assert_eq!(state.metrics.fix_continuations_total, 1);
}

#[test]
fn test_fix_continuation_attempt_resets_on_new_pass() {
    use crate::reducer::state::FixStatus;

    let mut state = PipelineState::initial(0, 2);

    // Pass 1 with fix continuation
    state = reduce(state, PipelineEvent::review_pass_started(1));
    state = reduce(
        state,
        PipelineEvent::Review(ReviewEvent::FixContinuationTriggered {
            pass: 1,
            status: FixStatus::IssuesRemain,
            summary: None,
        }),
    );
    assert_eq!(state.metrics.fix_continuation_attempt, 1);

    // New pass should reset
    state = reduce(state, PipelineEvent::review_pass_started(2));
    assert_eq!(state.metrics.fix_continuation_attempt, 0);
}

#[test]
fn test_review_passes_completed_increments_on_clean_pass() {
    let mut state = PipelineState::initial(0, 1);
    state = reduce(state, PipelineEvent::review_pass_started(1));

    assert_eq!(state.metrics.review_passes_completed, 0);

    // Clean pass
    let event = PipelineEvent::Review(ReviewEvent::PassCompletedClean { pass: 1 });
    state = reduce(state, event);

    assert_eq!(state.metrics.review_passes_completed, 1);
}

#[test]
fn test_review_passes_completed_increments_on_fix_attempt_completed() {
    let mut state = PipelineState::initial(0, 1);
    state = reduce(state, PipelineEvent::review_pass_started(1));

    assert_eq!(state.metrics.review_passes_completed, 0);

    // Fix attempt completed
    let event = PipelineEvent::Review(ReviewEvent::FixAttemptCompleted {
        pass: 1,
        changes_made: true,
    });
    state = reduce(state, event);

    assert_eq!(state.metrics.review_passes_completed, 1);
}

#[test]
fn test_review_passes_completed_not_incremented_when_issues_found() {
    let mut state = PipelineState::initial(0, 1);
    state = reduce(state, PipelineEvent::review_pass_started(1));

    assert_eq!(state.metrics.review_passes_completed, 0);

    // Pass completed with issues found - should NOT increment
    let event = PipelineEvent::Review(ReviewEvent::Completed {
        pass: 1,
        issues_found: true,
    });
    state = reduce(state, event);

    assert_eq!(state.metrics.review_passes_completed, 0);
}

#[test]
fn test_review_passes_completed_increments_on_fix_continuation_succeeded() {
    let mut state = PipelineState::initial(0, 1);
    state = reduce(state, PipelineEvent::review_pass_started(1));

    assert_eq!(state.metrics.review_passes_completed, 0);

    // Fix continuation succeeded
    let event = PipelineEvent::Review(ReviewEvent::FixContinuationSucceeded {
        pass: 1,
        total_attempts: 2,
    });
    state = reduce(state, event);

    assert_eq!(state.metrics.review_passes_completed, 1);
    assert_eq!(
        state.phase,
        crate::reducer::event::PipelinePhase::CommitMessage
    );
}

// ==============================================================================
// Checkpoint compatibility tests
// ==============================================================================

#[test]
fn test_new_metrics_fields_checkpoint_compatible() {
    let mut state = PipelineState::initial(2, 2);

    // Set new metrics fields
    state.metrics.dev_continuation_attempt = 2;
    state.metrics.fix_continuation_attempt = 1;
    state.metrics.current_review_pass = 1;

    // Serialize
    let serialized = serde_json::to_string(&state).expect("serialize failed");

    // Deserialize
    let restored: PipelineState = serde_json::from_str(&serialized).expect("deserialize failed");

    // Verify new fields are preserved
    assert_eq!(restored.metrics.dev_continuation_attempt, 2);
    assert_eq!(restored.metrics.fix_continuation_attempt, 1);
    assert_eq!(restored.metrics.current_review_pass, 1);
}

#[test]
fn test_old_checkpoint_loads_with_new_metrics_fields_defaulted() {
    // Simulate old checkpoint JSON without new fields
    let old_checkpoint_json = r#"{
        "phase": "Development",
        "previous_phase": null,
        "iteration": 1,
        "total_iterations": 2,
        "reviewer_pass": 0,
        "total_reviewer_passes": 2,
        "review_issues_found": false,
        "context_cleaned": false,
        "agent_chain": {
            "agents": [],
            "current_agent_index": 0,
            "models_per_agent": [],
            "current_model_index": 0,
            "retry_cycle": 0,
            "max_cycles": 1,
            "retry_delay_ms": 0,
            "backoff_multiplier": 1.0,
            "max_backoff_ms": 0,
            "backoff_pending_ms": null,
            "current_role": "Developer",
            "rate_limit_continuation_prompt": null,
            "last_session_id": null
        },
        "rebase": "NotStarted",
        "commit": "NotStarted",
        "execution_history": [],
        "checkpoint_saved_count": 0,
        "continuation": {
            "previous_status": null,
            "previous_summary": null,
            "previous_files_changed": null,
            "previous_next_steps": null,
            "continuation_attempt": 0,
            "invalid_output_attempts": 0,
            "context_write_pending": false,
            "context_cleanup_pending": false,
            "xsd_retry_count": 0,
            "xsd_retry_pending": false,
            "xsd_retry_session_reuse_pending": false,
            "max_xsd_retry_count": 10,
            "max_same_agent_retry_count": 2,
            "max_continue_count": 3
        },
        "dev_fix_triggered": false,
        "prompt_inputs": {},
        "metrics": {
            "dev_iterations_started": 1,
            "dev_iterations_completed": 0,
            "dev_attempts_total": 3,
            "analysis_attempts_total": 1,
            "analysis_attempts_in_current_iteration": 1,
            "review_passes_started": 0,
            "review_passes_completed": 0,
            "review_runs_total": 0,
            "fix_runs_total": 0,
            "fix_continuations_total": 0,
            "xsd_retry_attempts_total": 2,
            "xsd_retry_planning": 0,
            "xsd_retry_development": 2,
            "xsd_retry_review": 0,
            "xsd_retry_fix": 0,
            "xsd_retry_commit": 0,
            "same_agent_retry_attempts_total": 0,
            "agent_fallbacks_total": 0,
            "model_fallbacks_total": 0,
            "retry_cycles_started_total": 0,
            "commits_created_total": 0,
            "max_dev_iterations": 2,
            "max_review_passes": 2
        }
    }"#;

    let restored: PipelineState = serde_json::from_str(old_checkpoint_json)
        .expect("old checkpoint should deserialize with defaults");

    // New fields should default to 0
    assert_eq!(restored.metrics.dev_continuation_attempt, 0);
    assert_eq!(restored.metrics.fix_continuation_attempt, 0);
    assert_eq!(restored.metrics.current_review_pass, 0);

    // Existing fields should be preserved
    assert_eq!(restored.metrics.dev_iterations_started, 1);
    assert_eq!(restored.metrics.xsd_retry_attempts_total, 2);
}
// ============================================================================
// XSD Retry Metrics Tests (Step 13)
// ============================================================================

// ============================================================================
// TimeoutOutputKind Serde Round-trip Tests (AC-1)
// ============================================================================

#[test]
fn test_timeout_output_kind_no_result_serde_roundtrip() {
    let original = TimeoutOutputKind::NoResult;
    let json = serde_json::to_string(&original).expect("serialize NoResult");
    assert_eq!(json, r#""NoResult""#);
    let restored: TimeoutOutputKind = serde_json::from_str(&json).expect("deserialize NoResult");
    assert_eq!(restored, original);
}

#[test]
fn test_timeout_output_kind_partial_result_serde_roundtrip() {
    let original = TimeoutOutputKind::PartialResult;
    let json = serde_json::to_string(&original).expect("serialize PartialResult");
    assert_eq!(json, r#""PartialResult""#);
    let restored: TimeoutOutputKind =
        serde_json::from_str(&json).expect("deserialize PartialResult");
    assert_eq!(restored, original);
}

#[test]
fn test_timeout_output_kind_no_output_alias_deserializes_as_no_result() {
    // Old checkpoints used "NoOutput"; the serde alias must map it to NoResult.
    let restored: TimeoutOutputKind =
        serde_json::from_str(r#""NoOutput""#).expect("deserialize old NoOutput alias");
    assert_eq!(restored, TimeoutOutputKind::NoResult);
}

#[test]
fn test_timeout_output_kind_partial_output_alias_deserializes_as_partial_result() {
    // Old checkpoints used "PartialOutput"; the serde alias must map it to PartialResult.
    let restored: TimeoutOutputKind =
        serde_json::from_str(r#""PartialOutput""#).expect("deserialize old PartialOutput alias");
    assert_eq!(restored, TimeoutOutputKind::PartialResult);
}

#[test]
fn test_timeout_output_kind_defaults_to_partial_result_when_missing() {
    // When a TimedOut event is received without output_kind (old checkpoint),
    // it should default to PartialResult.
    #[derive(serde::Deserialize)]
    struct TimedOutWithoutOutputKind {
        // These fields are in the JSON but not needed for the test assertion.
        // Underscore prefix indicates intentionally unused.
        #[serde(rename = "role")]
        _role: crate::agents::AgentRole,
        #[serde(rename = "agent")]
        _agent: String,
        #[serde(default = "crate::reducer::event::default_timeout_output_kind")]
        output_kind: TimeoutOutputKind,
    }
    let json = r#"{"role":"Developer","agent":"claude"}"#;
    let event: TimedOutWithoutOutputKind =
        serde_json::from_str(json).expect("deserialize without output_kind");
    assert_eq!(event.output_kind, TimeoutOutputKind::PartialResult);
}