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
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
507
508
509
510
511
512
//! Batch task operations for efficient multi-task updates.
//!
//! Responsibilities:
//! - Apply operations to multiple tasks atomically or with partial success.
//! - Filter tasks by tags, status, priority, scope, and age for batch selection.
//! - Batch delete, archive, clone, split, and plan operations.
//! - Provide detailed progress and error reporting.
//!
//! Does not handle:
//! - CLI argument parsing or user interaction.
//! - Individual task validation beyond what's in the single-task operations.
//! - Persistence to disk (callers save the queue after batch operations).
//!
//! Assumptions/invariants:
//! - Callers provide a loaded QueueFile and valid RFC3339 timestamp.
//! - Tag filtering is case-insensitive and OR-based (any tag matches).
//! - Status/priority/scope filters use OR logic within each filter type.
//! - Task IDs are unique within the queue.

mod delete;
mod display;
mod filters;
mod generate;
mod plan;
mod update;

pub use delete::{batch_archive_tasks, batch_delete_tasks};
pub use display::print_batch_results;
pub use filters::{
    BatchTaskFilters, filter_tasks_by_tags, parse_older_than_cutoff, resolve_task_ids,
    resolve_task_ids_filtered,
};
pub use generate::{batch_clone_tasks, batch_split_tasks};
pub use plan::{batch_plan_append, batch_plan_prepend};
pub use update::{batch_apply_edit, batch_set_field, batch_set_status};

/// Result of a batch operation on a single task.
#[derive(Debug, Clone)]
pub struct BatchTaskResult {
    pub task_id: String,
    pub success: bool,
    pub error: Option<String>,
    pub created_task_ids: Vec<String>,
}

/// Overall result of a batch operation.
#[derive(Debug, Clone)]
pub struct BatchOperationResult {
    pub total: usize,
    pub succeeded: usize,
    pub failed: usize,
    pub results: Vec<BatchTaskResult>,
}

impl BatchOperationResult {
    pub fn all_succeeded(&self) -> bool {
        self.failed == 0
    }

    pub fn has_failures(&self) -> bool {
        self.failed > 0
    }
}

/// Collect unique task IDs from a list of tasks.
pub fn collect_task_ids(tasks: &[&crate::contracts::Task]) -> Vec<String> {
    tasks.iter().map(|t| t.id.clone()).collect()
}

/// Deduplicate task IDs while preserving order.
pub(crate) fn deduplicate_task_ids(task_ids: &[String]) -> Vec<String> {
    let mut seen = std::collections::HashSet::new();
    let mut result = Vec::new();
    for id in task_ids {
        let trimmed = id.trim().to_string();
        if !trimmed.is_empty() && seen.insert(trimmed.clone()) {
            result.push(trimmed);
        }
    }
    result
}

/// Validate that all task IDs exist in the queue.
///
/// Returns an error if any task ID is not found.
pub(crate) fn validate_task_ids_exist(
    queue: &crate::contracts::QueueFile,
    task_ids: &[String],
) -> anyhow::Result<()> {
    use anyhow::bail;

    for task_id in task_ids {
        let needle = task_id.trim();
        if needle.is_empty() {
            bail!("Empty task ID provided");
        }
        if !queue.tasks.iter().any(|t| t.id.trim() == needle) {
            bail!(
                "{}",
                crate::error_messages::task_not_found_batch_failure(needle)
            );
        }
    }
    Ok(())
}

/// Collector for batch operation results with standardized error handling.
///
/// Responsibilities:
/// - Track success/failure counts
/// - Collect individual task results
/// - Handle continue-on-error semantics
///
/// Does not handle:
/// - Task ID deduplication (use preprocess_batch_ids)
/// - Actual operation execution (caller responsibility)
pub(crate) struct BatchResultCollector {
    total: usize,
    results: Vec<BatchTaskResult>,
    succeeded: usize,
    failed: usize,
    continue_on_error: bool,
    op_name: &'static str,
}

impl BatchResultCollector {
    /// Create a new collector for a batch operation.
    pub fn new(total: usize, continue_on_error: bool, op_name: &'static str) -> Self {
        Self {
            total,
            results: Vec::with_capacity(total),
            succeeded: 0,
            failed: 0,
            continue_on_error,
            op_name,
        }
    }

    /// Record a successful operation on a task.
    pub fn record_success(&mut self, task_id: String, created_task_ids: Vec<String>) {
        self.results.push(BatchTaskResult {
            task_id,
            success: true,
            error: None,
            created_task_ids,
        });
        self.succeeded += 1;
    }

    /// Record a failed operation on a task.
    ///
    /// Returns an error if not in continue-on-error mode, allowing caller to propagate.
    pub fn record_failure(&mut self, task_id: String, error: String) -> anyhow::Result<()> {
        self.results.push(BatchTaskResult {
            task_id: task_id.clone(),
            success: false,
            error: Some(error.clone()),
            created_task_ids: Vec::new(),
        });
        self.failed += 1;

        if !self.continue_on_error {
            anyhow::bail!(
                "Batch {} failed at task {}: {}. Use --continue-on-error to process remaining tasks.",
                self.op_name,
                task_id,
                error
            );
        }
        Ok(())
    }

    /// Consume the collector and return the final result.
    pub fn finish(self) -> BatchOperationResult {
        BatchOperationResult {
            total: self.total,
            succeeded: self.succeeded,
            failed: self.failed,
            results: self.results,
        }
    }
}

/// Preprocess task IDs for batch operations.
///
/// Deduplicates task IDs and validates the list is not empty.
pub(crate) fn preprocess_batch_ids(
    task_ids: &[String],
    op_name: &str,
) -> anyhow::Result<Vec<String>> {
    let unique_ids = deduplicate_task_ids(task_ids);
    if unique_ids.is_empty() {
        anyhow::bail!("No task IDs provided for batch {}", op_name);
    }
    Ok(unique_ids)
}

#[cfg(test)]
mod tests {
    use super::{
        BatchResultCollector, batch_delete_tasks, batch_plan_append, batch_plan_prepend,
        parse_older_than_cutoff, preprocess_batch_ids, validate_task_ids_exist,
    };
    use crate::contracts::{QueueFile, Task};

    #[test]
    fn parse_older_than_cutoff_parses_days() {
        let now = "2026-02-05T00:00:00Z";
        let result = parse_older_than_cutoff(now, "7d").unwrap();
        assert!(result.contains("2026-01-29"));
    }

    #[test]
    fn parse_older_than_cutoff_parses_weeks() {
        let now = "2026-02-05T00:00:00Z";
        let result = parse_older_than_cutoff(now, "2w").unwrap();
        assert!(result.contains("2026-01-22"));
    }

    #[test]
    fn parse_older_than_cutoff_parses_date() {
        let result = parse_older_than_cutoff("2026-02-05T00:00:00Z", "2026-01-01").unwrap();
        assert!(result.contains("2026-01-01"));
    }

    #[test]
    fn parse_older_than_cutoff_parses_rfc3339() {
        let result =
            parse_older_than_cutoff("2026-02-05T00:00:00Z", "2026-01-15T12:00:00Z").unwrap();
        assert!(result.contains("2026-01-15"));
    }

    #[test]
    fn parse_older_than_cutoff_rejects_invalid() {
        let result = parse_older_than_cutoff("2026-02-05T00:00:00Z", "invalid");
        assert!(result.is_err());
    }

    #[test]
    fn batch_delete_tasks_removes_tasks() {
        let mut queue = QueueFile {
            version: 1,
            tasks: vec![
                Task {
                    id: "RQ-0001".to_string(),
                    title: "Task 1".to_string(),
                    ..Default::default()
                },
                Task {
                    id: "RQ-0002".to_string(),
                    title: "Task 2".to_string(),
                    ..Default::default()
                },
                Task {
                    id: "RQ-0003".to_string(),
                    title: "Task 3".to_string(),
                    ..Default::default()
                },
            ],
        };

        let result = batch_delete_tasks(
            &mut queue,
            &["RQ-0001".to_string(), "RQ-0002".to_string()],
            false,
        )
        .unwrap();

        assert_eq!(result.succeeded, 2);
        assert_eq!(result.failed, 0);
        assert_eq!(queue.tasks.len(), 1);
        assert_eq!(queue.tasks[0].id, "RQ-0003");
    }

    #[test]
    fn batch_delete_tasks_atomic_fails_on_missing() {
        let mut queue = QueueFile {
            version: 1,
            tasks: vec![Task {
                id: "RQ-0001".to_string(),
                title: "Task 1".to_string(),
                ..Default::default()
            }],
        };

        let result = batch_delete_tasks(
            &mut queue,
            &["RQ-0001".to_string(), "RQ-9999".to_string()],
            false,
        );
        assert!(result.is_err());
    }

    #[test]
    fn batch_plan_append_adds_items() {
        let mut queue = QueueFile {
            version: 1,
            tasks: vec![Task {
                id: "RQ-0001".to_string(),
                title: "Task 1".to_string(),
                plan: vec!["Step 1".to_string()],
                ..Default::default()
            }],
        };

        let result = batch_plan_append(
            &mut queue,
            &["RQ-0001".to_string()],
            &["Step 2".to_string(), "Step 3".to_string()],
            "2026-02-05T00:00:00Z",
            false,
        )
        .unwrap();

        assert_eq!(result.succeeded, 1);
        assert_eq!(queue.tasks[0].plan.len(), 3);
        assert_eq!(queue.tasks[0].plan[0], "Step 1");
        assert_eq!(queue.tasks[0].plan[1], "Step 2");
        assert_eq!(queue.tasks[0].plan[2], "Step 3");
    }

    #[test]
    fn batch_plan_prepend_adds_items_first() {
        let mut queue = QueueFile {
            version: 1,
            tasks: vec![Task {
                id: "RQ-0001".to_string(),
                title: "Task 1".to_string(),
                plan: vec!["Step 2".to_string()],
                ..Default::default()
            }],
        };

        let result = batch_plan_prepend(
            &mut queue,
            &["RQ-0001".to_string()],
            &["Step 1".to_string()],
            "2026-02-05T00:00:00Z",
            false,
        )
        .unwrap();

        assert_eq!(result.succeeded, 1);
        assert_eq!(queue.tasks[0].plan.len(), 2);
        assert_eq!(queue.tasks[0].plan[0], "Step 1");
        assert_eq!(queue.tasks[0].plan[1], "Step 2");
    }

    // Tests for BatchResultCollector

    #[test]
    fn batch_result_collector_records_success() {
        let mut collector = BatchResultCollector::new(2, false, "test");
        collector.record_success("RQ-0001".to_string(), Vec::new());
        collector.record_success("RQ-0002".to_string(), vec!["RQ-0003".to_string()]);
        let result = collector.finish();
        assert_eq!(result.total, 2);
        assert_eq!(result.succeeded, 2);
        assert_eq!(result.failed, 0);
        assert!(result.all_succeeded());
    }

    #[test]
    fn batch_result_collector_records_failure() {
        let mut collector = BatchResultCollector::new(1, true, "test");
        collector
            .record_failure("RQ-0001".to_string(), "error msg".to_string())
            .expect("record_failure should succeed with continue_on_error=true");
        let result = collector.finish();
        assert_eq!(result.total, 1);
        assert_eq!(result.succeeded, 0);
        assert_eq!(result.failed, 1);
        assert!(result.has_failures());
    }

    #[test]
    fn batch_result_collector_atomic_mode_fails_on_error() {
        let mut collector = BatchResultCollector::new(1, false, "test");
        let result = collector.record_failure("RQ-0001".to_string(), "error".to_string());
        assert!(result.is_err());
    }

    #[test]
    fn preprocess_batch_ids_deduplicates() {
        let ids = vec![
            "RQ-0001".to_string(),
            "RQ-0001".to_string(),
            "RQ-0002".to_string(),
        ];
        let result = preprocess_batch_ids(&ids, "test").unwrap();
        assert_eq!(result, vec!["RQ-0001", "RQ-0002"]);
    }

    #[test]
    fn preprocess_batch_ids_rejects_empty() {
        let result = preprocess_batch_ids(&[], "test");
        assert!(result.is_err());
    }

    #[test]
    fn batch_result_collector_mixed_results() {
        let mut collector = BatchResultCollector::new(3, true, "test");
        collector.record_success("RQ-0001".to_string(), Vec::new());
        collector
            .record_failure("RQ-0002".to_string(), "error".to_string())
            .expect("record_failure should succeed with continue_on_error=true");
        collector.record_success("RQ-0003".to_string(), vec!["RQ-0004".to_string()]);
        let result = collector.finish();
        assert_eq!(result.total, 3);
        assert_eq!(result.succeeded, 2);
        assert_eq!(result.failed, 1);
        assert!(result.has_failures());
        assert!(!result.all_succeeded());
    }

    #[test]
    fn batch_result_collector_error_message_content() {
        let mut collector = BatchResultCollector::new(1, true, "test");
        collector
            .record_failure("RQ-0001".to_string(), "task not found".to_string())
            .expect("record_failure should succeed with continue_on_error=true");
        let result = collector.finish();
        assert_eq!(result.results[0].task_id, "RQ-0001");
        assert_eq!(result.results[0].error.as_ref().unwrap(), "task not found");
    }

    #[test]
    fn preprocess_batch_ids_trims_whitespace() {
        let ids = vec!["  RQ-0001  ".to_string(), "RQ-0002".to_string()];
        let result = preprocess_batch_ids(&ids, "test").unwrap();
        assert_eq!(result, vec!["RQ-0001", "RQ-0002"]);
    }

    #[test]
    fn preprocess_batch_ids_preserves_order() {
        let ids = vec![
            "RQ-0003".to_string(),
            "RQ-0001".to_string(),
            "RQ-0003".to_string(),
            "RQ-0002".to_string(),
        ];
        let result = preprocess_batch_ids(&ids, "test").unwrap();
        assert_eq!(result, vec!["RQ-0003", "RQ-0001", "RQ-0002"]);
    }

    #[test]
    fn batch_plan_append_atomic_fails_on_missing() {
        let mut queue = QueueFile {
            version: 1,
            tasks: vec![Task {
                id: "RQ-0001".to_string(),
                title: "Task 1".to_string(),
                plan: vec!["Step 1".to_string()],
                ..Default::default()
            }],
        };

        let result = batch_plan_append(
            &mut queue,
            &["RQ-0001".to_string(), "RQ-9999".to_string()],
            &["Step 2".to_string()],
            "2026-02-05T00:00:00Z",
            false,
        );
        assert!(result.is_err());
        // Queue should remain unchanged in atomic mode
        assert_eq!(queue.tasks[0].plan.len(), 1);
    }

    #[test]
    fn batch_plan_prepend_atomic_fails_on_missing() {
        let mut queue = QueueFile {
            version: 1,
            tasks: vec![Task {
                id: "RQ-0001".to_string(),
                title: "Task 1".to_string(),
                plan: vec!["Step 1".to_string()],
                ..Default::default()
            }],
        };

        let result = batch_plan_prepend(
            &mut queue,
            &["RQ-0001".to_string(), "RQ-9999".to_string()],
            &["Step 0".to_string()],
            "2026-02-05T00:00:00Z",
            false,
        );
        assert!(result.is_err());
        // Queue should remain unchanged in atomic mode
        assert_eq!(queue.tasks[0].plan.len(), 1);
        assert_eq!(queue.tasks[0].plan[0], "Step 1");
    }

    #[test]
    fn validate_task_ids_exist_rejects_missing() {
        let queue = QueueFile {
            version: 1,
            tasks: vec![Task {
                id: "RQ-0001".to_string(),
                title: "Task 1".to_string(),
                ..Default::default()
            }],
        };

        let result = validate_task_ids_exist(&queue, &["RQ-0001".to_string()]);
        assert!(result.is_ok());

        let result = validate_task_ids_exist(&queue, &["RQ-9999".to_string()]);
        assert!(result.is_err());
    }
}