claude-sdk-rs 1.0.0

Rust SDK for Claude AI with CLI integration - type-safe async API for Claude Code and direct SDK usage
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Parallel command execution engine
//!
//! This module provides the ParallelExecutor for running multiple Claude commands
//! concurrently with proper resource management, output coordination, and error handling.

use crate::cli::execution::runner::CommandRunner;
use crate::cli::execution::{ExecutionContext, ExecutionResult};
use crate::cli::output::AgentId;
use crate::{cli::error::InteractiveError, cli::error::Result};
use std::collections::HashMap;
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc,
};
use tokio::sync::{mpsc, RwLock, Semaphore};
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};
use uuid::Uuid;

/// Configuration for parallel execution
#[derive(Debug, Clone)]
pub struct ParallelConfig {
    /// Maximum number of concurrent executions
    pub max_concurrent: usize,
    /// Whether to continue on error or stop all
    pub continue_on_error: bool,
    /// Timeout for the entire parallel operation
    pub total_timeout: std::time::Duration,
    /// Buffer size for output channels
    pub output_buffer_size: usize,
}

impl Default for ParallelConfig {
    fn default() -> Self {
        Self {
            max_concurrent: 4,
            continue_on_error: true,
            total_timeout: std::time::Duration::from_secs(300), // 5 minutes
            output_buffer_size: 1000,
        }
    }
}

/// Handle for a parallel execution task
#[derive(Debug, Clone)]
pub struct ExecutionHandle {
    pub id: Uuid,
    pub agent_id: AgentId,
    pub context: ExecutionContext,
}

/// Output message from a parallel execution
#[derive(Debug, Clone)]
pub struct ParallelOutput {
    pub handle: ExecutionHandle,
    pub content: String,
    pub is_final: bool,
}

/// Result of a parallel execution batch
#[derive(Debug)]
pub struct ParallelResult {
    pub results: HashMap<Uuid, Result<ExecutionResult>>,
    pub total_duration: std::time::Duration,
    pub successful_count: usize,
    pub failed_count: usize,
    pub outputs: Vec<ParallelOutput>,
}

/// Statistics for parallel execution
#[derive(Debug, Clone)]
pub struct ParallelStats {
    pub active_executions: usize,
    pub queued_executions: usize,
    pub completed_executions: usize,
    pub failed_executions: usize,
    pub total_executions: usize,
}

/// Parallel command execution engine
pub struct ParallelExecutor {
    runner: Arc<CommandRunner>,
    config: ParallelConfig,
    semaphore: Arc<Semaphore>,
    agent_counter: AtomicUsize,
    active_tasks: Arc<RwLock<HashMap<Uuid, JoinHandle<Result<ExecutionResult>>>>>,
    stats: Arc<RwLock<ParallelStats>>,
}

impl ParallelExecutor {
    /// Create a new parallel executor
    pub fn new(runner: Arc<CommandRunner>) -> Self {
        let config = ParallelConfig::default();
        let semaphore = Arc::new(Semaphore::new(config.max_concurrent));

        Self {
            runner,
            semaphore,
            config,
            agent_counter: AtomicUsize::new(0),
            active_tasks: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(RwLock::new(ParallelStats {
                active_executions: 0,
                queued_executions: 0,
                completed_executions: 0,
                failed_executions: 0,
                total_executions: 0,
            })),
        }
    }

    /// Create a new parallel executor with custom configuration
    pub fn with_config(runner: Arc<CommandRunner>, config: ParallelConfig) -> Self {
        let semaphore = Arc::new(Semaphore::new(config.max_concurrent));

        Self {
            runner,
            semaphore,
            config,
            agent_counter: AtomicUsize::new(0),
            active_tasks: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(RwLock::new(ParallelStats {
                active_executions: 0,
                queued_executions: 0,
                completed_executions: 0,
                failed_executions: 0,
                total_executions: 0,
            })),
        }
    }

    /// Execute multiple commands in parallel
    pub async fn execute_parallel(
        &self,
        contexts: Vec<ExecutionContext>,
    ) -> Result<ParallelResult> {
        if contexts.is_empty() {
            return Ok(ParallelResult {
                results: HashMap::new(),
                total_duration: std::time::Duration::default(),
                successful_count: 0,
                failed_count: 0,
                outputs: Vec::new(),
            });
        }

        let start_time = std::time::Instant::now();
        let (output_tx, mut output_rx) = mpsc::channel(self.config.output_buffer_size);

        info!("Starting parallel execution of {} commands", contexts.len());

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.total_executions += contexts.len();
            stats.queued_executions = contexts.len();
        }

        // Start all tasks
        let mut handles = HashMap::new();
        for context in contexts {
            let handle = self
                .spawn_execution_task(context, output_tx.clone())
                .await?;
            handles.insert(handle.id, handle);
        }

        // Drop the sender to signal no more outputs
        drop(output_tx);

        // Collect all outputs
        let mut outputs = Vec::new();
        while let Some(output) = output_rx.recv().await {
            outputs.push(output);
        }

        // Wait for all tasks to complete
        let results = self.collect_results(handles).await?;
        let total_duration = start_time.elapsed();

        let successful_count = results.values().filter(|r| r.is_ok()).count();
        let failed_count = results.len() - successful_count;

        // Update final stats
        {
            let mut stats = self.stats.write().await;
            stats.completed_executions += successful_count;
            stats.failed_executions += failed_count;
            stats.queued_executions = 0;
            stats.active_executions = 0;
        }

        info!(
            "Parallel execution completed: {} successful, {} failed, duration: {:?}",
            successful_count, failed_count, total_duration
        );

        Ok(ParallelResult {
            results,
            total_duration,
            successful_count,
            failed_count,
            outputs,
        })
    }

    /// Execute commands with streaming output
    pub async fn execute_parallel_streaming<F>(
        &self,
        contexts: Vec<ExecutionContext>,
        mut output_handler: F,
    ) -> Result<ParallelResult>
    where
        F: FnMut(ParallelOutput) -> Result<()> + Send + 'static,
    {
        if contexts.is_empty() {
            return Ok(ParallelResult {
                results: HashMap::new(),
                total_duration: std::time::Duration::default(),
                successful_count: 0,
                failed_count: 0,
                outputs: Vec::new(),
            });
        }

        let start_time = std::time::Instant::now();
        let (output_tx, mut output_rx) = mpsc::channel(self.config.output_buffer_size);

        info!(
            "Starting parallel streaming execution of {} commands",
            contexts.len()
        );

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.total_executions += contexts.len();
            stats.queued_executions = contexts.len();
        }

        // Start all tasks with streaming
        let mut handles = HashMap::new();
        for context in contexts {
            let handle = self
                .spawn_streaming_task(context, output_tx.clone())
                .await?;
            handles.insert(handle.id, handle);
        }

        // Drop the sender to signal no more outputs
        drop(output_tx);

        // Handle streaming outputs
        let mut outputs = Vec::new();
        tokio::spawn(async move {
            while let Some(output) = output_rx.recv().await {
                if let Err(e) = output_handler(output.clone()) {
                    error!("Output handler error: {}", e);
                }
                outputs.push(output);
            }
        });

        // Wait for all tasks to complete
        let results = self.collect_results(handles).await?;
        let total_duration = start_time.elapsed();

        let successful_count = results.values().filter(|r| r.is_ok()).count();
        let failed_count = results.len() - successful_count;

        // Update final stats
        {
            let mut stats = self.stats.write().await;
            stats.completed_executions += successful_count;
            stats.failed_executions += failed_count;
            stats.queued_executions = 0;
            stats.active_executions = 0;
        }

        info!(
            "Parallel streaming execution completed: {} successful, {} failed, duration: {:?}",
            successful_count, failed_count, total_duration
        );

        Ok(ParallelResult {
            results,
            total_duration,
            successful_count,
            failed_count,
            outputs: Vec::new(), // Outputs handled by callback
        })
    }

    /// Spawn a single execution task
    async fn spawn_execution_task(
        &self,
        context: ExecutionContext,
        output_tx: mpsc::Sender<ParallelOutput>,
    ) -> Result<ExecutionHandle> {
        let id = Uuid::new_v4();
        let agent_id = AgentId(self.agent_counter.fetch_add(1, Ordering::SeqCst));

        let handle = ExecutionHandle {
            id,
            agent_id,
            context: context.clone(),
        };

        let runner: Arc<CommandRunner> = Arc::clone(&self.runner);
        let semaphore = Arc::clone(&self.semaphore);
        let stats = Arc::clone(&self.stats);
        let handle_clone = handle.clone();
        let continue_on_error = self.config.continue_on_error;

        let task = tokio::spawn(async move {
            // Acquire semaphore permit
            let _permit = semaphore.acquire().await.map_err(|e| {
                InteractiveError::execution(format!("Failed to acquire semaphore: {}", e))
            })?;

            // Update stats
            {
                let mut stats = stats.write().await;
                stats.active_executions += 1;
                stats.queued_executions = stats.queued_executions.saturating_sub(1);
            }

            debug!(
                "Starting execution for agent {} with command: {}",
                handle_clone.agent_id.0, handle_clone.context.command_name
            );

            let result = runner.execute(context).await;

            // Send final output
            let output = match &result {
                Ok(exec_result) => ParallelOutput {
                    handle: handle_clone.clone(),
                    content: exec_result.output.clone(),
                    is_final: true,
                },
                Err(e) => ParallelOutput {
                    handle: handle_clone.clone(),
                    content: format!("Error: {}", e),
                    is_final: true,
                },
            };

            if let Err(e) = output_tx.send(output).await {
                warn!("Failed to send output: {}", e);
            }

            // Update stats
            {
                let mut stats = stats.write().await;
                stats.active_executions = stats.active_executions.saturating_sub(1);
            }

            if result.is_err() && !continue_on_error {
                error!("Task failed and continue_on_error is false, stopping");
            }

            result
        });

        // Store the task handle
        {
            let mut active_tasks = self.active_tasks.write().await;
            active_tasks.insert(id, task);
        }

        Ok(handle)
    }

    /// Spawn a streaming execution task
    async fn spawn_streaming_task(
        &self,
        context: ExecutionContext,
        output_tx: mpsc::Sender<ParallelOutput>,
    ) -> Result<ExecutionHandle> {
        let id = Uuid::new_v4();
        let agent_id = AgentId(self.agent_counter.fetch_add(1, Ordering::SeqCst));

        let handle = ExecutionHandle {
            id,
            agent_id,
            context: context.clone(),
        };

        let runner: Arc<CommandRunner> = Arc::clone(&self.runner);
        let semaphore = Arc::clone(&self.semaphore);
        let stats = Arc::clone(&self.stats);
        let handle_clone = handle.clone();

        let task = tokio::spawn(async move {
            // Acquire semaphore permit
            let _permit = semaphore.acquire().await.map_err(|e| {
                InteractiveError::execution(format!("Failed to acquire semaphore: {}", e))
            })?;

            // Update stats
            {
                let mut stats = stats.write().await;
                stats.active_executions += 1;
                stats.queued_executions = stats.queued_executions.saturating_sub(1);
            }

            debug!(
                "Starting streaming execution for agent {} with command: {}",
                handle_clone.agent_id.0, handle_clone.context.command_name
            );

            let _output_tx_clone = output_tx.clone();
            let _handle_clone2 = handle_clone.clone();

            // For now, we'll use regular execution since streaming execution method needs to be fixed
            let result = runner.execute(context).await;

            // Send final completion message
            let final_output = ParallelOutput {
                handle: handle_clone.clone(),
                content: match &result {
                    Ok(_) => "Command completed".to_string(),
                    Err(e) => format!("Error: {}", e),
                },
                is_final: true,
            };

            if let Err(e) = output_tx.send(final_output).await {
                warn!("Failed to send final output: {}", e);
            }

            // Update stats
            {
                let mut stats = stats.write().await;
                stats.active_executions = stats.active_executions.saturating_sub(1);
            }

            result
        });

        // Store the task handle
        {
            let mut active_tasks = self.active_tasks.write().await;
            active_tasks.insert(id, task);
        }

        Ok(handle)
    }

    /// Collect results from all tasks
    async fn collect_results(
        &self,
        _handles: HashMap<Uuid, ExecutionHandle>,
    ) -> Result<HashMap<Uuid, Result<ExecutionResult>>> {
        let mut results = HashMap::new();

        // Wait for all tasks to complete
        let active_tasks = {
            let mut tasks = self.active_tasks.write().await;
            std::mem::take(&mut *tasks)
        };

        for (id, task) in active_tasks {
            let result = match tokio::time::timeout(self.config.total_timeout, task).await {
                Ok(task_result) => match task_result {
                    Ok(execution_result) => execution_result,
                    Err(e) => Err(InteractiveError::execution(format!(
                        "Task join error: {}",
                        e
                    ))),
                },
                Err(_) => Err(InteractiveError::Timeout(
                    self.config.total_timeout.as_secs(),
                )),
            };

            results.insert(id, result);
        }

        Ok(results)
    }

    /// Cancel all active executions
    pub async fn cancel_all(&self) -> Result<usize> {
        let mut active_tasks = self.active_tasks.write().await;
        let count = active_tasks.len();

        for (_, task) in active_tasks.drain() {
            task.abort();
        }

        // Reset stats
        {
            let mut stats = self.stats.write().await;
            stats.active_executions = 0;
            stats.queued_executions = 0;
        }

        info!("Cancelled {} active executions", count);
        Ok(count)
    }

    /// Get current execution statistics
    pub async fn get_stats(&self) -> ParallelStats {
        self.stats.read().await.clone()
    }

    /// Get current configuration
    pub fn config(&self) -> &ParallelConfig {
        &self.config
    }

    /// Update configuration
    pub fn set_config(&mut self, config: ParallelConfig) {
        // Update semaphore if max_concurrent changed
        if config.max_concurrent != self.config.max_concurrent {
            self.semaphore = Arc::new(Semaphore::new(config.max_concurrent));
        }

        self.config = config;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::session::{
        manager::SessionManager,
        storage::{JsonFileStorage, StorageConfig},
    };
    use tempfile::tempdir;

    async fn create_test_executor() -> Result<ParallelExecutor> {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            data_dir: temp_dir.path().to_path_buf(),
            sessions_file: "test_sessions.json".to_string(),
            current_session_file: "test_current.json".to_string(),
        };

        let storage = Arc::new(JsonFileStorage::new(config));
        let session_manager = Arc::new(tokio::sync::RwLock::new(SessionManager::new(storage)));
        let runner = Arc::new(CommandRunner::new(session_manager)?);

        Ok(ParallelExecutor::new(runner))
    }

    #[tokio::test]
    async fn test_parallel_executor_creation() -> Result<()> {
        let executor = create_test_executor().await?;
        let stats = executor.get_stats().await;

        assert_eq!(stats.active_executions, 0);
        assert_eq!(stats.total_executions, 0);
        assert_eq!(executor.config.max_concurrent, 4);

        Ok(())
    }

    #[tokio::test]
    async fn test_empty_execution() -> Result<()> {
        let executor = create_test_executor().await?;
        let result = executor.execute_parallel(vec![]).await?;

        assert_eq!(result.successful_count, 0);
        assert_eq!(result.failed_count, 0);
        assert!(result.results.is_empty());

        Ok(())
    }

    #[tokio::test]
    async fn test_stats_tracking() -> Result<()> {
        let executor = create_test_executor().await?;
        let initial_stats = executor.get_stats().await;

        assert_eq!(initial_stats.total_executions, 0);

        // Stats would be updated during actual execution
        // This test verifies the stats structure is working

        Ok(())
    }

    #[tokio::test]
    async fn test_config_update() -> Result<()> {
        let mut executor = create_test_executor().await?;

        let new_config = ParallelConfig {
            max_concurrent: 8,
            continue_on_error: false,
            total_timeout: std::time::Duration::from_secs(600),
            output_buffer_size: 2000,
        };

        executor.set_config(new_config.clone());
        assert_eq!(executor.config.max_concurrent, 8);
        assert!(!executor.config.continue_on_error);

        Ok(())
    }
}