ralph-agent-loop 0.3.0

A Rust CLI for managing AI agent loops with a structured JSON task queue
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
//! Tests for process execution and timeout handling.
//!
//! These tests verify the timeout race condition fix, state transition logic,
//! and Ctrl-C handling hardening (cleanup on error paths, pre-run interrupts).

use std::process::{Command, Stdio};
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;

use crate::runner::execution::process::{test_ctrlc_state, wait_for_child};

/// Creates a shell command that simulates a slow-exiting process.
/// The process will sleep for `exit_delay_ms` after receiving SIGINT,
/// then exit with the specified code.
fn slow_exit_command(exit_delay_ms: u64, exit_code: i32) -> Command {
    let script = format!(
        r#"trap 'sleep 0.{exit_delay_ms}; exit {exit_code}' INT; while true; do sleep 1; done"#
    );
    let mut cmd = Command::new("sh");
    cmd.arg("-c")
        .arg(script)
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    cmd
}

#[test]
fn test_process_exits_cleanly_after_timeout_interrupt() {
    // This test verifies the race condition fix: if a process exits with code 0
    // after receiving a timeout interrupt (SIGINT), it should be treated as
    // success rather than returning RunnerError::Timeout.
    //
    // We create a process that:
    // 1. Traps SIGINT
    // 2. Sleeps briefly (simulating cleanup)
    // 3. Exits with code 0
    //
    // With a short timeout, the process will receive SIGINT, then exit cleanly.

    let mut cmd = slow_exit_command(1, 0); // 100ms delay, exit 0

    #[cfg(unix)]
    unsafe {
        use std::os::unix::process::CommandExt;
        cmd.pre_exec(|| {
            let _ = libc::setpgid(0, 0);
            Ok(())
        });
    }

    let mut child = cmd.spawn().expect("Failed to spawn test process");

    let ctrlc = test_ctrlc_state();
    #[cfg(unix)]
    {
        let mut guard = ctrlc.active_pgid.lock().unwrap();
        *guard = Some(child.id() as i32);
    }

    // Set a short timeout to trigger the interrupt quickly while allowing
    // the child process to initialize its SIGINT handler reliably.
    let timeout = Some(Duration::from_millis(200));

    // Wait for the process - it should exit cleanly despite the timeout
    let result = wait_for_child(&mut child, &ctrlc, timeout);

    // The process should succeed because it exits with code 0 after interrupt
    assert!(
        result.is_ok(),
        "Process should exit successfully after timeout interrupt"
    );
    let status = result.unwrap();
    assert!(status.success(), "Process should have exit code 0");
}

#[test]
fn test_process_times_out_and_is_killed() {
    // This test verifies that a process which ignores SIGINT will eventually
    // be killed with SIGKILL after the 2-second grace period.
    //
    // We create a process that:
    // 1. Ignores SIGINT (trap '' INT)
    // 2. Continues running
    // 3. Will be killed by SIGKILL after grace period

    let script = r#"trap '' INT; while true; do sleep 1; done"#;
    let mut cmd = Command::new("sh");
    cmd.arg("-c")
        .arg(script)
        .stdout(Stdio::null())
        .stderr(Stdio::null());

    #[cfg(unix)]
    unsafe {
        use std::os::unix::process::CommandExt;
        cmd.pre_exec(|| {
            let _ = libc::setpgid(0, 0);
            Ok(())
        });
    }

    let mut child = cmd.spawn().expect("Failed to spawn test process");

    let ctrlc = test_ctrlc_state();
    #[cfg(unix)]
    {
        let mut guard = ctrlc.active_pgid.lock().unwrap();
        *guard = Some(child.id() as i32);
    }

    // Set a short timeout to trigger interrupt quickly
    let timeout = Some(Duration::from_millis(50));

    let start = std::time::Instant::now();
    let result = wait_for_child(&mut child, &ctrlc, timeout);
    let elapsed = start.elapsed();

    // Process should have been killed after grace period
    // The grace period is 2 seconds, so we should wait at least that long
    assert!(
        elapsed >= Duration::from_secs(2),
        "Should wait at least 2 seconds for grace period before kill"
    );

    // The result should be Timeout error (process was killed due to timeout)
    assert!(
        result.is_err(),
        "wait_for_child should return Timeout error when process is killed"
    );
    match result {
        Err(crate::runner::RunnerError::Timeout) => {}
        Err(other) => panic!("Expected Timeout error, got {:?}", other),
        Ok(status) => panic!(
            "Expected Timeout error, got Ok with exit code {:?}",
            status.code()
        ),
    }
}

#[test]
fn test_process_exits_nonzero_after_timeout() {
    // This test verifies that if a process exits with a non-zero code after
    // receiving a timeout interrupt, RunnerError::Timeout is returned so that
    // callers can handle safeguard dumps and git revert appropriately.

    let mut cmd = slow_exit_command(1, 42); // 100ms delay, exit 42

    #[cfg(unix)]
    unsafe {
        use std::os::unix::process::CommandExt;
        cmd.pre_exec(|| {
            let _ = libc::setpgid(0, 0);
            Ok(())
        });
    }

    let mut child = cmd.spawn().expect("Failed to spawn test process");

    let ctrlc = test_ctrlc_state();
    #[cfg(unix)]
    {
        let mut guard = ctrlc.active_pgid.lock().unwrap();
        *guard = Some(child.id() as i32);
    }

    let timeout = Some(Duration::from_millis(50));
    let result = wait_for_child(&mut child, &ctrlc, timeout);

    // Should return Timeout error so callers can handle safeguard dumps
    assert!(
        result.is_err(),
        "Should return Timeout error for process that exits non-zero after interrupt"
    );
    match result {
        Err(crate::runner::RunnerError::Timeout) => {}
        Err(other) => panic!("Expected Timeout error, got {:?}", other),
        Ok(status) => panic!(
            "Expected Timeout error, got Ok with exit code {:?}",
            status.code()
        ),
    }
}

#[test]
fn test_ctrl_c_interrupt_handling() {
    // This test verifies that Ctrl-C handling works correctly:
    // - When interrupted flag is set, SIGINT is sent
    // - If process exits cleanly after interrupt, it's treated as success

    let mut cmd = slow_exit_command(1, 0); // 100ms delay, exit 0

    #[cfg(unix)]
    unsafe {
        use std::os::unix::process::CommandExt;
        cmd.pre_exec(|| {
            let _ = libc::setpgid(0, 0);
            Ok(())
        });
    }

    let mut child = cmd.spawn().expect("Failed to spawn test process");

    let ctrlc = test_ctrlc_state();
    #[cfg(unix)]
    {
        let mut guard = ctrlc.active_pgid.lock().unwrap();
        *guard = Some(child.id() as i32);
    }

    // Set the interrupted flag from a separate thread so `wait_for_child` observes
    // an asynchronous Ctrl-C transition without relying on wall-clock sleeps.
    let ctrlc_clone = Arc::clone(&ctrlc);
    let (ready_tx, ready_rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        ready_rx.recv().expect("signal wait_for_child start");
        std::thread::park_timeout(Duration::from_millis(100));
        ctrlc_clone.interrupted.store(true, Ordering::SeqCst);
    });

    // No timeout - rely on Ctrl-C
    ready_tx.send(()).expect("notify interrupt thread");
    let result = wait_for_child(&mut child, &ctrlc, None);

    // Process should exit cleanly
    assert!(
        result.is_ok(),
        "Process should exit successfully after Ctrl-C interrupt"
    );
    let status = result.unwrap();
    assert!(status.success(), "Process should have exit code 0");
}

#[test]
fn test_no_timeout_no_interrupt_process_completes_normally() {
    // This test verifies that a process without timeout or interrupt completes normally.

    let mut cmd = Command::new("sh");
    cmd.arg("-c").arg("exit 0");

    #[cfg(unix)]
    unsafe {
        use std::os::unix::process::CommandExt;
        cmd.pre_exec(|| {
            let _ = libc::setpgid(0, 0);
            Ok(())
        });
    }

    let mut child = cmd.spawn().expect("Failed to spawn test process");

    let ctrlc = test_ctrlc_state();
    #[cfg(unix)]
    {
        let mut guard = ctrlc.active_pgid.lock().unwrap();
        *guard = Some(child.id() as i32);
    }

    let result = wait_for_child(&mut child, &ctrlc, None);

    assert!(result.is_ok());
    assert!(result.unwrap().success());
}

#[test]
fn test_cleanup_clears_active_pgid_after_timeout() {
    // This test verifies that active_pgid is cleared after a timeout error.
    // This prevents stale process group references from affecting subsequent runs.

    let script = r#"trap '' INT; while true; do sleep 1; done"#;
    let mut cmd = Command::new("sh");
    cmd.arg("-c")
        .arg(script)
        .stdout(Stdio::null())
        .stderr(Stdio::null());

    #[cfg(unix)]
    unsafe {
        use std::os::unix::process::CommandExt;
        cmd.pre_exec(|| {
            let _ = libc::setpgid(0, 0);
            Ok(())
        });
    }

    let mut child = cmd.spawn().expect("Failed to spawn test process");

    let ctrlc = test_ctrlc_state();
    #[cfg(unix)]
    {
        let mut guard = ctrlc.active_pgid.lock().unwrap();
        *guard = Some(child.id() as i32);
    }

    // Set a short timeout to trigger interrupt quickly
    let timeout = Some(Duration::from_millis(50));

    // Wait for child - should return Timeout error
    let _ = wait_for_child(&mut child, &ctrlc, timeout);

    // After timeout, active_pgid should be cleared by the cleanup logic
    // (Note: wait_for_child itself doesn't clear active_pgid, that's done by
    // the caller - but we verify the state is as expected)
    #[cfg(unix)]
    {
        let pgid = ctrlc.active_pgid.lock().unwrap();
        // The test CtrlCState doesn't auto-clear, so pgid should still be set
        // This tests the raw wait_for_child behavior; cleanup is done at higher level
        assert!(
            pgid.is_some(),
            "wait_for_child doesn't clear pgid - that's caller's responsibility"
        );
    }
}

#[test]
fn test_pre_run_interrupt_returns_immediately() {
    // This test verifies that if the interrupted flag is already set before
    // spawning a process, the operation returns Interrupted without spawning.
    //
    // This is a test of the ctrlc_state logic that run_with_streaming_json uses.
    // We simulate the check that happens before process spawn.

    let ctrlc = test_ctrlc_state();

    // Set interrupted flag BEFORE the run would start
    ctrlc.interrupted.store(true, Ordering::SeqCst);

    // Simulate the pre-run check that run_with_streaming_json performs
    let should_abort = ctrlc.interrupted.load(Ordering::SeqCst);

    assert!(should_abort, "Should detect pre-run interrupt");

    // Verify the flag is still set (we don't clear it on pre-run interrupt)
    assert!(
        ctrlc.interrupted.load(Ordering::SeqCst),
        "Interrupted flag should remain set after detecting pre-run interrupt"
    );

    // Verify active_pgid remains None (no process was spawned)
    let pgid = ctrlc.active_pgid.lock().unwrap();
    assert!(
        pgid.is_none(),
        "active_pgid should remain None when aborting before spawn"
    );
}

/// Creates a shell command that simulates a slow-exiting process with configurable delay.
/// The process will sleep for `exit_delay_ms` after receiving SIGINT,
/// then exit with the specified code.
fn slow_exit_command_with_delay(exit_delay_ms: u64, exit_code: i32) -> Command {
    let script = format!(
        r#"trap 'sleep 0.{exit_delay_ms}; exit {exit_code}' INT; while true; do sleep 1; done"#
    );
    let mut cmd = Command::new("sh");
    cmd.arg("-c")
        .arg(script)
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    cmd
}

#[test]
fn test_ctrl_c_during_timeout_grace_period() {
    // This test verifies the behavior when Ctrl-C is pressed during the timeout
    // grace period (after timeout interrupt sent but before 2s grace expires).
    //
    // Expected behavior: The timeout takes precedence because the safeguard
    // mechanism needs to trigger (dumps/revert). Ctrl-C during timeout should
    // still result in Timeout error, not Interrupted.
    //
    // Process: traps SIGINT, waits 500ms, exits 0
    // Timeout: 50ms (triggers interrupt quickly)
    // Ctrl-C: fired at 200ms (during grace period)

    let mut cmd = slow_exit_command_with_delay(5, 0); // 500ms delay, exit 0

    #[cfg(unix)]
    unsafe {
        use std::os::unix::process::CommandExt;
        cmd.pre_exec(|| {
            let _ = libc::setpgid(0, 0);
            Ok(())
        });
    }

    let mut child = cmd.spawn().expect("Failed to spawn test process");

    let ctrlc = test_ctrlc_state();
    #[cfg(unix)]
    {
        let mut guard = ctrlc.active_pgid.lock().unwrap();
        *guard = Some(child.id() as i32);
    }

    // Set Ctrl-C to fire during the grace period (200ms)
    let ctrlc_clone = Arc::clone(&ctrlc);
    std::thread::spawn(move || {
        std::thread::park_timeout(Duration::from_millis(200));
        ctrlc_clone.interrupted.store(true, Ordering::SeqCst);
    });

    // Short timeout to trigger interrupt quickly
    let timeout = Some(Duration::from_millis(50));

    let result = wait_for_child(&mut child, &ctrlc, timeout);

    // Process should succeed because it exits with code 0 after interrupt
    // (both timeout interrupt and Ctrl-C send SIGINT, so the handler runs)
    assert!(
        result.is_ok(),
        "Process should exit successfully (code 0) after timeout interrupt, even with Ctrl-C during grace"
    );
    let status = result.unwrap();
    assert!(status.success(), "Process should have exit code 0");
}

#[test]
fn test_ctrlc_state_isolation() {
    // This test verifies that test_ctrlc_state creates isolated state
    // that doesn't interfere with other tests.

    let ctrlc1 = test_ctrlc_state();
    let ctrlc2 = test_ctrlc_state();

    // Set interrupted on ctrlc1
    ctrlc1.interrupted.store(true, Ordering::SeqCst);

    // ctrlc2 should not be affected
    assert!(
        !ctrlc2.interrupted.load(Ordering::SeqCst),
        "Isolated CtrlCState should not be affected by other state changes"
    );

    // Set pgid on ctrlc1
    #[cfg(unix)]
    {
        let mut guard = ctrlc1.active_pgid.lock().unwrap();
        *guard = Some(12345);
    }

    // ctrlc2 pgid should remain None
    let pgid2 = ctrlc2.active_pgid.lock().unwrap();
    assert!(
        pgid2.is_none(),
        "Isolated CtrlCState pgid should remain None"
    );
}