claude_code_sdk/
config.rs

1//! Configuration and safety limits for Claude SDK
2
3/// Safety limits for text processing
4#[derive(Debug, Clone)]
5pub struct SafetyLimits {
6    /// Maximum size for a single line from CLI output (bytes)
7    pub max_line_size: usize,
8    
9    /// Maximum size for a single text content block (bytes)
10    pub max_text_block_size: usize,
11    
12    /// Maximum total memory usage for buffered messages (bytes)
13    pub max_buffer_size: usize,
14    
15    /// Maximum length for log previews (characters)
16    pub max_log_preview_chars: usize,
17    
18    /// Maximum number of messages to buffer before applying backpressure
19    pub max_buffered_messages: usize,
20    
21    /// Timeout for JSON parsing operations (milliseconds)
22    pub json_parse_timeout_ms: u64,
23}
24
25impl Default for SafetyLimits {
26    fn default() -> Self {
27        Self {
28            // 10MB per line - should handle most reasonable responses
29            max_line_size: 10 * 1024 * 1024,
30            
31            // 5MB per text block - reasonable for code generation
32            max_text_block_size: 5 * 1024 * 1024,
33            
34            // 50MB total buffer - prevents runaway memory usage
35            max_buffer_size: 50 * 1024 * 1024,
36            
37            // 200 chars for log previews - enough for context
38            max_log_preview_chars: 200,
39            
40            // 100 messages max - prevents queue buildup
41            max_buffered_messages: 100,
42            
43            // 5 second timeout for JSON parsing
44            json_parse_timeout_ms: 5000,
45        }
46    }
47}
48
49impl SafetyLimits {
50    /// Create new safety limits with custom values
51    pub fn new() -> Self {
52        Self::default()
53    }
54    
55    /// Create conservative limits for memory-constrained environments
56    pub fn conservative() -> Self {
57        Self {
58            max_line_size: 1024 * 1024,        // 1MB
59            max_text_block_size: 512 * 1024,   // 512KB
60            max_buffer_size: 10 * 1024 * 1024, // 10MB
61            max_log_preview_chars: 100,
62            max_buffered_messages: 50,
63            json_parse_timeout_ms: 2000,
64        }
65    }
66    
67    /// Create generous limits for high-memory environments
68    pub fn generous() -> Self {
69        Self {
70            max_line_size: 50 * 1024 * 1024,   // 50MB
71            max_text_block_size: 25 * 1024 * 1024, // 25MB
72            max_buffer_size: 200 * 1024 * 1024, // 200MB
73            max_log_preview_chars: 500,
74            max_buffered_messages: 200,
75            json_parse_timeout_ms: 10000,
76        }
77    }
78    
79    /// Check if a line size is within limits
80    pub fn is_line_size_safe(&self, size: usize) -> bool {
81        size <= self.max_line_size
82    }
83    
84    /// Check if a text block size is within limits
85    pub fn is_text_block_safe(&self, size: usize) -> bool {
86        size <= self.max_text_block_size
87    }
88    
89    /// Get safe log preview for a string
90    pub fn safe_log_preview(&self, text: &str) -> String {
91        if text.len() <= self.max_log_preview_chars {
92            text.to_string()
93        } else {
94            format!("{}... ({} total chars)", 
95                text.chars().take(self.max_log_preview_chars).collect::<String>(),
96                text.len())
97        }
98    }
99}
100
101/// Errors related to safety limit violations
102#[derive(Debug, thiserror::Error)]
103pub enum SafetyError {
104    #[error("Line size {actual} bytes exceeds limit of {limit} bytes")]
105    LineTooLarge { actual: usize, limit: usize },
106    
107    #[error("Text block size {actual} bytes exceeds limit of {limit} bytes")]
108    TextBlockTooLarge { actual: usize, limit: usize },
109    
110    #[error("Buffer size {actual} bytes exceeds limit of {limit} bytes")]
111    BufferTooLarge { actual: usize, limit: usize },
112    
113    #[error("Too many buffered messages: {actual}, limit: {limit}")]
114    TooManyMessages { actual: usize, limit: usize },
115    
116    #[error("JSON parsing timeout after {timeout_ms}ms")]
117    ParseTimeout { timeout_ms: u64 },
118}