ccswarm 0.5.0

AI-powered multi-agent orchestration system with proactive intelligence, security monitoring, and session management
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
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Think modes available in Claude Code
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ThinkMode {
    Think,
    ThinkHard,
    ThinkHarder,
    UltraThink,
    MegaThink,
}

impl ThinkMode {
    pub fn to_prompt_suffix(&self) -> &str {
        match self {
            ThinkMode::Think => "think",
            ThinkMode::ThinkHard => "think hard",
            ThinkMode::ThinkHarder => "think harder",
            ThinkMode::UltraThink => "ultrathink",
            ThinkMode::MegaThink => "megathink",
        }
    }
}

impl std::fmt::Display for ThinkMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.to_prompt_suffix())
    }
}

/// Output format for Claude Code CLI
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub enum OutputFormat {
    /// Plain text output
    #[default]
    Text,
    /// Structured JSON output with metadata
    Json,
    /// Streaming JSON output (each message as separate JSON object)
    StreamJson,
}

impl OutputFormat {
    /// Convert to CLI argument value
    pub fn as_cli_arg(&self) -> &'static str {
        match self {
            OutputFormat::Text => "text",
            OutputFormat::Json => "json",
            OutputFormat::StreamJson => "stream-json",
        }
    }
}

/// Claude Code configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeConfig {
    /// Model to use (e.g., "claude-3.5-sonnet")
    pub model: String,

    /// Whether to skip permission prompts
    #[serde(rename = "dangerously_skip_permissions")]
    pub dangerous_skip: bool,

    /// Think mode to use
    pub think_mode: Option<ThinkMode>,

    /// Output in JSON format
    pub json_output: bool,

    /// Output format: "text", "json", or "stream-json"
    #[serde(default)]
    pub output_format: OutputFormat,

    /// Append to system prompt (recommended over replacing)
    #[serde(default)]
    pub append_system_prompt: Option<String>,

    /// Maximum agentic turns in non-interactive mode
    #[serde(default)]
    pub max_turns: Option<u32>,

    /// Custom commands available to this agent
    pub custom_commands: Vec<String>,

    /// MCP servers configuration
    #[serde(rename = "mcpServers", default)]
    pub mcp_servers: HashMap<String, serde_json::Value>,
}

impl Default for ClaudeConfig {
    fn default() -> Self {
        Self {
            model: "claude-3.5-sonnet".to_string(),
            dangerous_skip: true,
            think_mode: Some(ThinkMode::Think),
            json_output: true,
            output_format: OutputFormat::Json,
            append_system_prompt: None,
            max_turns: None,
            custom_commands: Vec::new(),
            mcp_servers: HashMap::new(),
        }
    }
}

impl ClaudeConfig {
    /// Create config for Master Claude
    pub fn for_master() -> Self {
        Self {
            model: "claude-3.5-sonnet".to_string(),
            dangerous_skip: true,
            think_mode: Some(ThinkMode::UltraThink),
            json_output: true,
            output_format: OutputFormat::Json,
            append_system_prompt: None,
            max_turns: None,
            custom_commands: vec![
                "ccswarm status".to_string(),
                "ccswarm review".to_string(),
                "ccswarm deploy".to_string(),
                "ccswarm quality-gate".to_string(),
            ],
            mcp_servers: HashMap::new(),
        }
    }

    /// Create config for worker agents
    pub fn for_agent(role: &str) -> Self {
        let think_mode = match role {
            "frontend" | "backend" => Some(ThinkMode::ThinkHard),
            "devops" => Some(ThinkMode::Think),
            "qa" => Some(ThinkMode::ThinkHard),
            _ => Some(ThinkMode::Think),
        };

        let custom_commands = match role {
            "frontend" => vec![
                "npm test".to_string(),
                "npm run lint".to_string(),
                "npm run build".to_string(),
            ],
            "backend" => vec![
                "npm test".to_string(),
                "npm run migrate".to_string(),
                "npm run api-test".to_string(),
            ],
            "devops" => vec![
                "terraform plan".to_string(),
                "kubectl get pods".to_string(),
                "docker build".to_string(),
            ],
            "qa" => vec![
                "npm test".to_string(),
                "npm run e2e".to_string(),
                "npm run coverage".to_string(),
            ],
            _ => Vec::new(),
        };

        Self {
            model: "claude-3.5-sonnet".to_string(),
            dangerous_skip: true,
            think_mode,
            json_output: true,
            output_format: OutputFormat::Json,
            append_system_prompt: None,
            max_turns: None,
            custom_commands,
            mcp_servers: HashMap::new(),
        }
    }
}

/// Agent configuration from JSON
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentConfig {
    /// Agent specialization
    pub specialization: String,

    /// Git worktree path
    pub worktree: String,

    /// Git branch name
    pub branch: String,

    /// Claude configuration
    pub claude_config: ClaudeConfig,

    /// CLAUDE.md template to use
    pub claude_md_template: String,
}

/// Project configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectConfig {
    pub name: String,
    pub repository: RepositoryConfig,
    pub master_claude: MasterClaudeConfig,
}

impl Default for ProjectConfig {
    fn default() -> Self {
        Self {
            name: "default-project".to_string(),
            repository: RepositoryConfig::default(),
            master_claude: MasterClaudeConfig::default(),
        }
    }
}

/// Repository configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepositoryConfig {
    pub url: String,
    pub main_branch: String,
    /// Local path to the repository (defaults to current directory)
    #[serde(default)]
    pub local_path: Option<PathBuf>,
    /// Enable worktree isolation for task execution
    #[serde(default)]
    pub worktree_isolation: bool,
}

impl Default for RepositoryConfig {
    fn default() -> Self {
        Self {
            url: "https://github.com/example/repo.git".to_string(),
            main_branch: "main".to_string(),
            local_path: None,
            worktree_isolation: false,
        }
    }
}

/// Master Claude configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MasterClaudeConfig {
    pub role: String,
    pub quality_threshold: f64,
    pub think_mode: ThinkMode,
    pub permission_level: String,
    pub claude_config: ClaudeConfig,

    /// Enable proactive mode by default
    #[serde(default = "default_proactive_mode")]
    pub enable_proactive_mode: bool,

    /// Proactive analysis frequency (seconds)
    #[serde(default = "default_proactive_frequency")]
    pub proactive_frequency: u64,

    /// High-frequency proactive analysis frequency (seconds)
    #[serde(default = "default_high_frequency")]
    pub high_frequency: u64,
}

fn default_proactive_mode() -> bool {
    true // Enable proactive mode by default
}

fn default_proactive_frequency() -> u64 {
    30 // Default 30 seconds
}

fn default_high_frequency() -> u64 {
    15 // Default 15 seconds (high frequency mode)
}

impl Default for MasterClaudeConfig {
    fn default() -> Self {
        Self {
            role: "master".to_string(),
            quality_threshold: 0.85,
            think_mode: ThinkMode::Think,
            permission_level: "standard".to_string(),
            claude_config: ClaudeConfig::default(),
            enable_proactive_mode: default_proactive_mode(),
            proactive_frequency: default_proactive_frequency(),
            high_frequency: default_high_frequency(),
        }
    }
}

/// Coordination configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinationConfig {
    pub communication_method: String,
    pub sync_interval: u64,
    pub quality_gate_frequency: String,
    pub master_review_trigger: String,
}

impl Default for CoordinationConfig {
    fn default() -> Self {
        Self {
            communication_method: "json".to_string(),
            sync_interval: 30,
            quality_gate_frequency: "on_task_completion".to_string(),
            master_review_trigger: "auto".to_string(),
        }
    }
}

/// Complete ccswarm configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CcswarmConfig {
    pub project: ProjectConfig,
    #[serde(default)]
    pub agents: HashMap<String, AgentConfig>,
    pub coordination: CoordinationConfig,
}

impl CcswarmConfig {
    /// Load configuration from file with validation
    pub async fn from_file(path: PathBuf) -> anyhow::Result<Self> {
        let contents = tokio::fs::read_to_string(&path).await.map_err(|e| {
            anyhow::anyhow!("Failed to read config file '{}': {}", path.display(), e)
        })?;
        let config: Self = serde_json::from_str(&contents)
            .map_err(|e| anyhow::anyhow!("Invalid JSON in '{}': {}", path.display(), e))?;
        config.validate().map_err(|e| {
            anyhow::anyhow!("Config validation failed for '{}': {}", path.display(), e)
        })?;
        Ok(config)
    }

    /// Save configuration to file
    pub async fn to_file(&self, path: PathBuf) -> anyhow::Result<()> {
        let contents = serde_json::to_string_pretty(self)?;
        tokio::fs::write(path, contents).await?;
        Ok(())
    }

    /// Validate configuration after deserialization
    pub fn validate(&self) -> anyhow::Result<()> {
        // Project name must not be empty
        if self.project.name.trim().is_empty() {
            anyhow::bail!("project.name must not be empty");
        }

        // Repository URL must not be empty
        if self.project.repository.url.trim().is_empty() {
            anyhow::bail!("project.repository.url must not be empty");
        }

        // Main branch must not be empty
        if self.project.repository.main_branch.trim().is_empty() {
            anyhow::bail!("project.repository.main_branch must not be empty");
        }

        // Quality threshold must be between 0.0 and 1.0
        let threshold = self.project.master_claude.quality_threshold;
        if !(0.0..=1.0).contains(&threshold) {
            anyhow::bail!(
                "project.master_claude.quality_threshold must be between 0.0 and 1.0, got {}",
                threshold
            );
        }

        // Validate agent configs
        for (name, agent) in &self.agents {
            if agent.specialization.trim().is_empty() {
                anyhow::bail!("agents.{}.specialization must not be empty", name);
            }
            if agent.worktree.trim().is_empty() {
                anyhow::bail!("agents.{}.worktree must not be empty", name);
            }
            if agent.branch.trim().is_empty() {
                anyhow::bail!("agents.{}.branch must not be empty", name);
            }
        }

        // Sync interval must be positive
        if self.coordination.sync_interval == 0 {
            anyhow::bail!("coordination.sync_interval must be greater than 0");
        }

        Ok(())
    }
}

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

    #[test]
    fn test_basic_config_creation() {
        let config = ClaudeConfig::default();
        assert_eq!(config.model, "claude-3.5-sonnet");
        assert!(config.dangerous_skip);
    }

    #[test]
    fn test_config_validation_valid() {
        let config = CcswarmConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_validation_empty_project_name() {
        let mut config = CcswarmConfig::default();
        config.project.name = "".to_string();
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_validation_invalid_threshold() {
        let mut config = CcswarmConfig::default();
        config.project.master_claude.quality_threshold = 1.5;
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_validation_zero_sync_interval() {
        let mut config = CcswarmConfig::default();
        config.coordination.sync_interval = 0;
        assert!(config.validate().is_err());
    }
}