ccswarm 0.4.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
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
use anyhow::{Context, Result};
use async_trait::async_trait;
use serde_json::json;
use std::path::Path;
use std::time::Instant;
use tokio::process::Command;

use super::{
    ClaudeCodeConfig, ProviderCapabilities, ProviderConfig, ProviderExecutor, ProviderHealthStatus,
};
use crate::agent::{Task, TaskResult};
use crate::identity::AgentIdentity;

/// Claude Code provider executor implementation
pub struct ClaudeCodeExecutor {
    config: ClaudeCodeConfig,
}

impl ClaudeCodeExecutor {
    /// Create a new Claude Code executor
    pub fn new(config: ClaudeCodeConfig) -> Self {
        Self { config }
    }

    /// Generate task-specific prompt for Claude Code
    fn generate_task_prompt(&self, identity: &AgentIdentity, task: &Task) -> String {
        let agent_header = format!(
            "🤖 AGENT: {}\n📁 WORKSPACE: {}\n🎯 SCOPE: {:?}\n",
            identity.specialization.name(),
            identity.workspace_path.display(),
            task.task_type
        );

        let task_context = format!(
            "TASK ID: {}\nTASK TYPE: {:?}\nPRIORITY: {:?}\n",
            task.id, task.task_type, task.priority
        );

        let boundaries = self.generate_boundary_reminder(&identity.specialization);

        format!(
            "{}\n{}\n{}\nTASK DESCRIPTION:\n{}\n\n{}",
            agent_header,
            task_context,
            boundaries,
            task.description,
            task.details.as_deref().unwrap_or("")
        )
    }

    /// Generate boundary reminder based on agent specialization
    fn generate_boundary_reminder(&self, specialization: &crate::identity::AgentRole) -> String {
        match specialization {
            crate::identity::AgentRole::Frontend { .. } => {
                "REMEMBER: You are a FRONTEND specialist. Focus ONLY on:\n\
                 - UI components and interfaces\n\
                 - Client-side logic and state management\n\
                 - Styling and responsive design\n\
                 - Frontend testing and optimization\n\
                 DELEGATE backend/API work to backend agents."
            }
            crate::identity::AgentRole::Backend { .. } => {
                "REMEMBER: You are a BACKEND specialist. Focus ONLY on:\n\
                 - API endpoints and server logic\n\
                 - Database operations and migrations\n\
                 - Authentication and authorization\n\
                 - Backend testing and performance\n\
                 DELEGATE frontend/UI work to frontend agents."
            }
            crate::identity::AgentRole::DevOps { .. } => {
                "REMEMBER: You are a DEVOPS specialist. Focus ONLY on:\n\
                 - Infrastructure and deployment\n\
                 - CI/CD pipelines and automation\n\
                 - Monitoring and observability\n\
                 - Security and compliance\n\
                 DELEGATE application code to dev agents."
            }
            crate::identity::AgentRole::QA { .. } => {
                "REMEMBER: You are a QA specialist. Focus ONLY on:\n\
                 - Test strategy and implementation\n\
                 - Quality assurance and validation\n\
                 - Bug reporting and regression testing\n\
                 - Performance and security testing\n\
                 DELEGATE implementation to dev agents."
            }
            crate::identity::AgentRole::Master { .. } => {
                "REMEMBER: You are the MASTER orchestrator. Focus ONLY on:\n\
                 - Coordinating between agents\n\
                 - Quality reviews and approvals\n\
                 - Strategic decisions and planning\n\
                 - NEVER write code directly - delegate to specialists."
            }
            crate::identity::AgentRole::Search { .. } => {
                "REMEMBER: You are a SEARCH specialist. Focus ONLY on:\n\
                 - Web search and information retrieval\n\
                 - Query optimization and filtering\n\
                 - Source evaluation and ranking\n\
                 - Result presentation and summarization\n\
                 - NEVER modify code - only gather information."
            }
        }
        .to_string()
    }

    /// Execute Claude Code command with enhanced error handling
    async fn execute_claude_command(
        &self,
        args: Vec<String>,
        working_dir: &Path,
        identity: &AgentIdentity,
    ) -> Result<String> {
        let mut cmd = Command::new("claude");

        // Set working directory - always use current directory to avoid Node.js module issues
        // This is a workaround for the yoga.wasm module resolution problem
        cmd.current_dir(".");

        // Add identity environment variables
        for (key, value) in &identity.env_vars {
            cmd.env(key, value);
        }

        // Add provider environment variables
        for (key, value) in self.config.get_env_vars() {
            cmd.env(key, value);
        }

        // Add command arguments
        cmd.args(&args);

        tracing::info!(
            "Executing Claude Code with args: {:?} in dir: {}",
            args,
            working_dir.display()
        );

        // Execute command with timeout
        let start = Instant::now();
        let output = tokio::time::timeout(
            std::time::Duration::from_secs(300), // 5 minute timeout
            cmd.output(),
        )
        .await
        .context("Claude Code command timed out")?
        .context("Failed to execute Claude Code")?;

        let duration = start.elapsed();

        tracing::debug!(
            "Claude Code execution completed in {:?} for agent {}",
            duration,
            identity.agent_id
        );

        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout).to_string();
            if stdout.trim().is_empty() {
                // Sometimes Claude outputs to stderr even on success
                let stderr = String::from_utf8_lossy(&output.stderr).to_string();
                if !stderr.trim().is_empty() {
                    return Ok(stderr);
                }
            }
            Ok(stdout)
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr);
            let stdout = String::from_utf8_lossy(&output.stdout);

            Err(anyhow::anyhow!(
                "Claude Code execution failed (exit code: {:?})\nStderr: {}\nStdout: {}",
                output.status.code(),
                stderr,
                stdout
            ))
        }
    }

    /// Build Claude Code command arguments based on current CLI options
    fn build_command_args(&self, prompt: &str) -> Vec<String> {
        let mut args = Vec::new();

        // Add prompt (print mode)
        args.push("-p".to_string());
        args.push(prompt.to_string());

        // Add output format (replaces deprecated --json flag)
        args.push("--output-format".to_string());
        args.push(self.config.output_format.as_cli_arg().to_string());

        // Add dangerous skip if enabled
        if self.config.dangerous_skip {
            args.push("--dangerously-skip-permissions".to_string());
        }

        // Add model (using aliases like "sonnet", "opus", "haiku", "opusplan")
        args.push("--model".to_string());
        args.push(self.config.model.clone());

        // Add append system prompt if specified
        if let Some(system_prompt) = &self.config.append_system_prompt {
            args.push("--append-system-prompt".to_string());
            args.push(system_prompt.clone());
        }

        // Session management options
        if let Some(session_id) = &self.config.session_id {
            args.push("--session-id".to_string());
            args.push(session_id.clone());
        }

        if let Some(resume) = &self.config.resume_session {
            args.push("--resume".to_string());
            args.push(resume.clone());
        }

        if self.config.continue_session {
            args.push("--continue".to_string());
        }

        if self.config.fork_session {
            args.push("--fork-session".to_string());
        }

        // Add max turns if specified
        if let Some(max_turns) = self.config.max_turns {
            args.push("--max-turns".to_string());
            args.push(max_turns.to_string());
        }

        // Add fallback model if specified
        if let Some(fallback) = &self.config.fallback_model {
            args.push("--fallback-model".to_string());
            args.push(fallback.clone());
        }

        // Tool restrictions
        for tool in &self.config.allowed_tools {
            args.push("--allowedTools".to_string());
            args.push(tool.clone());
        }

        for tool in &self.config.disallowed_tools {
            args.push("--disallowedTools".to_string());
            args.push(tool.clone());
        }

        // Logging options
        if self.config.verbose {
            args.push("--verbose".to_string());
        }

        if self.config.mcp_debug {
            args.push("--mcp-debug".to_string());
        }

        args
    }

    /// Parse task result from Claude Code output
    fn parse_task_result(
        &self,
        output: String,
        task: &Task,
        duration: std::time::Duration,
    ) -> TaskResult {
        use crate::providers::OutputFormat;

        // Try to parse as JSON if JSON output format is used
        if self.config.output_format == OutputFormat::Json
            || self.config.output_format == OutputFormat::StreamJson
        {
            if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(&output) {
                // Extract the result field if present (new JSON format)
                let result_output = if let Some(result) = json_value.get("result") {
                    json!({
                        "response": result,
                        "task_id": task.id,
                        "format": "json",
                        "cost_usd": json_value.get("total_cost_usd"),
                        "duration_ms": json_value.get("duration_ms"),
                        "session_id": json_value.get("session_id"),
                        "num_turns": json_value.get("num_turns")
                    })
                } else {
                    json_value
                };

                return TaskResult {
                    success: true,
                    output: result_output,
                    error: None,
                    duration,
                };
            }
        }

        // Fallback to text output
        TaskResult {
            success: true,
            output: serde_json::json!({
                "response": output,
                "task_id": task.id,
                "format": "text"
            }),
            error: None,
            duration,
        }
    }
}

#[async_trait]
impl ProviderExecutor for ClaudeCodeExecutor {
    async fn execute_prompt(
        &self,
        prompt: &str,
        identity: &AgentIdentity,
        working_dir: &Path,
    ) -> Result<String> {
        let args = self.build_command_args(prompt);
        self.execute_claude_command(args, working_dir, identity)
            .await
    }

    async fn execute_task(
        &self,
        task: &Task,
        identity: &AgentIdentity,
        working_dir: &Path,
    ) -> Result<TaskResult> {
        let start = Instant::now();

        // Generate enhanced prompt with identity and boundaries
        let prompt = self.generate_task_prompt(identity, task);

        tracing::info!(
            "Executing task '{}' with Claude Code for agent {}",
            task.description,
            identity.agent_id
        );

        // Execute the prompt
        match self.execute_prompt(&prompt, identity, working_dir).await {
            Ok(output) => {
                let duration = start.elapsed();
                let result = self.parse_task_result(output, task, duration);

                tracing::info!(
                    "Task completed successfully in {:?} for agent {}",
                    duration,
                    identity.agent_id
                );

                Ok(result)
            }
            Err(e) => {
                let duration = start.elapsed();

                tracing::error!(
                    "Task failed after {:?} for agent {}: {}",
                    duration,
                    identity.agent_id,
                    e
                );

                Ok(TaskResult {
                    success: false,
                    output: serde_json::json!({}),
                    error: Some(e.to_string()),
                    duration,
                })
            }
        }
    }

    async fn health_check(&self, working_dir: &Path) -> Result<ProviderHealthStatus> {
        let start = Instant::now();

        // Try to execute a simple version check
        let result = Command::new("claude")
            .arg("--version")
            .current_dir(working_dir)
            .output()
            .await;

        let duration = start.elapsed();
        let response_time_ms = duration.as_millis() as u64;

        match result {
            Ok(output) if output.status.success() => {
                let version = String::from_utf8_lossy(&output.stdout).trim().to_string();

                Ok(ProviderHealthStatus {
                    is_healthy: true,
                    version: Some(version),
                    last_check: chrono::Utc::now(),
                    error_message: None,
                    response_time_ms: Some(response_time_ms),
                })
            }
            Ok(output) => {
                let error = String::from_utf8_lossy(&output.stderr).to_string();
                Ok(ProviderHealthStatus {
                    is_healthy: false,
                    version: None,
                    last_check: chrono::Utc::now(),
                    error_message: Some(format!("Command failed: {}", error)),
                    response_time_ms: Some(response_time_ms),
                })
            }
            Err(e) => Ok(ProviderHealthStatus {
                is_healthy: false,
                version: None,
                last_check: chrono::Utc::now(),
                error_message: Some(format!("Failed to execute: {}", e)),
                response_time_ms: Some(response_time_ms),
            }),
        }
    }

    fn get_capabilities(&self) -> ProviderCapabilities {
        ProviderCapabilities {
            supports_json_output: true,
            supports_streaming: false, // Claude Code doesn't support streaming yet
            supports_file_operations: true,
            supports_git_operations: true,
            supports_code_execution: true,
            max_context_length: Some(200_000), // Claude 3.5 Sonnet context length
            supported_languages: vec![
                "rust".to_string(),
                "python".to_string(),
                "javascript".to_string(),
                "typescript".to_string(),
                "go".to_string(),
                "java".to_string(),
                "c++".to_string(),
                "c".to_string(),
                "c#".to_string(),
                "html".to_string(),
                "css".to_string(),
                "sql".to_string(),
                "bash".to_string(),
                "yaml".to_string(),
                "json".to_string(),
                "markdown".to_string(),
            ],
        }
    }
}