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
//! Daemon command implementation for background service management.
//!
//! Responsibilities:
//! - Re-export daemon subcommands (start, stop, serve, status, logs)
//! - Define shared types (DaemonState) and constants
//! - Provide shared helpers for daemon state management and lifecycle coordination
//!
//! Not handled here:
//! - Individual command implementations (see submodules)
//! - Windows service management (Unix-only implementation)
//!
//! Invariants/assumptions:
//! - Daemon uses a dedicated lock at `.ralph/cache/daemon.lock`
//! - Daemon state is stored at `.ralph/cache/daemon.json`
//! - Startup serialization uses a separate `.ralph/cache/daemon.start.lock`

mod logs;
mod serve;
mod start;
mod status;
mod stop;

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
use std::process::Child;
use std::sync::mpsc::{self, Receiver};
use std::time::{Duration, Instant};

pub use logs::logs;
pub use serve::serve;
pub use start::start;
pub use status::status;
pub use stop::stop;

/// Daemon state file name.
pub(super) const DAEMON_STATE_FILE: &str = "daemon.json";
/// Daemon readiness file name.
pub(super) const DAEMON_READY_FILE: &str = "daemon.ready";
/// Daemon lock directory name (relative to .ralph/cache).
pub(super) const DAEMON_LOCK_DIR: &str = "daemon.lock";
/// Daemon startup lock directory name (relative to .ralph/cache).
pub(super) const DAEMON_START_LOCK_DIR: &str = "daemon.start.lock";

/// Re-export for use in submodules.
pub(super) use logs::DAEMON_LOG_FILE_NAME;

/// Daemon state persisted to disk.
#[derive(Debug, Serialize, Deserialize)]
pub(super) struct DaemonState {
    /// Schema version for future compatibility.
    pub(super) version: u32,
    /// Process ID of the daemon.
    pub(super) pid: u32,
    /// ISO 8601 timestamp when the daemon started.
    pub(super) started_at: String,
    /// Repository root path.
    pub(super) repo_root: String,
    /// Full command line of the daemon process.
    pub(super) command: String,
}

struct DaemonCacheWatcher {
    _watcher: notify::RecommendedWatcher,
    rx: Receiver<notify::Result<notify::Event>>,
}

impl DaemonCacheWatcher {
    fn new(cache_dir: &Path) -> Result<Self> {
        use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};

        std::fs::create_dir_all(cache_dir).with_context(|| {
            format!("Failed to create daemon cache dir {}", cache_dir.display())
        })?;

        let (tx, rx) = mpsc::channel();
        let mut watcher = RecommendedWatcher::new(
            move |res| {
                let _ = tx.send(res);
            },
            Config::default(),
        )
        .context("Failed to create daemon cache watcher")?;
        watcher
            .watch(cache_dir, RecursiveMode::NonRecursive)
            .with_context(|| format!("Failed to watch daemon cache dir {}", cache_dir.display()))?;

        Ok(Self {
            _watcher: watcher,
            rx,
        })
    }

    fn recv_timeout(&self, timeout: Duration) -> bool {
        self.rx.recv_timeout(timeout).is_ok()
    }
}

/// Read daemon state from disk.
pub(super) fn get_daemon_state(cache_dir: &Path) -> Result<Option<DaemonState>> {
    let path = cache_dir.join(DAEMON_STATE_FILE);
    if !path.exists() {
        return Ok(None);
    }

    let content = fs::read_to_string(&path)
        .with_context(|| format!("Failed to read daemon state from {}", path.display()))?;

    let state: DaemonState = serde_json::from_str(&content)
        .with_context(|| format!("Failed to parse daemon state from {}", path.display()))?;

    Ok(Some(state))
}

/// Write daemon state to disk atomically.
pub(super) fn write_daemon_state(cache_dir: &Path, state: &DaemonState) -> Result<()> {
    let path = cache_dir.join(DAEMON_STATE_FILE);
    let content =
        serde_json::to_string_pretty(state).context("Failed to serialize daemon state")?;
    crate::fsutil::write_atomic(&path, content.as_bytes())
        .with_context(|| format!("Failed to write daemon state to {}", path.display()))?;
    Ok(())
}

fn daemon_ready_path(cache_dir: &Path) -> std::path::PathBuf {
    cache_dir.join(DAEMON_READY_FILE)
}

fn daemon_state_path(cache_dir: &Path) -> std::path::PathBuf {
    cache_dir.join(DAEMON_STATE_FILE)
}

fn daemon_lock_path(cache_dir: &Path) -> std::path::PathBuf {
    cache_dir.join(DAEMON_LOCK_DIR)
}

pub(super) fn write_daemon_ready(cache_dir: &Path, pid: u32) -> Result<()> {
    let path = daemon_ready_path(cache_dir);
    crate::fsutil::write_atomic(&path, format!("{pid}\n").as_bytes())
        .with_context(|| format!("Failed to write daemon ready marker to {}", path.display()))?;
    Ok(())
}

fn daemon_ready_matches_pid(cache_dir: &Path, pid: u32) -> Result<bool> {
    let path = daemon_ready_path(cache_dir);
    let raw = match fs::read_to_string(&path) {
        Ok(raw) => raw,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
        Err(err) => {
            return Err(anyhow::Error::from(err))
                .with_context(|| format!("Failed to read daemon ready marker {}", path.display()));
        }
    };

    let observed = raw.trim().parse::<u32>().with_context(|| {
        format!(
            "Failed to parse daemon ready marker {} as a PID",
            path.display()
        )
    })?;
    Ok(observed == pid)
}

fn remove_daemon_file(path: &Path, description: &str) {
    if let Err(error) = fs::remove_file(path)
        && error.kind() != std::io::ErrorKind::NotFound
    {
        log::debug!(
            "Failed to remove {description} {}: {}",
            path.display(),
            error
        );
    }
}

fn remove_daemon_dir(path: &Path, description: &str) {
    if let Err(error) = fs::remove_dir_all(path)
        && error.kind() != std::io::ErrorKind::NotFound
    {
        log::debug!(
            "Failed to remove {description} {}: {}",
            path.display(),
            error
        );
    }
}

pub(super) fn clear_daemon_runtime_artifacts(cache_dir: &Path, remove_lock: bool) {
    remove_daemon_file(&daemon_state_path(cache_dir), "daemon state file");
    remove_daemon_file(&daemon_ready_path(cache_dir), "daemon ready marker");
    if remove_lock {
        remove_daemon_dir(&daemon_lock_path(cache_dir), "daemon lock dir");
    }
}

fn daemon_shutdown_complete(cache_dir: &Path, pid: u32) -> bool {
    matches!(
        daemon_pid_liveness(pid),
        crate::lock::PidLiveness::NotRunning
    ) || (!daemon_state_path(cache_dir).exists()
        && !daemon_ready_path(cache_dir).exists()
        && !daemon_lock_path(cache_dir).exists())
}

/// Wait for the daemon to publish its explicit ready marker or exit early.
pub(super) fn wait_for_daemon_ready(
    cache_dir: &Path,
    pid: u32,
    timeout: Duration,
    child: &mut Child,
) -> Result<bool> {
    let watcher = DaemonCacheWatcher::new(cache_dir).ok();
    let deadline = Instant::now() + timeout;
    loop {
        if daemon_ready_matches_pid(cache_dir, pid)? {
            return Ok(true);
        }
        if child
            .try_wait()
            .with_context(|| format!("Failed to inspect daemon child {pid}"))?
            .is_some()
        {
            return Ok(false);
        }
        if Instant::now() >= deadline {
            return Ok(false);
        }
        let wait_slice = deadline
            .saturating_duration_since(Instant::now())
            .min(Duration::from_millis(100))
            .max(Duration::from_millis(1));
        if let Some(ref watcher) = watcher {
            let _ = watcher.recv_timeout(wait_slice);
        } else {
            std::thread::park_timeout(wait_slice);
        }
    }
}

/// Wait for the daemon to exit and release its runtime artifacts.
pub(super) fn wait_for_daemon_shutdown(
    cache_dir: &Path,
    pid: u32,
    timeout: Duration,
) -> Result<bool> {
    let watcher = DaemonCacheWatcher::new(cache_dir).ok();
    let deadline = Instant::now() + timeout;
    loop {
        if daemon_shutdown_complete(cache_dir, pid) {
            return Ok(true);
        }
        if Instant::now() >= deadline {
            return Ok(false);
        }
        let wait_slice = deadline
            .saturating_duration_since(Instant::now())
            .min(Duration::from_millis(100))
            .max(Duration::from_millis(1));
        if let Some(ref watcher) = watcher {
            let _ = watcher.recv_timeout(wait_slice);
        } else {
            std::thread::park_timeout(wait_slice);
        }
    }
}

/// Check PID liveness for daemon processes.
pub(super) fn daemon_pid_liveness(pid: u32) -> crate::lock::PidLiveness {
    crate::lock::pid_liveness(pid)
}

/// Render manual cleanup instructions for stale/indeterminate daemon state.
pub(super) fn manual_daemon_cleanup_instructions(cache_dir: &Path) -> String {
    format!(
        "If you are certain the daemon is stopped, manually remove:\n  rm {}\n  rm {}\n  rm -rf {}",
        cache_dir.join(DAEMON_STATE_FILE).display(),
        cache_dir.join(DAEMON_READY_FILE).display(),
        cache_dir.join(DAEMON_LOCK_DIR).display()
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Read;
    use std::process::{Command, Stdio};
    use std::time::Duration;
    use tempfile::TempDir;

    fn deterministic_non_running_pid() -> u32 {
        const MAX_SAFE_PID: u32 = i32::MAX as u32;
        for offset in 0..=1024 {
            let candidate = MAX_SAFE_PID - offset;
            if crate::lock::pid_is_running(candidate) == Some(false) {
                return candidate;
            }
        }

        panic!("failed to find a deterministic non-running PID candidate");
    }

    #[test]
    fn wait_for_daemon_ready_returns_true_when_marker_appears() {
        let temp = TempDir::new().expect("create temp dir");
        let cache_dir = temp.path().join(".ralph/cache");
        fs::create_dir_all(&cache_dir).expect("create cache dir");
        let expected_pid = 424_242_u32;

        let writer_cache_dir = cache_dir.clone();
        let writer = std::thread::spawn(move || {
            std::thread::park_timeout(Duration::from_millis(60));
            write_daemon_ready(&writer_cache_dir, expected_pid).expect("write daemon ready");
        });

        let mut child = Command::new("python3")
            .arg("-c")
            .arg("import time; time.sleep(5)")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .expect("spawn helper child");

        let ready =
            wait_for_daemon_ready(&cache_dir, expected_pid, Duration::from_secs(1), &mut child)
                .expect("wait for daemon ready");
        writer.join().expect("join writer thread");
        let _ = child.kill();
        let _ = child.wait();
        assert!(ready, "expected daemon state to appear before timeout");
    }

    #[test]
    fn wait_for_daemon_ready_returns_false_when_child_exits() {
        let temp = TempDir::new().expect("create temp dir");
        let cache_dir = temp.path().join(".ralph/cache");
        fs::create_dir_all(&cache_dir).expect("create cache dir");

        let mut child = Command::new("python3")
            .arg("-c")
            .arg("print('boom')")
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .expect("spawn helper child");

        let ready =
            wait_for_daemon_ready(&cache_dir, 123_456_u32, Duration::from_secs(1), &mut child)
                .expect("wait for daemon ready");
        let mut stdout = String::new();
        child
            .stdout
            .take()
            .expect("capture child stdout")
            .read_to_string(&mut stdout)
            .expect("read child stdout");
        assert!(!ready, "expected early failure when daemon child exits");
        assert!(stdout.contains("boom"));
    }

    #[test]
    fn wait_for_daemon_shutdown_returns_true_after_artifacts_clear() {
        let temp = TempDir::new().expect("create temp dir");
        let cache_dir = temp.path().join(".ralph/cache");
        fs::create_dir_all(&cache_dir).expect("create cache dir");
        let pid = deterministic_non_running_pid();

        write_daemon_state(
            &cache_dir,
            &DaemonState {
                version: 1,
                pid,
                started_at: "2026-01-01T00:00:00Z".to_string(),
                repo_root: "/tmp/repo".to_string(),
                command: "ralph daemon serve".to_string(),
            },
        )
        .expect("write daemon state");
        write_daemon_ready(&cache_dir, pid).expect("write daemon ready");
        fs::create_dir_all(cache_dir.join(DAEMON_LOCK_DIR)).expect("create daemon lock dir");

        clear_daemon_runtime_artifacts(&cache_dir, true);

        let ready = wait_for_daemon_shutdown(&cache_dir, pid, Duration::from_secs(1))
            .expect("wait for daemon shutdown");
        assert!(
            ready,
            "expected daemon shutdown check to observe cleared artifacts"
        );
    }

    #[test]
    fn manual_cleanup_instructions_include_state_and_lock_paths() {
        let temp = TempDir::new().expect("create temp dir");
        let cache_dir = temp.path().join(".ralph/cache");
        let instructions = manual_daemon_cleanup_instructions(&cache_dir);

        assert!(instructions.contains(&format!(
            "rm {}",
            cache_dir.join(DAEMON_STATE_FILE).display()
        )));
        assert!(instructions.contains(&format!(
            "rm {}",
            cache_dir.join(DAEMON_READY_FILE).display()
        )));
        assert!(instructions.contains(&format!(
            "rm -rf {}",
            cache_dir.join(DAEMON_LOCK_DIR).display()
        )));
    }

    #[test]
    fn manual_cleanup_instructions_do_not_reference_force_flag() {
        let temp = TempDir::new().expect("create temp dir");
        let cache_dir = temp.path().join(".ralph/cache");
        let instructions = manual_daemon_cleanup_instructions(&cache_dir);

        assert!(
            !instructions.contains("--force"),
            "daemon cleanup instructions must not mention nonexistent --force flag"
        );
    }
}