scud-cli 1.67.0

Fast, simple task master for AI-driven development
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
use anyhow::Result;
use colored::Colorize;
use dialoguer::{Confirm, Input, MultiSelect, Select};
use std::fs;
use std::path::PathBuf;

use crate::backpressure::BackpressureConfig;
use crate::commands::config as config_cmd;
use crate::commands::helpers::is_interactive;
use crate::config::{Config, LLMConfig};
use crate::storage::Storage;

/// Helper function to configure provider and model for a specific tier
fn configure_provider_and_model(tier: &str) -> Result<(String, String)> {
    let providers = vec![
        "Claude Code (recommended - no API key needed)",
        "Cursor Agent CLI (no API key needed)",
        "OpenAI Codex CLI (no API key needed)",
        "xAI (Grok)",
        "Anthropic API key (ANTHROPIC_API_KEY)",
        "Anthropic OAuth (Claude Code login - no API key needed)",
        "OpenAI (GPT API)",
        "OpenRouter",
    ];
    let provider_selection = Select::new()
        .with_prompt(format!("Select {} LLM provider", tier))
        .items(&providers)
        .default(if tier == "fast" { 3 } else { 0 }) // Default xAI for fast, Claude for smart
        .interact()?;

    let provider = match provider_selection {
        0 => "claude-cli",
        1 => "cursor",
        2 => "codex",
        3 => "xai",
        4 => "anthropic",
        5 => "anthropic-oauth",
        6 => "openai",
        7 => "openrouter",
        _ => "claude-cli",
    };

    // Build model options: suggested models + "Custom" option
    let suggested = Config::suggested_models_for_provider(provider);
    let mut model_options: Vec<String> = suggested.iter().map(|s| s.to_string()).collect();
    model_options.push("Custom (enter model name)".to_string());

    let default_model_index = if tier == "fast" && provider == "xai" {
        suggested
            .iter()
            .position(|m| *m == "xai/grok-code-fast-1")
            .unwrap_or(0)
    } else if tier == "smart" && provider == "claude-cli" {
        suggested.iter().position(|m| *m == "opus").unwrap_or(0)
    } else {
        0
    };

    let model_selection = Select::new()
        .with_prompt(format!(
            "Select {} model (or choose Custom to enter any model)",
            tier
        ))
        .items(&model_options)
        .default(default_model_index)
        .interact()?;

    let model = if model_selection == model_options.len() - 1 {
        // User selected "Custom"
        Input::<String>::new()
            .with_prompt("Enter model name")
            .interact_text()?
    } else {
        suggested[model_selection].to_string()
    };

    Ok((provider.to_string(), model))
}

/// Interactive backpressure configuration during init
fn configure_backpressure_interactive(storage: &Storage) -> Result<()> {
    println!();
    println!(
        "{}",
        "=== VALIDATION COMMANDS (BACKPRESSURE) ===".yellow().bold()
    );
    println!(
        "{}",
        "Backpressure runs validation commands between task waves".dimmed()
    );
    println!("{}", "to catch build/test failures early.".dimmed());
    println!();

    // Get auto-detected commands
    let auto_config = BackpressureConfig::load(Some(&storage.project_root().to_path_buf()))?;

    if !auto_config.commands.is_empty() {
        println!("{}", "Auto-detected commands:".blue());
        for cmd in &auto_config.commands {
            println!("  {} {}", "·".green(), cmd);
        }
        println!();
    }

    let options = vec![
        "Use auto-detect (recommended)",
        "Configure custom commands",
        "Skip (configure later with: scud config backpressure)",
    ];

    let selection = Select::new()
        .with_prompt("How would you like to configure validation?")
        .items(&options)
        .default(0)
        .interact()?;

    match selection {
        0 => {
            // Auto-detect - nothing to save, that's the default
            if auto_config.commands.is_empty() {
                println!(
                    "{}",
                    "  ⚠ No project type detected - add commands later with: scud config backpressure".yellow()
                );
            } else {
                println!("{}", "  ✓ Using auto-detected commands".green());
            }
        }
        1 => {
            // Custom configuration
            let commands = configure_backpressure_commands(&auto_config.commands)?;
            save_backpressure_config(storage, &commands)?;
            println!("{}", "  ✓ Custom backpressure commands saved".green());
        }
        2 => {
            // Skip
            println!(
                "{}",
                "  Skipped - configure later with: scud config backpressure".dimmed()
            );
        }
        _ => {}
    }

    Ok(())
}

/// Configure custom backpressure commands interactively
fn configure_backpressure_commands(auto_detected: &[String]) -> Result<Vec<String>> {
    println!();
    println!("{}", "Common validation commands:".blue());

    // Build a list of common commands based on what might be useful
    let mut suggestions: Vec<(&str, bool)> = vec![
        ("cargo build", false),
        ("cargo build --release", false),
        ("cargo test", false),
        ("cargo clippy -- -D warnings", false),
        ("cargo fmt --check", false),
        ("npm run build", false),
        ("npm test", false),
        ("npm run lint", false),
        ("npm run typecheck", false),
        ("go build ./...", false),
        ("go test ./...", false),
        ("pytest", false),
        ("python -m mypy .", false),
    ];

    // Mark auto-detected as selected by default
    for (cmd, selected) in &mut suggestions {
        if auto_detected.contains(&cmd.to_string()) {
            *selected = true;
        }
    }

    let items: Vec<&str> = suggestions.iter().map(|(cmd, _)| *cmd).collect();
    let defaults: Vec<bool> = suggestions.iter().map(|(_, selected)| *selected).collect();

    let selections = MultiSelect::new()
        .with_prompt("Select commands to run (space to toggle, enter to confirm)")
        .items(&items)
        .defaults(&defaults)
        .interact()?;

    let mut commands: Vec<String> = selections.iter().map(|&i| items[i].to_string()).collect();

    // Allow adding custom commands
    loop {
        let add_custom = Confirm::new()
            .with_prompt("Add a custom command?")
            .default(false)
            .interact()?;

        if !add_custom {
            break;
        }

        let custom: String = Input::new().with_prompt("Enter command").interact_text()?;

        if !custom.trim().is_empty() {
            commands.push(custom.trim().to_string());
            println!("  {} Added: {}", "".green(), custom.trim());
        }
    }

    if commands.is_empty() {
        println!(
            "{}",
            "  No commands selected - backpressure will be skipped".yellow()
        );
    } else {
        println!();
        println!("{}", "Selected commands:".blue());
        for (i, cmd) in commands.iter().enumerate() {
            println!("  {}. {}", i + 1, cmd.green());
        }
    }

    Ok(commands)
}

/// Save backpressure configuration to config file
fn save_backpressure_config(storage: &Storage, commands: &[String]) -> Result<()> {
    let config_path = storage.config_file();

    // Load existing config
    let content = fs::read_to_string(&config_path).unwrap_or_default();
    let mut config: toml::Value =
        toml::from_str(&content).unwrap_or(toml::Value::Table(toml::map::Map::new()));

    let table = config.as_table_mut().expect("Config must be a table");

    // Ensure swarm section exists
    if !table.contains_key("swarm") {
        table.insert(
            "swarm".to_string(),
            toml::Value::Table(toml::map::Map::new()),
        );
    }

    let swarm = table.get_mut("swarm").unwrap().as_table_mut().unwrap();

    // Create backpressure section
    let mut bp = toml::map::Map::new();
    let cmd_array: Vec<toml::Value> = commands
        .iter()
        .map(|s| toml::Value::String(s.clone()))
        .collect();
    bp.insert("commands".to_string(), toml::Value::Array(cmd_array));
    bp.insert("stop_on_failure".to_string(), toml::Value::Boolean(true));
    bp.insert("timeout_secs".to_string(), toml::Value::Integer(300));

    swarm.insert("backpressure".to_string(), toml::Value::Table(bp));

    // Save
    let output = toml::to_string_pretty(&config)?;
    fs::write(&config_path, output)?;

    Ok(())
}

pub fn run(project_root: Option<PathBuf>, provider_arg: Option<String>) -> Result<()> {
    let storage = Storage::new(project_root);

    if storage.is_initialized() {
        println!("{}", "✓ SCUD is already initialized".green());
        return Ok(());
    }

    println!("{}", "Initializing SCUD...".blue());
    println!();

    let (provider, model, smart_provider, smart_model, fast_provider, fast_model) = if let Some(
        provider_name,
    ) =
        provider_arg
    {
        // Non-interactive mode with command-line argument - use defaults for all tiers
        let provider = provider_name.to_lowercase();
        if !matches!(
            provider.as_str(),
            "xai"
                | "anthropic"
                | "anthropic-oauth"
                | "openai"
                | "openrouter"
                | "claude-cli"
                | "codex"
                | "cursor"
        ) {
            anyhow::bail!(
                "Invalid provider: {}. Valid options: claude-cli, cursor, codex, xai, anthropic, anthropic-oauth, openai, openrouter",
                provider
            );
        }
        let model = Config::default_model_for_provider(&provider).to_string();
        // Use defaults for smart/fast from Config (respects env vars)
        let defaults = Config::default();
        (
            provider,
            model,
            defaults.llm.smart_provider,
            defaults.llm.smart_model,
            defaults.llm.fast_provider,
            defaults.llm.fast_model,
        )
    } else if is_interactive() {
        println!(
            "{}",
            "SCUD supports separate models for different types of tasks:".blue()
        );
        println!("  • Fast models: Quick coding, generation tasks");
        println!("  • Smart models: Complex reasoning, analysis, validation");
        println!();

        // Configure FAST model/provider
        println!("{}", "=== FAST MODEL CONFIGURATION ===".yellow().bold());
        let (fast_provider, fast_model) = configure_provider_and_model("fast")?;

        // Configure SMART model/provider
        println!();
        println!("{}", "=== SMART MODEL CONFIGURATION ===".yellow().bold());
        let (smart_provider, smart_model) = configure_provider_and_model("smart")?;

        // Use fast provider/model as defaults for backward compatibility
        let provider = fast_provider.clone();
        let model = fast_model.clone();

        (
            provider,
            model,
            smart_provider,
            smart_model,
            fast_provider,
            fast_model,
        )
    } else {
        // Non-interactive without provider arg: use defaults from Config (respects env vars)
        let defaults = Config::default();
        (
            defaults.llm.provider,
            defaults.llm.model,
            defaults.llm.smart_provider,
            defaults.llm.smart_model,
            defaults.llm.fast_provider,
            defaults.llm.fast_model,
        )
    };

    let config = Config {
        llm: LLMConfig {
            provider,
            model,
            smart_provider,
            smart_model,
            fast_provider,
            fast_model,
            max_tokens: 16000,
        },
        swarm: crate::config::SwarmConfig::default(),
    };

    storage.initialize_with_config(&config)?;

    // Interactive backpressure configuration
    if is_interactive() {
        configure_backpressure_interactive(&storage)?;
    }

    println!("\n{}", "SCUD initialized successfully!".green().bold());

    // Auto-install all agents and commands
    println!("\n{}", "Installing SCUD agents and commands...".blue());
    if let Err(e) = config_cmd::agents_add(Some(storage.project_root().to_path_buf()), None, true) {
        println!("{}", format!("  Could not install agents: {}", e).yellow());
        println!("  You can install them later with: scud config agents add --all");
    }

    // Install spawn agents (harness/model routing definitions)
    println!("\n{}", "Installing spawn agent definitions...".blue());
    if let Err(e) = config_cmd::spawn_agents_add(
        Some(storage.project_root().to_path_buf()),
        None,
        true,
        false,
    ) {
        println!(
            "{}",
            format!("  Could not install spawn agents: {}", e).yellow()
        );
        println!("  You can install them later with: scud config spawn-agents add --all");
    } else {
        // Update spawn agents to match the configured providers/models
        if let Err(e) =
            config_cmd::spawn_agents_update_from_config(Some(storage.project_root().to_path_buf()))
        {
            println!(
                "{}",
                format!("  Could not update spawn agents: {}", e).yellow()
            );
        }
    }

    // Update CLAUDE.md with SCUD instructions
    if let Err(e) = update_claude_md(&storage) {
        println!(
            "{}",
            format!("  Could not update CLAUDE.md: {}", e).yellow()
        );
    }

    println!("\n{}", "Configuration:".blue());
    println!(
        "  Default Provider: {} ({})",
        config.llm.provider.yellow(),
        config.llm.model.yellow()
    );
    println!(
        "  Fast Provider: {} ({})",
        config.llm.fast_provider.yellow(),
        config.llm.fast_model.yellow()
    );
    println!(
        "  Smart Provider: {} ({})",
        config.llm.smart_provider.yellow(),
        config.llm.smart_model.yellow()
    );
    if config.requires_api_key() {
        println!("\n{}", "Environment variables required:".blue());
        let mut env_vars = std::collections::HashSet::new();
        env_vars.insert(config.api_key_env_var());
        if config.llm.fast_provider != config.llm.provider {
            env_vars.insert(Config::api_key_env_var_for_provider(
                &config.llm.fast_provider,
            ));
        }
        if config.llm.smart_provider != config.llm.provider
            && config.llm.smart_provider != config.llm.fast_provider
        {
            env_vars.insert(Config::api_key_env_var_for_provider(
                &config.llm.smart_provider,
            ));
        }
        for env_var in env_vars {
            if env_var != "NONE" {
                println!("  export {}=your-api-key", env_var.yellow());
            }
        }
    } else {
        println!("\n{}", "No API keys required (using CLI tools)".green());
    }
    println!("\n{}", "Next steps:".blue());
    println!("  1. Set your API key environment variable");
    println!("  2. Run: scud tags");
    println!("  3. Create or import tasks, then use: /scud:next\n");

    Ok(())
}

/// Update CLAUDE.md with SCUD instructions
fn update_claude_md(storage: &Storage) -> Result<()> {
    let claude_md_path = storage.project_root().join("CLAUDE.md");

    let scud_section = r#"
## SCUD Task Management

This project uses SCUD for AI-driven task management.

### Quick Start
- `scud tags` - List available phases
- `scud next` - Find next available task
- `scud set-status <id> in-progress` - Claim a task
- `scud view` - Open interactive task viewer

### Slash Commands
Use `/scud:` commands in Claude Code for task operations.
"#;

    let marker = "## SCUD Task Management";

    if claude_md_path.exists() {
        let content = fs::read_to_string(&claude_md_path)?;
        if content.contains(marker) {
            return Ok(()); // Already has SCUD section
        }
        // Append to existing file
        let new_content = format!("{}\n{}", content.trim_end(), scud_section);
        fs::write(&claude_md_path, new_content)?;
    } else {
        // Create new file
        fs::write(&claude_md_path, scud_section.trim_start())?;
    }

    println!("  {} Updated CLAUDE.md with SCUD instructions", "".green());
    Ok(())
}