vtcode 0.98.7

A Rust-based terminal coding agent with modular architecture supporting multiple LLM providers
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
//! End-to-end tests for file operations with quick-cache integration

use assert_fs::TempDir;
use serde_json::{Value, json};
use std::fs;
use vtcode_core::tools::cache::FileCache;
use vtcode_core::tools::registry::ToolRegistry;

#[cfg(test)]
mod e2e_tests {
    use super::*;
    use std::time::Duration;
    use tokio::time::sleep;

    /// Test end-to-end file operations with caching
    #[tokio::test]
    async fn test_file_operations_with_cache() {
        // Create temporary directory for testing
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");

        // Create test file
        let test_file = workspace_root.join("test_file.txt");
        let test_content = "This is test content for caching verification.";
        fs::write(&test_file, test_content).expect("Failed to write test file");

        // Initialize tool registry
        let registry = ToolRegistry::new(workspace_root.clone()).await;

        // Test 1: First read should cache the file
        let read_args = json!({
            "path": test_file.to_string_lossy(),
            "max_bytes": 1000
        });

        let result1 = registry.execute_tool_ref("read_file", &read_args).await;
        assert!(result1.is_ok(), "First read should succeed");

        let content1 = result1.unwrap();
        assert_eq!(content1["content"], test_content);

        // Test 2: Second read should use cache (verify by checking cache stats)
        let result2 = registry.execute_tool("read_file", read_args).await;
        assert!(result2.is_ok(), "Second read should succeed");

        let content2 = result2.unwrap();
        assert_eq!(content2["content"], test_content);

        // Test 3: Write operation should invalidate/update cache
        let new_content = "Updated content after caching.";
        let write_args = json!({
            "path": test_file.to_string_lossy(),
            "content": new_content,
            "mode": "overwrite"
        });

        let write_result = registry.execute_tool("write_file", write_args).await;
        assert!(write_result.is_ok(), "Write operation should succeed");

        // Test 4: Read after write should get updated content
        let read_args2 = json!({
            "path": test_file.to_string_lossy(),
            "max_bytes": 1000
        });

        let result3 = registry.execute_tool("read_file", read_args2).await;
        assert!(result3.is_ok(), "Read after write should succeed");

        let content3 = result3.unwrap();
        assert_eq!(content3["content"], new_content);

        // Verify file on disk matches
        let disk_content = fs::read_to_string(&test_file).expect("Failed to read from disk");
        assert_eq!(disk_content, new_content);
    }

    /// Test cache statistics tracking
    #[tokio::test]
    async fn test_cache_statistics_tracking() {
        let cache = FileCache::new(100);

        // Initially should have no hits/misses
        let initial_stats = cache.stats().await;
        assert_eq!(initial_stats.hits, 0);
        assert_eq!(initial_stats.misses, 0);

        // Cache miss
        let miss_result = cache.get_file("nonexistent").await;
        assert!(miss_result.is_none());

        let after_miss_stats = cache.stats().await;
        assert_eq!(after_miss_stats.hits, 0);
        assert_eq!(after_miss_stats.misses, 1);

        // Cache hit
        let test_data = Value::String("cached content".to_string());
        cache
            .put_file("test_key".to_string(), test_data.clone())
            .await;

        let hit_result = cache.get_file("test_key").await;
        assert!(hit_result.is_some());
        assert_eq!(hit_result.unwrap(), test_data);

        let after_hit_stats = cache.stats().await;
        assert_eq!(after_hit_stats.hits, 1);
        assert_eq!(after_hit_stats.misses, 1);
    }

    /// Test cache capacity limits
    #[tokio::test]
    async fn test_cache_capacity_limits() {
        let cache = FileCache::new(2); // Very small capacity for testing

        // Fill cache
        cache
            .put_file("key1".to_string(), Value::String("data1".to_string()))
            .await;
        cache
            .put_file("key2".to_string(), Value::String("data2".to_string()))
            .await;
        cache
            .put_file("key3".to_string(), Value::String("data3".to_string()))
            .await; // Should evict

        // Check capacity
        let (file_capacity, _dir_capacity) = cache.capacity();
        assert_eq!(file_capacity, 2);

        // Check current size
        let (file_len, _dir_len) = cache.len();
        assert!(file_len <= 2); // Should not exceed capacity
    }

    /// Test directory caching
    #[tokio::test]
    async fn test_directory_caching() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");

        // Create test directory structure
        let subdir = workspace_root.join("subdir");
        fs::create_dir(&subdir).expect("Failed to create subdir");
        fs::write(subdir.join("file1.txt"), "content1").expect("Failed to write file1");
        fs::write(subdir.join("file2.txt"), "content2").expect("Failed to write file2");

        let registry = ToolRegistry::new(workspace_root.clone()).await;

        // List directory (should cache result)
        let list_args = json!({
            "action": "list",
            "path": "subdir"
        });

        let result1 = registry
            .execute_tool_ref("unified_search", &list_args)
            .await;
        assert!(result1.is_ok(), "First list should succeed");

        // Second list should use cache
        let result2 = registry.execute_tool("unified_search", list_args).await;
        assert!(result2.is_ok(), "Second list should succeed");

        // Results should be identical
        assert_eq!(result1.unwrap(), result2.unwrap());
    }

    /// Test cache invalidation on file modification
    #[tokio::test]
    async fn test_cache_invalidation() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");

        let test_file = workspace_root.join("test.txt");
        let registry = ToolRegistry::new(workspace_root.clone()).await;

        // Create and read file
        fs::write(&test_file, "original").expect("Failed to write original content");

        let read_args = json!({
            "path": test_file.to_string_lossy()
        });

        let result1 = registry.execute_tool_ref("read_file", &read_args).await;
        assert!(result1.is_ok());
        assert_eq!(result1.unwrap()["content"], "original");

        // Modify file via tool
        let edit_args = json!({
            "path": test_file.to_string_lossy(),
            "old_str": "original",
            "new_str": "modified"
        });

        let edit_result = registry.execute_tool("edit_file", edit_args).await;
        assert!(edit_result.is_ok(), "Edit should succeed");

        // Read again - should get updated content (cache should be invalidated)
        let result2 = registry.execute_tool("read_file", read_args).await;
        assert!(result2.is_ok());
        assert_eq!(result2.unwrap()["content"], "modified");
    }

    #[tokio::test]
    async fn test_write_file_overwrite_mode() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");
        let registry = ToolRegistry::new(workspace_root.clone()).await;
        let test_file = workspace_root.join("test.txt");

        // Write initial content
        let args = json!({
            "path": "test.txt",
            "content": "Hello World",
            "mode": "overwrite"
        });

        let result = registry
            .execute_tool("write_file", args)
            .await
            .expect("write_file should succeed");
        assert_eq!(result["success"], true);

        // Verify file content
        let mut attempts = 0;
        let final_content = loop {
            let content = tokio::fs::read_to_string(&test_file)
                .await
                .expect("File should exist");
            if content == "Hello World" || attempts >= 25 {
                break content;
            }
            sleep(Duration::from_millis(20)).await;
            attempts += 1;
        };
        assert_eq!(final_content, "Hello World");
    }

    #[tokio::test]
    async fn test_write_file_append_mode() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");
        let registry = ToolRegistry::new(workspace_root.clone()).await;
        let test_file = workspace_root.join("test.txt");

        // Write initial content
        registry
            .execute_tool(
                "write_file",
                json!({
                    "path": "test.txt",
                    "content": "Hello",
                    "mode": "overwrite"
                }),
            )
            .await
            .expect("Initial write should succeed");

        // Append content
        let args = json!({
            "path": "test.txt",
            "content": " World",
            "mode": "append"
        });

        let result = registry
            .execute_tool("write_file", args)
            .await
            .expect("append write_file should succeed");
        assert_eq!(result["success"], true);
        assert_eq!(result["mode"], "append");

        // Verify file content
        let content = fs::read_to_string(&test_file).expect("File should exist");
        assert_eq!(content, "Hello World");
    }

    #[tokio::test]
    async fn test_write_file_skip_if_exists_mode() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");
        let registry = ToolRegistry::new(workspace_root.clone()).await;
        let test_file = workspace_root.join("test.txt");

        // Write initial content
        registry
            .execute_tool(
                "write_file",
                json!({
                    "path": "test.txt",
                    "content": "Original",
                    "mode": "overwrite"
                }),
            )
            .await
            .expect("Initial write should succeed");

        // Try to write with skip_if_exists
        let args = json!({
            "path": "test.txt",
            "content": "New Content",
            "mode": "skip_if_exists"
        });

        let result = registry
            .execute_tool("write_file", args)
            .await
            .expect("skip_if_exists write_file should succeed");
        assert_eq!(result["success"], true);
        assert_eq!(result["skipped"], true);

        // Verify file content is unchanged
        let content = fs::read_to_string(&test_file).expect("File should exist");
        assert_eq!(content, "Original");
    }

    #[tokio::test]
    async fn test_edit_file_exact_match() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");
        let registry = ToolRegistry::new(workspace_root.clone()).await;
        let test_file = workspace_root.join("test.txt");

        // Create initial file
        registry
            .execute_tool(
                "write_file",
                json!({
                    "path": "test.txt",
                    "content": "Hello World\nThis is a test file.",
                    "mode": "overwrite"
                }),
            )
            .await
            .expect("Initial write should succeed");

        // Edit file - replace "World" with "Universe"
        let edit_args = json!({
            "path": "test.txt",
            "old_str": "Hello World",
            "new_str": "Hello Universe"
        });

        let result = registry
            .execute_tool("edit_file", edit_args)
            .await
            .expect("edit_file should succeed");
        assert_eq!(result["success"], true);

        // Verify file content
        let content = fs::read_to_string(&test_file).expect("File should exist");
        assert_eq!(content, "Hello Universe\nThis is a test file.");
    }

    #[tokio::test]
    async fn test_edit_file_multiple_occurrences() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");
        let registry = ToolRegistry::new(workspace_root.clone()).await;
        let test_file = workspace_root.join("test.txt");

        // Create initial file with multiple occurrences
        registry
            .execute_tool(
                "write_file",
                json!({
                    "path": "test.txt",
                    "content": "test\ntest\nmore test content",
                    "mode": "overwrite"
                }),
            )
            .await
            .expect("Initial write should succeed");

        // Edit file - replace all "test" with "example"
        let edit_args = json!({
            "path": "test.txt",
            "old_str": "test",
            "new_str": "example"
        });

        let result = registry
            .execute_tool("edit_file", edit_args)
            .await
            .expect("edit_file should succeed");
        assert_eq!(result["success"], true);

        // Verify file content - all occurrences should be replaced
        let content = fs::read_to_string(&test_file).expect("File should exist");
        assert_eq!(content, "example\nexample\nmore example content");
    }

    #[tokio::test]
    async fn test_edit_file_with_newlines() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root =
            fs::canonicalize(temp_dir.path()).expect("Failed to canonicalize workspace");
        let registry = ToolRegistry::new(workspace_root.clone()).await;
        let test_file = workspace_root.join("test.txt");

        // Create initial file
        registry
            .execute_tool(
                "write_file",
                json!({
                    "path": "test.txt",
                    "content": "Line 1\nLine 2\nLine 3",
                    "mode": "overwrite"
                }),
            )
            .await
            .expect("Initial write should succeed");

        // Edit file - replace multiple lines
        let edit_args = json!({
            "path": "test.txt",
            "old_str": "Line 1\nLine 2",
            "new_str": "New Line 1\nNew Line 2"
        });

        let result = registry
            .execute_tool("edit_file", edit_args)
            .await
            .expect("edit_file should succeed");
        assert_eq!(result["success"], true);

        // Verify file content
        let content = fs::read_to_string(&test_file).expect("File should exist");
        assert_eq!(content, "New Line 1\nNew Line 2\nLine 3");
    }

    #[tokio::test]
    async fn test_write_file_creates_directories() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let workspace_root = temp_dir.path().to_path_buf();
        let registry = ToolRegistry::new(workspace_root.clone()).await;
        let nested_file = temp_dir.path().join("dir").join("subdir").join("test.txt");

        // Write to nested path that doesn't exist
        let args = json!({
            "path": "dir/subdir/test.txt",
            "content": "Nested content",
            "mode": "overwrite"
        });

        let result = registry
            .execute_tool("write_file", args)
            .await
            .expect("write_file should succeed");
        assert_eq!(result["success"], true);

        // Verify file content and directory creation
        let content = fs::read_to_string(&nested_file).expect("File should exist");
        assert_eq!(content, "Nested content");
    }
}