sprit-mutilagent 0.3.2

A robust command-line toolkit for managing multiple AI coding agents in isolated tmux sessions with comprehensive workspace management and health monitoring
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
//! Configuration management for the Sprite multi-agent workflow toolkit.
//!
//! This module provides functions for loading, saving, and validating YAML
//! configuration files using the data models defined in the models module.

use crate::error::SpriteError;
use crate::models::{Agent, ProjectConfig};
use crate::utils::project;
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Default configuration file path
#[allow(dead_code)]
pub const DEFAULT_CONFIG_PATH: &str = "agents/agents.yaml";

fn resolve_config_path(path: &Path) -> PathBuf {
    if path.is_absolute() {
        return path.to_path_buf();
    }

    if let Ok(project_root) = project::find_project_root() {
        return project_root.join(path);
    }

    path.to_path_buf()
}

/// Load configuration from a file.
///
/// This function loads a YAML configuration file, validates it, and returns
/// a ProjectConfig instance. It provides clear error messages for common
/// configuration issues.
#[allow(dead_code)]
pub fn load_config<P: AsRef<Path>>(path: P) -> Result<ProjectConfig> {
    let original_path = path.as_ref();
    let resolved_path = resolve_config_path(original_path);

    // Check if file exists
    if !resolved_path.exists() {
        return Err(SpriteError::config_with_source(
            format!("Configuration file not found: {}", resolved_path.display()),
            std::io::Error::new(std::io::ErrorKind::NotFound, "File not found"),
        )
        .into());
    }

    // Read file content
    let content = std::fs::read_to_string(&resolved_path).with_context(|| {
        format!(
            "Failed to read configuration file: {}",
            resolved_path.display()
        )
    })?;

    // Parse YAML
    let config: ProjectConfig = serde_yaml::from_str(&content).map_err(|e| {
        SpriteError::yaml(
            format!("Failed to parse YAML configuration: {}", e),
            e.location().map(|loc| loc.line()),
            e.location().map(|loc| loc.column()),
        )
    })?;

    // Validate configuration
    config
        .validate()
        .map_err(|e| SpriteError::config(format!("Configuration validation failed: {}", e)))?;

    Ok(config)
}

/// Save configuration to a file.
///
/// This function saves a ProjectConfig instance to a YAML file, creating
/// the parent directory if necessary.
#[allow(dead_code)]
pub fn save_config<P: AsRef<Path>>(config: &ProjectConfig, path: P) -> Result<()> {
    let original_path = path.as_ref();
    let resolved_path = resolve_config_path(original_path);

    // Validate configuration before saving
    config
        .validate()
        .map_err(|e| SpriteError::config(format!("Cannot save invalid configuration: {}", e)))?;

    // Create parent directory if it doesn't exist
    if let Some(parent) = resolved_path.parent() {
        std::fs::create_dir_all(parent).with_context(|| {
            format!(
                "Failed to create configuration directory: {}",
                parent.display()
            )
        })?;
    }

    // Serialize to YAML
    let content = serde_yaml::to_string(config)
        .map_err(|e| SpriteError::config(format!("Failed to serialize configuration: {}", e)))?;

    // Write to file
    std::fs::write(&resolved_path, content).with_context(|| {
        format!(
            "Failed to write configuration file: {}",
            resolved_path.display()
        )
    })?;

    Ok(())
}

/// Create a default configuration with example agents.
///
/// This function creates a sample configuration with three agents that
/// users can modify for their own needs.
#[allow(dead_code)]
pub fn create_default_config() -> ProjectConfig {
    let mut agents = Vec::new();

    // Agent 1: Frontend development
    agents.push(Agent::new("1", "feature/frontend-work"));
    if let Some(agent) = agents.last_mut() {
        agent.model = Some("claude-sonnet-4".to_string());
        agent.description = Some("Frontend development specialist".to_string());
        agent.config.custom_settings.insert(
            "role".to_string(),
            serde_yaml::Value::String("frontend".to_string()),
        );
        agent
            .config
            .startup_commands
            .push("npm install".to_string());
        agent
            .config
            .startup_commands
            .push("npm run dev".to_string());
    }

    // Agent 2: Backend development
    agents.push(Agent::new("2", "feature/backend-work"));
    if let Some(agent) = agents.last_mut() {
        agent.model = Some("claude-sonnet-4".to_string());
        agent.description = Some("Backend development specialist".to_string());
        agent.config.custom_settings.insert(
            "role".to_string(),
            serde_yaml::Value::String("backend".to_string()),
        );
        agent
            .config
            .startup_commands
            .push("cargo build".to_string());
        agent.config.startup_commands.push("cargo run".to_string());
    }

    // Agent 3: Testing and QA
    agents.push(Agent::new("3", "feature/testing-work"));
    if let Some(agent) = agents.last_mut() {
        agent.model = Some("claude-sonnet-4".to_string());
        agent.description = Some("Testing and QA specialist".to_string());
        agent.config.custom_settings.insert(
            "role".to_string(),
            serde_yaml::Value::String("testing".to_string()),
        );
        agent.config.startup_commands.push("cargo test".to_string());
    }

    ProjectConfig {
        version: "1.0".to_string(),
        agents,
        session_name: "sprite-session".to_string(),
        sync: crate::models::SyncConfig::default(),
        settings: crate::models::ProjectSettings::default(),
    }
}

/// Initialize a new configuration file.
///
/// This function creates a new default configuration and saves it to the
/// specified path. It will fail if the file already exists unless force is true.
#[allow(dead_code)]
pub fn init_config<P: AsRef<Path>>(path: P, force: bool) -> Result<ProjectConfig> {
    let original_path = path.as_ref();
    let resolved_path = resolve_config_path(original_path);

    // Check if file already exists
    if resolved_path.exists() && !force {
        return Err(SpriteError::config(format!(
            "Configuration file already exists: {}. Use --force to overwrite.",
            resolved_path.display()
        ))
        .into());
    }

    let config = create_default_config();
    save_config(&config, &resolved_path)?;

    Ok(config)
}

/// Load configuration from the default location.
///
/// This is a convenience function that loads configuration from the
/// standard agents/agents.yaml location.
#[allow(dead_code)]
pub fn load_default_config() -> Result<ProjectConfig> {
    load_config(DEFAULT_CONFIG_PATH)
}

/// Save configuration to the default location.
///
/// This is a convenience function that saves configuration to the
/// standard agents/agents.yaml location.
#[allow(dead_code)]
pub fn save_default_config(config: &ProjectConfig) -> Result<()> {
    save_config(config, DEFAULT_CONFIG_PATH)
}

/// Validate that we're in a git repository.
///
/// This function checks if the current directory is a git repository,
/// which is required for Sprite to function properly.
#[allow(dead_code)]
pub fn validate_git_repository() -> Result<()> {
    let output = std::process::Command::new("git")
        .args(["rev-parse", "--git-dir"])
        .output()
        .with_context(|| "Failed to execute git command. Is git installed?")?;

    if !output.status.success() {
        return Err(SpriteError::config(
            "Not a git repository. Sprite must be run from within a git repository.",
        )
        .into());
    }

    Ok(())
}

/// Get the current git branch.
///
/// This function returns the name of the current git branch.
#[allow(dead_code)]
pub fn get_current_branch() -> Result<String> {
    let output = std::process::Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .output()
        .with_context(|| "Failed to get current git branch")?;

    if !output.status.success() {
        return Err(SpriteError::git("Failed to get current branch").into());
    }

    let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(branch)
}

/// Check if a git branch exists.
///
/// This function checks if a branch with the given name exists in the repository.
#[allow(dead_code)]
pub fn branch_exists(branch: &str) -> Result<bool> {
    let output = std::process::Command::new("git")
        .args(["rev-parse", "--verify", &format!("refs/heads/{}", branch)])
        .output()
        .with_context(|| "Failed to check if branch exists")?;

    Ok(output.status.success())
}

/// Get the git repository root directory.
///
/// This function returns the absolute path to the git repository root.
#[allow(dead_code)]
pub fn get_git_root() -> Result<PathBuf> {
    let output = std::process::Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .output()
        .with_context(|| "Failed to get git repository root")?;

    if !output.status.success() {
        return Err(SpriteError::git("Failed to get git repository root").into());
    }

    let root_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(PathBuf::from(root_str))
}

/// Validate that an agent configuration is compatible with the current git repository.
#[allow(dead_code)]
pub fn validate_agent_config(agent: &Agent) -> Result<()> {
    // Check if the agent's branch exists
    if !branch_exists(&agent.branch)? {
        return Err(SpriteError::agent(
            format!(
                "Branch '{}' does not exist. Create it first with 'git checkout -b {}'",
                agent.branch, agent.branch
            ),
            Some(&agent.id),
        )
        .into());
    }

    // Check if the workspace path is within the git repository
    let git_root = get_git_root()?;
    let workspace_path = agent.workspace_path();

    if !workspace_path.starts_with(&git_root) {
        return Err(SpriteError::agent(
            format!(
                "Agent workspace '{}' must be within the git repository",
                workspace_path.display()
            ),
            Some(&agent.id),
        )
        .into());
    }

    Ok(())
}

/// Migrate configuration from an older version.
///
/// This function handles configuration migrations between versions.
#[allow(dead_code)]
pub fn migrate_config(
    _config: &mut ProjectConfig,
    from_version: &str,
    to_version: &str,
) -> Result<()> {
    if from_version == to_version {
        return Ok(());
    }

    match (from_version, to_version) {
        ("1.0", "1.0") => Ok(()),
        _ => Err(SpriteError::config(format!(
            "Unsupported migration from version {} to {}",
            from_version, to_version
        ))
        .into()),
    }
}

/// Get configuration statistics.
///
/// This function returns useful statistics about the configuration.
#[allow(dead_code)]
pub fn get_config_stats(config: &ProjectConfig) -> HashMap<String, serde_yaml::Value> {
    let mut stats = HashMap::new();

    stats.insert(
        "version".to_string(),
        serde_yaml::Value::String(config.version.clone()),
    );
    stats.insert(
        "agent_count".to_string(),
        serde_yaml::Value::Number(config.agents.len().into()),
    );
    stats.insert(
        "session_name".to_string(),
        serde_yaml::Value::String(config.session_name.clone()),
    );
    stats.insert(
        "auto_sync".to_string(),
        serde_yaml::Value::Bool(config.sync.auto_sync),
    );

    let active_agents = config.agents.iter().filter(|a| a.is_active()).count();
    stats.insert(
        "active_agents".to_string(),
        serde_yaml::Value::Number(active_agents.into()),
    );

    let error_agents = config.agents.iter().filter(|a| a.has_error()).count();
    stats.insert(
        "error_agents".to_string(),
        serde_yaml::Value::Number(error_agents.into()),
    );

    stats
}

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

    #[test]
    fn test_create_default_config() {
        let config = create_default_config();
        assert_eq!(config.version, "1.0");
        assert_eq!(config.agents.len(), 3);
        assert_eq!(config.session_name, "sprite-session");
        assert!(!config.sync.auto_sync);
    }

    #[test]
    fn test_save_and_load_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("test_config.yaml");

        let original_config = create_default_config();
        save_config(&original_config, &config_path).unwrap();

        let loaded_config = load_config(&config_path).unwrap();
        assert_eq!(original_config.version, loaded_config.version);
        assert_eq!(original_config.agents.len(), loaded_config.agents.len());
        assert_eq!(original_config.session_name, loaded_config.session_name);
    }

    #[test]
    fn test_load_nonexistent_config() {
        let result = load_config("nonexistent.yaml");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("not found"));
    }

    #[test]
    fn test_init_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("new_config.yaml");

        let config = init_config(&config_path, false).unwrap();
        assert!(config_path.exists());
        assert_eq!(config.agents.len(), 3);

        // Test that it fails when file already exists
        let result = init_config(&config_path, false);
        assert!(result.is_err());

        // Test that it succeeds with force
        let config2 = init_config(&config_path, true).unwrap();
        assert_eq!(config2.agents.len(), 3);
    }

    #[test]
    fn test_validate_git_repository() {
        // This test will fail if not run in a git repository
        // In CI, it should be run from within a git repo
        let _result = validate_git_repository();
        // We don't assert success here since it depends on the test environment
    }

    #[test]
    fn test_get_config_stats() {
        let config = create_default_config();
        let stats = get_config_stats(&config);

        assert_eq!(stats.get("version").unwrap().as_str().unwrap(), "1.0");
        assert_eq!(stats.get("agent_count").unwrap().as_i64().unwrap(), 3);
        assert_eq!(stats.get("active_agents").unwrap().as_i64().unwrap(), 0);
        assert_eq!(stats.get("error_agents").unwrap().as_i64().unwrap(), 0);
    }

    #[test]
    fn test_invalid_config() {
        let invalid_yaml = "invalid: yaml: content:";
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("invalid.yaml");
        fs::write(&config_path, invalid_yaml).unwrap();

        let result = load_config(&config_path);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("Failed to parse YAML"));
    }
}