wasmsh-vm 0.7.0

Cooperative virtual machine for executing wasmsh IR
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
//! Cooperative virtual machine for the wasmsh executor subset.
//!
//! The full shell still runs primarily in `wasmsh-runtime`. This VM is
//! used for the lowered IR subset and provides the shared budgeting,
//! cancellation, diagnostics, and output accounting primitives that the
//! runtime also relies on for resumable execution.

pub mod pipe;

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use wasmsh_ast::Word;
use wasmsh_builtins::{BuiltinContext, BuiltinRegistry, VecSink as BuiltinSink};
use wasmsh_ir::{Ir, IrProgram, IrRedirection};
use wasmsh_state::ShellState;

/// Outcome of VM execution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StepResult {
    /// The program completed with the given exit code.
    Done(i32),
    /// The step budget was exhausted; the caller should yield.
    Yield,
    /// Execution was cancelled externally.
    Cancelled,
    /// Output byte limit was exceeded.
    OutputLimitExceeded,
}

/// Configurable execution limits.
#[derive(Debug, Clone, Default)]
pub struct ExecutionLimits {
    /// Maximum VM steps (0 = unlimited).
    pub step_limit: u64,
    /// Maximum bytes of combined stdout+stderr output (0 = unlimited).
    pub output_byte_limit: u64,
    /// Maximum bytes buffered in pipes/streaming buffers (0 = unlimited).
    pub pipe_byte_limit: u64,
    /// Maximum nested execution depth (0 = unlimited).
    pub recursion_limit: u32,
}

/// A structured diagnostic event emitted during execution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiagnosticEvent {
    pub level: DiagLevel,
    pub category: DiagCategory,
    pub message: String,
}

/// Diagnostic severity.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagLevel {
    Trace,
    Info,
    Warning,
    Error,
}

/// Category of diagnostic event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagCategory {
    Parse,
    Expansion,
    Runtime,
    Filesystem,
    Builtin,
    Budget,
}

/// Structured budget category tracked during execution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BudgetCategory {
    Steps,
    VisibleOutputBytes,
    PipeBytes,
    RecursionDepth,
}

/// Stable exhaustion reason for a specific tracked budget.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExhaustionReason {
    pub category: BudgetCategory,
    pub used: u64,
    pub limit: u64,
}

impl ExhaustionReason {
    #[must_use]
    pub fn diagnostic_message(&self) -> String {
        match self.category {
            BudgetCategory::Steps => {
                format!(
                    "step budget exhausted: {} steps (limit: {})",
                    self.used, self.limit
                )
            }
            BudgetCategory::VisibleOutputBytes => format!(
                "output limit exceeded: {} bytes (limit: {})",
                self.used, self.limit
            ),
            BudgetCategory::PipeBytes => format!(
                "pipe buffer limit exceeded: {} bytes (limit: {})",
                self.used, self.limit
            ),
            BudgetCategory::RecursionDepth => format!(
                "maximum recursion depth exceeded: {} frames (limit: {})",
                self.used, self.limit
            ),
        }
    }
}

/// Structured stop reason for the current execution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StopReason {
    Exhausted(ExhaustionReason),
    Cancelled,
}

/// Shared budget accounting across the VM and runtime layers.
#[derive(Debug, Clone, Default)]
pub struct BudgetTracker {
    pub steps: u64,
    pub visible_output_bytes: u64,
    pub pipe_bytes: u64,
    pub recursion_depth: u32,
    stop_reason: Option<StopReason>,
}

impl BudgetTracker {
    #[must_use]
    pub fn stop_reason(&self) -> Option<&StopReason> {
        self.stop_reason.as_ref()
    }

    pub fn clear_stop_reason(&mut self) {
        self.stop_reason = None;
    }

    fn exhaust(&mut self, reason: ExhaustionReason) -> ExhaustionReason {
        self.stop_reason = Some(StopReason::Exhausted(reason.clone()));
        reason
    }

    pub fn note_cancelled(&mut self) {
        self.stop_reason = Some(StopReason::Cancelled);
    }

    pub fn begin_step(&mut self, limit: u64) -> Result<(), ExhaustionReason> {
        if limit > 0 && self.steps >= limit {
            return Err(self.exhaust(ExhaustionReason {
                category: BudgetCategory::Steps,
                used: self.steps,
                limit,
            }));
        }
        self.steps += 1;
        Ok(())
    }

    pub fn track_visible_output(&mut self, bytes: u64, limit: u64) -> Result<(), ExhaustionReason> {
        self.visible_output_bytes = self.visible_output_bytes.saturating_add(bytes);
        if limit > 0 && self.visible_output_bytes > limit {
            return Err(self.exhaust(ExhaustionReason {
                category: BudgetCategory::VisibleOutputBytes,
                used: self.visible_output_bytes,
                limit,
            }));
        }
        Ok(())
    }

    pub fn set_pipe_bytes(&mut self, bytes: u64, limit: u64) -> Result<(), ExhaustionReason> {
        self.pipe_bytes = bytes;
        if limit > 0 && self.pipe_bytes > limit {
            return Err(self.exhaust(ExhaustionReason {
                category: BudgetCategory::PipeBytes,
                used: self.pipe_bytes,
                limit,
            }));
        }
        Ok(())
    }

    pub fn enter_recursion(&mut self, limit: u32) -> Result<(), ExhaustionReason> {
        self.recursion_depth = self.recursion_depth.saturating_add(1);
        if limit > 0 && self.recursion_depth > limit {
            return Err(self.exhaust(ExhaustionReason {
                category: BudgetCategory::RecursionDepth,
                used: self.recursion_depth as u64,
                limit: limit as u64,
            }));
        }
        Ok(())
    }

    pub fn exit_recursion(&mut self) {
        self.recursion_depth = self.recursion_depth.saturating_sub(1);
    }
}

/// A cancellation token that can be shared across threads.
#[derive(Debug, Clone)]
pub struct CancellationToken {
    flag: Arc<AtomicBool>,
}

impl CancellationToken {
    #[must_use]
    pub fn new() -> Self {
        Self {
            flag: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Signal cancellation.
    pub fn cancel(&self) {
        self.flag.store(true, Ordering::Relaxed);
    }

    /// Check whether cancellation was requested.
    #[must_use]
    pub fn is_cancelled(&self) -> bool {
        self.flag.load(Ordering::Relaxed)
    }

    /// Reset the cancellation flag.
    pub fn reset(&self) {
        self.flag.store(false, Ordering::Relaxed);
    }
}

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

/// The shell virtual machine.
#[allow(missing_debug_implementations)]
pub struct Vm {
    /// Shell state (variables, params, cwd, etc.).
    pub state: ShellState,
    /// Number of steps executed so far.
    pub steps: u64,
    /// Execution limits.
    pub limits: ExecutionLimits,
    /// Bytes of output produced so far.
    pub output_bytes: u64,
    /// Shared budget accounting and stable stop reasons.
    pub budget: BudgetTracker,
    /// Cancellation token.
    cancel: CancellationToken,
    /// Collected diagnostic events.
    pub diagnostics: Vec<DiagnosticEvent>,
    /// Builtin command registry.
    builtins: BuiltinRegistry,
    /// Collected stdout output from command execution.
    pub stdout: Vec<u8>,
    /// Collected stderr output from command execution.
    pub stderr: Vec<u8>,
}

pub trait VmExecutor {
    fn assign(&mut self, vm: &mut Vm, name: &str, value: Option<&Word>);

    fn execute_builtin(
        &mut self,
        vm: &mut Vm,
        name: &str,
        argv: &[Word],
        redirections: &[IrRedirection],
    ) -> i32;
}

struct BuiltinVmExecutor {
    builtins: BuiltinRegistry,
}

impl VmExecutor for BuiltinVmExecutor {
    fn assign(&mut self, vm: &mut Vm, name: &str, value: Option<&Word>) {
        let value = value.map_or_else(String::new, |word| {
            wasmsh_expand::expand_word(word, &mut vm.state)
        });
        vm.state.set_var(name.into(), value.into());
        vm.state.last_status = 0;
    }

    fn execute_builtin(
        &mut self,
        vm: &mut Vm,
        name: &str,
        argv: &[Word],
        _redirections: &[IrRedirection],
    ) -> i32 {
        let Some(builtin_fn) = self.builtins.get(name) else {
            vm.emit_diagnostic(
                DiagLevel::Error,
                DiagCategory::Builtin,
                format!("unknown builtin: {name}"),
            );
            vm.state.last_status = 127;
            return 127;
        };

        let expanded: Vec<String> = argv
            .iter()
            .map(|word| wasmsh_expand::expand_word(word, &mut vm.state))
            .collect();
        let argv_refs: Vec<&str> = expanded.iter().map(String::as_str).collect();
        let mut sink = BuiltinSink::default();
        let status = {
            let mut ctx = BuiltinContext {
                state: &mut vm.state,
                output: &mut sink,
                fs: None,
                stdin: None,
            };
            builtin_fn(&mut ctx, &argv_refs)
        };
        vm.write_streams(&sink.stdout, &sink.stderr);
        vm.state.last_status = status;
        status
    }
}

impl Vm {
    /// Create a new VM with the given state and limits.
    #[must_use]
    pub fn new(state: ShellState, step_budget: u64) -> Self {
        Self {
            state,
            steps: 0,
            limits: ExecutionLimits {
                step_limit: step_budget,
                ..ExecutionLimits::default()
            },
            output_bytes: 0,
            budget: BudgetTracker::default(),
            cancel: CancellationToken::new(),
            diagnostics: Vec::new(),
            builtins: BuiltinRegistry::new(),
            stdout: Vec::new(),
            stderr: Vec::new(),
        }
    }

    /// Create a VM with full execution limits.
    #[must_use]
    pub fn with_limits(state: ShellState, limits: ExecutionLimits) -> Self {
        Self {
            state,
            steps: 0,
            limits,
            output_bytes: 0,
            budget: BudgetTracker::default(),
            cancel: CancellationToken::new(),
            diagnostics: Vec::new(),
            builtins: BuiltinRegistry::new(),
            stdout: Vec::new(),
            stderr: Vec::new(),
        }
    }

    /// Emit a diagnostic event.
    pub fn emit_diagnostic(&mut self, level: DiagLevel, category: DiagCategory, message: String) {
        self.diagnostics.push(DiagnosticEvent {
            level,
            category,
            message,
        });
    }

    fn budget_stop(&mut self, result: StepResult, message: String) -> StepResult {
        self.emit_diagnostic(DiagLevel::Error, DiagCategory::Budget, message);
        result
    }

    #[must_use]
    pub fn stop_reason(&self) -> Option<&StopReason> {
        self.budget.stop_reason()
    }

    /// Track output bytes and check the limit. Returns true if within limits.
    pub fn track_output(&mut self, bytes: u64) -> bool {
        self.output_bytes += bytes;
        self.budget
            .track_visible_output(bytes, self.limits.output_byte_limit)
            .is_ok()
    }

    /// Append stdout bytes and update output accounting.
    pub fn write_stdout(&mut self, data: &[u8]) {
        self.stdout.extend_from_slice(data);
        self.track_output(data.len() as u64);
    }

    /// Append stderr bytes and update output accounting.
    pub fn write_stderr(&mut self, data: &[u8]) {
        self.stderr.extend_from_slice(data);
        self.track_output(data.len() as u64);
    }

    /// Append both stdout and stderr bytes and update output accounting once.
    pub fn write_streams(&mut self, stdout: &[u8], stderr: &[u8]) {
        self.stdout.extend_from_slice(stdout);
        self.stderr.extend_from_slice(stderr);
        self.track_output((stdout.len() + stderr.len()) as u64);
    }

    /// Check whether the accumulated output has exceeded the configured limit.
    pub fn check_output_limit(&mut self) -> Result<(), StepResult> {
        if let Some(StopReason::Exhausted(reason)) = self.stop_reason() {
            if reason.category == BudgetCategory::VisibleOutputBytes {
                return Err(
                    self.budget_stop(StepResult::OutputLimitExceeded, reason.diagnostic_message())
                );
            }
        }
        Ok(())
    }

    /// Consume one execution step using the VM's shared budget/cancel semantics.
    pub fn begin_step(&mut self) -> Result<(), StepResult> {
        if self.cancel.is_cancelled() {
            self.budget.note_cancelled();
            return Err(self.budget_stop(StepResult::Cancelled, "execution cancelled".to_string()));
        }
        self.check_output_limit()?;
        if let Err(reason) = self.budget.begin_step(self.limits.step_limit) {
            self.steps = self.budget.steps;
            return Err(self.budget_stop(StepResult::Yield, reason.diagnostic_message()));
        }
        self.steps = self.budget.steps;
        Ok(())
    }

    /// Get the cancellation token (can be cloned and shared).
    #[must_use]
    pub fn cancellation_token(&self) -> CancellationToken {
        self.cancel.clone()
    }

    /// Execute an IR program to completion (or until yield/cancel).
    pub fn run(&mut self, program: &IrProgram) -> StepResult {
        let builtins = std::mem::take(&mut self.builtins);
        let mut executor = BuiltinVmExecutor { builtins };
        let result = self.run_with_executor(program, &mut executor);
        self.builtins = executor.builtins;
        result
    }

    pub fn run_with_executor<E: VmExecutor>(
        &mut self,
        program: &IrProgram,
        executor: &mut E,
    ) -> StepResult {
        let mut pc = 0;
        let instructions = &program.instructions;

        while pc < instructions.len() {
            if let Err(stop) = self.begin_step() {
                return stop;
            }

            match &instructions[pc] {
                Ir::Assign { name, value } => {
                    executor.assign(self, name.as_str(), value.as_ref());
                }
                Ir::ExecuteBuiltin {
                    name,
                    argv,
                    redirections,
                } => {
                    let status = executor.execute_builtin(self, name, argv, redirections);
                    self.state.last_status = status;
                }
                Ir::JumpIfFailure { target } => {
                    if self.state.last_status != 0 {
                        pc = *target;
                        continue;
                    }
                }
                Ir::JumpIfSuccess { target } => {
                    if self.state.last_status == 0 {
                        pc = *target;
                        continue;
                    }
                }
                Ir::ReturnLastStatus => {
                    return StepResult::Done(self.state.last_status);
                }
                Ir::Return { status } => {
                    self.state.last_status = *status;
                    return StepResult::Done(*status);
                }
                Ir::Nop => {}
            }

            pc += 1;
        }

        StepResult::Done(self.state.last_status)
    }
}

impl Default for Vm {
    fn default() -> Self {
        Self::new(ShellState::new(), 0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wasmsh_ast::{RedirectionOp, Span, WordPart};

    #[derive(Default)]
    struct TestExecutor {
        seen_redirections: Vec<Vec<IrRedirection>>,
    }

    impl VmExecutor for TestExecutor {
        fn assign(&mut self, vm: &mut Vm, name: &str, value: Option<&Word>) {
            let value = value.map_or_else(String::new, |word| {
                wasmsh_expand::expand_word(word, &mut vm.state)
            });
            vm.state.set_var(name.into(), value.into());
            vm.state.last_status = 0;
        }

        fn execute_builtin(
            &mut self,
            vm: &mut Vm,
            name: &str,
            argv: &[Word],
            redirections: &[IrRedirection],
        ) -> i32 {
            self.seen_redirections.push(redirections.to_vec());
            let expanded: Vec<String> = argv
                .iter()
                .map(|word| wasmsh_expand::expand_word(word, &mut vm.state))
                .collect();
            let status = match name {
                "echo" => {
                    let text = expanded[1..].join(" ");
                    vm.write_stdout(format!("{text}\n").as_bytes());
                    0
                }
                "true" => 0,
                "false" => 1,
                _ => 127,
            };
            vm.state.last_status = status;
            status
        }
    }

    #[test]
    fn run_empty_program() {
        let mut vm = Vm::default();
        let prog = IrProgram::new(vec![]);
        assert_eq!(vm.run(&prog), StepResult::Done(0));
    }

    #[test]
    fn run_return() {
        let mut vm = Vm::default();
        let prog = IrProgram::new(vec![Ir::Return { status: 42 }]);
        assert_eq!(vm.run(&prog), StepResult::Done(42));
        assert_eq!(vm.state.last_status, 42);
    }

    #[test]
    fn run_set_var() {
        let mut vm = Vm::default();
        let prog = IrProgram::new(vec![
            Ir::Assign {
                name: "FOO".into(),
                value: Some(literal_word("bar")),
            },
            Ir::Return { status: 0 },
        ]);
        assert_eq!(vm.run(&prog), StepResult::Done(0));
        assert_eq!(vm.state.get_var("FOO").unwrap(), "bar");
    }

    #[test]
    fn run_builtin_placeholder() {
        let mut vm = Vm::default();
        let prog = IrProgram::new(vec![
            Ir::ExecuteBuiltin {
                name: "echo".into(),
                argv: vec![literal_word("echo"), literal_word("hello")],
                redirections: Vec::new(),
            },
            Ir::Return { status: 0 },
        ]);
        assert_eq!(vm.run(&prog), StepResult::Done(0));
        assert_eq!(String::from_utf8(vm.stdout).unwrap(), "hello\n");
    }

    #[test]
    fn step_counting() {
        let mut vm = Vm::default();
        let prog = IrProgram::new(vec![Ir::Nop, Ir::Nop, Ir::Nop]);
        vm.run(&prog);
        assert_eq!(vm.steps, 3);
    }

    #[test]
    fn step_budget_yield() {
        let mut vm = Vm::new(ShellState::new(), 2);
        let prog = IrProgram::new(vec![Ir::Nop, Ir::Nop, Ir::Nop, Ir::Nop]);
        assert_eq!(vm.run(&prog), StepResult::Yield);
        assert_eq!(vm.steps, 2);
    }

    #[test]
    fn output_limit() {
        let mut vm = Vm::with_limits(
            ShellState::new(),
            ExecutionLimits {
                step_limit: 0,
                output_byte_limit: 10,
                ..ExecutionLimits::default()
            },
        );
        assert!(vm.track_output(5));
        assert!(vm.track_output(5));
        assert!(!vm.track_output(1));
    }

    #[test]
    fn diagnostics_collected() {
        let mut vm = Vm::default();
        vm.emit_diagnostic(
            DiagLevel::Warning,
            DiagCategory::Budget,
            "step limit approaching".into(),
        );
        assert_eq!(vm.diagnostics.len(), 1);
        assert_eq!(vm.diagnostics[0].level, DiagLevel::Warning);
        assert_eq!(vm.diagnostics[0].category, DiagCategory::Budget);
    }

    #[test]
    fn cancellation() {
        let mut vm = Vm::default();
        let token = vm.cancellation_token();
        token.cancel();
        let prog = IrProgram::new(vec![Ir::Nop]);
        assert_eq!(vm.run(&prog), StepResult::Cancelled);
        assert!(vm
            .diagnostics
            .iter()
            .any(|d| d.message.contains("execution cancelled")));
    }

    #[test]
    fn cancellation_token_reset() {
        let token = CancellationToken::new();
        assert!(!token.is_cancelled());
        token.cancel();
        assert!(token.is_cancelled());
        token.reset();
        assert!(!token.is_cancelled());
    }

    #[test]
    fn status_propagation() {
        let mut vm = Vm::default();
        let prog = IrProgram::new(vec![
            Ir::Assign {
                name: "X".into(),
                value: Some(literal_word("1")),
            },
            Ir::Return { status: 7 },
        ]);
        vm.run(&prog);
        assert_eq!(vm.state.last_status, 7);
        assert_eq!(vm.state.get_var("?").unwrap(), "7");
        assert_eq!(vm.state.get_var("X").unwrap(), "1");
    }

    #[test]
    fn begin_step_matches_vm_budget_semantics() {
        let mut vm = Vm::new(ShellState::new(), 1);
        assert_eq!(vm.begin_step(), Ok(()));
        assert_eq!(vm.steps, 1);
        assert_eq!(vm.begin_step(), Err(StepResult::Yield));
        assert!(vm
            .diagnostics
            .iter()
            .any(|d| d.message.contains("step budget exhausted")));
    }

    #[test]
    fn output_limit_is_reported_through_begin_step() {
        let mut vm = Vm::with_limits(
            ShellState::new(),
            ExecutionLimits {
                step_limit: 0,
                output_byte_limit: 3,
                ..ExecutionLimits::default()
            },
        );
        vm.write_stdout(b"four");
        assert_eq!(vm.begin_step(), Err(StepResult::OutputLimitExceeded));
        assert!(vm
            .diagnostics
            .iter()
            .any(|d| d.message.contains("output limit exceeded")));
    }

    #[test]
    fn step_limit_exposes_structured_stop_reason() {
        let mut vm = Vm::new(ShellState::new(), 1);
        assert_eq!(vm.begin_step(), Ok(()));
        assert_eq!(vm.begin_step(), Err(StepResult::Yield));
        assert_eq!(
            vm.stop_reason(),
            Some(&StopReason::Exhausted(ExhaustionReason {
                category: BudgetCategory::Steps,
                used: 1,
                limit: 1,
            }))
        );
    }

    #[test]
    fn cancellation_remains_distinct_from_budget_exhaustion() {
        let mut vm = Vm::default();
        vm.cancellation_token().cancel();
        assert_eq!(vm.begin_step(), Err(StepResult::Cancelled));
        assert_eq!(vm.stop_reason(), Some(&StopReason::Cancelled));
    }

    #[test]
    fn budget_tracker_tracks_pipe_and_recursion_limits() {
        let mut tracker = BudgetTracker::default();
        let pipe = tracker.set_pipe_bytes(9, 8).unwrap_err();
        assert_eq!(pipe.category, BudgetCategory::PipeBytes);
        assert_eq!(pipe.limit, 8);

        let mut tracker = BudgetTracker::default();
        tracker.enter_recursion(2).unwrap();
        tracker.enter_recursion(2).unwrap();
        let recursion = tracker.enter_recursion(2).unwrap_err();
        assert_eq!(recursion.category, BudgetCategory::RecursionDepth);
        assert_eq!(recursion.used, 3);
    }

    #[test]
    fn run_assignment_and_expanding_builtin_with_executor() {
        let mut vm = Vm::default();
        let mut executor = TestExecutor::default();
        let prog = IrProgram::new(vec![
            Ir::Assign {
                name: "FOO".into(),
                value: Some(literal_word("bar")),
            },
            Ir::ExecuteBuiltin {
                name: "echo".into(),
                argv: vec![literal_word("echo"), parameter_word("FOO")],
                redirections: Vec::new(),
            },
            Ir::ReturnLastStatus,
        ]);
        assert_eq!(
            vm.run_with_executor(&prog, &mut executor),
            StepResult::Done(0)
        );
        assert_eq!(vm.state.get_var("FOO").unwrap(), "bar");
        assert_eq!(String::from_utf8(vm.stdout).unwrap(), "bar\n");
    }

    #[test]
    fn jump_if_failure_skips_rhs_of_and_list() {
        let mut vm = Vm::default();
        let mut executor = TestExecutor::default();
        let prog = IrProgram::new(vec![
            Ir::ExecuteBuiltin {
                name: "false".into(),
                argv: vec![literal_word("false")],
                redirections: Vec::new(),
            },
            Ir::JumpIfFailure { target: 3 },
            Ir::ExecuteBuiltin {
                name: "echo".into(),
                argv: vec![literal_word("echo"), literal_word("nope")],
                redirections: Vec::new(),
            },
            Ir::ReturnLastStatus,
        ]);
        assert_eq!(
            vm.run_with_executor(&prog, &mut executor),
            StepResult::Done(1)
        );
        assert!(vm.stdout.is_empty());
    }

    #[test]
    fn jump_if_success_skips_rhs_of_or_list() {
        let mut vm = Vm::default();
        let mut executor = TestExecutor::default();
        let prog = IrProgram::new(vec![
            Ir::ExecuteBuiltin {
                name: "true".into(),
                argv: vec![literal_word("true")],
                redirections: Vec::new(),
            },
            Ir::JumpIfSuccess { target: 3 },
            Ir::ExecuteBuiltin {
                name: "echo".into(),
                argv: vec![literal_word("echo"), literal_word("nope")],
                redirections: Vec::new(),
            },
            Ir::ReturnLastStatus,
        ]);
        assert_eq!(
            vm.run_with_executor(&prog, &mut executor),
            StepResult::Done(0)
        );
        assert!(vm.stdout.is_empty());
    }

    #[test]
    fn executor_receives_redirection_plan() {
        let mut vm = Vm::default();
        let mut executor = TestExecutor::default();
        let prog = IrProgram::new(vec![
            Ir::ExecuteBuiltin {
                name: "echo".into(),
                argv: vec![literal_word("echo"), literal_word("hello")],
                redirections: vec![IrRedirection {
                    fd: None,
                    op: RedirectionOp::Output,
                    target: literal_word("/out.txt"),
                    here_doc_body: None,
                }],
            },
            Ir::ReturnLastStatus,
        ]);
        assert_eq!(
            vm.run_with_executor(&prog, &mut executor),
            StepResult::Done(0)
        );
        assert_eq!(executor.seen_redirections.len(), 1);
        assert_eq!(executor.seen_redirections[0][0].op, RedirectionOp::Output);
    }

    fn literal_word(text: &str) -> Word {
        Word {
            parts: vec![WordPart::Literal(text.into())],
            span: Span { start: 0, end: 0 },
        }
    }

    fn parameter_word(name: &str) -> Word {
        Word {
            parts: vec![WordPart::Parameter(name.into())],
            span: Span { start: 0, end: 0 },
        }
    }
}