vtcode-core 0.103.1

Core library for VT Code - a Rust-based terminal coding agent
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Async middleware for LLM-compatible tool execution
//!
//! Proper composition pattern with async/await support.
//! Suitable for tokio-based systems handling LLM operations.

use crate::tools::improvements_errors::ObservabilityContext;
use serde_json::{Map, Value};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Instant;

/// Type alias for the async continuation function
type AsyncContinuation<'a> =
    Box<dyn Fn(ToolRequest) -> Pin<Box<dyn Future<Output = ToolResult> + Send>> + Send + Sync + 'a>;

/// Type alias for the owned async continuation function
type AsyncContinuationOwned =
    Box<dyn Fn(ToolRequest) -> Pin<Box<dyn Future<Output = ToolResult> + Send>> + Send + Sync>;

/// Async middleware trait
#[async_trait::async_trait]
pub trait AsyncMiddleware: Send + Sync {
    /// Middleware name
    fn name(&self) -> &str;

    /// Execute middleware
    async fn execute<'a>(&'a self, request: ToolRequest, next: AsyncContinuation<'a>)
    -> ToolResult;
}

/// Tool request
#[derive(Clone, Debug)]
pub struct ToolRequest {
    pub tool_name: String,
    pub arguments: String,
    pub context: String,
}

/// Tool result
#[derive(Clone, Debug)]
pub struct ToolResult {
    pub success: bool,
    pub output: Option<String>,
    pub error: Option<String>,
    pub duration_ms: u64,
    pub from_cache: bool,
}

/// Async middleware chain executor
pub struct AsyncMiddlewareChain {
    middlewares: Vec<Arc<dyn AsyncMiddleware>>,
}

impl AsyncMiddlewareChain {
    pub fn new() -> Self {
        Self {
            middlewares: Vec::new(),
        }
    }

    pub fn with_middleware(mut self, middleware: Arc<dyn AsyncMiddleware>) -> Self {
        self.middlewares.push(middleware);
        self
    }

    /// Execute request through chain (simplified)
    pub async fn execute_simple<F>(&self, request: ToolRequest, executor: F) -> ToolResult
    where
        F: Fn(ToolRequest) -> ToolResult + Send + Sync + 'static,
    {
        if self.middlewares.is_empty() {
            return executor(request);
        }

        let executor = Arc::new(executor);
        let middlewares = self.middlewares.clone();

        fn build_chain(
            middlewares: &[Arc<dyn AsyncMiddleware>],
            executor: Arc<dyn Fn(ToolRequest) -> ToolResult + Send + Sync>,
        ) -> AsyncContinuationOwned {
            if middlewares.is_empty() {
                Box::new(move |req: ToolRequest| {
                    let result = executor(req);
                    Box::pin(async move { result })
                })
            } else {
                let current = middlewares[0].clone();
                let rest = build_chain(&middlewares[1..], executor);
                let rest = Arc::new(rest);
                Box::new(move |req: ToolRequest| {
                    let current = current.clone();
                    let rest = rest.clone();
                    Box::pin(async move {
                        let next: AsyncContinuationOwned = Box::new(move |r: ToolRequest| {
                            let rest = rest.clone();
                            Box::pin(async move { rest(r).await })
                        });
                        current.execute(req, next).await
                    })
                })
            }
        }

        let chain = build_chain(&middlewares, executor);
        chain(request).await
    }
}

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

fn normalize_context(context: &str) -> String {
    let mut normalized = Map::new();
    let parsed: Value = serde_json::from_str(context).unwrap_or_else(|_| Value::Object(Map::new()));

    if let Some(session) = parsed.get("session_id").and_then(Value::as_str)
        && !session.is_empty()
    {
        normalized.insert("session_id".into(), Value::String(session.to_string()));
    }

    if let Some(task) = parsed.get("task_id").and_then(Value::as_str)
        && !task.is_empty()
    {
        normalized.insert("task_id".into(), Value::String(task.to_string()));
    }

    if let Some(version) = parsed.get("plan_version").and_then(Value::as_u64) {
        normalized.insert("plan_version".into(), Value::Number(version.into()));
    }

    if let Some(plan) = parsed.get("plan_summary").and_then(Value::as_object) {
        let mut summary = Map::new();
        if let Some(status) = plan.get("status").and_then(Value::as_str) {
            summary.insert("status".into(), Value::String(status.to_string()));
        }
        if let Some(total) = plan.get("total_steps").and_then(Value::as_u64) {
            summary.insert("total_steps".into(), Value::Number(total.into()));
        }
        if let Some(completed) = plan.get("completed_steps").and_then(Value::as_u64) {
            summary.insert("completed_steps".into(), Value::Number(completed.into()));
        }
        if !summary.is_empty() {
            normalized.insert("plan_summary".into(), Value::Object(summary));
        }
    }

    if let Some(phase) = parsed
        .get("plan_phase")
        .and_then(|v| v.as_str())
        .filter(|p| !p.is_empty())
    {
        normalized.insert("plan_phase".into(), Value::String(phase.to_string()));
    }

    serde_json::to_string(&Value::Object(normalized)).unwrap_or_else(|_| "{}".to_string())
}

/// Async logging middleware
pub struct AsyncLoggingMiddleware {
    obs_context: Arc<ObservabilityContext>,
}

impl AsyncLoggingMiddleware {
    pub fn new(obs_context: Arc<ObservabilityContext>) -> Self {
        Self { obs_context }
    }
}

#[async_trait::async_trait]
impl AsyncMiddleware for AsyncLoggingMiddleware {
    fn name(&self) -> &str {
        "async_logging"
    }

    async fn execute<'a>(
        &'a self,
        request: ToolRequest,
        next: Box<
            dyn Fn(ToolRequest) -> Pin<Box<dyn std::future::Future<Output = ToolResult> + Send>>
                + Send
                + Sync
                + 'a,
        >,
    ) -> ToolResult {
        let tool_name = request.tool_name.clone();
        let normalized_context = normalize_context(&request.context);
        let context_json: Option<Value> = serde_json::from_str(&normalized_context).ok();
        let session_id = context_json
            .as_ref()
            .and_then(|v| v.get("session_id").and_then(|s| s.as_str()))
            .unwrap_or("");
        let task_id = context_json
            .as_ref()
            .and_then(|v| v.get("task_id").and_then(|s| s.as_str()))
            .unwrap_or("");
        let plan_summary = context_json.as_ref().and_then(|v| v.get("plan_summary"));
        let plan_status = plan_summary
            .and_then(|v| v.get("status").and_then(|s| s.as_str()))
            .unwrap_or("");
        let plan_phase = context_json
            .as_ref()
            .and_then(|v| v.get("plan_phase").and_then(|p| p.as_str()))
            .unwrap_or("");
        let plan_total_steps = plan_summary
            .and_then(|v| v.get("total_steps").and_then(|n| n.as_u64()))
            .unwrap_or(0);
        let plan_completed_steps = plan_summary
            .and_then(|v| v.get("completed_steps").and_then(|n| n.as_u64()))
            .unwrap_or(0);
        let plan_version = context_json
            .as_ref()
            .and_then(|v| v.get("plan_version").and_then(|n| n.as_u64()))
            .unwrap_or(0);

        tracing::debug!(
            tool = %tool_name,
            session_id = %session_id,
            task_id = %task_id,
            plan_version,
            plan_status = %plan_status,
            plan_phase = %plan_phase,
            plan_total_steps,
            plan_completed_steps,
            "tool execution started"
        );
        tracing::trace!(
            tool = %tool_name,
            context = %normalized_context,
            "tool execution context payload"
        );

        let start = Instant::now();
        let mut result = next(request).await;
        let duration = start.elapsed().as_millis() as u64;

        result.duration_ms = duration;

        if result.success {
            tracing::debug!(
                tool = %tool_name,
                duration_ms = duration,
                session_id = %session_id,
                task_id = %task_id,
                plan_version,
                plan_status = %plan_status,
                plan_phase = %plan_phase,
                plan_total_steps,
                plan_completed_steps,
                from_cache = result.from_cache,
                "tool execution completed"
            );
            self.obs_context.event(
                crate::tools::EventType::ToolSelected,
                "executor",
                format!("executed {} in {}ms", tool_name, duration),
                Some(1.0),
            );
        } else {
            tracing::error!(
                tool = %tool_name,
                error = ?result.error,
                session_id = %context_json
                    .as_ref()
                    .and_then(|v| v.get("session_id").and_then(|s| s.as_str()))
                    .unwrap_or(""),
                task_id = %context_json
                    .as_ref()
                    .and_then(|v| v.get("task_id").and_then(|s| s.as_str()))
                    .unwrap_or(""),
                "tool execution failed"
            );
        }

        result
    }
}

/// Async caching middleware with UnifiedCache (migrated from LruCache)
pub struct AsyncCachingMiddleware {
    cache: Arc<crate::cache::UnifiedCache<AsyncCacheKey, String>>,
    obs_context: Arc<ObservabilityContext>,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct AsyncCacheKey(String);

impl crate::cache::CacheKey for AsyncCacheKey {
    fn to_cache_key(&self) -> String {
        self.0.clone()
    }
}

impl AsyncCachingMiddleware {
    pub fn new(
        max_entries: usize,
        ttl_seconds: u64,
        obs_context: Arc<ObservabilityContext>,
    ) -> Self {
        let cache = crate::cache::UnifiedCache::new(
            max_entries,
            std::time::Duration::from_secs(ttl_seconds),
            crate::cache::EvictionPolicy::Lru,
        );

        Self {
            cache: Arc::new(cache),
            obs_context,
        }
    }

    fn cache_key(tool: &str, args: &str, context: &str) -> String {
        // Use a hashed key to avoid creating large string cache keys while still uniquely identifying args
        use std::collections::hash_map::DefaultHasher;
        use std::hash::Hasher;
        let mut hasher = DefaultHasher::new();
        hasher.write(args.as_bytes());
        let normalized = normalize_context(context);
        if !normalized.is_empty() {
            hasher.write(normalized.as_bytes());
        }
        format!("{}::{}", tool, hasher.finish())
    }
}

#[async_trait::async_trait]
impl AsyncMiddleware for AsyncCachingMiddleware {
    fn name(&self) -> &str {
        "async_caching"
    }

    async fn execute<'a>(
        &'a self,
        request: ToolRequest,
        next: Box<
            dyn Fn(ToolRequest) -> Pin<Box<dyn std::future::Future<Output = ToolResult> + Send>>
                + Send
                + Sync
                + 'a,
        >,
    ) -> ToolResult {
        let key = AsyncCacheKey(Self::cache_key(
            &request.tool_name,
            &request.arguments,
            &request.context,
        ));

        // Check cache (migrated to UnifiedCache)
        if let Some(cached) = self.cache.get_owned(&key) {
            self.obs_context.event(
                crate::tools::EventType::CacheHit,
                "cache",
                "returning cached result",
                Some(1.0),
            );

            return ToolResult {
                success: true,
                output: Some(cached),
                error: None,
                duration_ms: 0,
                from_cache: true,
            };
        }

        // Execute
        let result = next(request).await;

        // Cache successful result (migrated to UnifiedCache)
        if result.success
            && let Some(ref output) = result.output
        {
            let size = output.len() as u64;
            self.cache.insert(key, output.clone(), size);
        }

        result
    }
}

/// Async retry middleware with exponential backoff
pub struct AsyncRetryMiddleware {
    max_attempts: u32,
    initial_backoff_ms: u64,
    max_backoff_ms: u64,
    obs_context: Arc<ObservabilityContext>,
}

impl AsyncRetryMiddleware {
    pub fn new(
        max_attempts: u32,
        initial_backoff_ms: u64,
        max_backoff_ms: u64,
        obs_context: Arc<ObservabilityContext>,
    ) -> Self {
        Self {
            max_attempts,
            initial_backoff_ms,
            max_backoff_ms,
            obs_context,
        }
    }

    fn backoff_duration(&self, attempt: u32) -> std::time::Duration {
        let backoff = self.initial_backoff_ms * 2_u64.pow(attempt);
        std::time::Duration::from_millis(backoff.min(self.max_backoff_ms))
    }
}

#[async_trait::async_trait]
impl AsyncMiddleware for AsyncRetryMiddleware {
    fn name(&self) -> &str {
        "async_retry"
    }

    async fn execute<'a>(
        &'a self,
        request: ToolRequest,
        next: Box<
            dyn Fn(ToolRequest) -> Pin<Box<dyn std::future::Future<Output = ToolResult> + Send>>
                + Send
                + Sync
                + 'a,
        >,
    ) -> ToolResult {
        for attempt in 0..self.max_attempts {
            if attempt > 0 {
                let backoff = self.backoff_duration(attempt - 1);
                tracing::debug!(
                    attempt = attempt,
                    backoff_ms = backoff.as_millis(),
                    "retrying after backoff"
                );
                tokio::time::sleep(backoff).await;
            }

            let result = next(request.clone()).await;

            if result.success {
                if attempt > 0 {
                    self.obs_context.event(
                        crate::tools::EventType::FallbackSuccess,
                        "retry",
                        format!("succeeded on attempt {}", attempt + 1),
                        Some(1.0),
                    );
                }
                return result;
            }

            // Skip retry for non-retryable errors (auth failures, policy
            // violations, invalid parameters) to fail fast.
            if let Some(ref error_msg) = result.error {
                let category = vtcode_commons::classify_error_message(error_msg);
                if !category.is_retryable() {
                    tracing::debug!(
                        attempt = attempt,
                        category = ?category,
                        "non-retryable error, skipping remaining attempts"
                    );
                    return result;
                }
            }

            self.obs_context.event(
                crate::tools::EventType::FallbackAttempt,
                "retry",
                format!("attempt {} failed", attempt + 1),
                None,
            );
        }

        ToolResult {
            success: false,
            output: None,
            error: Some(format!("all {} attempts failed", self.max_attempts)),
            duration_ms: 0,
            from_cache: false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::atomic::{AtomicUsize, Ordering};

    type BoxedToolFuture = Pin<Box<dyn Future<Output = ToolResult> + Send>>;
    type BoxedExecutor = Box<dyn Fn(ToolRequest) -> BoxedToolFuture + Send + Sync>;

    fn make_executor(output: &'static str) -> BoxedExecutor {
        Box::new(move |_req: ToolRequest| {
            Box::pin(async move {
                ToolResult {
                    success: true,
                    output: Some(output.to_string()),
                    error: None,
                    duration_ms: 0,
                    from_cache: false,
                }
            })
        })
    }

    #[tokio::test]
    async fn test_async_logging_middleware() {
        let obs = Arc::new(ObservabilityContext::noop());
        let middleware = AsyncLoggingMiddleware::new(obs);

        let request = ToolRequest {
            tool_name: "test_tool".to_string(),
            arguments: "arg1".to_string(),
            context: "ctx".to_string(),
        };

        let executor = make_executor("result");

        let result = middleware.execute(request, executor).await;

        assert!(result.success);
    }

    #[tokio::test]
    async fn test_async_caching_middleware() {
        let obs = Arc::new(ObservabilityContext::noop());
        let cache = AsyncCachingMiddleware::new(10, 60, obs);

        let request = ToolRequest {
            tool_name: "cached_tool".to_string(),
            arguments: "arg1".to_string(),
            context: "ctx".to_string(),
        };

        // First call
        let executor1 = make_executor("result1");

        let result1 = cache.execute(request.clone(), executor1).await;
        assert!(!result1.from_cache);

        // Second call (should be cached)
        let executor2 = make_executor("result2");

        let result2 = cache.execute(request, executor2).await;
        assert!(result2.from_cache);
        assert_eq!(result2.output, Some("result1".to_string())); // Returns cached value
    }

    #[tokio::test]
    async fn async_retry_skips_non_retryable_errors() {
        let obs = Arc::new(ObservabilityContext::noop());
        let middleware = AsyncRetryMiddleware::new(3, 1, 2, obs);
        let attempts = Arc::new(AtomicUsize::new(0));

        let executor_attempts = attempts.clone();
        let executor: BoxedExecutor = Box::new(move |_req: ToolRequest| {
            let executor_attempts = executor_attempts.clone();
            Box::pin(async move {
                executor_attempts.fetch_add(1, Ordering::SeqCst);
                ToolResult {
                    success: false,
                    output: None,
                    error: Some("invalid api key".to_string()),
                    duration_ms: 0,
                    from_cache: false,
                }
            })
        });

        let result = middleware
            .execute(
                ToolRequest {
                    tool_name: "auth_tool".to_string(),
                    arguments: "{}".to_string(),
                    context: "{}".to_string(),
                },
                executor,
            )
            .await;

        assert!(!result.success);
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn async_retry_retries_retryable_errors_until_success() {
        let obs = Arc::new(ObservabilityContext::noop());
        let middleware = AsyncRetryMiddleware::new(3, 1, 2, obs);
        let attempts = Arc::new(AtomicUsize::new(0));

        let executor_attempts = attempts.clone();
        let executor: BoxedExecutor = Box::new(move |_req: ToolRequest| {
            let executor_attempts = executor_attempts.clone();
            Box::pin(async move {
                let attempt = executor_attempts.fetch_add(1, Ordering::SeqCst);
                if attempt < 2 {
                    ToolResult {
                        success: false,
                        output: None,
                        error: Some("429 Too Many Requests".to_string()),
                        duration_ms: 0,
                        from_cache: false,
                    }
                } else {
                    ToolResult {
                        success: true,
                        output: Some("ok".to_string()),
                        error: None,
                        duration_ms: 0,
                        from_cache: false,
                    }
                }
            })
        });

        let result = middleware
            .execute(
                ToolRequest {
                    tool_name: "rate_limited_tool".to_string(),
                    arguments: "{}".to_string(),
                    context: "{}".to_string(),
                },
                executor,
            )
            .await;

        assert!(result.success);
        assert_eq!(result.output.as_deref(), Some("ok"));
        assert_eq!(attempts.load(Ordering::SeqCst), 3);
    }
}