cueloop 0.4.1

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
//! Gitignore management for CueLoop initialization.
//!
//! Purpose:
//! - Gitignore management for CueLoop initialization.
//!
//! Responsibilities:
//! - Ensure active runtime `workspaces/` entries are in `.gitignore` to prevent dirty repo issues.
//! - Ensure active runtime `logs/` entries are in `.gitignore` to prevent committing unredacted debug logs.
//! - Ensure active runtime `trust.jsonc` entries are in `.gitignore` to keep local trust decisions untracked.
//! - Optionally ensure queue/done files are ignored for local-private queue mode.
//! - Provide idempotent updates to `.gitignore`.
//!
//! Not handled here:
//! - Reading or parsing existing `.gitignore` patterns (only simple line-based checks).
//! - Global gitignore configuration (only repo-local `.gitignore`).
//!
//!
//! Usage:
//! - Used through the crate module tree or integration test harness.
//!
//! Invariants/assumptions:
//! - Updates are additive only (never removes entries).
//! - Safe to run multiple times (idempotent).

use anyhow::{Context, Result};
use std::fs;
use std::path::Path;

/// Ensures CueLoop-specific entries exist in `.gitignore`.
///
/// The active runtime dir comes from config path authority: repos get `.cueloop/*` entries.
///
/// This function is idempotent - calling it multiple times is safe.
pub fn ensure_cueloop_gitignore_entries(repo_root: &Path) -> Result<()> {
    let gitignore_path = repo_root.join(".gitignore");

    let existing_content = if gitignore_path.exists() {
        fs::read_to_string(&gitignore_path)
            .with_context(|| format!("read {}", gitignore_path.display()))?
    } else {
        String::new()
    };

    let runtime_name = active_runtime_name(repo_root);
    let logs_entry = format!("{runtime_name}/logs/");
    let workspaces_entry = format!("{runtime_name}/workspaces/");
    let trust_entry = format!("{runtime_name}/trust.jsonc");

    let needs_workspaces_entry = !existing_content
        .lines()
        .any(|line| is_runtime_ignore_entry(line, &runtime_name, "workspaces"));
    let needs_logs_entry = !existing_content
        .lines()
        .any(|line| is_runtime_ignore_entry(line, &runtime_name, "logs"));
    let needs_trust_entry = !existing_content
        .lines()
        .any(|line| line.trim() == trust_entry);

    if !needs_workspaces_entry && !needs_logs_entry && !needs_trust_entry {
        log::debug!("{workspaces_entry}, {logs_entry}, and {trust_entry} already in .gitignore");
        return Ok(());
    }

    let mut new_content = existing_content;
    if !new_content.is_empty() && !new_content.ends_with('\n') {
        new_content.push('\n');
    }

    if needs_logs_entry {
        if !new_content.is_empty() {
            new_content.push('\n');
        }
        new_content.push_str("# CueLoop debug logs (raw/unredacted; do not commit)\n");
        new_content.push_str(&logs_entry);
        new_content.push('\n');
    }

    if needs_workspaces_entry {
        if !new_content.is_empty() {
            new_content.push('\n');
        }
        new_content.push_str("# CueLoop parallel mode workspace directories\n");
        new_content.push_str(&workspaces_entry);
        new_content.push('\n');
    }

    if needs_trust_entry {
        if !new_content.is_empty() {
            new_content.push('\n');
        }
        new_content.push_str("# CueLoop local trust decision (machine-local; do not commit)\n");
        new_content.push_str(&trust_entry);
        new_content.push('\n');
    }

    fs::write(&gitignore_path, new_content)
        .with_context(|| format!("write {}", gitignore_path.display()))?;

    if needs_logs_entry {
        log::info!("Added '{}' to .gitignore", logs_entry);
    }
    if needs_workspaces_entry {
        log::info!("Added '{}' to .gitignore", workspaces_entry);
    }
    if needs_trust_entry {
        log::info!("Added '{}' to .gitignore", trust_entry);
    }

    Ok(())
}

fn is_runtime_ignore_entry(line: &str, runtime_name: &str, child: &str) -> bool {
    let trimmed = line.trim();
    trimmed == format!("{runtime_name}/{child}/") || trimmed == format!("{runtime_name}/{child}")
}

fn active_runtime_name(repo_root: &Path) -> String {
    crate::config::project_runtime_dir(repo_root)
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or(crate::constants::identity::PROJECT_RUNTIME_DIR)
        .to_string()
}

/// Ensure queue/done files are ignored for local-private queue mode.
pub fn ensure_local_queue_gitignore_entries(repo_root: &Path) -> Result<()> {
    let runtime_name = active_runtime_name(repo_root);
    let entries = vec![
        format!("{runtime_name}/queue.jsonc"),
        format!("{runtime_name}/done.jsonc"),
    ];
    ensure_exact_gitignore_entries(
        repo_root,
        "# CueLoop local queue state",
        &entries,
        "local queue/done files",
    )
}

fn ensure_exact_gitignore_entries(
    repo_root: &Path,
    header: &str,
    entries: &[String],
    label: &str,
) -> Result<()> {
    let gitignore_path = repo_root.join(".gitignore");
    let existing_content = if gitignore_path.exists() {
        fs::read_to_string(&gitignore_path)
            .with_context(|| format!("read {}", gitignore_path.display()))?
    } else {
        String::new()
    };
    let missing = entries
        .iter()
        .filter(|entry| {
            !existing_content
                .lines()
                .any(|line| line.trim() == entry.as_str())
        })
        .collect::<Vec<_>>();
    if missing.is_empty() {
        return Ok(());
    }

    let mut new_content = existing_content;
    if !new_content.is_empty() && !new_content.ends_with('\n') {
        new_content.push('\n');
    }
    if !new_content.is_empty() {
        new_content.push('\n');
    }
    new_content.push_str(header);
    new_content.push('\n');
    for entry in missing {
        new_content.push_str(entry);
        new_content.push('\n');
    }
    fs::write(&gitignore_path, new_content)
        .with_context(|| format!("write {}", gitignore_path.display()))?;
    log::info!("Added {} to .gitignore", label);
    Ok(())
}

/// Migrate .json ignore patterns to .jsonc in .gitignore.
///
/// This updates CueLoop-managed ignore patterns from .json to .jsonc variants.
/// Patterns like `.cueloop/queue.json` become `.cueloop/queue.jsonc`.
///
/// Returns true if any changes were made.
pub fn migrate_json_to_jsonc_gitignore(repo_root: &std::path::Path) -> anyhow::Result<bool> {
    let gitignore_path = repo_root.join(".gitignore");
    if !gitignore_path.exists() {
        return Ok(false);
    }

    let content = fs::read_to_string(&gitignore_path)
        .with_context(|| format!("read {}", gitignore_path.display()))?;

    // Define patterns to migrate: (old_pattern, new_pattern)
    let patterns_to_migrate: &[(&str, &str)] = &[
        (".cueloop/queue.json", ".cueloop/queue.jsonc"),
        (".cueloop/done.json", ".cueloop/done.jsonc"),
        (".cueloop/config.json", ".cueloop/config.jsonc"),
        (".cueloop/*.json", ".cueloop/*.jsonc"),
    ];

    let mut updated = content.clone();
    let mut made_changes = false;

    for (old_pattern, new_pattern) in patterns_to_migrate {
        // Check if old pattern exists and new pattern doesn't
        let has_old = updated.lines().any(|line| {
            let trimmed = line.trim();
            trimmed == *old_pattern || trimmed == old_pattern.trim_end_matches('/')
        });
        let has_new = updated.lines().any(|line| {
            let trimmed = line.trim();
            trimmed == *new_pattern || trimmed == new_pattern.trim_end_matches('/')
        });

        if has_old && !has_new {
            updated = updated.replace(old_pattern, new_pattern);
            log::info!(
                "Migrated .gitignore pattern: {} -> {}",
                old_pattern,
                new_pattern
            );
            made_changes = true;
        }
    }

    if made_changes {
        fs::write(&gitignore_path, updated)
            .with_context(|| format!("write {}", gitignore_path.display()))?;
    }

    Ok(made_changes)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

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

        ensure_cueloop_gitignore_entries(repo_root)?;

        let gitignore_path = repo_root.join(".gitignore");
        assert!(gitignore_path.exists());
        let content = fs::read_to_string(&gitignore_path)?;
        assert!(content.contains(".cueloop/workspaces/"));
        assert!(content.contains(".cueloop/logs/"));
        assert!(content.contains(".cueloop/trust.jsonc"));
        assert!(content.contains("# CueLoop parallel mode"));
        assert!(content.contains("# CueLoop debug logs"));
        assert!(content.contains("# CueLoop local trust decision"));
        Ok(())
    }

    #[test]
    fn ensure_cueloop_gitignore_entries_appends_to_existing() -> Result<()> {
        let temp = TempDir::new()?;
        let repo_root = temp.path();
        let gitignore_path = repo_root.join(".gitignore");
        fs::write(&gitignore_path, ".env\ntarget/\n")?;

        ensure_cueloop_gitignore_entries(repo_root)?;

        let content = fs::read_to_string(&gitignore_path)?;
        assert!(content.contains(".env"));
        assert!(content.contains("target/"));
        assert!(content.contains(".cueloop/workspaces/"));
        assert!(content.contains(".cueloop/logs/"));
        assert!(content.contains(".cueloop/trust.jsonc"));
        Ok(())
    }

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

        // Run twice
        ensure_cueloop_gitignore_entries(repo_root)?;
        ensure_cueloop_gitignore_entries(repo_root)?;

        let gitignore_path = repo_root.join(".gitignore");
        let content = fs::read_to_string(&gitignore_path)?;

        // Should only have one entry for each
        let workspaces_count = content.matches(".cueloop/workspaces/").count();
        let logs_count = content.matches(".cueloop/logs/").count();
        let trust_count = content.matches(".cueloop/trust.jsonc").count();
        assert_eq!(
            workspaces_count, 1,
            "Should only have one .cueloop/workspaces/ entry"
        );
        assert_eq!(logs_count, 1, "Should only have one .cueloop/logs/ entry");
        assert_eq!(
            trust_count, 1,
            "Should only have one .cueloop/trust.jsonc entry"
        );
        Ok(())
    }

    #[test]
    fn ensure_cueloop_gitignore_entries_detects_existing_workspaces_entry() -> Result<()> {
        let temp = TempDir::new()?;
        let repo_root = temp.path();
        let gitignore_path = repo_root.join(".gitignore");
        fs::write(&gitignore_path, ".cueloop/workspaces/\n")?;

        ensure_cueloop_gitignore_entries(repo_root)?;

        let content = fs::read_to_string(&gitignore_path)?;
        // Should add logs and trust but not duplicate workspaces
        assert!(content.contains(".cueloop/logs/"));
        assert!(content.contains(".cueloop/trust.jsonc"));
        let workspaces_count = content.matches(".cueloop/workspaces/").count();
        assert_eq!(
            workspaces_count, 1,
            "Should not add duplicate workspaces entry"
        );
        Ok(())
    }

    #[test]
    fn ensure_cueloop_gitignore_entries_detects_existing_logs_entry() -> Result<()> {
        let temp = TempDir::new()?;
        let repo_root = temp.path();
        let gitignore_path = repo_root.join(".gitignore");
        fs::write(&gitignore_path, ".cueloop/logs/\n")?;

        ensure_cueloop_gitignore_entries(repo_root)?;

        let content = fs::read_to_string(&gitignore_path)?;
        // Should add workspaces and trust but not duplicate logs
        assert!(content.contains(".cueloop/workspaces/"));
        assert!(content.contains(".cueloop/trust.jsonc"));
        let logs_count = content.matches(".cueloop/logs/").count();
        assert_eq!(logs_count, 1, "Should not add duplicate logs entry");
        Ok(())
    }

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

        ensure_local_queue_gitignore_entries(repo_root)?;
        ensure_local_queue_gitignore_entries(repo_root)?;

        let content = fs::read_to_string(repo_root.join(".gitignore"))?;
        assert_eq!(content.matches(".cueloop/queue.jsonc").count(), 1);
        assert_eq!(content.matches(".cueloop/done.jsonc").count(), 1);
        Ok(())
    }

    #[test]
    fn ensure_cueloop_gitignore_entries_detects_existing_entry_without_trailing_slash() -> Result<()>
    {
        let temp = TempDir::new()?;
        let repo_root = temp.path();
        let gitignore_path = repo_root.join(".gitignore");
        fs::write(&gitignore_path, ".cueloop/workspaces\n.cueloop/logs\n")?;

        ensure_cueloop_gitignore_entries(repo_root)?;

        let content = fs::read_to_string(&gitignore_path)?;
        // Should not add the trailing-slash version if non-trailing exists
        let workspaces_count = content
            .lines()
            .filter(|l| l.contains(".cueloop/workspaces"))
            .count();
        let logs_count = content
            .lines()
            .filter(|l| l.contains(".cueloop/logs"))
            .count();
        assert_eq!(
            workspaces_count, 1,
            "Should not add duplicate workspaces entry"
        );
        assert_eq!(logs_count, 1, "Should not add duplicate logs entry");
        Ok(())
    }

    #[test]
    fn is_runtime_ignore_entry_matches_variations() {
        assert!(is_runtime_ignore_entry(
            ".cueloop/logs/",
            ".cueloop",
            "logs"
        ));
        assert!(is_runtime_ignore_entry(".cueloop/logs", ".cueloop", "logs"));
        assert!(is_runtime_ignore_entry(
            "  .cueloop/logs/  ",
            ".cueloop",
            "logs"
        ));
        assert!(is_runtime_ignore_entry(
            "  .cueloop/logs  ",
            ".cueloop",
            "logs"
        ));
        assert!(!is_runtime_ignore_entry(
            ".cueloop/logs/debug.log",
            ".cueloop",
            "logs"
        ));
        assert!(!is_runtime_ignore_entry(
            "# .cueloop/logs/",
            ".cueloop",
            "logs"
        ));
        assert!(!is_runtime_ignore_entry(
            "something else",
            ".cueloop",
            "logs"
        ));
    }
}