agent-sandbox 0.1.2

A sandboxed execution environment for AI agents via WASM
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
use agent_sandbox::Sandbox;
use agent_sandbox::config::SandboxConfig;

fn temp_sandbox() -> (tempfile::TempDir, Sandbox) {
    let tmp = tempfile::tempdir().unwrap();
    let config = SandboxConfig {
        work_dir: tmp.path().to_path_buf(),
        ..Default::default()
    };
    let sandbox = Sandbox::new(config).unwrap();
    (tmp, sandbox)
}

#[tokio::test]
async fn test_create_sandbox() {
    let (_tmp, sandbox) = temp_sandbox();
    // Sandbox created successfully
    sandbox.destroy().await.unwrap();
}

#[tokio::test]
async fn test_write_and_read_file() {
    let (_tmp, sandbox) = temp_sandbox();

    sandbox
        .write_file("test.txt", b"hello world")
        .await
        .unwrap();

    let content = sandbox.read_file("test.txt").await.unwrap();
    assert_eq!(content, b"hello world");
}

#[tokio::test]
async fn test_write_file_creates_parent_dirs() {
    let (_tmp, sandbox) = temp_sandbox();

    sandbox.write_file("a/b/c.txt", b"nested").await.unwrap();

    let content = sandbox.read_file("a/b/c.txt").await.unwrap();
    assert_eq!(content, b"nested");
}

#[tokio::test]
async fn test_list_dir() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("a.txt"), "a").unwrap();
    std::fs::write(tmp.path().join("b.txt"), "b").unwrap();
    std::fs::create_dir(tmp.path().join("subdir")).unwrap();

    let entries = sandbox.list_dir(".").await.unwrap();
    assert_eq!(entries.len(), 3);
    assert!(entries.iter().any(|e| e.name == "a.txt" && e.is_file));
    assert!(entries.iter().any(|e| e.name == "b.txt" && e.is_file));
    assert!(entries.iter().any(|e| e.name == "subdir" && e.is_dir));
}

#[tokio::test]
async fn test_exec_echo() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox
        .exec("echo", &["hello".into(), "world".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert_eq!(
        String::from_utf8_lossy(&result.stdout).trim(),
        "hello world"
    );
}

#[tokio::test]
async fn test_exec_cat() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("hello.txt"), "hello sandbox").unwrap();

    let result = sandbox
        .exec("cat", &["/work/hello.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert!(String::from_utf8_lossy(&result.stdout).contains("hello sandbox"));
}

#[tokio::test]
async fn test_exec_ls() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("file1.txt"), "").unwrap();
    std::fs::write(tmp.path().join("file2.txt"), "").unwrap();

    let result = sandbox.exec("ls", &["/work".into()]).await.unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("file1.txt"));
    assert!(output.contains("file2.txt"));
}

#[tokio::test]
async fn test_exec_find() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::create_dir_all(tmp.path().join("a/b")).unwrap();
    std::fs::write(tmp.path().join("a/b/deep.txt"), "deep").unwrap();

    let result = sandbox
        .exec("find", &["/work".into(), "-name".into(), "*.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("deep.txt"));
}

#[tokio::test]
async fn test_exec_grep() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(
        tmp.path().join("code.rs"),
        "fn main() {\n    println!(\"hello\");\n}\n",
    )
    .unwrap();

    let result = sandbox
        .exec("grep", &["main".into(), "/work/code.rs".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("fn main()"));
}

#[tokio::test]
async fn test_exec_wc() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("lines.txt"), "one\ntwo\nthree\n").unwrap();

    let result = sandbox
        .exec("wc", &["-l".into(), "/work/lines.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("3"));
}

#[tokio::test]
async fn test_exec_mkdir_and_touch() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox
        .exec("mkdir", &["-p".into(), "/work/newdir/sub".into()])
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);

    let result = sandbox
        .exec("touch", &["/work/newdir/sub/file.txt".into()])
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);

    let result = sandbox
        .exec("ls", &["/work/newdir/sub".into()])
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("file.txt"));
}

#[tokio::test]
async fn test_path_traversal_blocked() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox.read_file("../../../etc/passwd").await;
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("traversal"));
}

#[tokio::test]
async fn test_command_not_found() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox.exec("nonexistent_cmd", &[]).await;
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("not found"));
}

#[tokio::test]
async fn test_fuel_exhaustion_timeout() {
    let tmp = tempfile::tempdir().unwrap();
    let config = SandboxConfig {
        work_dir: tmp.path().to_path_buf(),
        fuel_limit: 1000, // Very low fuel to trigger exhaustion
        ..Default::default()
    };
    let sandbox = Sandbox::new(config).unwrap();

    // Even a simple echo should exhaust 1000 fuel units
    let result = sandbox.exec("echo", &["hello".into()]).await;
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("timed out") || err.contains("fuel"),
        "Expected timeout/fuel error, got: {}",
        err
    );
}

#[tokio::test]
async fn test_diff_reports_changes() {
    let (tmp, _sandbox) = temp_sandbox();

    // Write initial file
    std::fs::write(tmp.path().join("existing.txt"), "original").unwrap();

    // Create a new sandbox to snapshot current state
    let config = SandboxConfig {
        work_dir: tmp.path().to_path_buf(),
        ..Default::default()
    };
    let sandbox = Sandbox::new(config).unwrap();

    // Create a new file
    std::fs::write(tmp.path().join("new.txt"), "new content").unwrap();

    // Modify existing file
    std::fs::write(tmp.path().join("existing.txt"), "modified").unwrap();

    let changes = sandbox.diff().await.unwrap();
    assert!(
        changes.iter().any(|c| c.path == "new.txt"),
        "Expected 'new.txt' in changes: {:?}",
        changes.iter().map(|c| &c.path).collect::<Vec<_>>()
    );
    assert!(
        changes.iter().any(|c| c.path == "existing.txt"),
        "Expected 'existing.txt' in changes: {:?}",
        changes.iter().map(|c| &c.path).collect::<Vec<_>>()
    );
}

#[tokio::test]
async fn test_destroy_prevents_operations() {
    let (_tmp, sandbox) = temp_sandbox();

    sandbox.destroy().await.unwrap();

    let result = sandbox.read_file("anything.txt").await;
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("destroyed"));
}

#[tokio::test]
async fn test_exec_sed() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("input.txt"), "hello world\n").unwrap();

    let result = sandbox
        .exec("sed", &["s/world/rust/g".into(), "/work/input.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("hello rust"));
}

#[tokio::test]
async fn test_exec_basename_dirname() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox
        .exec("basename", &["/work/path/to/file.txt".into()])
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(String::from_utf8_lossy(&result.stdout).trim(), "file.txt");

    let result = sandbox
        .exec("dirname", &["/work/path/to/file.txt".into()])
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(
        String::from_utf8_lossy(&result.stdout).trim(),
        "/work/path/to"
    );
}

// --- Additional tool exec tests ---

#[tokio::test]
async fn test_exec_head() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(
        tmp.path().join("data.txt"),
        "line1\nline2\nline3\nline4\nline5\n",
    )
    .unwrap();

    let result = sandbox
        .exec("head", &["-n".into(), "2".into(), "/work/data.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("line1"));
    assert!(output.contains("line2"));
    assert!(!output.contains("line3"));
}

#[tokio::test]
async fn test_exec_tail() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(
        tmp.path().join("data.txt"),
        "line1\nline2\nline3\nline4\nline5\n",
    )
    .unwrap();

    let result = sandbox
        .exec("tail", &["-n".into(), "2".into(), "/work/data.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(!output.contains("line3"));
    assert!(output.contains("line4"));
    assert!(output.contains("line5"));
}

#[tokio::test]
async fn test_exec_sort() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("unsorted.txt"), "banana\napple\ncherry\n").unwrap();

    let result = sandbox
        .exec("sort", &["/work/unsorted.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert_eq!(
        String::from_utf8_lossy(&result.stdout).trim(),
        "apple\nbanana\ncherry"
    );
}

#[tokio::test]
async fn test_exec_uniq() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("dups.txt"), "a\na\nb\nb\nb\nc\n").unwrap();

    let result = sandbox
        .exec("uniq", &["/work/dups.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert_eq!(String::from_utf8_lossy(&result.stdout).trim(), "a\nb\nc");
}

#[tokio::test]
async fn test_exec_cp() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("src.txt"), "copy me").unwrap();

    let result = sandbox
        .exec("cp", &["/work/src.txt".into(), "/work/dst.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let content = std::fs::read_to_string(tmp.path().join("dst.txt")).unwrap();
    assert_eq!(content, "copy me");
}

#[tokio::test]
async fn test_exec_mv() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("old.txt"), "move me").unwrap();

    let result = sandbox
        .exec("mv", &["/work/old.txt".into(), "/work/new.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert!(!tmp.path().join("old.txt").exists());
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("new.txt")).unwrap(),
        "move me"
    );
}

#[tokio::test]
async fn test_exec_rm() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("delete.txt"), "bye").unwrap();
    assert!(tmp.path().join("delete.txt").exists());

    let result = sandbox
        .exec("rm", &["/work/delete.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert!(!tmp.path().join("delete.txt").exists());
}

#[tokio::test]
async fn test_exec_base64() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("plain.txt"), "hello").unwrap();

    let result = sandbox
        .exec("base64", &["/work/plain.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert_eq!(String::from_utf8_lossy(&result.stdout).trim(), "aGVsbG8=");
}

#[tokio::test]
async fn test_exec_sha256sum() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("hash.txt"), "hello").unwrap();

    let result = sandbox
        .exec("sha256sum", &["/work/hash.txt".into()])
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    // sha256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
    assert!(output.contains("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"));
}

#[tokio::test]
async fn test_exec_diff() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("a.txt"), "line1\nline2\nline3\n").unwrap();
    std::fs::write(tmp.path().join("b.txt"), "line1\nmodified\nline3\n").unwrap();

    let result = sandbox
        .exec("diff", &["/work/a.txt".into(), "/work/b.txt".into()])
        .await
        .unwrap();

    // diff returns exit code 1 when files differ
    assert_eq!(result.exit_code, 1);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("line2") || output.contains("modified"));
}

#[tokio::test]
async fn test_exec_cut() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("csv.txt"), "a,b,c\n1,2,3\n").unwrap();

    let result = sandbox
        .exec(
            "cut",
            &[
                "-d".into(),
                ",".into(),
                "-f".into(),
                "2".into(),
                "/work/csv.txt".into(),
            ],
        )
        .await
        .unwrap();

    assert_eq!(result.exit_code, 0);
    assert_eq!(String::from_utf8_lossy(&result.stdout).trim(), "b\n2");
}

#[tokio::test]
async fn test_exec_env() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox.exec("env", &[]).await.unwrap();

    assert_eq!(result.exit_code, 0);
    let output = String::from_utf8_lossy(&result.stdout);
    assert!(output.contains("TOOLBOX_CMD=env"));
}

// --- Security tests ---

#[tokio::test]
async fn test_security_path_traversal_variants() {
    let (_tmp, sandbox) = temp_sandbox();

    // Various traversal attempts via readFile
    let traversals = [
        "../../../etc/passwd",
        "../../etc/shadow",
        "foo/../../..",
        "./../../etc/hosts",
        "foo/../../../etc/passwd",
    ];

    for path in traversals {
        let result = sandbox.read_file(path).await;
        assert!(
            result.is_err(),
            "Path '{}' should be blocked but was allowed",
            path
        );
        assert!(
            result.unwrap_err().to_string().contains("traversal"),
            "Path '{}' should return traversal error",
            path
        );
    }
}

#[tokio::test]
async fn test_security_write_file_traversal() {
    let (_tmp, sandbox) = temp_sandbox();

    // Attempt to write outside the sandbox
    let result = sandbox
        .write_file("../../../tmp/escape.txt", b"pwned")
        .await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("traversal"));
}

#[tokio::test]
async fn test_security_list_dir_traversal() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox.list_dir("../../../etc").await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("traversal"));
}

#[tokio::test]
async fn test_security_symlink_escape() {
    let (tmp, sandbox) = temp_sandbox();

    // Create a symlink inside work dir that points outside
    let link_path = tmp.path().join("escape_link");
    std::os::unix::fs::symlink("/etc", &link_path).unwrap();

    // Reading via the symlink should fail — the resolved path is outside the sandbox
    let result = sandbox.read_file("escape_link/passwd").await;
    assert!(
        result.is_err(),
        "Symlink escape to /etc/passwd should be blocked"
    );
}

#[tokio::test]
async fn test_security_cat_cannot_read_host_files() {
    let (_tmp, sandbox) = temp_sandbox();

    // WASM sandbox should not have access to /etc/passwd via cat
    let result = sandbox.exec("cat", &["/etc/passwd".into()]).await.unwrap();

    // Should fail since /etc is not mounted
    assert_ne!(result.exit_code, 0);
    assert!(String::from_utf8_lossy(&result.stdout).is_empty());
}

#[tokio::test]
async fn test_security_find_confined_to_sandbox() {
    let (_tmp, sandbox) = temp_sandbox();

    // find should not be able to traverse outside /work
    let result = sandbox
        .exec("find", &["/".into(), "-name".into(), "passwd".into()])
        .await
        .unwrap();

    let output = String::from_utf8_lossy(&result.stdout);
    // Should not find /etc/passwd — only /work is mounted
    assert!(
        !output.contains("/etc/passwd"),
        "find should not see /etc/passwd, got: {}",
        output
    );
}

#[tokio::test]
async fn test_security_cp_cannot_write_outside_sandbox() {
    let (tmp, sandbox) = temp_sandbox();

    std::fs::write(tmp.path().join("secret.txt"), "data").unwrap();

    // Attempt to copy to a path outside /work
    let result = sandbox
        .exec("cp", &["/work/secret.txt".into(), "/tmp/escape.txt".into()])
        .await
        .unwrap();

    // Should fail — /tmp is not writable/mounted
    assert_ne!(result.exit_code, 0);
    assert!(!std::path::Path::new("/tmp/escape.txt").exists());
}

#[tokio::test]
async fn test_security_env_vars_isolated() {
    let tmp = tempfile::tempdir().unwrap();
    let config = SandboxConfig {
        work_dir: tmp.path().to_path_buf(),
        env_vars: [("SECRET_KEY".into(), "s3cret".into())]
            .into_iter()
            .collect(),
        ..Default::default()
    };
    let sandbox = Sandbox::new(config).unwrap();

    let result = sandbox.exec("env", &[]).await.unwrap();
    let output = String::from_utf8_lossy(&result.stdout);

    // Configured env vars should be visible
    assert!(output.contains("SECRET_KEY=s3cret"));

    // Host env vars like HOME, USER, PATH should NOT leak into the sandbox
    assert!(
        !output.contains("HOME="),
        "Host HOME should not leak into sandbox"
    );
    assert!(
        !output.contains("USER="),
        "Host USER should not leak into sandbox"
    );
}

#[tokio::test]
async fn test_security_fuel_limit_prevents_infinite_loop() {
    let tmp = tempfile::tempdir().unwrap();
    let config = SandboxConfig {
        work_dir: tmp.path().to_path_buf(),
        fuel_limit: 100_000, // Low enough to stop runaway but enough to start
        ..Default::default()
    };
    let sandbox = Sandbox::new(config).unwrap();

    // Try running a command — with limited fuel it should error, not hang
    let result = sandbox.exec("echo", &["test".into()]).await;
    // Either succeeds quickly or fails with timeout/fuel — should NOT hang
    assert!(
        result.is_ok() || result.unwrap_err().to_string().contains("timed out"),
        "Low fuel should either complete or timeout, not hang"
    );
}

#[tokio::test]
async fn test_security_timeout_prevents_hang() {
    let tmp = tempfile::tempdir().unwrap();
    let config = SandboxConfig {
        work_dir: tmp.path().to_path_buf(),
        timeout: std::time::Duration::from_secs(2), // 2 second timeout
        fuel_limit: u64::MAX,                       // Effectively unlimited fuel
        ..Default::default()
    };
    let sandbox = Sandbox::new(config).unwrap();

    let start = std::time::Instant::now();
    // Even with unlimited fuel, we should not wait longer than timeout + margin
    let _result = sandbox.exec("echo", &["hello".into()]).await;
    let elapsed = start.elapsed();

    assert!(
        elapsed < std::time::Duration::from_secs(10),
        "Execution should respect timeout, took {:?}",
        elapsed
    );
}

#[tokio::test]
async fn test_security_destroyed_sandbox_blocks_all_ops() {
    let (_tmp, sandbox) = temp_sandbox();

    sandbox.destroy().await.unwrap();

    // All operations should fail with "destroyed"
    assert!(sandbox.read_file("any.txt").await.is_err());
    assert!(sandbox.write_file("any.txt", b"data").await.is_err());
    assert!(sandbox.list_dir(".").await.is_err());
    assert!(sandbox.exec("echo", &["hello".into()]).await.is_err());
    assert!(sandbox.diff().await.is_err());
}

#[tokio::test]
async fn test_security_grep_cannot_read_host_files() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox
        .exec("grep", &["root".into(), "/etc/passwd".into()])
        .await
        .unwrap();

    // grep should fail because /etc is not mounted
    assert_ne!(result.exit_code, 0);
}

#[tokio::test]
async fn test_security_rm_cannot_delete_outside_sandbox() {
    let (_tmp, sandbox) = temp_sandbox();

    let result = sandbox.exec("rm", &["/etc/hostname".into()]).await.unwrap();

    // rm outside /work should fail
    assert_ne!(result.exit_code, 0);
}

#[tokio::test]
async fn test_security_multiple_sandboxes_isolated() {
    let tmp1 = tempfile::tempdir().unwrap();
    let tmp2 = tempfile::tempdir().unwrap();

    let sandbox1 = Sandbox::new(SandboxConfig {
        work_dir: tmp1.path().to_path_buf(),
        ..Default::default()
    })
    .unwrap();

    let sandbox2 = Sandbox::new(SandboxConfig {
        work_dir: tmp2.path().to_path_buf(),
        ..Default::default()
    })
    .unwrap();

    // Write a file in sandbox1
    std::fs::write(tmp1.path().join("secret.txt"), "sandbox1 secret").unwrap();

    // Sandbox2 should not see sandbox1's files
    let result = sandbox2
        .exec("cat", &["/work/secret.txt".into()])
        .await
        .unwrap();
    assert_ne!(
        result.exit_code, 0,
        "Sandbox2 should not see sandbox1's files"
    );

    // Sandbox1 should see its own file
    let result = sandbox1
        .exec("cat", &["/work/secret.txt".into()])
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    assert!(String::from_utf8_lossy(&result.stdout).contains("sandbox1 secret"));
}