agentfs 0.2.0

Agent Persistence
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
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
//! Integration tests for AgentFS
//!
//! These tests verify that our implementation adheres to the Agent Filesystem
//! Specification (SPEC.md) and matches the behavior of the original agentfs-main.

use agentfs::{AgentFS, FileSystem, KvStore, ToolRecorder};
use agentsql::SqlBackend;

/// Helper to create an in-memory SQLite AgentFS instance for testing
async fn create_test_agentfs() -> AgentFS {
    let backend = SqlBackend::sqlite(":memory:")
        .await
        .expect("Failed to create SQLite backend");

    AgentFS::new(Box::new(backend), "test-agent", "/agent")
        .await
        .expect("Failed to create AgentFS")
}

#[tokio::test]
async fn test_agentfs_creation() {
    let agentfs = create_test_agentfs().await;
    assert_eq!(agentfs.agent_id(), "test-agent");
}

#[tokio::test]
async fn test_kv_operations() {
    let agentfs = create_test_agentfs().await;

    // Set a value
    agentfs.kv.set("test_key", b"test_value").await.unwrap();

    // Get the value
    let value = agentfs.kv.get("test_key").await.unwrap();
    assert_eq!(value, Some(b"test_value".to_vec()));

    // Check existence
    assert!(agentfs.kv.exists("test_key").await.unwrap());
    assert!(!agentfs.kv.exists("nonexistent").await.unwrap());

    // Delete the value
    agentfs.kv.delete("test_key").await.unwrap();

    // Verify deletion
    let value = agentfs.kv.get("test_key").await.unwrap();
    assert_eq!(value, None);
}

#[tokio::test]
async fn test_kv_scan() {
    let agentfs = create_test_agentfs().await;

    // Set multiple values with prefixes
    agentfs.kv.set("prefix_1", b"v1").await.unwrap();
    agentfs.kv.set("prefix_2", b"v2").await.unwrap();
    agentfs.kv.set("other_key", b"v3").await.unwrap();

    // Scan for prefix
    let keys = agentfs.kv.scan("prefix").await.unwrap();
    assert_eq!(keys.len(), 2);
    assert!(keys.contains(&"prefix_1".to_string()));
    assert!(keys.contains(&"prefix_2".to_string()));
}

#[tokio::test]
async fn test_filesystem_mkdir() {
    let agentfs = create_test_agentfs().await;

    // Create a directory
    agentfs.fs.mkdir("/test_dir").await.unwrap();

    // Check directory exists
    assert!(agentfs.fs.exists("/test_dir").await.unwrap());

    // Get stats
    let stats = agentfs.fs.stat("/test_dir").await.unwrap();
    assert!(stats.is_some());
    let stats = stats.unwrap();
    assert!(stats.is_directory());
    assert_eq!(stats.ino, 2); // Root is 1, first created dir is 2
}

#[tokio::test]
async fn test_filesystem_write_and_read() {
    let agentfs = create_test_agentfs().await;

    // Create parent directory first
    agentfs.fs.mkdir("/test_dir").await.unwrap();

    // Write a file
    let data = b"Hello, AgentFS!";
    agentfs
        .fs
        .write_file("/test_dir/test.txt", data)
        .await
        .unwrap();

    // Read the file
    let read_data = agentfs
        .fs
        .read_file("/test_dir/test.txt")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(read_data, data);

    // Check file exists
    assert!(agentfs.fs.exists("/test_dir/test.txt").await.unwrap());
}

#[tokio::test]
async fn test_filesystem_readdir() {
    let agentfs = create_test_agentfs().await;

    // Create a directory
    agentfs.fs.mkdir("/test_dir").await.unwrap();

    // Write multiple files
    agentfs
        .fs
        .write_file("/test_dir/file1.txt", b"content1")
        .await
        .unwrap();
    agentfs
        .fs
        .write_file("/test_dir/file2.txt", b"content2")
        .await
        .unwrap();

    // Create a subdirectory
    agentfs.fs.mkdir("/test_dir/subdir").await.unwrap();

    // List directory
    let entries = agentfs.fs.readdir("/test_dir").await.unwrap().unwrap();
    assert_eq!(entries.len(), 3);
    assert!(entries.contains(&"file1.txt".to_string()));
    assert!(entries.contains(&"file2.txt".to_string()));
    assert!(entries.contains(&"subdir".to_string()));
}

#[tokio::test]
async fn test_filesystem_stat() {
    let agentfs = create_test_agentfs().await;

    // Create directory
    agentfs.fs.mkdir("/test_dir").await.unwrap();

    // Write a file
    let data = b"Test content";
    agentfs
        .fs
        .write_file("/test_dir/test.txt", data)
        .await
        .unwrap();

    // Get file stats
    let stats = agentfs.fs.stat("/test_dir/test.txt").await.unwrap();
    assert!(stats.is_some());
    let stats = stats.unwrap();
    assert!(stats.is_file());
    assert_eq!(stats.size, data.len() as i64);
    assert_eq!(stats.nlink, 1); // Single link

    // Get directory stats
    let dir_stats = agentfs.fs.stat("/test_dir").await.unwrap().unwrap();
    assert!(dir_stats.is_directory());
}

#[tokio::test]
async fn test_filesystem_remove() {
    let agentfs = create_test_agentfs().await;

    // Create and remove a file
    agentfs.fs.mkdir("/test_dir").await.unwrap();
    agentfs
        .fs
        .write_file("/test_dir/test.txt", b"content")
        .await
        .unwrap();

    assert!(agentfs.fs.exists("/test_dir/test.txt").await.unwrap());

    agentfs.fs.remove("/test_dir/test.txt").await.unwrap();

    assert!(!agentfs.fs.exists("/test_dir/test.txt").await.unwrap());

    // Remove empty directory
    agentfs.fs.remove("/test_dir").await.unwrap();
    assert!(!agentfs.fs.exists("/test_dir").await.unwrap());
}

#[tokio::test]
async fn test_filesystem_remove_non_empty_directory() {
    let agentfs = create_test_agentfs().await;

    // Create directory with a file
    agentfs.fs.mkdir("/test_dir").await.unwrap();
    agentfs
        .fs
        .write_file("/test_dir/test.txt", b"content")
        .await
        .unwrap();

    // Try to remove non-empty directory (should fail)
    let result = agentfs.fs.remove("/test_dir").await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_filesystem_overwrite() {
    let agentfs = create_test_agentfs().await;

    agentfs.fs.mkdir("/test_dir").await.unwrap();

    // Write initial content
    agentfs
        .fs
        .write_file("/test_dir/test.txt", b"original")
        .await
        .unwrap();

    // Overwrite with new content
    agentfs
        .fs
        .write_file("/test_dir/test.txt", b"updated")
        .await
        .unwrap();

    // Read and verify new content
    let data = agentfs
        .fs
        .read_file("/test_dir/test.txt")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(data, b"updated");
}

#[tokio::test]
async fn test_filesystem_symlink() {
    let agentfs = create_test_agentfs().await;

    // Create a file
    agentfs.fs.mkdir("/test_dir").await.unwrap();
    agentfs
        .fs
        .write_file("/test_dir/original.txt", b"content")
        .await
        .unwrap();

    // Create a symlink
    agentfs
        .fs
        .symlink("/test_dir/original.txt", "/test_dir/link.txt")
        .await
        .unwrap();

    // Verify symlink exists
    assert!(agentfs.fs.exists("/test_dir/link.txt").await.unwrap());

    // Check lstat (should show it's a symlink)
    let lstat = agentfs.fs.lstat("/test_dir/link.txt").await.unwrap().unwrap();
    assert!(lstat.is_symlink());

    // Check stat (should follow symlink to file)
    let stat = agentfs.fs.stat("/test_dir/link.txt").await.unwrap().unwrap();
    assert!(stat.is_file());

    // Read symlink target
    let target = agentfs
        .fs
        .readlink("/test_dir/link.txt")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(target, "/test_dir/original.txt");

    // Read through symlink
    let data = agentfs
        .fs
        .read_file("/test_dir/link.txt")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(data, b"content");
}

#[tokio::test]
async fn test_filesystem_path_normalization() {
    let agentfs = create_test_agentfs().await;

    // Create directory with various path formats
    agentfs.fs.mkdir("/test_dir").await.unwrap();
    agentfs
        .fs
        .write_file("/test_dir/file.txt", b"content")
        .await
        .unwrap();

    // All these should resolve to the same file
    assert!(agentfs.fs.exists("/test_dir/file.txt").await.unwrap());
    assert!(agentfs.fs.exists("/test_dir//file.txt").await.unwrap());
    assert!(agentfs.fs.exists("/test_dir/./file.txt").await.unwrap());
}

#[tokio::test]
async fn test_root_directory_operations() {
    let agentfs = create_test_agentfs().await;

    // Root should exist
    assert!(agentfs.fs.exists("/").await.unwrap());

    // Root should be a directory
    let stats = agentfs.fs.stat("/").await.unwrap().unwrap();
    assert!(stats.is_directory());
    assert_eq!(stats.ino, 1); // Root inode is always 1

    // Can't remove root
    let result = agentfs.fs.remove("/").await;
    assert!(result.is_err());

    // Can list root
    let entries = agentfs.fs.readdir("/").await.unwrap().unwrap();
    assert!(entries.is_empty()); // Initially empty
}

#[tokio::test]
async fn test_inode_reuse_after_delete() {
    let agentfs = create_test_agentfs().await;

    // Create and delete a file
    agentfs.fs.mkdir("/test").await.unwrap();
    agentfs
        .fs
        .write_file("/test/file.txt", b"content")
        .await
        .unwrap();

    let original_stats = agentfs.fs.stat("/test/file.txt").await.unwrap().unwrap();
    let original_ino = original_stats.ino;

    agentfs.fs.remove("/test/file.txt").await.unwrap();

    // Create a new file - should get a different inode
    agentfs
        .fs
        .write_file("/test/file2.txt", b"new content")
        .await
        .unwrap();

    let new_stats = agentfs.fs.stat("/test/file2.txt").await.unwrap().unwrap();
    assert_ne!(new_stats.ino, original_ino);
}

#[tokio::test]
async fn test_empty_file() {
    let agentfs = create_test_agentfs().await;

    agentfs.fs.mkdir("/test").await.unwrap();

    // Write empty file
    agentfs
        .fs
        .write_file("/test/empty.txt", b"")
        .await
        .unwrap();

    // Read empty file
    let data = agentfs
        .fs
        .read_file("/test/empty.txt")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(data.len(), 0);

    // Check stats
    let stats = agentfs.fs.stat("/test/empty.txt").await.unwrap().unwrap();
    assert_eq!(stats.size, 0);
}

#[tokio::test]
async fn test_nested_directories() {
    let agentfs = create_test_agentfs().await;

    // Create nested directory structure
    agentfs.fs.mkdir("/level1").await.unwrap();
    agentfs.fs.mkdir("/level1/level2").await.unwrap();
    agentfs.fs.mkdir("/level1/level2/level3").await.unwrap();

    // Write file at deepest level
    agentfs
        .fs
        .write_file("/level1/level2/level3/deep.txt", b"deep content")
        .await
        .unwrap();

    // Verify we can read it
    let data = agentfs
        .fs
        .read_file("/level1/level2/level3/deep.txt")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(data, b"deep content");

    // Verify path resolution works at each level
    assert!(agentfs.fs.exists("/level1").await.unwrap());
    assert!(agentfs.fs.exists("/level1/level2").await.unwrap());
    assert!(agentfs.fs.exists("/level1/level2/level3").await.unwrap());
}

#[tokio::test]
async fn test_file_in_root() {
    let agentfs = create_test_agentfs().await;

    // Write file directly in root
    agentfs
        .fs
        .write_file("/root_file.txt", b"in root")
        .await
        .unwrap();

    // Read it back
    let data = agentfs
        .fs
        .read_file("/root_file.txt")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(data, b"in root");

    // List root to see it
    let entries = agentfs.fs.readdir("/").await.unwrap().unwrap();
    assert!(entries.contains(&"root_file.txt".to_string()));
}

// Tool Calls API Tests

#[tokio::test]
async fn test_tool_calls_workflow() {
    let agentfs = create_test_agentfs().await;

    // Start a tool call
    let params = serde_json::json!({"query": "test query"});
    let id = agentfs.tools.start("web_search", Some(params.clone())).await.unwrap();

    // Verify tool call was created
    let tool_call = agentfs.tools.get(id).await.unwrap().unwrap();
    assert_eq!(tool_call.name, "web_search");
    assert_eq!(tool_call.parameters, Some(params));
    assert_eq!(tool_call.status, agentfs::tools::ToolCallStatus::Pending);
    assert!(tool_call.completed_at.is_none());
    assert!(tool_call.duration_ms.is_none());

    // Mark as successful
    let result = serde_json::json!({"results": ["result1", "result2"]});
    agentfs.tools.success(id, Some(result.clone())).await.unwrap();

    // Verify tool call was updated
    let tool_call = agentfs.tools.get(id).await.unwrap().unwrap();
    assert_eq!(tool_call.status, agentfs::tools::ToolCallStatus::Success);
    assert_eq!(tool_call.result, Some(result));
    assert!(tool_call.completed_at.is_some());
    assert!(tool_call.duration_ms.is_some());
    assert!(tool_call.duration_ms.unwrap() >= 0);
}

#[tokio::test]
async fn test_tool_calls_error() {
    let agentfs = create_test_agentfs().await;

    // Start a tool call
    let id = agentfs.tools.start("failing_tool", None).await.unwrap();

    // Mark as failed
    agentfs.tools.error(id, "Connection timeout").await.unwrap();

    // Verify error was recorded
    let tool_call = agentfs.tools.get(id).await.unwrap().unwrap();
    assert_eq!(tool_call.status, agentfs::tools::ToolCallStatus::Error);
    assert_eq!(tool_call.error, Some("Connection timeout".to_string()));
    assert!(tool_call.result.is_none());
    assert!(tool_call.completed_at.is_some());
    assert!(tool_call.duration_ms.is_some());
}

#[tokio::test]
async fn test_tool_calls_record() {
    let agentfs = create_test_agentfs().await;

    // Use single-shot record API
    let params = serde_json::json!({"url": "https://example.com"});
    let result = serde_json::json!({"status": 200});

    let started_at = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs() as i64;

    tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

    let completed_at = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs() as i64;

    let id = agentfs.tools.record(
        "http_request",
        started_at,
        completed_at,
        Some(params.clone()),
        Some(result.clone()),
        None,
    ).await.unwrap();

    // Verify tool call
    let tool_call = agentfs.tools.get(id).await.unwrap().unwrap();
    assert_eq!(tool_call.name, "http_request");
    assert_eq!(tool_call.parameters, Some(params));
    assert_eq!(tool_call.result, Some(result));
    assert_eq!(tool_call.status, agentfs::tools::ToolCallStatus::Success);
    assert_eq!(tool_call.started_at, started_at);
    assert_eq!(tool_call.completed_at, Some(completed_at));
}

#[tokio::test]
async fn test_tool_calls_stats() {
    let agentfs = create_test_agentfs().await;

    // Create multiple tool calls
    let id1 = agentfs.tools.start("api_call", None).await.unwrap();
    tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
    agentfs.tools.success(id1, None).await.unwrap();

    let id2 = agentfs.tools.start("api_call", None).await.unwrap();
    tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
    agentfs.tools.success(id2, None).await.unwrap();

    let id3 = agentfs.tools.start("api_call", None).await.unwrap();
    agentfs.tools.error(id3, "Failed").await.unwrap();

    // Get statistics
    let stats = agentfs.tools.stats_for("api_call").await.unwrap().unwrap();
    assert_eq!(stats.name, "api_call");
    assert_eq!(stats.total_calls, 3);
    assert_eq!(stats.successful, 2);
    assert_eq!(stats.failed, 1);
    assert!(stats.avg_duration_ms >= 0.0);

    // Non-existent tool should return None
    let no_stats = agentfs.tools.stats_for("nonexistent").await.unwrap();
    assert!(no_stats.is_none());
}

#[tokio::test]
async fn test_tool_calls_list() {
    let agentfs = create_test_agentfs().await;

    // Create several tool calls
    agentfs.tools.start("tool1", None).await.unwrap();
    agentfs.tools.start("tool2", None).await.unwrap();
    agentfs.tools.start("tool3", None).await.unwrap();

    // List all
    let all_calls = agentfs.tools.list(None).await.unwrap();
    assert_eq!(all_calls.len(), 3);

    // List with limit
    let limited_calls = agentfs.tools.list(Some(2)).await.unwrap();
    assert_eq!(limited_calls.len(), 2);
}

#[tokio::test]
async fn test_path_sandboxing() {
    let agentfs = create_test_agentfs().await;

    // Test 1: Basic paths are sandboxed within mount point
    agentfs.fs.write_file("/test.txt", b"content").await.unwrap();
    let content = agentfs.fs.read_file("/test.txt").await.unwrap();
    assert_eq!(content, Some(b"content".to_vec()));

    // Test 2: Relative paths work
    agentfs.fs.write_file("relative.txt", b"relative").await.unwrap();
    let content = agentfs.fs.read_file("relative.txt").await.unwrap();
    assert_eq!(content, Some(b"relative".to_vec()));

    // Test 3: Paths with mount point prefix are properly stripped
    agentfs.fs.write_file("/agent/prefixed.txt", b"prefixed").await.unwrap();
    let content = agentfs.fs.read_file("/prefixed.txt").await.unwrap();
    assert_eq!(content, Some(b"prefixed".to_vec()));
    let content2 = agentfs.fs.read_file("/agent/prefixed.txt").await.unwrap();
    assert_eq!(content2, Some(b"prefixed".to_vec()));

    // Test 4: Directory traversal attempts are normalized and can't escape
    agentfs.fs.mkdir("/sandbox").await.unwrap();
    agentfs.fs.write_file("/sandbox/file.txt", b"safe").await.unwrap();

    // Attempting to traverse up with .. just normalizes to root
    agentfs.fs.write_file("/sandbox/../escape_attempt.txt", b"normalized").await.unwrap();
    // This should create the file at /escape_attempt.txt (not outside mount point)
    let content = agentfs.fs.read_file("/escape_attempt.txt").await.unwrap();
    assert_eq!(content, Some(b"normalized".to_vec()));

    // Test 5: Multiple .. components can't escape root
    // First create the directory that will result from normalization
    agentfs.fs.mkdir("/etc").await.unwrap();
    agentfs.fs.write_file("/../../../etc/passwd", b"blocked").await.unwrap();
    // This normalizes to /etc/passwd within the mount point
    let content = agentfs.fs.read_file("/etc/passwd").await.unwrap();
    assert_eq!(content, Some(b"blocked".to_vec()));

    // Test 6: Verify filesystem operations all respect sandboxing
    agentfs.fs.mkdir("/agent/sandboxed").await.unwrap();
    agentfs.fs.write_file("/agent/sandboxed/test.txt", b"data").await.unwrap();
    assert!(agentfs.fs.exists("/sandboxed/test.txt").await.unwrap());
    assert!(agentfs.fs.exists("/agent/sandboxed/test.txt").await.unwrap());

    let entries = agentfs.fs.readdir("/").await.unwrap().unwrap();
    // Should have: test.txt, relative.txt, prefixed.txt, sandbox/, escape_attempt.txt, etc/, sandboxed/
    assert!(entries.len() >= 6);

    // Test 7: Stat operations work with sandboxed paths
    let stats = agentfs.fs.stat("/test.txt").await.unwrap();
    assert!(stats.is_some());
    let stats = agentfs.fs.stat("/agent/test.txt").await.unwrap();
    assert!(stats.is_some());
}