cueloop 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
//! Tests for git workspace helpers.
//!
//! Purpose:
//! - Tests for git workspace helpers.
//!
//! Responsibilities:
//! - Verify workspace path resolution, clone/reset lifecycle, and removal safety.
//! - Keep regression coverage for origin retargeting and invalid workspace replacement.
//! - Isolate environment-variable-sensitive tests behind serialization.
//!
//! Not handled here:
//! - Parallel orchestration behavior outside git workspace management.
//! - PR or branch-push flows.
//!
//!
//! Usage:
//! - Used through the crate module tree or integration test harness.
//!
//! Invariants/assumptions:
//! - Tests use temporary repositories and temp-root derived paths.
//! - HOME mutation tests hold a global lock for process safety.

use std::env;
use std::path::PathBuf;
use std::sync::Mutex;

use anyhow::Result;
use serial_test::serial;
use tempfile::TempDir;

use crate::contracts::{Config, ParallelConfig};
use crate::testsupport::git as git_test;

use super::{create_workspace_at, ensure_workspace_exists, remove_workspace, workspace_root};

static ENV_LOCK: Mutex<()> = Mutex::new(());

#[test]
fn workspace_root_uses_repo_root_for_relative_path() {
    let cfg = Config {
        parallel: ParallelConfig {
            workspace_root: Some(PathBuf::from(".cueloop/workspaces/custom")),
            ..ParallelConfig::default()
        },
        ..Config::default()
    };
    let repo_root = crate::testsupport::path::portable_abs_path("cueloop-test");
    let root = workspace_root(&repo_root, &cfg);
    assert_eq!(root, repo_root.join(".cueloop/workspaces/custom"));
}

#[test]
fn workspace_root_accepts_absolute_path() {
    let absolute_root = crate::testsupport::path::portable_abs_path("cueloop-workspaces");
    let cfg = Config {
        parallel: ParallelConfig {
            workspace_root: Some(absolute_root.clone()),
            ..ParallelConfig::default()
        },
        ..Config::default()
    };
    let repo_root = crate::testsupport::path::portable_abs_path("cueloop-test");
    let root = workspace_root(&repo_root, &cfg);
    assert_eq!(root, absolute_root);
}

#[test]
fn workspace_root_defaults_outside_repo() {
    let cfg = Config {
        parallel: ParallelConfig::default(),
        ..Config::default()
    };
    let repo_root = crate::testsupport::path::portable_abs_path("cueloop-test");
    let root = workspace_root(&repo_root, &cfg);
    assert_eq!(
        root,
        repo_root
            .parent()
            .unwrap()
            .join(".workspaces")
            .join("cueloop-test")
            .join("parallel")
    );
}

#[test]
fn create_and_remove_workspace_round_trips() -> Result<()> {
    let temp = seeded_repo()?;
    let base_branch = current_branch(temp.path())?;
    let root = temp.path().join(".cueloop/workspaces/parallel");

    let spec = create_workspace_at(temp.path(), &root, "RQ-0001", &base_branch)?;
    assert!(spec.path.exists(), "workspace path should exist");
    assert_eq!(spec.branch, base_branch);

    remove_workspace(&root, &spec, true)?;
    assert!(!spec.path.exists());
    Ok(())
}

#[test]
fn create_workspace_reuses_existing_and_cleans() -> Result<()> {
    let temp = seeded_repo()?;
    let base_branch = current_branch(temp.path())?;
    let root = temp.path().join(".cueloop/workspaces/parallel");

    let first = create_workspace_at(temp.path(), &root, "RQ-0001", &base_branch)?;
    std::fs::write(first.path.join("dirty.txt"), "dirty")?;

    let second = create_workspace_at(temp.path(), &root, "RQ-0001", &base_branch)?;
    assert_eq!(first.path, second.path);
    assert!(!second.path.join("dirty.txt").exists());
    assert_eq!(second.branch, base_branch);

    remove_workspace(&root, &second, true)?;
    Ok(())
}

#[test]
fn create_workspace_reuses_existing_with_conflicting_untracked_tracked_path() -> Result<()> {
    let temp = seeded_repo()?;
    std::fs::create_dir_all(temp.path().join(".cueloop"))?;
    std::fs::write(
        temp.path().join(".cueloop/config.jsonc"),
        "{tracked_config}",
    )?;
    git_test::commit_all(temp.path(), "add tracked cueloop config")?;

    let base_branch = current_branch(temp.path())?;
    let root = temp.path().join(".cueloop/workspaces/parallel");
    let first = create_workspace_at(temp.path(), &root, "RQ-0009", &base_branch)?;

    git_test::git_run(&first.path, &["checkout", "-b", "stale-no-config"])?;
    git_test::git_run(&first.path, &["rm", "--cached", ".cueloop/config.jsonc"])?;
    git_test::git_run(
        &first.path,
        &["commit", "-m", "drop tracked config in stale branch"],
    )?;

    // Leave an untracked file at a path that is tracked on base_branch.
    std::fs::write(
        first.path.join(".cueloop/config.jsonc"),
        "{untracked_config}",
    )?;

    let stale_status = git_test::git_output(
        &first.path,
        &["status", "--porcelain", "--untracked-files=all"],
    )?;
    assert!(
        stale_status
            .lines()
            .any(|line| line.trim() == "?? .cueloop/config.jsonc"),
        "expected stale branch to have untracked .cueloop/config.jsonc, got: {stale_status}"
    );

    let second = create_workspace_at(temp.path(), &root, "RQ-0009", &base_branch)?;
    assert_eq!(first.path, second.path);
    let status_after = git_test::git_output(
        &second.path,
        &["status", "--porcelain", "--untracked-files=all"],
    )?;
    assert!(
        status_after.trim().is_empty(),
        "expected clean workspace after reuse reset, got: {status_after}"
    );
    assert!(second.path.join(".cueloop/config.jsonc").exists());

    remove_workspace(&root, &second, true)?;
    Ok(())
}

#[test]
fn create_workspace_with_existing_branch() -> Result<()> {
    let temp = seeded_repo()?;
    let base_branch = current_branch(temp.path())?;
    let root = temp.path().join(".cueloop/workspaces/parallel");

    let spec = create_workspace_at(temp.path(), &root, "RQ-0002", &base_branch)?;
    assert!(spec.path.exists());
    assert_eq!(spec.branch, base_branch);

    remove_workspace(&root, &spec, true)?;
    Ok(())
}

#[test]
fn create_workspace_requires_origin_remote() -> Result<()> {
    let temp = TempDir::new()?;
    git_test::init_repo(temp.path())?;
    std::fs::write(temp.path().join("init.txt"), "init")?;
    git_test::commit_all(temp.path(), "init")?;

    let base_branch = current_branch(temp.path())?;
    let root = temp.path().join(".cueloop/workspaces/parallel");

    let err = create_workspace_at(temp.path(), &root, "RQ-0003", &base_branch)
        .expect_err("missing origin should fail");
    assert!(err.to_string().contains("origin"));
    Ok(())
}

#[test]
fn remove_workspace_requires_force_when_dirty() -> Result<()> {
    let temp = seeded_repo()?;
    let base_branch = current_branch(temp.path())?;
    let root = temp.path().join(".cueloop/workspaces/parallel");

    let spec = create_workspace_at(temp.path(), &root, "RQ-0004", &base_branch)?;
    std::fs::write(spec.path.join("dirty.txt"), "dirty")?;
    let err = remove_workspace(&root, &spec, false).expect_err("dirty should fail");
    assert!(err.to_string().contains("dirty"));
    assert!(spec.path.exists());

    remove_workspace(&root, &spec, true)?;
    Ok(())
}

#[test]
fn ensure_workspace_exists_creates_missing_workspace() -> Result<()> {
    let temp = seeded_repo()?;
    let branch = current_branch(temp.path())?;
    let workspace_path = temp.path().join("workspaces/RQ-0001");

    ensure_workspace_exists(temp.path(), &workspace_path, &branch)?;

    assert!(workspace_path.exists(), "workspace path should exist");
    assert!(
        workspace_path.join(".git").exists(),
        "workspace should be a git repo"
    );
    assert_eq!(current_branch(&workspace_path)?, branch);

    Ok(())
}

#[test]
fn ensure_workspace_exists_reuses_existing_and_cleans() -> Result<()> {
    let temp = seeded_repo()?;
    let branch = current_branch(temp.path())?;
    let workspace_path = temp.path().join("workspaces/RQ-0001");

    ensure_workspace_exists(temp.path(), &workspace_path, &branch)?;
    std::fs::write(workspace_path.join("dirty.txt"), "dirty")?;
    std::fs::create_dir_all(workspace_path.join("untracked_dir"))?;
    std::fs::write(workspace_path.join("untracked_dir/file.txt"), "untracked")?;

    ensure_workspace_exists(temp.path(), &workspace_path, &branch)?;

    assert!(!workspace_path.join("dirty.txt").exists());
    assert!(!workspace_path.join("untracked_dir").exists());
    Ok(())
}

#[test]
fn ensure_workspace_exists_replaces_invalid_workspace() -> Result<()> {
    let temp = seeded_repo()?;
    let branch = current_branch(temp.path())?;
    let workspace_path = temp.path().join("workspaces/RQ-0001");

    std::fs::create_dir_all(&workspace_path)?;
    std::fs::write(workspace_path.join("some_file.txt"), "content")?;

    ensure_workspace_exists(temp.path(), &workspace_path, &branch)?;

    assert!(workspace_path.join(".git").exists());
    assert!(!workspace_path.join("some_file.txt").exists());
    Ok(())
}

#[test]
fn ensure_workspace_exists_replaces_unusable_git_workspace() -> Result<()> {
    let temp = seeded_repo()?;
    let branch = current_branch(temp.path())?;
    let workspace_path = temp.path().join("workspaces/RQ-0005");

    ensure_workspace_exists(temp.path(), &workspace_path, &branch)?;

    let pack_dir = workspace_path.join(".git/objects/pack");
    let mut removed_pack_artifacts = 0;
    for entry in std::fs::read_dir(&pack_dir)? {
        let path = entry?.path();
        if path.is_file() {
            std::fs::remove_file(path)?;
            removed_pack_artifacts += 1;
        }
    }
    assert!(
        removed_pack_artifacts > 0,
        "expected packed clone objects under {}",
        pack_dir.display()
    );
    std::fs::write(workspace_path.join("stale.txt"), "stale")?;

    ensure_workspace_exists(temp.path(), &workspace_path, &branch)?;

    assert!(workspace_path.join(".git").exists());
    assert!(!workspace_path.join("stale.txt").exists());
    assert_eq!(current_branch(&workspace_path)?, branch);
    Ok(())
}

#[test]
fn ensure_workspace_exists_fails_without_origin() -> Result<()> {
    let temp = TempDir::new()?;
    git_test::init_repo(temp.path())?;
    std::fs::write(temp.path().join("init.txt"), "init")?;
    git_test::commit_all(temp.path(), "init")?;

    let branch = current_branch(temp.path())?;
    let workspace_path = temp.path().join("workspaces/RQ-0001");

    let err = ensure_workspace_exists(temp.path(), &workspace_path, &branch)
        .expect_err("should fail without origin");
    assert!(err.to_string().contains("origin"));
    Ok(())
}

#[test]
#[serial]
fn workspace_root_expands_tilde_to_home() {
    let _guard = ENV_LOCK.lock().expect("env lock");
    let original_home = env::var("HOME").ok();

    unsafe { env::set_var("HOME", "/custom/home") };

    let cfg = Config {
        parallel: ParallelConfig {
            workspace_root: Some(PathBuf::from("~/cueloop-workspaces")),
            ..ParallelConfig::default()
        },
        ..Config::default()
    };
    let repo_root = crate::testsupport::path::portable_abs_path("cueloop-test");
    let root = workspace_root(&repo_root, &cfg);
    assert_eq!(root, PathBuf::from("/custom/home/cueloop-workspaces"));

    restore_home(original_home);
}

#[test]
#[serial]
fn workspace_root_expands_tilde_alone_to_home() {
    let _guard = ENV_LOCK.lock().expect("env lock");
    let original_home = env::var("HOME").ok();

    unsafe { env::set_var("HOME", "/custom/home") };

    let cfg = Config {
        parallel: ParallelConfig {
            workspace_root: Some(PathBuf::from("~")),
            ..ParallelConfig::default()
        },
        ..Config::default()
    };
    let repo_root = crate::testsupport::path::portable_abs_path("cueloop-test");
    let root = workspace_root(&repo_root, &cfg);
    assert_eq!(root, PathBuf::from("/custom/home"));

    restore_home(original_home);
}

#[test]
#[serial]
fn workspace_root_relative_when_home_unset() {
    let _guard = ENV_LOCK.lock().expect("env lock");
    let original_home = env::var("HOME").ok();

    unsafe { env::remove_var("HOME") };

    let cfg = Config {
        parallel: ParallelConfig {
            workspace_root: Some(PathBuf::from("~/workspaces")),
            ..ParallelConfig::default()
        },
        ..Config::default()
    };
    let repo_root = crate::testsupport::path::portable_abs_path("cueloop-test");
    let root = workspace_root(&repo_root, &cfg);
    assert_eq!(root, repo_root.join("~/workspaces"));

    restore_home(original_home);
}

fn seeded_repo() -> Result<TempDir> {
    let temp = TempDir::new()?;
    git_test::init_repo(temp.path())?;
    std::fs::write(temp.path().join("init.txt"), "init")?;
    git_test::commit_all(temp.path(), "init")?;
    git_test::git_run(
        temp.path(),
        &["remote", "add", "origin", "https://example.com/repo.git"],
    )?;
    Ok(temp)
}

fn current_branch(repo_root: &std::path::Path) -> Result<String> {
    git_test::git_output(repo_root, &["rev-parse", "--abbrev-ref", "HEAD"])
}

fn restore_home(original_home: Option<String>) {
    match original_home {
        Some(value) => unsafe { env::set_var("HOME", value) },
        None => unsafe { env::remove_var("HOME") },
    }
}