ralph-agent-loop 0.4.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
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
//! 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 command that exits after a configurable SIGINT cleanup delay and
/// writes a readiness marker only after the SIGINT handler is installed.
fn slow_exit_command(exit_delay_ms: u64, exit_code: i32, ready_file: &std::path::Path) -> Command {
    let script = format!(
        r#"import pathlib
import signal
import sys
import time

ready_file = pathlib.Path(sys.argv[1])

def handle(_signum, _frame):
    time.sleep({delay_seconds:.3})
    raise SystemExit({exit_code})

signal.signal(signal.SIGINT, handle)
ready_file.write_text("ready", encoding="utf-8")

while True:
    time.sleep(1)
"#,
        delay_seconds = exit_delay_ms as f64 / 1000.0,
        exit_code = exit_code,
    );
    let mut cmd = Command::new("python3");
    cmd.arg("-c")
        .arg(script)
        .arg(ready_file)
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    cmd
}

/// Creates a command that deterministically ignores SIGINT and keeps running.
/// The child writes its readiness marker after installing the ignore handler.
///
/// Python's `signal.SIG_IGN` is more reliable here than shell `trap '' INT`,
/// which can still exit early on some `/bin/sh` implementations under CI load.
fn ignore_sigint_command(ready_file: &std::path::Path) -> Command {
    let mut cmd = Command::new("python3");
    cmd.arg("-c")
        .arg(
            r#"import pathlib
import signal
import sys
import time

ready_file = pathlib.Path(sys.argv[1])

signal.signal(signal.SIGINT, signal.SIG_IGN)
ready_file.write_text("ready", encoding="utf-8")

while True:
    time.sleep(1)
"#,
        )
        .arg(ready_file)
        .stdout(Stdio::null())
        .stderr(Stdio::null());
    cmd
}

fn make_ready_file() -> (tempfile::TempDir, std::path::PathBuf) {
    let dir = tempfile::tempdir().expect("create temp dir for process readiness");
    let ready_file = dir.path().join("ready");
    (dir, ready_file)
}

/// Waits until the child has written its readiness marker.
fn wait_for_ready_file(child: &mut std::process::Child, ready_file: &std::path::Path) {
    let start = std::time::Instant::now();
    while !ready_file.exists() {
        if let Some(status) = child
            .try_wait()
            .expect("poll child while waiting for ready")
        {
            panic!("test child exited before becoming ready: {status}");
        }
        if start.elapsed() > Duration::from_secs(5) {
            let _ = child.kill();
            let _ = child.wait();
            panic!("timed out waiting for test child readiness file");
        }
        std::thread::sleep(Duration::from_millis(10));
    }
}

#[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 (_ready_dir, ready_file) = make_ready_file();
    let mut cmd = slow_exit_command(100, 0, &ready_file); // 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");
    wait_for_ready_file(&mut child, &ready_file);

    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(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 via `signal.SIG_IGN`
    // 2. Continues running
    // 3. Will be killed by SIGKILL after grace period

    let (_ready_dir, ready_file) = make_ready_file();
    let mut cmd = ignore_sigint_command(&ready_file);

    #[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");
    wait_for_ready_file(&mut child, &ready_file);

    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(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 (_ready_dir, ready_file) = make_ready_file();
    let mut cmd = slow_exit_command(100, 42, &ready_file); // 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");
    wait_for_ready_file(&mut child, &ready_file);

    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(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 (_ready_dir, ready_file) = make_ready_file();
    let mut cmd = slow_exit_command(100, 0, &ready_file); // 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");
    wait_for_ready_file(&mut child, &ready_file);

    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(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 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(child, &ctrlc, None);

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

#[test]
fn test_wait_for_child_leaves_active_pgid_for_caller_cleanup() {
    // This test verifies that `wait_for_child` does not clear `active_pgid` on its own.
    // Higher-level cleanup owns that responsibility after timeout handling completes.

    let (_ready_dir, ready_file) = make_ready_file();
    let mut cmd = ignore_sigint_command(&ready_file);

    #[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");
    wait_for_ready_file(&mut child, &ready_file);

    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(child, &ctrlc, timeout);

    // `wait_for_child` should leave `active_pgid` alone.
    // Higher-level cleanup clears it after process supervision finishes.
    #[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"
    );
}

#[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: a timeout-triggered SIGINT still yields success when the
    // process exits cleanly before the hard-kill deadline, even if Ctrl-C also
    // arrives during that grace period.
    //
    // Process: traps SIGINT, waits 500ms, exits 0
    // Timeout: 50ms (triggers interrupt quickly)
    // Ctrl-C: fired at 200ms (during grace period)

    let (_ready_dir, ready_file) = make_ready_file();
    let mut cmd = slow_exit_command(500, 0, &ready_file); // 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");
    wait_for_ready_file(&mut child, &ready_file);

    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(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"
    );
}