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
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
//! Interactive onboarding wizard for CueLoop initialization.
//!
//! Purpose:
//! - Interactive onboarding wizard for CueLoop initialization.
//!
//! Responsibilities:
//! - Display welcome screen and collect user preferences.
//! - Guide users through runner, model, phase, and queue-tracking selection.
//! - Let users opt into explicit ignored-file sync entries for parallel workers.
//! - Optionally create a first task during setup.
//!
//! Not handled here:
//! - File creation (see `super::writers`).
//! - CLI argument parsing (handled by CLI layer).
//!
//!
//! Usage:
//! - Used through the crate module tree or integration test harness.
//!
//! Invariants/assumptions:
//! - Wizard is only run in interactive TTY environments.
//! - User inputs are validated before returning WizardAnswers.

use crate::commands::init::parallel_sync;
use crate::config;
use crate::contracts::{Runner, TaskPriority};
use anyhow::{Context, Result};
use dialoguer::{Confirm, Input, MultiSelect, Select};
use std::path::Path;

/// Queue/done tracking mode selected during interactive initialization.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueueTrackingMode {
    /// Keep queue/done files trackable for shared team task state.
    TrackedShared,
    /// Gitignore queue/done files for local-only task state.
    LocalIgnored,
}

/// Answers collected from the interactive wizard.
#[derive(Debug, Clone)]
pub struct WizardAnswers {
    /// Selected AI runner.
    pub runner: Runner,
    /// Selected model (as string for flexibility).
    pub model: String,
    /// Number of phases (1, 2, or 3).
    pub phases: u8,
    /// Queue/done tracking choice.
    pub queue_tracking_mode: QueueTrackingMode,
    /// Explicit ignored local files selected for parallel worker sync.
    pub parallel_ignored_file_allowlist: Vec<String>,
    /// Whether to create a first task.
    pub create_first_task: bool,
    /// Title for the first task (if created).
    pub first_task_title: Option<String>,
    /// Description/request for the first task (if created).
    pub first_task_description: Option<String>,
    /// Priority for the first task.
    pub first_task_priority: TaskPriority,
}

impl Default for WizardAnswers {
    fn default() -> Self {
        Self {
            runner: Runner::Claude,
            model: "sonnet".to_string(),
            phases: 3,
            queue_tracking_mode: QueueTrackingMode::TrackedShared,
            parallel_ignored_file_allowlist: Vec::new(),
            create_first_task: false,
            first_task_title: None,
            first_task_description: None,
            first_task_priority: TaskPriority::Medium,
        }
    }
}

/// Run the interactive onboarding wizard and collect user preferences.
pub fn run_wizard(repo_root: &Path) -> Result<WizardAnswers> {
    // Welcome screen
    print_welcome();

    // Runner selection
    let runners = [
        (
            "Claude",
            "Anthropic's Claude Code CLI - Best for complex reasoning",
        ),
        ("Codex", "OpenAI's Codex CLI - Great for code generation"),
        ("OpenCode", "OpenCode agent - Open source alternative"),
        (
            "Gemini",
            "Google's Gemini CLI - Good for large context windows",
        ),
        ("Cursor", "Cursor's agent mode - IDE-integrated workflow"),
        ("Kimi", "Moonshot AI Kimi - Strong coding capabilities"),
        ("Pi", "Inflection Pi - Conversational AI assistant"),
    ];

    let runner_idx = Select::new()
        .with_prompt("Select your AI runner")
        .items(
            runners
                .iter()
                .map(|(name, desc)| format!("{} - {}", name, desc))
                .collect::<Vec<_>>(),
        )
        .default(0)
        .interact()
        .context("failed to get runner selection")?;

    let runner = match runner_idx {
        0 => Runner::Claude,
        1 => Runner::Codex,
        2 => Runner::Opencode,
        3 => Runner::Gemini,
        4 => Runner::Cursor,
        5 => Runner::Kimi,
        6 => Runner::Pi,
        _ => Runner::Claude, // default fallback
    };

    // Model selection based on runner
    let model = select_model(&runner)?;

    // Phase selection
    let phases = select_phases()?;

    // Queue and parallel-worker setup choices
    let queue_tracking_mode = select_queue_tracking_mode(repo_root)?;
    let parallel_ignored_file_allowlist = select_parallel_sync_allowlist(repo_root)?;

    // First task creation
    let create_first_task = Confirm::new()
        .with_prompt("Would you like to create your first task now?")
        .default(true)
        .interact()
        .context("failed to get first task confirmation")?;

    let (first_task_title, first_task_description, first_task_priority) = if create_first_task {
        let title: String = Input::new()
            .with_prompt("Task title")
            .allow_empty(false)
            .interact_text()
            .context("failed to get task title")?;

        let description: String = Input::new()
            .with_prompt("Task description (what should be done)")
            .allow_empty(true)
            .interact_text()
            .context("failed to get task description")?;

        let priorities = vec!["Low", "Medium", "High", "Critical"];
        let priority_idx = Select::new()
            .with_prompt("Task priority")
            .items(&priorities)
            .default(1)
            .interact()
            .context("failed to get priority selection")?;

        let priority = match priority_idx {
            0 => TaskPriority::Low,
            1 => TaskPriority::Medium,
            2 => TaskPriority::High,
            3 => TaskPriority::Critical,
            _ => TaskPriority::Medium,
        };

        (Some(title), Some(description), priority)
    } else {
        (None, None, TaskPriority::Medium)
    };

    // Summary and confirmation
    let answers = WizardAnswers {
        runner,
        model,
        phases,
        queue_tracking_mode,
        parallel_ignored_file_allowlist,
        create_first_task,
        first_task_title,
        first_task_description,
        first_task_priority,
    };

    print_summary(repo_root, &answers);

    let proceed = Confirm::new()
        .with_prompt("Proceed with setup?")
        .default(true)
        .interact()
        .context("failed to get confirmation")?;

    if !proceed {
        anyhow::bail!("Setup cancelled by user");
    }

    Ok(answers)
}

/// Print the welcome screen with ASCII art.
fn print_welcome() {
    println!();
    println!("{}", colored::Colorize::bright_cyan("CueLoop"));
    println!("{}", colored::Colorize::bright_black("───────"));
    println!();
    println!("{}", colored::Colorize::bold("Welcome to CueLoop!"));
    println!();
    println!("CueLoop is an AI task queue for structured agent workflows.");
    println!("This wizard will help you set up your project and create your first task.");
    println!();
}

/// Select model based on the chosen runner.
fn select_model(runner: &Runner) -> Result<String> {
    let models: Vec<(&str, &str)> = match runner {
        Runner::Claude => vec![
            ("sonnet", "Balanced speed and intelligence (recommended)"),
            ("opus", "Most powerful, best for complex tasks"),
            ("haiku", "Fastest, good for simple tasks"),
            ("custom", "Other model (specify)"),
        ],
        Runner::Codex => vec![
            (
                "gpt-5.4",
                "Latest general GPT-5 model for Codex (recommended)",
            ),
            ("gpt-5.3-codex", "Codex optimized for coding"),
            ("gpt-5.3-codex-spark", "Codex Spark variant for coding"),
            ("gpt-5.3", "General GPT-5.3"),
            ("custom", "Other model (specify)"),
        ],
        Runner::Gemini => vec![
            (
                "zai-coding-plan/glm-4.7",
                "Default Gemini model (recommended)",
            ),
            ("custom", "Other model (specify)"),
        ],
        Runner::Opencode => vec![
            ("zai-coding-plan/glm-4.7", "GLM-4.7 model (recommended)"),
            ("custom", "Other model (specify)"),
        ],
        Runner::Kimi => vec![
            ("kimi-for-coding", "Kimi coding model (recommended)"),
            ("custom", "Other model (specify)"),
        ],
        Runner::Pi => vec![
            ("gpt-5.3", "GPT-5.3 model (recommended)"),
            ("custom", "Other model (specify)"),
        ],
        Runner::Cursor => vec![
            ("auto", "Let Cursor choose automatically (recommended)"),
            ("custom", "Other model (specify)"),
        ],
        Runner::Plugin(_) => vec![
            ("default", "Use runner default"),
            ("custom", "Specify custom model"),
        ],
    };

    let items: Vec<String> = models
        .iter()
        .map(|(name, desc)| format!("{} - {}", name, desc))
        .collect();

    let idx = Select::new()
        .with_prompt("Select model")
        .items(&items)
        .default(0)
        .interact()
        .context("failed to get model selection")?;

    let selected = models[idx].0;

    if selected == "custom" {
        let custom: String = Input::new()
            .with_prompt("Enter model name")
            .allow_empty(false)
            .interact_text()
            .context("failed to get custom model")?;
        Ok(custom)
    } else {
        Ok(selected.to_string())
    }
}

/// Select the number of phases with explanations.
fn select_phases() -> Result<u8> {
    let phase_options = [
        (
            "3-phase (Full)",
            "Plan → Implement + CI → Review + Complete [Recommended]",
        ),
        (
            "2-phase (Standard)",
            "Plan → Implement (faster, less review)",
        ),
        (
            "1-phase (Quick)",
            "Single-pass execution (simple fixes only)",
        ),
    ];

    let items: Vec<String> = phase_options
        .iter()
        .map(|(name, desc)| format!("{} - {}", name, desc))
        .collect();

    let idx = Select::new()
        .with_prompt("Select workflow mode")
        .items(&items)
        .default(0)
        .interact()
        .context("failed to get phase selection")?;

    Ok(match idx {
        0 => 3,
        1 => 2,
        2 => 1,
        _ => 3,
    })
}

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

/// Select how queue/done files should be tracked.
fn select_queue_tracking_mode(repo_root: &Path) -> Result<QueueTrackingMode> {
    let runtime_name = runtime_dir_name(repo_root);
    let tracked_desc = format!(
        "Commit {runtime_name}/queue.jsonc and {runtime_name}/done.jsonc so the team shares task state [Recommended]"
    );
    let options = [
        ("Tracked shared queue", tracked_desc.as_str()),
        (
            "Local private queue",
            "Gitignore queue/done so task state stays local to this checkout",
        ),
    ];
    let items = options
        .iter()
        .map(|(name, desc)| format!("{name} - {desc}"))
        .collect::<Vec<_>>();
    let idx = Select::new()
        .with_prompt("How should CueLoop queue files be tracked?")
        .items(&items)
        .default(0)
        .interact()
        .context("failed to get queue tracking selection")?;
    Ok(if idx == 1 {
        QueueTrackingMode::LocalIgnored
    } else {
        QueueTrackingMode::TrackedShared
    })
}

/// Let the user select extra ignored local files for parallel-worker sync.
fn select_parallel_sync_allowlist(repo_root: &Path) -> Result<Vec<String>> {
    let candidates = parallel_sync::discover_parallel_sync_candidates(repo_root)
        .context("discover parallel ignored-file sync candidates")?;
    if candidates.is_empty() {
        println!();
        println!(
            "Parallel sync: no extra ignored local files found. .env and .env.* are synced by default."
        );
        println!(
            "To sync another small ignored file later, set trusted parallel.ignored_file_allowlist in the active runtime config (for example \"local/tool-config.json\"). Directory trees such as \"node_modules/*\" or entries ending in \"/\" are rejected. See docs/configuration/queue-and-parallel.md#ignored-local-file-sync."
        );
        return Ok(Vec::new());
    }

    println!();
    println!(
        "Parallel sync: .env and .env.* are synced by default. Select any additional small ignored files workers need."
    );
    println!(
        "Manual trusted config key: parallel.ignored_file_allowlist (valid: \"local/tool-config.json\"; invalid: \"node_modules/*\" or \"dir/\"). See docs/configuration/queue-and-parallel.md#ignored-local-file-sync."
    );
    let selections = MultiSelect::new()
        .with_prompt("Additional ignored files to sync to parallel workers")
        .items(&candidates)
        .defaults(&vec![true; candidates.len()])
        .interact()
        .context("failed to get parallel ignored-file sync selection")?;

    Ok(selections
        .into_iter()
        .filter_map(|idx| candidates.get(idx).cloned())
        .collect())
}

/// Print a summary of the wizard answers.
fn print_summary(repo_root: &Path, answers: &WizardAnswers) {
    let runtime_name = runtime_dir_name(repo_root);
    println!();
    println!("{}", colored::Colorize::bold("Setup Summary:"));
    println!("{}", colored::Colorize::bright_black("──────────────"));
    println!(
        "Runner: {} ({})",
        colored::Colorize::bright_green(format!("{:?}", answers.runner).as_str()),
        answers.model
    );
    println!(
        "Workflow: {}-phase",
        colored::Colorize::bright_green(format!("{}", answers.phases).as_str())
    );

    println!(
        "Queue mode: {}",
        match answers.queue_tracking_mode {
            QueueTrackingMode::TrackedShared => colored::Colorize::bright_green("tracked shared"),
            QueueTrackingMode::LocalIgnored => colored::Colorize::bright_yellow("local ignored"),
        }
    );
    if answers.parallel_ignored_file_allowlist.is_empty() {
        println!(
            "Parallel sync extras: {}",
            colored::Colorize::bright_black("(none)")
        );
    } else {
        println!(
            "Parallel sync extras: {}",
            colored::Colorize::bright_green(
                answers.parallel_ignored_file_allowlist.join(", ").as_str()
            )
        );
    }

    if answers.create_first_task {
        if let Some(ref title) = answers.first_task_title {
            println!(
                "First Task: {}",
                colored::Colorize::bright_green(title.as_str())
            );
        }
    } else {
        println!("First Task: {}", colored::Colorize::bright_black("(none)"));
    }

    println!();
    println!("Files to create:");
    println!("  - {runtime_name}/config.jsonc");
    println!("  - {runtime_name}/queue.jsonc");
    println!("  - {runtime_name}/done.jsonc");
    println!();
}

/// Print completion message with next steps.
pub fn print_completion_message(answers: Option<&WizardAnswers>, queue_path: &Path) {
    let runtime_name = queue_path
        .parent()
        .and_then(|path| path.file_name())
        .and_then(|name| name.to_str())
        .unwrap_or(crate::constants::identity::PROJECT_RUNTIME_DIR);
    println!();
    println!(
        "{}",
        colored::Colorize::bright_green("✓ CueLoop initialized successfully!")
    );
    println!();
    println!("{}", colored::Colorize::bold("Next steps:"));
    println!("  1. Run 'cueloop app open' to open the macOS app (optional)");
    println!("  2. Run 'cueloop run one' to execute your first task");
    println!("  3. Edit {runtime_name}/config.jsonc to customize settings");
    println!(
        "     - Parallel sync extras use trusted parallel.ignored_file_allowlist (valid: \"local/tool-config.json\"; invalid: \"node_modules/*\"); see docs/configuration/queue-and-parallel.md#ignored-local-file-sync"
    );
    println!("  4. Keep {runtime_name}/trust.jsonc untracked; init adds it to .gitignore");

    if let Some(answers) = answers
        && answers.create_first_task
    {
        println!();
        println!("Your first task is ready to go!");
    }

    println!();
}

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

    #[test]
    fn wizard_answers_default() {
        let answers = WizardAnswers::default();
        assert_eq!(answers.runner, Runner::Claude);
        assert_eq!(answers.model, "sonnet");
        assert_eq!(answers.phases, 3);
        assert_eq!(
            answers.queue_tracking_mode,
            QueueTrackingMode::TrackedShared
        );
        assert!(answers.parallel_ignored_file_allowlist.is_empty());
        assert!(!answers.create_first_task);
        assert!(answers.first_task_title.is_none());
        assert!(answers.first_task_description.is_none());
        assert_eq!(answers.first_task_priority, TaskPriority::Medium);
    }
}