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
//! Connectivity checking via TCP probing.
//!
//! Provides network connectivity detection for the offline freeze-and-resume feature.
//! Uses TCP connections to known-good hosts to verify connectivity.

use std::net::{SocketAddr, TcpStream};
use std::time::Duration;

use crate::reducer::effect::EffectResult;
use crate::reducer::event::{AgentEvent, PipelineEvent};
use crate::reducer::state::ConnectivityState;
use crate::reducer::ui_event::UIEvent;

/// Probe targets: Cloudflare DNS (1.1.1.1:443) and Google DNS (8.8.8.8:53).
///
/// Using direct IPs avoids DNS dependency, making the check more fundamental.
/// The any() combinator means only ONE needs to succeed.
const PROBE_TARGETS: &[(&str, u16)] = &[("1.1.1.1", 443), ("8.8.8.8", 53)];

/// TCP connection timeout for each probe.
const PROBE_TIMEOUT: Duration = Duration::from_secs(5);

/// Probe network connectivity by attempting TCP connections to known-good hosts.
///
/// Returns `true` if at least one probe target is reachable, `false` otherwise.
/// This is a blocking I/O operation in the boundary layer (architecturally correct).
fn probe_connectivity() -> bool {
    PROBE_TARGETS.iter().any(|(host, port)| {
        let addr: SocketAddr = match format!("{host}:{port}").parse() {
            Ok(a) => a,
            Err(_) => return false,
        };
        TcpStream::connect_timeout(&addr, PROBE_TIMEOUT).is_ok()
    })
}

/// Determine UI message for connectivity check based on current state.
///
/// Pure policy function - determines what message to show based on connectivity state.
fn connectivity_check_message(is_offline: bool, check_pending: bool) -> String {
    if is_offline {
        "Still offline — verifying connectivity...".to_string()
    } else if check_pending {
        "Verifying network connectivity...".to_string()
    } else {
        "Checking network connectivity...".to_string()
    }
}

/// Determine UI message for offline polling based on current state.
///
/// Pure policy function - determines what message to show during polling.
fn offline_poll_message(is_offline: bool, poll_pending: bool) -> String {
    if is_offline && poll_pending {
        "Still offline — polling for connectivity...".to_string()
    } else if is_offline {
        "Offline detected — run paused. No continuation budget or retry budget is being consumed. Waiting for connectivity to return.".to_string()
    } else {
        "Polling for connectivity...".to_string()
    }
}

/// Determine UI message for resume confirmation when connectivity is restored.
///
/// Pure policy function - returns the message shown when the pipeline resumes
/// from an offline state.
fn resume_confirmation_message() -> String {
    "Connectivity restored — resuming workflow. No continuation budget or retry budget was consumed during the offline window.".to_string()
}

/// Message type enumeration for poll UI messages.
///
/// Pure policy enum - categorizes the type of poll message to emit.
#[derive(Clone, Copy)]
enum PollUiMessageType {
    /// Emit offline polling message only (still offline).
    OfflinePoll,
    /// Emit connectivity check message only (not offline).
    ConnectivityCheck,
    /// Emit resume confirmation after offline polling context (restoring from offline).
    ResumeConfirmation,
}

/// Determine which type of poll UI message to emit.
///
/// Pure policy function - makes the branching decision about which message type
/// to emit based on connectivity state and probe result.
fn determine_poll_ui_message_type(
    was_offline: bool,
    is_offline: bool,
    probe_result: bool,
) -> PollUiMessageType {
    if was_offline && probe_result {
        // Connectivity restored while we were offline: emit resume confirmation
        PollUiMessageType::ResumeConfirmation
    } else if is_offline {
        // Still offline: emit offline polling message
        PollUiMessageType::OfflinePoll
    } else {
        // Not offline: emit connectivity check message
        PollUiMessageType::ConnectivityCheck
    }
}

/// Determine which UI messages to emit after a connectivity poll.
///
/// Thin wiring function - calls pure policy function and translates result to messages.
/// Boundary functions should contain no policy logic, only wiring.
fn poll_ui_messages(
    was_offline: bool,
    connectivity: &ConnectivityState,
    probe_result: bool,
) -> Vec<String> {
    let message_type =
        determine_poll_ui_message_type(was_offline, connectivity.is_offline, probe_result);
    match message_type {
        PollUiMessageType::ResumeConfirmation => vec![
            offline_poll_message(connectivity.is_offline, connectivity.poll_pending),
            resume_confirmation_message(),
        ],
        PollUiMessageType::OfflinePoll => {
            vec![offline_poll_message(
                connectivity.is_offline,
                connectivity.poll_pending,
            )]
        }
        PollUiMessageType::ConnectivityCheck => {
            vec![connectivity_check_message(
                connectivity.is_offline,
                connectivity.check_pending,
            )]
        }
    }
}

/// Determine which UI events to emit after a connectivity poll.
///
/// Thin wiring function - calls pure policy function and wraps result in UIEvent.
/// Boundary functions should contain no policy logic, only wiring.
fn poll_ui_events(
    was_offline: bool,
    connectivity: &ConnectivityState,
    probe_result: bool,
) -> Vec<UIEvent> {
    poll_ui_messages(was_offline, connectivity, probe_result)
        .into_iter()
        .map(|msg| UIEvent::AgentActivity {
            agent: "connectivity".to_string(),
            message: msg,
        })
        .collect()
}

/// Determine the connectivity event based on probe result.
///
/// Pure policy function - maps probe result to the appropriate event.
fn poll_connectivity_event(probe_result: bool) -> PipelineEvent {
    if probe_result {
        PipelineEvent::Agent(AgentEvent::ConnectivityCheckSucceeded)
    } else {
        PipelineEvent::Agent(AgentEvent::ConnectivityCheckFailed)
    }
}

/// Handle the `CheckNetworkConnectivity` effect.
///
/// This is a one-time connectivity probe triggered immediately after a Network-class
/// agent failure. It probes connectivity and emits the appropriate event:
/// - If online: `AgentEvent::ConnectivityCheckSucceeded`
/// - If offline: `AgentEvent::ConnectivityCheckFailed`
///
/// The reducer processes these events to update ConnectivityState.
pub(super) fn check_network_connectivity(connectivity: &ConnectivityState) -> EffectResult {
    let message = connectivity_check_message(connectivity.is_offline, connectivity.check_pending);
    let ui_event = UIEvent::AgentActivity {
        agent: "connectivity".to_string(),
        message,
    };

    if probe_connectivity() {
        EffectResult::event(PipelineEvent::Agent(AgentEvent::ConnectivityCheckSucceeded))
            .with_ui_event(ui_event)
    } else {
        EffectResult::event(PipelineEvent::Agent(AgentEvent::ConnectivityCheckFailed))
            .with_ui_event(ui_event)
    }
}

/// Handle the `PollForConnectivity` effect.
///
/// This is emitted repeatedly while `is_offline=true`. Each execution:
/// 1. Sleeps for `interval_ms`
/// 2. Probes connectivity
/// 3. Emits the appropriate event:
///    - If still offline: `AgentEvent::ConnectivityCheckFailed`
///    - If back online: `AgentEvent::ConnectivityCheckSucceeded`
///    - If back online while offline: emits resume confirmation UIEvent
///
/// The orchestrator re-derives this effect each cycle while offline, providing
/// debounced polling without handler-side loops.
pub(super) fn poll_for_connectivity(
    interval_ms: u64,
    connectivity: &ConnectivityState,
) -> EffectResult {
    let was_offline = connectivity.is_offline;

    std::thread::sleep(Duration::from_millis(interval_ms));

    let probe_result = probe_connectivity();

    // Pure policy: determine event and UI events
    let event = poll_connectivity_event(probe_result);
    let ui_events = poll_ui_events(was_offline, connectivity, probe_result);

    // Emit all UI events via chained with_ui_event calls
    let mut result = EffectResult::event(event);
    for ui_event in ui_events {
        result = result.with_ui_event(ui_event);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_probe_connectivity_does_not_panic() {
        // Verify the function executes without panicking (return type is bool)
        let _ = probe_connectivity();
    }

    #[test]
    fn test_check_network_connectivity_returns_effect_result() {
        let connectivity_state = ConnectivityState::default();
        let result = check_network_connectivity(&connectivity_state);
        // Should always return an event (not Option)
        let _ = result.event;
    }

    #[test]
    fn test_check_network_connectivity_emits_ui_event_when_online() {
        let connectivity_state = ConnectivityState::default();
        let result = check_network_connectivity(&connectivity_state);
        // Should have a UI event
        assert!(
            !result.ui_events.is_empty(),
            "Should emit UI event when checking connectivity"
        );
    }

    #[test]
    fn test_poll_for_connectivity_returns_effect_result() {
        // Use a very short interval for testing
        let connectivity_state = ConnectivityState {
            is_offline: true,
            poll_pending: true,
            ..ConnectivityState::default()
        };
        let result = poll_for_connectivity(1, &connectivity_state);
        // Should always return an event (not Option)
        let _ = result.event;
    }

    #[test]
    fn test_poll_for_connectivity_emits_ui_event() {
        let connectivity_state = ConnectivityState {
            is_offline: true,
            poll_pending: true,
            ..ConnectivityState::default()
        };
        let result = poll_for_connectivity(1, &connectivity_state);
        // Should have a UI event
        assert!(
            !result.ui_events.is_empty(),
            "Should emit UI event when polling for connectivity"
        );
    }

    #[test]
    fn test_poll_for_connectivity_emits_resume_confirmation_on_restore() {
        // Verify the message-selection logic directly via the resume_confirmation_message helper.
        // The actual network probe result is not testable in a unit test, so we test the
        // policy function directly.
        let resume = resume_confirmation_message();
        assert!(
            resume.contains("Connectivity restored"),
            "Resume message should contain 'Connectivity restored', got: {resume}"
        );
        assert!(
            resume.contains("resuming") || resume.contains("resume"),
            "Resume message should mention resuming workflow, got: {resume}"
        );
    }

    #[test]
    fn test_poll_ui_events_does_not_emit_resume_when_still_offline() {
        // Given: state that was offline and probe failed (still offline)
        let connectivity = ConnectivityState {
            is_offline: true,
            poll_pending: true,
            ..ConnectivityState::default()
        };
        let was_offline = true;
        let probe_result = false; // Probe failed - still offline!

        // When: poll_ui_events is called
        let events = poll_ui_events(was_offline, &connectivity, probe_result);

        // Then: should NOT emit resume message
        assert_eq!(
            events.len(),
            1,
            "Should emit exactly 1 event when still offline"
        );
        let msg = match &events[0] {
            UIEvent::AgentActivity { message, .. } => message.clone(),
            other => panic!("Expected AgentActivity, got: {:?}", other),
        };
        assert!(
            !msg.contains("Connectivity restored"),
            "Should NOT emit resume message when probe failed, got: {}",
            msg
        );
        assert!(
            msg.contains("Still offline"),
            "Should emit still-offline message, got: {}",
            msg
        );
    }

    #[test]
    fn test_poll_ui_events_emits_resume_when_online_restored() {
        // Given: state that was offline but probe succeeded (connectivity restored)
        let connectivity = ConnectivityState {
            is_offline: true, // Still true because reducer hasn't processed event yet
            poll_pending: true,
            ..ConnectivityState::default()
        };
        let was_offline = true;
        let probe_result = true; // Probe succeeded - connectivity restored!

        // When: poll_ui_events is called
        let events = poll_ui_events(was_offline, &connectivity, probe_result);

        // Then: should emit TWO events on restore:
        // 1. First: offline polling context message
        // 2. Second: resume confirmation message
        assert_eq!(events.len(), 2, "Should emit exactly 2 events on restore");

        // First event: offline polling context
        let first_msg = match &events[0] {
            UIEvent::AgentActivity { message, .. } => message.clone(),
            other => panic!("Expected AgentActivity, got: {:?}", other),
        };
        assert!(
            first_msg.contains("Still offline") || first_msg.contains("polling"),
            "First event should be offline polling context, got: {}",
            first_msg
        );

        // Second event: resume confirmation
        let second_msg = match &events[1] {
            UIEvent::AgentActivity { message, .. } => message.clone(),
            other => panic!("Expected AgentActivity, got: {:?}", other),
        };
        assert!(
            second_msg.contains("Connectivity restored"),
            "Second event should be resume message, got: {}",
            second_msg
        );
    }

    #[test]
    fn test_poll_ui_events_emits_check_message_when_online_and_not_offline() {
        // Given: state that was online (not offline) and probe succeeded
        let connectivity = ConnectivityState {
            is_offline: false,
            check_pending: true,
            ..ConnectivityState::default()
        };
        let was_offline = false;
        let probe_result = true;

        // When: poll_ui_events is called
        let events = poll_ui_events(was_offline, &connectivity, probe_result);

        // Then: should emit connectivity check message (not resume, not offline)
        assert_eq!(events.len(), 1, "Should emit exactly 1 event");
        let msg = match &events[0] {
            UIEvent::AgentActivity { message, .. } => message.clone(),
            other => panic!("Expected AgentActivity, got: {:?}", other),
        };
        assert!(
            !msg.contains("Connectivity restored"),
            "Should NOT emit resume when was_offline=false, got: {}",
            msg
        );
        assert!(
            !msg.contains("Still offline"),
            "Should NOT emit still-offline when online, got: {}",
            msg
        );
    }
}