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
449
450
451
//! Cleanup guard for parallel run loop to ensure resources are cleaned up on any exit path.
//!
//! Responsibilities:
//! - Own and manage resources that need cleanup: in-flight workers,
//!   workspace directories, and parallel state.
//! - Perform best-effort cleanup on Drop to prevent resource leaks on early returns.
//!
//! Not handled here:
//! - Actual worker execution logic (see `super::worker`).
//! - Integration loop execution (see `super::integration`).
//! - State persistence format (see `super::state`).
//!
//! Invariants/assumptions:
//! - Cleanup is idempotent and best-effort; errors are logged but not propagated.
//! - Drop must never panic (no RefCell to avoid panic during unwinding).
//! - The guard owns resources and releases them during cleanup.

use crate::commands::run::parallel::state;
use crate::commands::run::parallel::worker::{FinishedWorker, WorkerState, terminate_workers};
use crate::commands::run::parallel::workspace_cleanup::remove_workspace_best_effort;
use crate::git::WorkspaceSpec;
use anyhow::Result;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::mpsc::{self, Receiver, Sender};
use std::time::Duration;

/// Guard that ensures cleanup of parallel run resources on any exit path.
///
/// This guard owns all resources that must be cleaned up, so early returns
/// via `?` still trigger cleanup through the Drop implementation.
///
/// Uses &mut self methods (no RefCell) to guarantee Drop never panics.
pub(crate) struct ParallelCleanupGuard {
    /// Path to the parallel state file.
    state_path: PathBuf,
    /// In-memory parallel state (persisted during cleanup).
    state_file: state::ParallelStateFile,
    /// Map of in-flight worker processes (terminated during cleanup).
    in_flight: HashMap<String, WorkerState>,
    /// Map of all known workspaces (removed during cleanup).
    workspaces: HashMap<String, WorkspaceSpec>,
    /// Root directory for workspaces.
    workspace_root: PathBuf,
    /// Worker-exit event channel shared with monitor threads.
    worker_events_tx: Sender<FinishedWorker>,
    worker_events_rx: Receiver<FinishedWorker>,
    /// Whether cleanup has already been performed.
    completed: bool,
}

impl ParallelCleanupGuard {
    /// Create a new cleanup guard for direct-push parallel orchestration.
    pub fn new_simple(
        state_path: PathBuf,
        state_file: state::ParallelStateFile,
        workspace_root: PathBuf,
    ) -> Self {
        let (worker_events_tx, worker_events_rx) = mpsc::channel();
        Self {
            state_path,
            state_file,
            in_flight: HashMap::new(),
            workspaces: HashMap::new(),
            workspace_root,
            worker_events_tx,
            worker_events_rx,
            completed: false,
        }
    }

    /// Mark cleanup as completed successfully (disarms the guard).
    ///
    /// After calling this, Drop will be a no-op.
    pub fn mark_completed(&mut self) {
        self.completed = true;
    }

    /// Get mutable access to the state file.
    pub fn state_file_mut(&mut self) -> &mut state::ParallelStateFile {
        &mut self.state_file
    }

    /// Get immutable access to the state file.
    pub fn state_file(&self) -> &state::ParallelStateFile {
        &self.state_file
    }

    /// Get immutable access to in-flight workers.
    pub fn in_flight(&self) -> &HashMap<String, WorkerState> {
        &self.in_flight
    }

    /// Clone the shared worker-event sender for new monitor threads.
    pub fn worker_event_sender(&self) -> Sender<FinishedWorker> {
        self.worker_events_tx.clone()
    }

    /// Drain worker-exit events that are already available.
    pub fn drain_finished_workers(&mut self) -> Vec<FinishedWorker> {
        let mut finished = Vec::new();
        while let Ok(event) = self.worker_events_rx.try_recv() {
            finished.push(event);
        }
        finished
    }

    /// Wait for at least one worker-exit event up to the provided timeout, then drain the rest.
    pub fn wait_for_finished_workers(&mut self, timeout: Duration) -> Vec<FinishedWorker> {
        match self.worker_events_rx.recv_timeout(timeout) {
            Ok(first) => {
                let mut finished = Vec::new();
                finished.push(first);
                finished.extend(self.drain_finished_workers());
                finished
            }
            Err(mpsc::RecvTimeoutError::Timeout) => Vec::new(),
            Err(mpsc::RecvTimeoutError::Disconnected) => Vec::new(),
        }
    }

    /// Register a workspace for cleanup.
    pub fn register_workspace(&mut self, task_id: String, spec: WorkspaceSpec) {
        self.workspaces.insert(task_id, spec);
    }

    /// Register an in-flight worker for cleanup.
    pub fn register_worker(&mut self, task_id: String, worker: WorkerState) {
        self.in_flight.insert(task_id, worker);
    }

    /// Remove a worker from cleanup tracking (e.g., after completion).
    pub fn remove_worker(&mut self, task_id: &str) -> Option<WorkerState> {
        self.in_flight.remove(task_id)
    }

    /// Perform full cleanup and return any error.
    ///
    /// This is idempotent - safe to call multiple times.
    pub fn cleanup(&mut self) -> Result<()> {
        if self.completed {
            return Ok(());
        }

        log::debug!("ParallelCleanupGuard: performing cleanup");

        // Step 1: Terminate in-flight workers
        terminate_workers(&mut self.in_flight);

        // Step 2: Remove tracked workspaces except blocked_push workspaces.
        // Blocked workspaces are retained for explicit operator retry.
        let blocked_task_ids: HashSet<String> = self
            .state_file
            .workers
            .iter()
            .filter(|worker| matches!(worker.lifecycle, state::WorkerLifecycle::BlockedPush))
            .map(|worker| worker.task_id.trim().to_string())
            .collect();
        for (task_id, spec) in &self.workspaces {
            if blocked_task_ids.contains(task_id.trim()) {
                continue;
            }
            if spec.path.exists() {
                remove_workspace_best_effort(&self.workspace_root, spec, "cleanup guard");
            }
        }

        // Step 3: Drop non-terminal workers and persist state.
        // Terminal workers are retained for status/retry visibility.
        self.state_file
            .workers
            .retain(state::WorkerRecord::is_terminal);
        if let Err(err) = state::save_state(&self.state_path, &self.state_file) {
            log::warn!("Failed to save parallel state during cleanup: {:#}", err);
        }

        self.completed = true;
        Ok(())
    }

    /// Perform cleanup, logging and suppressing any errors.
    ///
    /// This is called from Drop to ensure cleanup never panics.
    fn cleanup_best_effort(&mut self) {
        if let Err(err) = self.cleanup() {
            log::warn!("ParallelCleanupGuard: cleanup error: {:#}", err);
        }
    }
}

impl Drop for ParallelCleanupGuard {
    fn drop(&mut self) {
        // Ensure cleanup runs even if the guard is dropped without explicit cleanup call
        self.cleanup_best_effort();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::run::parallel::worker::start_worker_monitor;
    use crate::lock;
    use std::process::{Child, Command};
    use tempfile::TempDir;

    fn create_test_guard(temp: &TempDir) -> ParallelCleanupGuard {
        let workspace_root = temp.path().join("workspaces");
        std::fs::create_dir_all(&workspace_root).unwrap();

        let state_path = temp.path().join("state.json");
        let state_file =
            state::ParallelStateFile::new("2026-02-20T00:00:00Z".to_string(), "main".to_string());

        ParallelCleanupGuard::new_simple(state_path, state_file, workspace_root)
    }

    fn register_sleeping_worker(
        guard: &mut ParallelCleanupGuard,
        temp: &TempDir,
        task_id: &str,
    ) -> Result<u32> {
        let child: Child = Command::new("sleep").arg("10").spawn()?;
        let pid = child.id();
        let workspace_path = temp.path().join("workspaces").join(task_id);
        std::fs::create_dir_all(&workspace_path)?;

        let worker = start_worker_monitor(
            task_id,
            "Test task".to_string(),
            WorkspaceSpec {
                path: workspace_path,
                branch: "main".to_string(),
            },
            child,
            guard.worker_event_sender(),
        );
        guard.register_worker(task_id.to_string(), worker);
        Ok(pid)
    }

    #[cfg(unix)]
    fn kill_test_process(pid: u32) {
        // SAFETY: test-owned child pid; failure is tolerated because the process may already exit.
        unsafe {
            let _ = libc::kill(pid as i32, libc::SIGKILL);
        }
    }

    #[cfg(windows)]
    fn kill_test_process(pid: u32) {
        let _ = Command::new("taskkill")
            .args(["/PID", &pid.to_string(), "/T", "/F"])
            .status();
    }

    #[cfg(all(not(unix), not(windows)))]
    fn kill_test_process(_pid: u32) {}

    #[test]
    fn guard_cleanup_kills_worker_and_clears_state() -> Result<()> {
        let temp = TempDir::new()?;
        let mut guard = create_test_guard(&temp);

        let pid = register_sleeping_worker(&mut guard, &temp, "RQ-0001")?;
        let workspace_path = temp.path().join("workspaces").join("RQ-0001");
        guard
            .state_file_mut()
            .upsert_worker(state::WorkerRecord::new(
                "RQ-0001",
                workspace_path.clone(),
                "2026-02-20T00:00:00Z".to_string(),
            ));

        // Verify worker is running
        assert_eq!(
            lock::pid_is_running(pid),
            Some(true),
            "Worker should be running before cleanup"
        );

        // Perform cleanup
        guard.cleanup()?;

        // Verify worker is terminated (allow for indeterminate result)
        let running = lock::pid_is_running(pid);
        assert!(
            running == Some(false) || running.is_none(),
            "Worker should be terminated after cleanup, got: {:?}",
            running
        );

        // Verify state is cleared
        assert!(
            guard.state_file.workers.is_empty(),
            "workers should be empty after cleanup"
        );

        Ok(())
    }

    #[test]
    fn guard_disarm_prevents_cleanup() -> Result<()> {
        let temp = TempDir::new()?;
        let mut guard = create_test_guard(&temp);

        let pid = register_sleeping_worker(&mut guard, &temp, "RQ-0001")?;

        // Disarm the guard
        guard.mark_completed();

        // Drop the guard (should not cleanup because it's disarmed)
        drop(guard);

        // Verify worker is still running
        assert_eq!(
            lock::pid_is_running(pid),
            Some(true),
            "Worker should still be running after disarmed drop"
        );

        // Clean up the child process
        kill_test_process(pid);

        Ok(())
    }

    #[test]
    fn guard_cleanup_is_idempotent() -> Result<()> {
        let temp = TempDir::new()?;
        let mut guard = create_test_guard(&temp);

        let pid = register_sleeping_worker(&mut guard, &temp, "RQ-0001")?;

        // First cleanup
        guard.cleanup()?;

        // Verify worker is terminated (allow for indeterminate result)
        let running = lock::pid_is_running(pid);
        assert!(
            running == Some(false) || running.is_none(),
            "Worker should be terminated after first cleanup, got: {:?}",
            running
        );

        // Second cleanup should be a no-op (idempotent)
        guard.cleanup()?;

        Ok(())
    }

    #[test]
    fn guard_cleanup_runs_on_drop() -> Result<()> {
        let temp = TempDir::new()?;

        let mut guard = create_test_guard(&temp);

        let pid = register_sleeping_worker(&mut guard, &temp, "RQ-0001")?;

        // Verify worker is running
        assert_eq!(
            lock::pid_is_running(pid),
            Some(true),
            "Worker should be running before drop"
        );

        // Explicitly drop the guard to trigger cleanup
        // This ensures temp dir is still valid during cleanup
        drop(guard);

        // Verify worker is terminated after guard is dropped
        // Allow for indeterminate result (None) as the process may have
        // been reaped by the time we check
        let running = lock::pid_is_running(pid);
        assert!(
            running == Some(false) || running.is_none(),
            "Worker should be terminated after guard drop, got: {:?}",
            running
        );

        Ok(())
    }

    #[test]
    fn guard_cleanup_retains_terminal_workers_for_status_retry() -> Result<()> {
        let temp = TempDir::new()?;
        let mut guard = create_test_guard(&temp);

        let running_workspace = temp.path().join("workspaces").join("RQ-0001");
        let completed_workspace = temp.path().join("workspaces").join("RQ-0002");
        std::fs::create_dir_all(&running_workspace)?;
        std::fs::create_dir_all(&completed_workspace)?;

        guard
            .state_file_mut()
            .upsert_worker(state::WorkerRecord::new(
                "RQ-0001",
                running_workspace,
                "2026-02-20T00:00:00Z".to_string(),
            ));

        let mut completed = state::WorkerRecord::new(
            "RQ-0002",
            completed_workspace,
            "2026-02-20T00:00:00Z".to_string(),
        );
        completed.mark_completed("2026-02-20T00:01:00Z".to_string());
        guard.state_file_mut().upsert_worker(completed);

        guard.cleanup()?;

        assert_eq!(guard.state_file.workers.len(), 1);
        assert_eq!(guard.state_file.workers[0].task_id, "RQ-0002");
        assert!(guard.state_file.workers[0].is_terminal());
        Ok(())
    }

    #[test]
    fn guard_cleanup_retains_blocked_workspace_for_retry() -> Result<()> {
        let temp = TempDir::new()?;
        let mut guard = create_test_guard(&temp);

        let blocked_workspace = temp.path().join("workspaces").join("RQ-0099");
        std::fs::create_dir_all(&blocked_workspace)?;

        guard.register_workspace(
            "RQ-0099".to_string(),
            WorkspaceSpec {
                path: blocked_workspace.clone(),
                branch: "main".to_string(),
            },
        );

        let mut blocked = state::WorkerRecord::new(
            "RQ-0099",
            blocked_workspace.clone(),
            "2026-02-20T00:00:00Z".to_string(),
        );
        blocked.mark_blocked("2026-02-20T00:05:00Z".to_string(), "blocked");
        guard.state_file_mut().upsert_worker(blocked);

        guard.cleanup()?;

        assert!(blocked_workspace.exists());
        assert_eq!(guard.state_file.workers.len(), 1);
        assert!(matches!(
            guard.state_file.workers[0].lifecycle,
            state::WorkerLifecycle::BlockedPush
        ));
        Ok(())
    }
}