tinysandbox 0.1.0

Ultra-minimal Linux-like sandbox for AI agents: shell, coreutils, VFS, and a Wasmtime-hosted JS runtime in one crate
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! In-process shell sandbox over a VFS.
//!
//! ```
//! use tinysandbox::sandbox::Sandbox;
//! use tinysandbox::vfs::{InMemoryVfs, VfsQuota};
//!
//! # fn main() {
//! # tokio::runtime::Builder::new_current_thread()
//! #     .enable_time()
//! #     .build()
//! #     .unwrap()
//! #     .block_on(async {
//! let sandbox = Sandbox::builder().vfs(InMemoryVfs::new(VfsQuota::unlimited())).build();
//!
//! let result = sandbox.exec("echo hello").await;
//! assert_eq!(result.exit_code, 0);
//! assert_eq!(result.stdout, "hello\n");
//! #     });
//! # }
//! ```

mod builtins;
pub mod command;
pub mod fs;

use std::collections::{BTreeMap, BTreeSet};
use std::future::Future;
use std::io::Cursor;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};

use command::SharedWriter;
use fs::{Fs, errno_message, normalize_absolute};
use tokio::time;

use crate::shell::{
    self, AndOrList, AndOrOp, Command as AstCommand, Pipeline, Redirect, RedirectOp,
    RedirectTarget, Segment, SimpleCommand, Word,
};
use crate::vfs::{Errno, FileType, InMemoryVfs, Metadata, Vfs, VfsError, VfsStats};

pub use command::{
    BoxAsyncRead, BoxAsyncWrite, Command, CommandContext, CommandFuture, CommandResult, Limits,
};

#[derive(Debug, Clone)]
pub struct ExecResult {
    pub stdout: String,
    pub stderr: String,
    pub exit_code: i32,
    pub metrics: ExecMetrics,
}

#[derive(Debug, Clone)]
pub struct ExecMetrics {
    pub wall_time: Duration,
    pub commands: Vec<CommandTiming>,
    pub pipe_bytes: Vec<usize>,
    pub stdout_truncated: bool,
    pub stderr_truncated: bool,
    pub peak_wasm_memory_bytes: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct CommandTiming {
    pub name: String,
    pub duration: Duration,
    pub exit_code: i32,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SandboxStats {
    pub vfs: Option<VfsStats>,
    pub commands_run: u64,
}

pub struct Sandbox {
    vfs: Arc<dyn Vfs>,
    commands: Arc<BTreeMap<String, Arc<dyn Command>>>,
    command_names: Arc<BTreeSet<String>>,
    limits: Limits,
    session: Mutex<Session>,
    commands_run: AtomicU64,
}

pub struct SandboxBuilder {
    vfs: Arc<dyn Vfs>,
    commands: BTreeMap<String, Arc<dyn Command>>,
    limits: Limits,
    cwd: String,
    env: BTreeMap<String, String>,
}

impl Sandbox {
    pub fn builder() -> SandboxBuilder {
        SandboxBuilder::new()
    }

    pub fn vfs(&self) -> Arc<dyn Vfs> {
        Arc::clone(&self.vfs)
    }

    pub fn stats(&self) -> SandboxStats {
        SandboxStats {
            vfs: self.vfs.stats().and_then(Result::ok),
            commands_run: self.commands_run.load(Ordering::Relaxed),
        }
    }

    /// Executes a shell program against a snapshot of the current session.
    ///
    /// The wall-clock timeout is exec-wide. When it fires, partial stdout,
    /// stderr, metrics, and session mutations are discarded and the result
    /// exits 124. Blocking host calls already running on worker threads are not
    /// cancelled by that timeout, so VFS implementations should keep individual
    /// operations bounded. Concurrent execs each start from the session state
    /// visible at their own start; when they complete, the last stored session
    /// wins.
    pub async fn exec(&self, input: &str) -> ExecResult {
        let started = Instant::now();
        let future = self.exec_inner(input);
        match time::timeout(self.limits.wall_time, future).await {
            Ok(mut result) => {
                result.metrics.wall_time = started.elapsed();
                result
            }
            Err(_) => ExecResult {
                stdout: String::new(),
                stderr: "tinysandbox: command timed out\n".to_owned(),
                exit_code: 124,
                metrics: ExecMetrics {
                    wall_time: started.elapsed(),
                    commands: Vec::new(),
                    pipe_bytes: Vec::new(),
                    stdout_truncated: false,
                    stderr_truncated: false,
                    peak_wasm_memory_bytes: None,
                },
            },
        }
    }

    async fn exec_inner(&self, input: &str) -> ExecResult {
        let program = match shell::parse(input) {
            Ok(program) => program,
            Err(err) => {
                return ExecResult {
                    stdout: String::new(),
                    stderr: format!("{err}\n"),
                    exit_code: 2,
                    metrics: ExecMetrics::empty(),
                };
            }
        };

        let mut session = self.session_snapshot();
        let mut exec = ExecState::new(session.last_status);
        for list in &program.lists {
            exec.last_status = self.exec_and_or_list(list, &mut session, &mut exec).await;
            if exec.limit_hit {
                break;
            }
        }

        session.last_status = exec.last_status;
        self.store_session(session);
        self.commands_run.fetch_add(
            u64::try_from(exec.command_count).unwrap_or(u64::MAX),
            Ordering::Relaxed,
        );

        let (stdout, stdout_truncated) = truncate_output(exec.stdout, self.limits.stdout_bytes);
        let (stderr, stderr_truncated) = truncate_output(exec.stderr, self.limits.stderr_bytes);
        ExecResult {
            stdout: String::from_utf8_lossy(&stdout).into_owned(),
            stderr: String::from_utf8_lossy(&stderr).into_owned(),
            exit_code: exec.last_status,
            metrics: ExecMetrics {
                wall_time: Duration::ZERO,
                commands: exec.timings,
                pipe_bytes: exec.pipe_bytes,
                stdout_truncated,
                stderr_truncated,
                peak_wasm_memory_bytes: exec.peak_wasm_memory_bytes,
            },
        }
    }

    async fn exec_and_or_list(
        &self,
        list: &AndOrList,
        session: &mut Session,
        exec: &mut ExecState,
    ) -> i32 {
        let mut status = self.exec_pipeline(&list.first, session, exec).await;
        for item in &list.rest {
            let should_run = match item.op {
                AndOrOp::And => status == 0,
                AndOrOp::Or => status != 0,
            };
            if should_run {
                status = self.exec_pipeline(&item.pipeline, session, exec).await;
            }
            if exec.limit_hit {
                break;
            }
        }
        status
    }

    async fn exec_pipeline(
        &self,
        pipeline: &Pipeline,
        session: &mut Session,
        exec: &mut ExecState,
    ) -> i32 {
        let mut input = Vec::new();
        let mut status = 0;
        for (index, command) in pipeline.commands.iter().enumerate() {
            let is_last = index + 1 == pipeline.commands.len();
            let (next_input, command_status) = match command {
                AstCommand::Simple(simple) => {
                    self.exec_simple(simple, session, exec, input, is_last)
                        .await
                }
            };
            status = command_status;
            input = next_input;
            if !is_last {
                exec.pipe_bytes.push(input.len());
            }
            if exec.limit_hit {
                return status;
            }
        }
        status
    }

    async fn exec_simple(
        &self,
        simple: &SimpleCommand,
        session: &mut Session,
        exec: &mut ExecState,
        mut stdin: Vec<u8>,
        is_last: bool,
    ) -> (Vec<u8>, i32) {
        if exec.command_count >= self.limits.max_commands {
            exec.stderr
                .extend_from_slice(b"tinysandbox: maximum command count exceeded\n");
            exec.limit_hit = true;
            return (Vec::new(), 125);
        }
        exec.command_count += 1;

        let assignment_values =
            expand_assignments(&simple.assignments, &session.env, exec.last_status);
        let words = expand_words(&simple.words, &session.env, exec.last_status);
        if words.is_empty() {
            for (name, value) in assignment_values {
                session.env.insert(name, value);
            }
            return (Vec::new(), 0);
        }

        let command_name = words[0].clone();
        let args = words[1..].to_vec();
        let fs = Fs::new(
            Arc::clone(&self.vfs),
            Arc::clone(&self.command_names),
            session.cwd.clone(),
        );
        let redirects = match prepare_redirects(simple, &fs, &session.env, exec.last_status).await {
            Ok(redirects) => redirects,
            Err((path, err)) => {
                exec.stderr.extend_from_slice(
                    format!("{command_name}: {path}: {}\n", errno_message(err.errno())).as_bytes(),
                );
                return (Vec::new(), 1);
            }
        };
        if let Some(data) = redirects.stdin {
            stdin = data;
        }

        let mut command_env = session.env.clone();
        for (name, value) in assignment_values {
            command_env.insert(name, value);
        }
        command_env.insert("?".to_owned(), exec.last_status.to_string());

        let started = Instant::now();
        let mut special_stdout = Vec::new();
        let mut special_stderr = Vec::new();
        let shell_ctx = ShellBuiltinContext {
            session,
            fs: &fs,
            env: &mut command_env,
            stdout: &mut special_stdout,
            stderr: &mut special_stderr,
        };
        let (mut stdout_bytes, mut stderr_bytes, status) = if let Some(status) = self
            .run_shell_builtin(&command_name, &args, shell_ctx)
            .await
        {
            (special_stdout, special_stderr, status)
        } else if let Some(command) = self.commands.get(&command_name) {
            let stdout = SharedWriter::new();
            let stderr = SharedWriter::new();
            let ctx = CommandContext {
                args,
                env: command_env,
                cwd: session.cwd.clone(),
                stdin: Box::pin(Cursor::new(stdin)),
                stdout: stdout.boxed(),
                stderr: stderr.boxed(),
                fs: fs.clone(),
                limits: self.limits,
                commands: Arc::clone(&self.command_names),
            };
            let result = command.run(ctx).await;
            if let Some(bytes) = result.peak_wasm_memory_bytes {
                exec.peak_wasm_memory_bytes = Some(
                    exec.peak_wasm_memory_bytes
                        .map_or(bytes, |current| current.max(bytes)),
                );
            }
            (stdout.bytes(), stderr.bytes(), result.exit_code)
        } else {
            (
                Vec::new(),
                format!("{command_name}: command not found\n").into_bytes(),
                127,
            )
        };
        let duration = started.elapsed();

        let mut status = status;
        let mut routed_stdout = Vec::new();
        let mut routed_stderr = Vec::new();
        for (destination, data) in [
            (&redirects.stdout, stdout_bytes.as_slice()),
            (&redirects.stderr, stderr_bytes.as_slice()),
        ] {
            match destination {
                OutputDestination::Capture(CaptureFd::Stdout) => {
                    routed_stdout.extend_from_slice(data)
                }
                OutputDestination::Capture(CaptureFd::Stderr) => {
                    routed_stderr.extend_from_slice(data)
                }
                OutputDestination::File(target) => {
                    if let Err(err) = fs.write_file(&target.path, data, true).await {
                        exec.stderr.extend_from_slice(
                            format!(
                                "{command_name}: {}: {}\n",
                                target.path,
                                errno_message(err.errno())
                            )
                            .as_bytes(),
                        );
                        status = 1;
                    }
                }
            }
        }
        stdout_bytes = routed_stdout;
        stderr_bytes = routed_stderr;

        exec.timings.push(CommandTiming {
            name: command_name,
            duration,
            exit_code: status,
        });
        exec.stderr.extend_from_slice(&stderr_bytes);
        if is_last {
            exec.stdout.extend_from_slice(&stdout_bytes);
            (Vec::new(), status)
        } else {
            (stdout_bytes, status)
        }
    }

    async fn run_shell_builtin(
        &self,
        name: &str,
        args: &[String],
        ctx: ShellBuiltinContext<'_>,
    ) -> Option<i32> {
        match name {
            "cd" => {
                if args.len() > 1 {
                    ctx.stderr.extend_from_slice(b"cd: too many arguments\n");
                    return Some(1);
                }
                let target = if let Some(target) = args.first() {
                    target.clone()
                } else if let Some(home) = ctx.session.env.get("HOME") {
                    home.clone()
                } else {
                    ctx.stderr.extend_from_slice(b"cd: HOME not set\n");
                    return Some(1);
                };
                let path = ctx.fs.resolve(&target);
                match ctx.fs.stat(&path).await {
                    Ok(Metadata {
                        file_type: FileType::Directory,
                        ..
                    }) => {
                        let old_pwd = ctx.session.cwd.clone();
                        ctx.session.cwd = path;
                        ctx.session.env.insert("OLDPWD".to_owned(), old_pwd.clone());
                        ctx.session
                            .env
                            .insert("PWD".to_owned(), ctx.session.cwd.clone());
                        ctx.env.insert("OLDPWD".to_owned(), old_pwd);
                        ctx.env.insert("PWD".to_owned(), ctx.session.cwd.clone());
                        Some(0)
                    }
                    Ok(_) => {
                        ctx.stderr.extend_from_slice(
                            format!("cd: {target}: Not a directory\n").as_bytes(),
                        );
                        Some(1)
                    }
                    Err(err) => {
                        ctx.stderr.extend_from_slice(
                            format!("cd: {target}: {}\n", errno_message(err.errno())).as_bytes(),
                        );
                        Some(1)
                    }
                }
            }
            "export" => {
                if args.is_empty() {
                    // Tinysandbox tracks one session environment, not Bash's exported
                    // bit, so listing shows every session variable.
                    for (key, value) in &ctx.session.env {
                        ctx.stdout.extend_from_slice(
                            format!("declare -x {key}=\"{value}\"\n").as_bytes(),
                        );
                    }
                    return Some(0);
                }
                for arg in args {
                    if let Some((name, value)) = arg.split_once('=') {
                        if is_assignment_name(name) {
                            ctx.session.env.insert(name.to_owned(), value.to_owned());
                            ctx.env.insert(name.to_owned(), value.to_owned());
                        } else {
                            ctx.stderr.extend_from_slice(
                                format!("export: `{arg}': not a valid identifier\n").as_bytes(),
                            );
                            return Some(1);
                        }
                    } else if is_assignment_name(arg) {
                        ctx.session.env.entry(arg.clone()).or_default();
                    } else {
                        ctx.stderr.extend_from_slice(
                            format!("export: `{arg}': not a valid identifier\n").as_bytes(),
                        );
                        return Some(1);
                    }
                }
                Some(0)
            }
            "unset" => {
                for arg in args {
                    if is_assignment_name(arg) {
                        ctx.session.env.remove(arg);
                        ctx.env.remove(arg);
                    } else {
                        ctx.stderr.extend_from_slice(
                            format!("unset: `{arg}': not a valid identifier\n").as_bytes(),
                        );
                        return Some(1);
                    }
                }
                Some(0)
            }
            _ => None,
        }
    }

    fn session_snapshot(&self) -> Session {
        self.session
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone()
    }

    fn store_session(&self, session: Session) {
        *self.session.lock().unwrap_or_else(PoisonError::into_inner) = session;
    }
}

impl SandboxBuilder {
    fn new() -> Self {
        let mut commands = BTreeMap::new();
        builtins::register(&mut commands);
        #[cfg(feature = "js")]
        crate::js::register(&mut commands);
        commands.insert(
            "cd".to_owned(),
            Arc::new(|_ctx: CommandContext| {
                Box::pin(async { CommandResult::success() }) as CommandFuture
            }),
        );
        commands.insert(
            "export".to_owned(),
            Arc::new(|_ctx: CommandContext| {
                Box::pin(async { CommandResult::success() }) as CommandFuture
            }),
        );
        commands.insert(
            "unset".to_owned(),
            Arc::new(|_ctx: CommandContext| {
                Box::pin(async { CommandResult::success() }) as CommandFuture
            }),
        );

        let mut env = BTreeMap::new();
        env.insert("PWD".to_owned(), "/".to_owned());
        Self {
            vfs: Arc::new(InMemoryVfs::default()),
            commands,
            limits: Limits::default(),
            cwd: "/".to_owned(),
            env,
        }
    }

    pub fn vfs(mut self, vfs: impl Vfs + 'static) -> Self {
        self.vfs = Arc::new(vfs);
        self
    }

    pub fn vfs_arc(mut self, vfs: Arc<dyn Vfs>) -> Self {
        self.vfs = vfs;
        self
    }

    pub fn command<F, Fut>(mut self, name: impl Into<String>, command: F) -> Self
    where
        F: Fn(CommandContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = CommandResult> + Send + 'static,
    {
        let name = name.into();
        assert_not_reserved(&name);
        self.commands.insert(name, Arc::new(command));
        self
    }

    pub fn command_obj(mut self, name: impl Into<String>, command: impl Command + 'static) -> Self {
        let name = name.into();
        assert_not_reserved(&name);
        self.commands.insert(name, Arc::new(command));
        self
    }

    pub fn limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    pub fn env(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.env.insert(name.into(), value.into());
        self
    }

    pub fn cwd(mut self, cwd: impl Into<String>) -> Self {
        self.cwd = normalize_absolute(cwd.into());
        self.env.insert("PWD".to_owned(), self.cwd.clone());
        self
    }

    pub fn build(self) -> Sandbox {
        let command_names = Arc::new(self.commands.keys().cloned().collect());
        Sandbox {
            vfs: self.vfs,
            commands: Arc::new(self.commands),
            command_names,
            limits: self.limits,
            session: Mutex::new(Session {
                cwd: self.cwd,
                env: self.env,
                last_status: 0,
            }),
            commands_run: AtomicU64::new(0),
        }
    }
}

#[derive(Debug, Clone)]
struct Session {
    cwd: String,
    env: BTreeMap<String, String>,
    last_status: i32,
}

struct ShellBuiltinContext<'a> {
    session: &'a mut Session,
    fs: &'a Fs,
    env: &'a mut BTreeMap<String, String>,
    stdout: &'a mut Vec<u8>,
    stderr: &'a mut Vec<u8>,
}

struct ExecState {
    stdout: Vec<u8>,
    stderr: Vec<u8>,
    timings: Vec<CommandTiming>,
    pipe_bytes: Vec<usize>,
    last_status: i32,
    command_count: usize,
    limit_hit: bool,
    peak_wasm_memory_bytes: Option<usize>,
}

impl ExecState {
    fn new(last_status: i32) -> Self {
        Self {
            stdout: Vec::new(),
            stderr: Vec::new(),
            timings: Vec::new(),
            pipe_bytes: Vec::new(),
            last_status,
            command_count: 0,
            limit_hit: false,
            peak_wasm_memory_bytes: None,
        }
    }
}

impl ExecMetrics {
    fn empty() -> Self {
        Self {
            wall_time: Duration::ZERO,
            commands: Vec::new(),
            pipe_bytes: Vec::new(),
            stdout_truncated: false,
            stderr_truncated: false,
            peak_wasm_memory_bytes: None,
        }
    }
}

#[derive(Debug, Clone)]
struct PreparedRedirects {
    stdin: Option<Vec<u8>>,
    stdout: OutputDestination,
    stderr: OutputDestination,
}

#[derive(Debug, Clone)]
enum OutputDestination {
    Capture(CaptureFd),
    File(RedirectFile),
}

#[derive(Debug, Clone, Copy)]
enum CaptureFd {
    Stdout,
    Stderr,
}

#[derive(Debug, Clone)]
struct RedirectFile {
    path: String,
}

async fn prepare_redirects(
    simple: &SimpleCommand,
    fs: &Fs,
    env: &BTreeMap<String, String>,
    last_status: i32,
) -> Result<PreparedRedirects, (String, VfsError)> {
    let mut redirects = PreparedRedirects {
        stdin: None,
        stdout: OutputDestination::Capture(CaptureFd::Stdout),
        stderr: OutputDestination::Capture(CaptureFd::Stderr),
    };

    for redirect in &simple.redirects {
        match &redirect.target {
            RedirectTarget::Fd(fd) => apply_fd_redirect(&mut redirects, redirect, *fd)?,
            RedirectTarget::Word(word) => {
                let path = redirect_target(word, env, last_status)?;
                match (
                    redirect.fd.unwrap_or(default_redirect_fd(redirect.op)),
                    redirect.op,
                ) {
                    (0, RedirectOp::Read) => {
                        redirects.stdin =
                            Some(fs.read_file(&path).await.map_err(|err| (path, err))?);
                    }
                    (1, RedirectOp::Write) => {
                        preflight_output(fs, &path, false).await?;
                        redirects.stdout = OutputDestination::File(RedirectFile { path });
                    }
                    (1, RedirectOp::Append) => {
                        preflight_output(fs, &path, true).await?;
                        redirects.stdout = OutputDestination::File(RedirectFile { path });
                    }
                    (2, RedirectOp::Write) => {
                        preflight_output(fs, &path, false).await?;
                        redirects.stderr = OutputDestination::File(RedirectFile { path });
                    }
                    (2, RedirectOp::Append) => {
                        preflight_output(fs, &path, true).await?;
                        redirects.stderr = OutputDestination::File(RedirectFile { path });
                    }
                    (_, _) => return Err((path, VfsError::new(Errno::EINVAL))),
                }
            }
        }
    }
    Ok(redirects)
}

async fn preflight_output(fs: &Fs, path: &str, append: bool) -> Result<(), (String, VfsError)> {
    fs.write_file(path, &[], append)
        .await
        .map_err(|err| (path.to_owned(), err))
}

fn apply_fd_redirect(
    redirects: &mut PreparedRedirects,
    redirect: &Redirect,
    target_fd: u32,
) -> Result<(), (String, VfsError)> {
    let fd = redirect.fd.unwrap_or(1);
    if !matches!(fd, 1 | 2) || !matches!(target_fd, 1 | 2) {
        return Err((target_fd.to_string(), VfsError::new(Errno::EINVAL)));
    }
    let target = match target_fd {
        1 => redirects.stdout.clone(),
        2 => redirects.stderr.clone(),
        _ => unreachable!("validated target fd"),
    };
    match fd {
        1 => redirects.stdout = target,
        2 => redirects.stderr = target,
        _ => unreachable!("validated source fd"),
    }
    Ok(())
}

fn default_redirect_fd(op: RedirectOp) -> u32 {
    match op {
        RedirectOp::Read => 0,
        RedirectOp::Write | RedirectOp::Append => 1,
    }
}

fn redirect_target(
    word: &Word,
    env: &BTreeMap<String, String>,
    last_status: i32,
) -> Result<String, (String, VfsError)> {
    let words = expand_word(word, env, last_status);
    match words.as_slice() {
        [path] => Ok(path.clone()),
        [] => Err((String::new(), VfsError::new(Errno::ENOENT))),
        [first, ..] => Err((first.clone(), VfsError::new(Errno::EINVAL))),
    }
}

fn expand_assignments(
    assignments: &[crate::shell::Assignment],
    env: &BTreeMap<String, String>,
    last_status: i32,
) -> Vec<(String, String)> {
    assignments
        .iter()
        .map(|assignment| {
            (
                assignment.name.clone(),
                expand_assignment_value(&assignment.value, env, last_status),
            )
        })
        .collect()
}

fn expand_words(words: &[Word], env: &BTreeMap<String, String>, last_status: i32) -> Vec<String> {
    words
        .iter()
        .flat_map(|word| expand_word(word, env, last_status))
        .collect()
}

fn expand_word(word: &Word, env: &BTreeMap<String, String>, last_status: i32) -> Vec<String> {
    let mut fields = vec![String::new()];
    let mut produced = false;
    for segment in &word.segments {
        match segment {
            Segment::Literal { value, .. } => {
                produced = true;
                fields.last_mut().expect("field exists").push_str(value);
            }
            Segment::Expansion { name, quoted: true } => {
                produced = true;
                fields
                    .last_mut()
                    .expect("field exists")
                    .push_str(&expansion_value(name, env, last_status));
            }
            Segment::Expansion {
                name,
                quoted: false,
            } => {
                let value = expansion_value(name, env, last_status);
                let parts: Vec<_> = value.split_whitespace().collect();
                if parts.is_empty() {
                    if !value.is_empty() && fields.last().is_some_and(|field| !field.is_empty()) {
                        fields.push(String::new());
                    }
                    continue;
                }
                produced = true;
                if value.chars().next().is_some_and(char::is_whitespace)
                    && fields.last().is_some_and(|field| !field.is_empty())
                {
                    fields.push(String::new());
                }
                fields.last_mut().expect("field exists").push_str(parts[0]);
                for part in parts.into_iter().skip(1) {
                    fields.push(part.to_owned());
                }
                if value.chars().last().is_some_and(char::is_whitespace) {
                    fields.push(String::new());
                }
            }
        }
    }
    if !produced {
        return Vec::new();
    }
    while fields.last().is_some_and(String::is_empty) && fields.len() > 1 {
        fields.pop();
    }
    fields
}

fn expand_assignment_value(
    word: &Word,
    env: &BTreeMap<String, String>,
    last_status: i32,
) -> String {
    let mut out = String::new();
    for segment in &word.segments {
        match segment {
            Segment::Literal { value, .. } => out.push_str(value),
            Segment::Expansion { name, .. } => {
                out.push_str(&expansion_value(name, env, last_status))
            }
        }
    }
    out
}

fn expansion_value(name: &str, env: &BTreeMap<String, String>, last_status: i32) -> String {
    if name == "?" {
        last_status.to_string()
    } else {
        env.get(name).cloned().unwrap_or_default()
    }
}

fn assert_not_reserved(name: &str) {
    if matches!(name, "cd" | "export" | "unset") {
        panic!(
            "SandboxBuilder::command cannot register reserved shell builtin '{name}'; cd, export, and unset are interpreted by the shell"
        );
    }
}

fn is_assignment_name(name: &str) -> bool {
    let mut chars = name.chars();
    matches!(chars.next(), Some(ch) if ch.is_ascii_alphabetic() || ch == '_')
        && chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_')
}

fn truncate_output(mut bytes: Vec<u8>, cap: usize) -> (Vec<u8>, bool) {
    if bytes.len() <= cap {
        return (bytes, false);
    }
    let marker = b"\n[tinysandbox: output truncated]\n";
    if cap <= marker.len() {
        return (marker.to_vec(), true);
    }
    let keep = cap - marker.len();
    let head = keep / 2;
    let tail = keep - head;
    let mut out = Vec::with_capacity(cap);
    out.extend_from_slice(&bytes[..head]);
    out.extend_from_slice(marker);
    out.extend_from_slice(&bytes[bytes.len() - tail..]);
    bytes.clear();
    (out, true)
}