vtcode-core 0.19.1

Core library for VTCode - a Rust-based terminal coding agent
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
use crate::core::timeout_detector::{OperationType, TIMEOUT_DETECTOR};
use anyhow::Result;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::time::{SystemTime, UNIX_EPOCH};

/// Represents an error that occurred during execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionError {
    pub id: String,
    pub timestamp: u64,
    pub error_type: ErrorType,
    pub message: String,
    pub context: ErrorContext,
    pub recovery_attempts: Vec<RecoveryAttempt>,
    pub resolved: bool,
}

/// Type of error that can occur
#[derive(Debug, Clone, Serialize, Deserialize, Eq, Hash, PartialEq)]
pub enum ErrorType {
    ToolExecution,
    ApiCall,
    ContextCompression,
    FileSystem,
    Network,
    Validation,
    Unknown,
}

/// Context information about where and why the error occurred
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorContext {
    pub conversation_turn: usize,
    pub user_input: Option<String>,
    pub tool_name: Option<String>,
    pub tool_args: Option<Value>,
    pub api_request_size: Option<usize>,
    pub context_size: Option<usize>,
    pub stack_trace: Option<String>,
}

/// A recovery attempt that was made
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RecoveryAttempt {
    pub timestamp: u64,
    pub strategy: RecoveryStrategy,
    pub success: bool,
    pub result: String,
    pub new_context_size: Option<usize>,
}

/// Recovery strategy used to handle the error
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RecoveryStrategy {
    RetryWithBackoff {
        delay_ms: u64,
        attempt_number: usize,
    },
    ContextCompression {
        compression_ratio: f64,
    },
    SimplifyRequest {
        removed_parameters: Vec<String>,
    },
    AlternativeTool {
        original_tool: String,
        alternative_tool: String,
    },
    ContextReset {
        preserved_data: IndexMap<String, Value>,
    },
    ManualIntervention,
}

/// Error recovery manager
pub struct ErrorRecoveryManager {
    errors: Vec<ExecutionError>,
    recovery_strategies: IndexMap<ErrorType, Vec<RecoveryStrategy>>,
    context_compression_threshold: usize,
    operation_type_mapping: IndexMap<ErrorType, OperationType>,
}

impl ErrorRecoveryManager {
    pub fn new() -> Self {
        let mut recovery_strategies = IndexMap::new();
        let mut operation_type_mapping = IndexMap::new();

        // Define recovery strategies for different error types
        recovery_strategies.insert(
            ErrorType::ToolExecution,
            vec![
                RecoveryStrategy::RetryWithBackoff {
                    delay_ms: 1000,
                    attempt_number: 1,
                },
                RecoveryStrategy::AlternativeTool {
                    original_tool: "".to_string(),
                    alternative_tool: "".to_string(),
                },
                RecoveryStrategy::ContextCompression {
                    compression_ratio: 0.5,
                },
            ],
        );

        recovery_strategies.insert(
            ErrorType::ApiCall,
            vec![
                RecoveryStrategy::RetryWithBackoff {
                    delay_ms: 2000,
                    attempt_number: 1,
                },
                RecoveryStrategy::ContextCompression {
                    compression_ratio: 0.7,
                },
                RecoveryStrategy::ContextReset {
                    preserved_data: IndexMap::new(),
                },
            ],
        );

        recovery_strategies.insert(
            ErrorType::ContextCompression,
            vec![RecoveryStrategy::ContextReset {
                preserved_data: IndexMap::new(),
            }],
        );

        // Map error types to operation types for timeout detector integration
        operation_type_mapping.insert(ErrorType::ToolExecution, OperationType::ToolExecution);
        operation_type_mapping.insert(ErrorType::ApiCall, OperationType::ApiCall);
        operation_type_mapping.insert(ErrorType::Network, OperationType::NetworkRequest);
        operation_type_mapping.insert(ErrorType::FileSystem, OperationType::FileOperation);
        operation_type_mapping.insert(ErrorType::Validation, OperationType::Processing);
        operation_type_mapping.insert(ErrorType::Unknown, OperationType::Processing);

        Self {
            errors: Vec::new(),
            recovery_strategies,
            context_compression_threshold: 50000, // tokens
            operation_type_mapping,
        }
    }

    /// Record a new error
    pub fn record_error(
        &mut self,
        error_type: ErrorType,
        message: String,
        context: ErrorContext,
    ) -> String {
        let error_id = format!(
            "error_{}_{}",
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            self.errors.len()
        );

        let error = ExecutionError {
            id: error_id.clone(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            error_type: error_type.clone(),
            message,
            context,
            recovery_attempts: Vec::new(),
            resolved: false,
        };

        self.errors.push(error);
        error_id
    }

    /// Record a recovery attempt
    pub fn record_recovery_attempt(
        &mut self,
        error_id: &str,
        strategy: RecoveryStrategy,
        success: bool,
        result: String,
        new_context_size: Option<usize>,
    ) {
        let attempt = RecoveryAttempt {
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            strategy,
            success,
            result,
            new_context_size,
        };

        if let Some(error) = self.errors.iter_mut().find(|e| e.id == error_id) {
            error.recovery_attempts.push(attempt);
            if success {
                error.resolved = true;
            }
        }
    }

    /// Get recovery strategies for a specific error type
    pub fn get_recovery_strategies(&self, error_type: &ErrorType) -> &[RecoveryStrategy] {
        self.recovery_strategies
            .get(error_type)
            .map(|strategies| strategies.as_slice())
            .unwrap_or(&[])
    }

    /// Determine if context compression is needed based on current context size
    pub fn should_compress_context(&self, context_size: usize) -> bool {
        context_size > self.context_compression_threshold
    }

    /// Generate a context preservation plan
    pub fn generate_context_preservation_plan(
        &self,
        context_size: usize,
        error_count: usize,
    ) -> ContextPreservationPlan {
        let compression_needed = context_size > self.context_compression_threshold;
        let critical_errors = error_count > 5;

        let strategies = if critical_errors {
            vec![
                PreservationStrategy::ImmediateCompression { target_ratio: 0.5 },
                PreservationStrategy::SelectiveRetention {
                    preserve_decisions: true,
                    preserve_errors: true,
                },
                PreservationStrategy::ContextReset {
                    preserve_session_data: true,
                },
            ]
        } else if compression_needed {
            vec![
                PreservationStrategy::GradualCompression { target_ratio: 0.7 },
                PreservationStrategy::SelectiveRetention {
                    preserve_decisions: true,
                    preserve_errors: false,
                },
            ]
        } else {
            vec![PreservationStrategy::NoAction]
        };

        ContextPreservationPlan {
            current_context_size: context_size,
            error_count,
            recommended_strategies: strategies,
            urgency: if critical_errors {
                Urgency::Critical
            } else if compression_needed {
                Urgency::High
            } else {
                Urgency::Low
            },
        }
    }

    /// Get error statistics
    pub fn get_error_statistics(&self) -> ErrorStatistics {
        let total_errors = self.errors.len();
        let resolved_errors = self.errors.iter().filter(|e| e.resolved).count();
        let unresolved_errors = total_errors - resolved_errors;

        let errors_by_type = self.errors.iter().fold(IndexMap::new(), |mut acc, error| {
            *acc.entry(error.error_type.clone()).or_insert(0) += 1;
            acc
        });

        let avg_recovery_attempts = if total_errors > 0 {
            self.errors
                .iter()
                .map(|e| e.recovery_attempts.len())
                .sum::<usize>() as f64
                / total_errors as f64
        } else {
            0.0
        };

        ErrorStatistics {
            total_errors,
            resolved_errors,
            unresolved_errors,
            errors_by_type,
            avg_recovery_attempts,
            recent_errors: self.errors.iter().rev().take(5).cloned().collect(),
        }
    }

    /// Check if a specific error pattern is recurring
    pub fn detect_error_pattern(&self, error_type: &ErrorType, time_window_seconds: u64) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let recent_errors = self
            .errors
            .iter()
            .filter(|e| e.error_type == *error_type && (now - e.timestamp) < time_window_seconds)
            .count();

        recent_errors >= 3 // Consider it a pattern if 3+ similar errors in time window
    }

    /// Get the corresponding operation type for an error type
    pub fn get_operation_type(&self, error_type: &ErrorType) -> OperationType {
        self.operation_type_mapping
            .get(error_type)
            .cloned()
            .unwrap_or(OperationType::Processing)
    }

    /// Execute an operation with intelligent timeout detection and recovery
    pub async fn execute_with_recovery<F, Fut, T>(
        &mut self,
        operation_id: String,
        error_type: ErrorType,
        _context: ErrorContext,
        operation: F,
    ) -> Result<T, anyhow::Error>
    where
        F: Fn() -> Fut,
        Fut: std::future::Future<Output = Result<T, anyhow::Error>>,
    {
        let operation_type = self.get_operation_type(&error_type);

        TIMEOUT_DETECTOR
            .execute_with_timeout_retry(operation_id, operation_type, operation)
            .await
    }

    /// Check if an operation should be retried based on error analysis
    pub async fn should_retry_operation(
        &self,
        error_type: &ErrorType,
        error: &anyhow::Error,
        attempt: u32,
    ) -> bool {
        let operation_type = self.get_operation_type(error_type);
        TIMEOUT_DETECTOR
            .should_retry(&operation_type, error, attempt)
            .await
    }

    /// Get timeout statistics for monitoring and optimization
    pub async fn get_timeout_stats(&self) -> crate::core::timeout_detector::TimeoutStats {
        TIMEOUT_DETECTOR.get_stats().await
    }

    /// Configure timeout settings for a specific error type
    pub async fn configure_timeout_for_error_type(
        &self,
        error_type: ErrorType,
        config: crate::core::timeout_detector::TimeoutConfig,
    ) {
        let operation_type = self.get_operation_type(&error_type);
        TIMEOUT_DETECTOR.set_config(operation_type, config).await;
    }

    /// Generate enhanced recovery plan based on timeout detector insights
    pub async fn generate_enhanced_recovery_plan(
        &self,
        context_size: usize,
        error_count: usize,
    ) -> EnhancedContextPreservationPlan {
        let timeout_stats = self.get_timeout_stats().await;
        let base_plan = self.generate_context_preservation_plan(context_size, error_count);

        // Enhance the plan based on timeout detector insights
        let timeout_rate = if timeout_stats.total_operations > 0 {
            timeout_stats.timed_out_operations as f64 / timeout_stats.total_operations as f64
        } else {
            0.0
        };

        let retry_success_rate = if timeout_stats.total_retry_attempts > 0 {
            timeout_stats.successful_retries as f64 / timeout_stats.total_retry_attempts as f64
        } else {
            1.0
        };

        // Adjust urgency based on timeout patterns
        let _adjusted_urgency = if timeout_rate > 0.3 {
            // High timeout rate indicates systemic issues
            Urgency::Critical
        } else if retry_success_rate < 0.5 {
            // Low retry success rate indicates recovery issues
            Urgency::High
        } else {
            base_plan.urgency.clone()
        };

        EnhancedContextPreservationPlan {
            base_plan,
            timeout_rate,
            retry_success_rate,
            timeout_stats,
        }
    }

    /// Get the number of errors (for context preservation plan)
    pub fn error_count(&self) -> usize {
        self.errors.len()
    }
}

/// Plan for preserving context during error recovery
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextPreservationPlan {
    pub current_context_size: usize,
    pub error_count: usize,
    pub recommended_strategies: Vec<PreservationStrategy>,
    pub urgency: Urgency,
}

/// Strategy for preserving context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PreservationStrategy {
    ImmediateCompression {
        target_ratio: f64,
    },
    GradualCompression {
        target_ratio: f64,
    },
    SelectiveRetention {
        preserve_decisions: bool,
        preserve_errors: bool,
    },
    ContextReset {
        preserve_session_data: bool,
    },
    NoAction,
}

/// Urgency level for context preservation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Urgency {
    Low,
    High,
    Critical,
}

/// Statistics about errors in the session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorStatistics {
    pub total_errors: usize,
    pub resolved_errors: usize,
    pub unresolved_errors: usize,
    pub errors_by_type: IndexMap<ErrorType, usize>,
    pub avg_recovery_attempts: f64,
    pub recent_errors: Vec<ExecutionError>,
}

/// Enhanced context preservation plan with timeout detector insights
#[derive(Debug, Clone)]
pub struct EnhancedContextPreservationPlan {
    pub base_plan: ContextPreservationPlan,
    pub timeout_rate: f64,
    pub retry_success_rate: f64,
    pub timeout_stats: crate::core::timeout_detector::TimeoutStats,
}

impl Default for ErrorRecoveryManager {
    fn default() -> Self {
        Self::new()
    }
}