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
//! File processing for the watch command.
//!
//! Responsibilities:
//! - Process pending files and detect comments.
//! - Coordinate between debounce logic, comment detection, and task handling.
//! - Scope removal reconciliation to files processed in the current batch.
//!
//! Not handled here:
//! - File watching or event handling (see `event_loop/mod.rs`).
//! - Low-level comment detection (see `comments.rs`).
//! - Task creation or identity rules (see `tasks.rs` / `identity.rs`).
//!
//! Invariants/assumptions:
//! - Files are skipped if recently processed (within debounce window).
//! - Missing files are treated as zero-comment scans for reconciliation.
//! - Generic read failures do not trigger removal reconciliation.
//! - Old entries in `last_processed` are cleaned up periodically.

use crate::commands::watch::comments::detect_comments;
use crate::commands::watch::debounce::{can_reprocess, cleanup_old_entries};
use crate::commands::watch::state::WatchState;
use crate::commands::watch::tasks::handle_detected_comments;
use crate::commands::watch::types::{DetectedComment, WatchOptions};
use crate::config::Resolved;
use anyhow::Result;
use regex::Regex;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Process pending files and detect comments.
pub fn process_pending_files(
    resolved: &Resolved,
    state: &Arc<Mutex<WatchState>>,
    comment_regex: &Regex,
    opts: &WatchOptions,
    last_processed: &mut HashMap<PathBuf, Instant>,
) -> Result<()> {
    let files: Vec<PathBuf> = match state.lock() {
        Ok(mut guard) => guard.take_pending(),
        Err(e) => {
            log::error!("Watch 'state' mutex poisoned, cannot process files: {}", e);
            return Ok(());
        }
    };

    if files.is_empty() {
        return Ok(());
    }

    let debounce = Duration::from_millis(opts.debounce_ms);
    let mut all_comments: Vec<DetectedComment> = Vec::new();
    let mut processed_files: Vec<PathBuf> = Vec::new();

    for file_path in files {
        // Skip if file was recently processed (within debounce window)
        if !can_reprocess(&file_path, last_processed, debounce) {
            continue;
        }

        match detect_comments(&file_path, comment_regex) {
            Ok(comments) => {
                processed_files.push(file_path.clone());
                if !comments.is_empty() {
                    log::debug!(
                        "Detected {} comments in {}",
                        comments.len(),
                        file_path.display()
                    );
                    all_comments.extend(comments);
                }
                // Record when this file was processed
                last_processed.insert(file_path, Instant::now());
            }
            Err(e) if !file_path.exists() => {
                log::debug!(
                    "Treating missing file {} as removed for watch reconciliation: {}",
                    file_path.display(),
                    e
                );
                processed_files.push(file_path.clone());
                last_processed.insert(file_path, Instant::now());
            }
            Err(e) => {
                log::warn!("Failed to process file {}: {}", file_path.display(), e);
            }
        }
    }

    // Periodically clean up old entries to prevent unbounded growth
    cleanup_old_entries(last_processed, debounce);

    if !processed_files.is_empty() {
        handle_detected_comments(resolved, &all_comments, &processed_files, opts)?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::watch::comments::build_comment_regex;
    use crate::commands::watch::types::CommentType;
    use crate::contracts::{Config, QueueFile};
    use std::io::Write;
    use std::path::PathBuf;
    use tempfile::{NamedTempFile, TempDir};

    fn create_test_resolved(temp_dir: &TempDir) -> Resolved {
        let queue_path = temp_dir.path().join("queue.json");
        let done_path = temp_dir.path().join("done.json");

        // Create empty queue file
        let queue = QueueFile::default();
        let queue_json = serde_json::to_string_pretty(&queue).unwrap();
        std::fs::write(&queue_path, queue_json).unwrap();

        Resolved {
            config: Config::default(),
            repo_root: temp_dir.path().to_path_buf(),
            queue_path,
            done_path,
            id_prefix: "RQ".to_string(),
            id_width: 4,
            global_config_path: None,
            project_config_path: None,
        }
    }

    #[test]
    fn process_pending_files_handles_state_mutex_poison() {
        let temp_dir = TempDir::new().unwrap();
        let resolved = create_test_resolved(&temp_dir);
        let state = Arc::new(Mutex::new(WatchState::new(100)));

        let opts = WatchOptions {
            patterns: vec!["*.rs".to_string()],
            debounce_ms: 100,
            auto_queue: false,
            notify: false,
            ignore_patterns: vec![],
            comment_types: vec![CommentType::Todo],
            paths: vec![PathBuf::from(".")],
            force: false,
            close_removed: false,
        };

        let comment_regex = build_comment_regex(&opts.comment_types).unwrap();
        let mut last_processed: HashMap<PathBuf, Instant> = HashMap::new();

        // Clone for the poisoning thread
        let state_clone = state.clone();

        // Spawn a thread that will panic while holding the state mutex
        let poison_handle = std::thread::spawn(move || {
            let _guard = state_clone.lock().unwrap();
            panic!("Intentional panic to poison state mutex");
        });

        // Wait for the panic
        let _ = poison_handle.join();

        // Now the state mutex is poisoned - verify process_pending_files handles it gracefully
        let result = process_pending_files(
            &resolved,
            &state,
            &comment_regex,
            &opts,
            &mut last_processed,
        );

        // Should return Ok, not panic
        assert!(
            result.is_ok(),
            "process_pending_files should handle state mutex poison gracefully"
        );
    }

    #[test]
    fn process_pending_files_happy_path() {
        let temp_dir = TempDir::new().unwrap();
        let resolved = create_test_resolved(&temp_dir);
        let state = Arc::new(Mutex::new(WatchState::new(100)));

        // Create a temp file with a TODO comment
        let mut temp_file = NamedTempFile::new_in(temp_dir.path()).unwrap();
        writeln!(temp_file, "// TODO: test task").unwrap();
        temp_file.flush().unwrap();

        // Add the file to pending
        let file_path = temp_file.path().to_path_buf();
        state.lock().unwrap().add_file(file_path.clone());

        let opts = WatchOptions {
            patterns: vec!["*.rs".to_string()],
            debounce_ms: 100,
            auto_queue: false, // Don't actually queue, just test processing
            notify: false,
            ignore_patterns: vec![],
            comment_types: vec![CommentType::Todo],
            paths: vec![PathBuf::from(".")],
            force: false,
            close_removed: false,
        };

        let comment_regex = build_comment_regex(&opts.comment_types).unwrap();
        let mut last_processed: HashMap<PathBuf, Instant> = HashMap::new();

        // Process the pending file
        let result = process_pending_files(
            &resolved,
            &state,
            &comment_regex,
            &opts,
            &mut last_processed,
        );

        assert!(result.is_ok());

        // Verify the file was recorded in last_processed
        assert!(last_processed.contains_key(&file_path));

        // Verify state is empty (files were taken)
        assert!(state.lock().unwrap().pending_files.is_empty());
    }

    // =====================================================================
    // Additional process_pending_files tests
    // =====================================================================

    #[test]
    fn process_pending_files_empty_pending_does_nothing() {
        let temp_dir = TempDir::new().unwrap();
        let resolved = create_test_resolved(&temp_dir);
        let state = Arc::new(Mutex::new(WatchState::new(100)));

        // State starts empty, no files added

        let opts = WatchOptions {
            patterns: vec!["*.rs".to_string()],
            debounce_ms: 100,
            auto_queue: false,
            notify: false,
            ignore_patterns: vec![],
            comment_types: vec![CommentType::Todo],
            paths: vec![PathBuf::from(".")],
            force: false,
            close_removed: false,
        };

        let comment_regex = build_comment_regex(&opts.comment_types).unwrap();
        let mut last_processed: HashMap<PathBuf, Instant> = HashMap::new();

        let result = process_pending_files(
            &resolved,
            &state,
            &comment_regex,
            &opts,
            &mut last_processed,
        );

        assert!(result.is_ok());
        assert!(last_processed.is_empty()); // Nothing was processed
    }

    #[test]
    fn process_pending_files_skips_recently_processed() {
        let temp_dir = TempDir::new().unwrap();
        let resolved = create_test_resolved(&temp_dir);
        let state = Arc::new(Mutex::new(WatchState::new(100)));

        // Create a temp file with a TODO
        let mut temp_file = NamedTempFile::new_in(temp_dir.path()).unwrap();
        writeln!(temp_file, "// TODO: test task").unwrap();
        temp_file.flush().unwrap();

        let file_path = temp_file.path().to_path_buf();

        // Pre-populate last_processed with current time (file was just processed)
        let mut last_processed: HashMap<PathBuf, Instant> = HashMap::new();
        last_processed.insert(file_path.clone(), Instant::now());

        // Add file to pending
        state.lock().unwrap().add_file(file_path.clone());

        let opts = WatchOptions {
            patterns: vec!["*.rs".to_string()],
            debounce_ms: 1000, // Long debounce - file should be skipped
            auto_queue: false,
            notify: false,
            ignore_patterns: vec![],
            comment_types: vec![CommentType::Todo],
            paths: vec![PathBuf::from(".")],
            force: false,
            close_removed: false,
        };

        let comment_regex = build_comment_regex(&opts.comment_types).unwrap();

        // Process should skip the file due to recent processing
        let result = process_pending_files(
            &resolved,
            &state,
            &comment_regex,
            &opts,
            &mut last_processed,
        );

        assert!(result.is_ok());
        // File should still be in last_processed but timestamp should not change
        assert!(last_processed.contains_key(&file_path));
    }

    #[test]
    fn process_pending_files_handles_read_error_gracefully() {
        let temp_dir = TempDir::new().unwrap();
        let resolved = create_test_resolved(&temp_dir);
        let state = Arc::new(Mutex::new(WatchState::new(100)));

        // Use a non-existent file path (will cause read error)
        let nonexistent_path = temp_dir.path().join("does_not_exist.rs");
        state.lock().unwrap().add_file(nonexistent_path);

        let opts = WatchOptions {
            patterns: vec!["*.rs".to_string()],
            debounce_ms: 100,
            auto_queue: false,
            notify: false,
            ignore_patterns: vec![],
            comment_types: vec![CommentType::Todo],
            paths: vec![PathBuf::from(".")],
            force: false,
            close_removed: false,
        };

        let comment_regex = build_comment_regex(&opts.comment_types).unwrap();
        let mut last_processed: HashMap<PathBuf, Instant> = HashMap::new();

        // Should return Ok even though file read failed
        let result = process_pending_files(
            &resolved,
            &state,
            &comment_regex,
            &opts,
            &mut last_processed,
        );

        assert!(result.is_ok());
    }

    #[test]
    fn process_pending_files_records_missing_file_for_reconciliation() {
        let temp_dir = TempDir::new().unwrap();
        let resolved = create_test_resolved(&temp_dir);
        let state = Arc::new(Mutex::new(WatchState::new(100)));

        let missing_path = temp_dir.path().join("does_not_exist.rs");
        state.lock().unwrap().add_file(missing_path.clone());

        let opts = WatchOptions {
            patterns: vec!["*.rs".to_string()],
            debounce_ms: 100,
            auto_queue: false,
            notify: false,
            ignore_patterns: vec![],
            comment_types: vec![CommentType::Todo],
            paths: vec![PathBuf::from(".")],
            force: false,
            close_removed: true,
        };

        let comment_regex = build_comment_regex(&opts.comment_types).unwrap();
        let mut last_processed: HashMap<PathBuf, Instant> = HashMap::new();

        let result = process_pending_files(
            &resolved,
            &state,
            &comment_regex,
            &opts,
            &mut last_processed,
        );

        assert!(result.is_ok());
        assert!(last_processed.contains_key(&missing_path));
    }

    #[test]
    fn process_pending_files_processes_multiple_files() {
        let temp_dir = TempDir::new().unwrap();
        let resolved = create_test_resolved(&temp_dir);
        let state = Arc::new(Mutex::new(WatchState::new(100)));

        // Create multiple files with TODOs
        let mut file1 = NamedTempFile::new_in(temp_dir.path()).unwrap();
        writeln!(file1, "// TODO: task 1").unwrap();
        file1.flush().unwrap();

        let mut file2 = NamedTempFile::new_in(temp_dir.path()).unwrap();
        writeln!(file2, "// FIXME: task 2").unwrap();
        file2.flush().unwrap();

        let path1 = file1.path().to_path_buf();
        let path2 = file2.path().to_path_buf();

        state.lock().unwrap().add_file(path1.clone());
        state.lock().unwrap().add_file(path2.clone());

        let opts = WatchOptions {
            patterns: vec!["*.rs".to_string()],
            debounce_ms: 100,
            auto_queue: false,
            notify: false,
            ignore_patterns: vec![],
            comment_types: vec![CommentType::All],
            paths: vec![PathBuf::from(".")],
            force: false,
            close_removed: false,
        };

        let comment_regex = build_comment_regex(&opts.comment_types).unwrap();
        let mut last_processed: HashMap<PathBuf, Instant> = HashMap::new();

        let result = process_pending_files(
            &resolved,
            &state,
            &comment_regex,
            &opts,
            &mut last_processed,
        );

        assert!(result.is_ok());
        // Both files should be recorded in last_processed
        assert!(last_processed.contains_key(&path1));
        assert!(last_processed.contains_key(&path2));
    }
}