Skip to main content

harn_vm/vm/
state.rs

1use std::collections::{BTreeMap, HashMap, HashSet};
2use std::sync::Arc;
3use std::time::Instant;
4
5use crate::chunk::{Chunk, ChunkRef, Constant};
6use crate::runtime_limits::RuntimeLimits;
7use crate::value::{
8    ModuleFunctionRegistry, VmAsyncBuiltinFn, VmBuiltinFn, VmEnv, VmError, VmTaskHandle, VmValue,
9};
10use crate::BuiltinId;
11
12use super::debug::DebugHook;
13use super::modules::LoadedModule;
14use super::VmBuiltinMetadata;
15
16/// RAII guard that starts a tracing span on creation and ends it on drop.
17pub(crate) struct ScopeSpan(u64);
18
19impl ScopeSpan {
20    pub(crate) fn new(kind: crate::tracing::SpanKind, name: String) -> Self {
21        Self(crate::tracing::span_start(kind, name))
22    }
23}
24
25impl Drop for ScopeSpan {
26    fn drop(&mut self) {
27        crate::tracing::span_end(self.0);
28    }
29}
30
31#[derive(Clone)]
32pub(crate) struct LocalSlot {
33    pub(crate) value: VmValue,
34    pub(crate) initialized: bool,
35    pub(crate) synced: bool,
36}
37
38impl Drop for LocalSlot {
39    fn drop(&mut self) {
40        // Slot locals hold script values directly (e.g. a `var` bound to a
41        // deeply nested list). When a frame is torn down, the default
42        // recursive drop of such a value would overflow the native stack and
43        // abort the process. For the overwhelmingly common scalar slot this is
44        // a single `matches!` check and then the normal trivial drop; only a
45        // nested container is moved out and torn down iteratively, so hot
46        // frame teardown is unaffected.
47        if crate::value::recursion::is_recursive_container(&self.value) {
48            crate::value::recursion::dismantle(std::mem::replace(&mut self.value, VmValue::Nil));
49        }
50    }
51}
52
53#[derive(Clone)]
54pub(crate) struct InterruptHandler {
55    pub(crate) handle: i64,
56    pub(crate) signals: Vec<String>,
57    pub(crate) once: bool,
58    pub(crate) graceful_timeout_ms: Option<u64>,
59    pub(crate) handler: VmValue,
60}
61
62/// Call frame for function execution.
63pub(crate) struct CallFrame {
64    pub(crate) chunk: ChunkRef,
65    pub(crate) ip: usize,
66    pub(crate) stack_base: usize,
67    pub(crate) saved_env: VmEnv,
68    /// Env snapshot captured at call-time, *after* argument binding. Used
69    /// by the debugger's `restartFrame` to rewind this frame to its
70    /// entry state (re-binding args from the original values) without
71    /// re-entering the call site. Cheap to clone because `VmEnv` is
72    /// already cloned into `saved_env` on every call. `None` for
73    /// scratch frames (evaluate, import init) where restart isn't
74    /// meaningful.
75    pub(crate) initial_env: Option<VmEnv>,
76    pub(crate) initial_local_slots: Option<Vec<LocalSlot>>,
77    /// Iterator stack depth to restore when this frame unwinds.
78    pub(crate) saved_iterator_depth: usize,
79    /// Function name for stack traces (empty for top-level pipeline).
80    pub(crate) fn_name: String,
81    /// Number of arguments actually passed by the caller (for default arg support).
82    pub(crate) argc: usize,
83    /// Saved VM_SOURCE_DIR to restore when this frame is popped.
84    /// Set when entering a closure that originated from an imported module.
85    pub(crate) saved_source_dir: Option<std::path::PathBuf>,
86    /// Module-local named functions available to symbolic calls within this frame.
87    pub(crate) module_functions: Option<ModuleFunctionRegistry>,
88    /// Shared module-level env for top-level `var` / `let` bindings of
89    /// this frame's originating module. Looked up after `self.env` and
90    /// before `self.globals` by `GetVar` / `SetVar`, giving each module
91    /// its own live static state that persists across calls. See the
92    /// `module_state` field on `VmClosure` for the full rationale.
93    pub(crate) module_state: Option<crate::value::ModuleState>,
94    /// Slot-indexed locals for compiler-resolved names in this frame.
95    pub(crate) local_slots: Vec<LocalSlot>,
96    /// Env scope index that corresponds to compiler local scope depth 0.
97    pub(crate) local_scope_base: usize,
98    /// Current compiler local scope depth, updated by PushScope/PopScope.
99    pub(crate) local_scope_depth: usize,
100}
101
102/// Exception handler for try/catch.
103pub(crate) struct ExceptionHandler {
104    pub(crate) catch_ip: usize,
105    pub(crate) stack_depth: usize,
106    pub(crate) frame_depth: usize,
107    pub(crate) env_scope_depth: usize,
108    /// When present, this catch only handles errors whose enum_name matches.
109    pub(crate) error_type: Option<crate::value::HarnStr>,
110}
111
112/// A structured-concurrency nursery (`scope { }`). Tasks spawned while this
113/// scope is innermost record their id here; `TaskScopeExit` joins them.
114pub(crate) struct TaskScope {
115    /// Ids of tasks spawned in this scope that have not been explicitly
116    /// `await`ed away. Joined (normal exit) or cancelled (unwind) on close.
117    pub(crate) task_ids: Vec<String>,
118    /// Frame depth at which the scope was opened, for unwind pruning.
119    pub(crate) frame_depth: usize,
120    /// Env scope depth at open, for unwind pruning.
121    pub(crate) env_scope_depth: usize,
122}
123
124/// Iterator state for for-in loops.
125pub(crate) enum IterState {
126    Vec {
127        items: Arc<Vec<VmValue>>,
128        idx: usize,
129    },
130    Dict {
131        entries: Arc<crate::value::DictMap>,
132        keys: Vec<String>,
133        idx: usize,
134    },
135    Channel {
136        receiver: std::sync::Arc<tokio::sync::Mutex<tokio::sync::mpsc::Receiver<VmValue>>>,
137        close: std::sync::Arc<crate::value::VmChannelCloseState>,
138    },
139    Generator {
140        gen: Arc<crate::value::VmGenerator>,
141    },
142    Stream {
143        stream: Arc<crate::value::VmStream>,
144    },
145    /// Step through a lazy range without materializing a Vec.
146    /// Inclusive ranges keep `end` as an actual value so `i64::MAX to i64::MAX`
147    /// still yields one item instead of overflowing a one-past-end sentinel.
148    Range {
149        next: i64,
150        end: i64,
151        inclusive: bool,
152        done: bool,
153    },
154    VmIter {
155        handle: crate::vm::iter::VmIterHandle,
156    },
157}
158
159#[derive(Clone)]
160pub(crate) enum VmBuiltinDispatch {
161    Sync(VmBuiltinFn),
162    Async(VmAsyncBuiltinFn),
163}
164
165#[derive(Clone)]
166pub(crate) struct VmBuiltinEntry {
167    pub(crate) name: Arc<str>,
168    pub(crate) dispatch: VmBuiltinDispatch,
169}
170
171/// The Harn bytecode virtual machine.
172pub struct Vm {
173    pub(crate) stack: Vec<VmValue>,
174    pub(crate) env: VmEnv,
175    pub(crate) output: String,
176    pub(crate) builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
177    pub(crate) async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
178    pub(crate) builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
179    /// Numeric side index for builtins. Name-keyed maps remain authoritative;
180    /// this index is the hot path for direct builtin bytecode and callback refs.
181    pub(crate) builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
182    /// IDs with detected name collisions. Collided names safely fall back to
183    /// the authoritative name-keyed lookup path.
184    pub(crate) builtin_id_collisions: Arc<HashSet<BuiltinId>>,
185    /// Iterator state for for-in loops.
186    pub(crate) iterators: Vec<IterState>,
187    /// Call frame stack.
188    pub(crate) frames: Vec<CallFrame>,
189    /// Exception handler stack.
190    pub(crate) exception_handlers: Vec<ExceptionHandler>,
191    /// Spawned async task handles.
192    pub(crate) spawned_tasks: BTreeMap<String, VmTaskHandle>,
193    /// Shared process-local synchronization primitives inherited by child VMs.
194    pub(crate) sync_runtime: Arc<crate::synchronization::VmSyncRuntime>,
195    /// Shared process-local cells, maps, and mailboxes inherited by child VMs.
196    pub(crate) shared_state_runtime: Arc<crate::shared_state::VmSharedStateRuntime>,
197    /// Per-isolate inline cache entries keyed by compiled chunk identity.
198    pub(crate) inline_caches: HashMap<u64, Vec<crate::chunk::InlineCacheEntry>>,
199    /// VM-scoped pool registry inherited by child VMs and scoped into Tokio tasks.
200    pub(crate) pool_registry: Arc<crate::stdlib::pool::PoolRegistry>,
201    /// Shared task/channel wait graph for this VM execution tree.
202    pub(crate) wait_for_graph: Arc<crate::wait_for_graph::VmWaitForGraph>,
203    /// Permits acquired by lexical synchronization blocks in this VM.
204    pub(crate) held_sync_guards: Vec<crate::synchronization::VmSyncHeldGuard>,
205    /// Locks held by an ancestor VM that is *suspended on this VM's execution*:
206    /// an inline async-builtin child runs while its parent is parked
207    /// mid-instruction still holding these permits. Re-acquiring more permits
208    /// than the primitive can grant is a provably-unresolvable self-deadlock, so
209    /// HARN-ORC-011 fires across the child boundary. Empty for new concurrent
210    /// tasks (`spawn`/`parallel`/triggers), where the parent keeps running and
211    /// blocking can be legitimately resolvable.
212    pub(crate) inherited_held_keys: Arc<Vec<crate::synchronization::VmSyncHeldKey>>,
213    /// Structured-concurrency nursery stack. Each `scope { }` block pushes a
214    /// `TaskScope`; tasks spawned while it is innermost register their id here.
215    /// On normal exit (`TaskScopeExit`) the scope's tasks are joined and the
216    /// first error propagates; on unwind they are cancelled. Modeled on
217    /// `held_sync_guards` (push on enter, prune/cancel on frame/handler exit).
218    pub(crate) task_scopes: Vec<TaskScope>,
219    /// Counter for generating unique task IDs.
220    pub(crate) task_counter: u64,
221    /// Counter for logical runtime-context task groups.
222    pub(crate) runtime_context_counter: u64,
223    /// Logical runtime task context visible through `runtime_context()`.
224    pub(crate) runtime_context: crate::runtime_context::RuntimeContext,
225    /// Active deadline stack: (deadline_instant, frame_depth).
226    pub(crate) deadlines: Vec<(Instant, usize)>,
227    /// Breakpoints, keyed by source-file path so a breakpoint at line N
228    /// in `auto.harn` doesn't also fire when execution hits line N in an
229    /// imported lib. The empty-string key is a wildcard used by callers
230    /// that don't track source paths (legacy `set_breakpoints` API).
231    pub(crate) breakpoints: BTreeMap<String, std::collections::BTreeSet<usize>>,
232    /// Function-name breakpoints. Any closure call whose
233    /// `CompiledFunction.name` matches an entry here raises a stop on
234    /// entry, regardless of the call site's file or line. Lets the IDE
235    /// break on `llm_call` / `host_run_pipeline` / any user pipeline
236    /// function without pinning down a source location first.
237    pub(crate) function_breakpoints: std::collections::BTreeSet<String>,
238    /// Latched on `push_closure_frame` when the callee's name matches
239    /// `function_breakpoints`; consumed by the next step so the stop is
240    /// reported with reason="function breakpoint" and the breakpoint
241    /// name available for the DAP `stopped` event.
242    pub(crate) pending_function_bp: Option<String>,
243    /// Whether the VM is in step mode.
244    pub(crate) step_mode: bool,
245    /// The frame depth at which stepping started (for step-over).
246    pub(crate) step_frame_depth: usize,
247    /// Whether the VM is currently stopped at a debug point.
248    pub(crate) stopped: bool,
249    /// Last source line executed (to detect line changes).
250    pub(crate) last_line: usize,
251    /// Source directory for resolving imports.
252    pub(crate) source_dir: Option<std::path::PathBuf>,
253    /// Modules currently being imported (cycle prevention).
254    pub(crate) imported_paths: Vec<std::path::PathBuf>,
255    /// Imports that hit an in-progress module (an import cycle) and so could
256    /// not bind inline. Drained by `flush_deferred_cyclic_imports` once the
257    /// involved modules finish loading.
258    pub(crate) deferred_cyclic_imports: Vec<super::modules::DeferredCyclicImport>,
259    /// Loaded module cache keyed by canonical or synthetic module path.
260    pub(crate) module_cache: Arc<BTreeMap<std::path::PathBuf, LoadedModule>>,
261    /// Source text keyed by canonical or synthetic module path for debugger retrieval.
262    pub(crate) source_cache: Arc<BTreeMap<std::path::PathBuf, String>>,
263    /// Source file path for error reporting.
264    pub(crate) source_file: Option<String>,
265    /// Source text for error reporting.
266    pub(crate) source_text: Option<String>,
267    /// Optional bridge for delegating unknown builtins in bridge mode.
268    pub(crate) bridge: Option<Arc<crate::bridge::HostBridge>>,
269    /// Builtins denied by sandbox mode (`--deny` / `--allow` flags).
270    pub(crate) denied_builtins: Arc<HashSet<String>>,
271    /// Cancellation token for cooperative graceful shutdown (set by parent).
272    pub(crate) cancel_token: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
273    pub(crate) interrupt_signal_token: Option<std::sync::Arc<std::sync::Mutex<Option<String>>>>,
274    /// Remaining instruction-boundary checks before a requested host
275    /// cancellation is forcefully raised. This gives `is_cancelled()` loops a
276    /// deterministic chance to return cleanly without letting non-cooperative
277    /// CPU-bound code run forever.
278    pub(crate) cancel_grace_instructions_remaining: Option<usize>,
279    /// User-visible interrupt handlers registered through `std/signal`.
280    pub(crate) interrupt_handlers: Vec<InterruptHandler>,
281    pub(crate) next_interrupt_handle: i64,
282    pub(crate) pending_interrupt_signal: Option<String>,
283    pub(crate) interrupted: bool,
284    pub(crate) dispatching_interrupt: bool,
285    pub(crate) interrupt_handler_deadline: Option<Instant>,
286    /// Captured stack trace from the most recent error (fn_name, line, col).
287    pub(crate) error_stack_trace: Vec<(String, usize, usize, Option<String>)>,
288    /// Yield channel sender for generator execution. When set, `Op::Yield`
289    /// sends values through this channel instead of being a no-op.
290    pub(crate) yield_sender: Option<tokio::sync::mpsc::Sender<Result<VmValue, VmError>>>,
291    /// Project root directory (detected via harn.toml).
292    /// Used as base directory for metadata, store, and checkpoint operations.
293    pub(crate) project_root: Option<std::path::PathBuf>,
294    /// Global constants (e.g. `pi`, `e`). Checked as a fallback in `GetVar`
295    /// after the environment, so user-defined variables can shadow them.
296    pub(crate) globals: Arc<crate::value::DictMap>,
297    /// Optional debugger hook invoked when execution advances to a new source line.
298    pub(crate) debug_hook: Option<parking_lot::Mutex<Box<DebugHook>>>,
299    /// Effective runtime ceilings for this VM execution.
300    pub(crate) runtime_limits: RuntimeLimits,
301}
302
303/// Reusable VM baseline for hosts that need many clean executions with the
304/// same stable builtin/source setup.
305///
306/// The baseline intentionally does not snapshot execution state. Each
307/// instantiation gets fresh stacks, frames, tasks, cancellation fields, sync
308/// primitives, shared cells/maps/mailboxes, and debug state. Builtin tables are
309/// shared through `Arc` until a per-execution rebind needs copy-on-write.
310#[derive(Clone)]
311pub struct VmBaseline {
312    builtins: Arc<BTreeMap<String, VmBuiltinFn>>,
313    async_builtins: Arc<BTreeMap<String, VmAsyncBuiltinFn>>,
314    builtin_metadata: Arc<BTreeMap<String, VmBuiltinMetadata>>,
315    builtins_by_id: Arc<HashMap<BuiltinId, VmBuiltinEntry>>,
316    builtin_id_collisions: Arc<HashSet<BuiltinId>>,
317    source_dir: Option<std::path::PathBuf>,
318    source_file: Option<String>,
319    source_text: Option<String>,
320    project_root: Option<std::path::PathBuf>,
321    globals: Arc<crate::value::DictMap>,
322    denied_builtins: Arc<HashSet<String>>,
323    runtime_limits: RuntimeLimits,
324}
325
326impl VmBaseline {
327    pub fn from_vm(vm: &Vm) -> Self {
328        Self {
329            builtins: Arc::clone(&vm.builtins),
330            async_builtins: Arc::clone(&vm.async_builtins),
331            builtin_metadata: Arc::clone(&vm.builtin_metadata),
332            builtins_by_id: Arc::clone(&vm.builtins_by_id),
333            builtin_id_collisions: Arc::clone(&vm.builtin_id_collisions),
334            source_dir: vm.source_dir.clone(),
335            source_file: vm.source_file.clone(),
336            source_text: vm.source_text.clone(),
337            project_root: vm.project_root.clone(),
338            globals: Arc::clone(&vm.globals),
339            denied_builtins: Arc::clone(&vm.denied_builtins),
340            runtime_limits: vm.runtime_limits,
341        }
342    }
343
344    pub fn instantiate(&self) -> Vm {
345        let mut source_cache = BTreeMap::new();
346        if let (Some(file), Some(text)) = (&self.source_file, &self.source_text) {
347            source_cache.insert(std::path::PathBuf::from(file), text.clone());
348        }
349        if let Some(dir) = &self.source_dir {
350            crate::stdlib::set_thread_source_dir(dir);
351        }
352
353        let mut vm = Vm {
354            stack: Vec::with_capacity(256),
355            env: VmEnv::new(),
356            output: String::new(),
357            builtins: Arc::clone(&self.builtins),
358            async_builtins: Arc::clone(&self.async_builtins),
359            builtin_metadata: Arc::clone(&self.builtin_metadata),
360            builtins_by_id: Arc::clone(&self.builtins_by_id),
361            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
362            iterators: Vec::new(),
363            frames: Vec::new(),
364            exception_handlers: Vec::new(),
365            spawned_tasks: BTreeMap::new(),
366            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
367            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
368            inline_caches: HashMap::new(),
369            pool_registry: crate::stdlib::pool::new_pool_registry(),
370            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
371            held_sync_guards: Vec::new(),
372            inherited_held_keys: Arc::new(Vec::new()),
373            task_scopes: Vec::new(),
374            task_counter: 0,
375            runtime_context_counter: 0,
376            runtime_context: crate::runtime_context::RuntimeContext::root(),
377            deadlines: Vec::new(),
378            breakpoints: BTreeMap::new(),
379            function_breakpoints: std::collections::BTreeSet::new(),
380            pending_function_bp: None,
381            step_mode: false,
382            step_frame_depth: 0,
383            stopped: false,
384            last_line: 0,
385            source_dir: self.source_dir.clone(),
386            imported_paths: Vec::new(),
387            deferred_cyclic_imports: Vec::new(),
388            module_cache: Arc::new(BTreeMap::new()),
389            source_cache: Arc::new(source_cache),
390            source_file: self.source_file.clone(),
391            source_text: self.source_text.clone(),
392            bridge: None,
393            denied_builtins: Arc::clone(&self.denied_builtins),
394            cancel_token: None,
395            interrupt_signal_token: None,
396            cancel_grace_instructions_remaining: None,
397            interrupt_handlers: Vec::new(),
398            next_interrupt_handle: 1,
399            pending_interrupt_signal: None,
400            interrupted: false,
401            dispatching_interrupt: false,
402            interrupt_handler_deadline: None,
403            error_stack_trace: Vec::new(),
404            yield_sender: None,
405            project_root: self.project_root.clone(),
406            globals: Arc::clone(&self.globals),
407            debug_hook: None,
408            runtime_limits: self.runtime_limits,
409        };
410
411        crate::stdlib::rebind_execution_state_builtins(&mut vm);
412        vm
413    }
414}
415
416impl Vm {
417    pub(crate) fn fresh_local_slots(chunk: &Chunk) -> Vec<LocalSlot> {
418        chunk
419            .local_slots
420            .iter()
421            .map(|_| LocalSlot {
422                value: VmValue::Nil,
423                initialized: false,
424                synced: false,
425            })
426            .collect()
427    }
428
429    pub(crate) fn bind_param_slots(
430        slots: &mut [LocalSlot],
431        func: &crate::chunk::CompiledFunction,
432        args: &[VmValue],
433        synced: bool,
434    ) {
435        Self::bind_param_slots_args(slots, func, &super::CallArgs::Slice(args), synced);
436    }
437
438    pub(crate) fn bind_param_slots_args(
439        slots: &mut [LocalSlot],
440        func: &crate::chunk::CompiledFunction,
441        args: &super::CallArgs<'_>,
442        synced: bool,
443    ) {
444        let param_count = func.params.len();
445        for (i, _param) in func.params.iter().enumerate() {
446            if i >= slots.len() {
447                break;
448            }
449            if func.has_rest_param && i == param_count - 1 {
450                let rest_args = args.to_vec_from(i);
451                slots[i].value = VmValue::List(std::sync::Arc::new(rest_args));
452                slots[i].initialized = true;
453                slots[i].synced = synced;
454            } else if let Some(arg) = args.get(i) {
455                slots[i].value = arg.clone();
456                slots[i].initialized = true;
457                slots[i].synced = synced;
458            }
459        }
460    }
461
462    pub(crate) fn visible_variables(&self) -> crate::value::DictMap {
463        let mut vars = self.env.all_variables();
464        let Some(frame) = self.frames.last() else {
465            return vars;
466        };
467        for (slot, info) in frame.local_slots.iter().zip(frame.chunk.local_slots.iter()) {
468            if slot.initialized && info.scope_depth <= frame.local_scope_depth {
469                vars.insert(crate::value::intern_key(&info.name), slot.value.clone());
470            }
471        }
472        vars
473    }
474
475    pub(crate) fn sync_current_frame_locals_to_env(&mut self) {
476        let frames = &mut self.frames;
477        let env = &mut self.env;
478        let Some(frame) = frames.last_mut() else {
479            return;
480        };
481        let local_scope_base = frame.local_scope_base;
482        let local_scope_depth = frame.local_scope_depth;
483        for (slot, info) in frame
484            .local_slots
485            .iter_mut()
486            .zip(frame.chunk.local_slots.iter())
487        {
488            if slot.initialized && !slot.synced && info.scope_depth <= local_scope_depth {
489                slot.synced = true;
490                let scope_idx = local_scope_base + info.scope_depth;
491                while env.scopes.len() <= scope_idx {
492                    env.push_scope();
493                }
494                Arc::make_mut(&mut env.scopes[scope_idx].vars)
495                    .insert(info.name.clone(), (slot.value.clone(), info.mutable));
496            }
497        }
498    }
499
500    pub(crate) fn closure_call_env_for_current_frame(
501        &self,
502        closure: &crate::value::VmClosure,
503    ) -> VmEnv {
504        if closure.module_state().is_some() {
505            return closure.env.cloned_for_call();
506        }
507        let call_env = Self::closure_call_env(&self.env, closure);
508        // Same compile-time short-circuit as the env walk in
509        // `closure_call_env`: when the callee body never resolves an
510        // outer name through the env, injecting closure-typed *slot*
511        // locals from the caller's frame is wasted work too.
512        if !closure.func.chunk.references_outer_names {
513            return call_env;
514        }
515        let mut call_env = call_env;
516        let Some(frame) = self.frames.last() else {
517            return call_env;
518        };
519        for (slot, info) in frame
520            .local_slots
521            .iter()
522            .zip(frame.chunk.local_slots.iter())
523            .filter(|(slot, info)| slot.initialized && info.scope_depth <= frame.local_scope_depth)
524        {
525            if matches!(slot.value, VmValue::Closure(_)) && !call_env.contains(&info.name) {
526                let _ = call_env.define(&info.name, slot.value.clone(), info.mutable);
527            }
528        }
529        call_env
530    }
531
532    pub(crate) fn active_local_slot_value(&self, name: &str) -> Option<VmValue> {
533        let frame = self.frames.last()?;
534        let idx = self.active_local_slot_index(name)?;
535        frame.local_slots.get(idx).map(|slot| slot.value.clone())
536    }
537
538    /// Returns the slot index of an initialized active local with the given
539    /// name, walking from innermost to outermost scope. Used by legacy by-name
540    /// hot paths that still want to mutate the slot value in place without
541    /// paying a defensive `VmValue::clone` first.
542    pub(crate) fn active_local_slot_index(&self, name: &str) -> Option<usize> {
543        let frame = self.frames.last()?;
544        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
545            if info.name == name && info.scope_depth <= frame.local_scope_depth {
546                if let Some(slot) = frame.local_slots.get(idx) {
547                    if slot.initialized {
548                        return Some(idx);
549                    }
550                }
551            }
552        }
553        None
554    }
555
556    pub(crate) fn assign_active_local_slot(
557        &mut self,
558        name: &str,
559        value: VmValue,
560        debug: bool,
561    ) -> Result<bool, VmError> {
562        let Some(frame) = self.frames.last_mut() else {
563            return Ok(false);
564        };
565        for (idx, info) in frame.chunk.local_slots.iter().enumerate().rev() {
566            if info.name == name && info.scope_depth <= frame.local_scope_depth {
567                if !debug && !info.mutable {
568                    return Err(VmError::ImmutableAssignment(name.to_string()));
569                }
570                if let Some(slot) = frame.local_slots.get_mut(idx) {
571                    crate::value::recursion::dismantle(std::mem::replace(&mut slot.value, value));
572                    slot.initialized = true;
573                    slot.synced = false;
574                    return Ok(true);
575                }
576            }
577        }
578        Ok(false)
579    }
580
581    pub fn new() -> Self {
582        Self {
583            stack: Vec::with_capacity(256),
584            env: VmEnv::new(),
585            output: String::new(),
586            builtins: Arc::new(BTreeMap::new()),
587            async_builtins: Arc::new(BTreeMap::new()),
588            builtin_metadata: Arc::new(BTreeMap::new()),
589            builtins_by_id: Arc::new(HashMap::new()),
590            builtin_id_collisions: Arc::new(HashSet::new()),
591            iterators: Vec::new(),
592            frames: Vec::new(),
593            exception_handlers: Vec::new(),
594            spawned_tasks: BTreeMap::new(),
595            sync_runtime: Arc::new(crate::synchronization::VmSyncRuntime::new()),
596            shared_state_runtime: Arc::new(crate::shared_state::VmSharedStateRuntime::new()),
597            inline_caches: HashMap::new(),
598            pool_registry: crate::stdlib::pool::new_pool_registry(),
599            wait_for_graph: Arc::new(crate::wait_for_graph::VmWaitForGraph::new()),
600            held_sync_guards: Vec::new(),
601            inherited_held_keys: Arc::new(Vec::new()),
602            task_scopes: Vec::new(),
603            task_counter: 0,
604            runtime_context_counter: 0,
605            runtime_context: crate::runtime_context::RuntimeContext::root(),
606            deadlines: Vec::new(),
607            breakpoints: BTreeMap::new(),
608            function_breakpoints: std::collections::BTreeSet::new(),
609            pending_function_bp: None,
610            step_mode: false,
611            step_frame_depth: 0,
612            stopped: false,
613            last_line: 0,
614            source_dir: None,
615            imported_paths: Vec::new(),
616            deferred_cyclic_imports: Vec::new(),
617            module_cache: Arc::new(BTreeMap::new()),
618            source_cache: Arc::new(BTreeMap::new()),
619            source_file: None,
620            source_text: None,
621            bridge: None,
622            denied_builtins: Arc::new(HashSet::new()),
623            cancel_token: None,
624            interrupt_signal_token: None,
625            cancel_grace_instructions_remaining: None,
626            interrupt_handlers: Vec::new(),
627            next_interrupt_handle: 1,
628            pending_interrupt_signal: None,
629            interrupted: false,
630            dispatching_interrupt: false,
631            interrupt_handler_deadline: None,
632            error_stack_trace: Vec::new(),
633            yield_sender: None,
634            project_root: None,
635            globals: Arc::new(crate::value::DictMap::new()),
636            debug_hook: None,
637            runtime_limits: RuntimeLimits::default(),
638        }
639    }
640
641    pub fn baseline(&self) -> VmBaseline {
642        VmBaseline::from_vm(self)
643    }
644
645    /// Return the effective runtime limit profile for this VM.
646    pub fn runtime_limits(&self) -> RuntimeLimits {
647        self.runtime_limits
648    }
649
650    /// Return a host/debug report describing the VM's effective runtime limits.
651    pub fn runtime_limit_report(&self) -> crate::RuntimeLimitsReport {
652        self.runtime_limits.report()
653    }
654
655    /// Returns true if any debugging affordance is active — DAP hook,
656    /// line breakpoints, or function breakpoints. Call-site code uses
657    /// this to decide whether to capture per-frame restart snapshots
658    /// (`initial_env`, `initial_local_slots`); without a debugger those
659    /// snapshots are dead weight, so skipping them removes two
660    /// allocations from every function call hot path.
661    ///
662    /// All three signals are stable across a function call's lifetime
663    /// (they're set before pipeline execution starts), so the gate is
664    /// consistent between frame creation and any later `restart_frame`
665    /// invocation. The three `is_empty` checks compile to a handful of
666    /// branch-predicted memory probes — cheaper than a single
667    /// `BTreeMap` clone, which is what we're avoiding.
668    #[inline]
669    pub(crate) fn debugger_attached(&self) -> bool {
670        self.debug_hook.is_some()
671            || !self.breakpoints.is_empty()
672            || !self.function_breakpoints.is_empty()
673    }
674
675    /// Set the bridge for delegating unknown builtins in bridge mode.
676    pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::HostBridge>) {
677        self.bridge = Some(bridge);
678    }
679
680    /// Set builtins that are denied in sandbox mode.
681    /// When called, the given builtin names will produce a permission error.
682    pub fn set_denied_builtins(&mut self, denied: HashSet<String>) {
683        self.denied_builtins = Arc::new(denied);
684    }
685
686    /// Set source info for error reporting (file path and source text).
687    pub fn set_source_info(&mut self, file: &str, text: &str) {
688        self.source_file = Some(file.to_string());
689        self.source_text = Some(text.to_string());
690        Arc::make_mut(&mut self.source_cache)
691            .insert(std::path::PathBuf::from(file), text.to_string());
692    }
693
694    /// Initialize execution (push the initial frame).
695    pub fn start(&mut self, chunk: &Chunk) {
696        // The top-level pipeline frame captures env at start so
697        // restartFrame on the outermost frame rewinds to the
698        // pre-pipeline state — basically "restart session" in
699        // debugger terms. Skipped when no debugger is attached:
700        // the snapshot is dead weight in that case and dominates
701        // call-overhead bench numbers (~5-10%).
702        let debugger = self.debugger_attached();
703        let initial_env = if debugger {
704            Some(self.env.clone())
705        } else {
706            None
707        };
708        let initial_local_slots = if debugger {
709            Some(Self::fresh_local_slots(chunk))
710        } else {
711            None
712        };
713        self.frames.push(CallFrame {
714            chunk: Arc::new(chunk.clone()),
715            ip: 0,
716            stack_base: self.stack.len(),
717            saved_env: self.env.clone(),
718            initial_env,
719            initial_local_slots,
720            saved_iterator_depth: self.iterators.len(),
721            fn_name: String::new(),
722            argc: 0,
723            saved_source_dir: None,
724            module_functions: None,
725            module_state: None,
726            local_slots: Self::fresh_local_slots(chunk),
727            local_scope_base: self.env.scope_depth().saturating_sub(1),
728            local_scope_depth: 0,
729        });
730    }
731
732    /// Create a child VM that shares builtins and env but has fresh execution state.
733    /// Used for parallel/spawn to fork the VM for concurrent tasks.
734    pub(crate) fn child_vm(&self) -> Vm {
735        Vm {
736            stack: Vec::with_capacity(64),
737            env: self.env.clone(),
738            output: String::new(),
739            builtins: Arc::clone(&self.builtins),
740            async_builtins: Arc::clone(&self.async_builtins),
741            builtin_metadata: Arc::clone(&self.builtin_metadata),
742            builtins_by_id: Arc::clone(&self.builtins_by_id),
743            builtin_id_collisions: Arc::clone(&self.builtin_id_collisions),
744            iterators: Vec::new(),
745            frames: Vec::new(),
746            exception_handlers: Vec::new(),
747            spawned_tasks: BTreeMap::new(),
748            sync_runtime: self.sync_runtime.clone(),
749            shared_state_runtime: self.shared_state_runtime.clone(),
750            inline_caches: HashMap::new(),
751            pool_registry: self.pool_registry.clone(),
752            wait_for_graph: self.wait_for_graph.clone(),
753            held_sync_guards: Vec::new(),
754            inherited_held_keys: Arc::new(Vec::new()),
755            task_scopes: Vec::new(),
756            task_counter: 0,
757            runtime_context_counter: self.runtime_context_counter,
758            runtime_context: self.runtime_context.clone(),
759            deadlines: self.deadlines.clone(),
760            breakpoints: BTreeMap::new(),
761            function_breakpoints: std::collections::BTreeSet::new(),
762            pending_function_bp: None,
763            step_mode: false,
764            step_frame_depth: 0,
765            stopped: false,
766            last_line: 0,
767            source_dir: self.source_dir.clone(),
768            imported_paths: Vec::new(),
769            deferred_cyclic_imports: Vec::new(),
770            module_cache: Arc::clone(&self.module_cache),
771            source_cache: Arc::clone(&self.source_cache),
772            source_file: self.source_file.clone(),
773            source_text: self.source_text.clone(),
774            bridge: self.bridge.clone(),
775            denied_builtins: Arc::clone(&self.denied_builtins),
776            cancel_token: self.cancel_token.clone(),
777            interrupt_signal_token: self.interrupt_signal_token.clone(),
778            cancel_grace_instructions_remaining: None,
779            interrupt_handlers: Vec::new(),
780            next_interrupt_handle: 1,
781            pending_interrupt_signal: None,
782            interrupted: self.interrupted,
783            dispatching_interrupt: false,
784            interrupt_handler_deadline: None,
785            error_stack_trace: Vec::new(),
786            yield_sender: None,
787            project_root: self.project_root.clone(),
788            globals: Arc::clone(&self.globals),
789            debug_hook: None,
790            runtime_limits: self.runtime_limits,
791        }
792    }
793
794    /// Create a child VM for external adapters that need to invoke Harn
795    /// closures while sharing the parent's builtins, globals, and module state.
796    pub(crate) fn child_vm_for_host(&self) -> Vm {
797        self.child_vm()
798    }
799
800    /// Request cancellation for every outstanding child task owned by this VM
801    /// and then abort the join handles. This prevents un-awaited spawned tasks
802    /// from outliving their parent execution scope.
803    pub(crate) fn cancel_spawned_tasks(&mut self) {
804        for (_, task) in std::mem::take(&mut self.spawned_tasks) {
805            task.cancel_token
806                .store(true, std::sync::atomic::Ordering::SeqCst);
807            task.handle.abort();
808        }
809    }
810
811    /// Set the source directory for import resolution and introspection.
812    /// Also auto-detects the project root if not already set.
813    pub fn set_source_dir(&mut self, dir: &std::path::Path) {
814        let dir = crate::stdlib::process::normalize_context_path(dir);
815        self.source_dir = Some(dir.clone());
816        crate::stdlib::set_thread_source_dir(&dir);
817        // Auto-detect project root if not explicitly set.
818        if self.project_root.is_none() {
819            self.project_root = crate::stdlib::process::find_project_root(&dir);
820        }
821    }
822
823    /// Explicitly set the project root directory.
824    /// Used by ACP/CLI to override auto-detection.
825    pub fn set_project_root(&mut self, root: &std::path::Path) {
826        self.project_root = Some(root.to_path_buf());
827    }
828
829    /// Get the project root directory, falling back to source_dir.
830    pub fn project_root(&self) -> Option<&std::path::Path> {
831        self.project_root.as_deref().or(self.source_dir.as_deref())
832    }
833
834    /// Return all registered builtin names (sync + async).
835    pub fn builtin_names(&self) -> Vec<String> {
836        let mut names: Vec<String> = self.builtins.keys().cloned().collect();
837        names.extend(self.async_builtins.keys().cloned());
838        names
839    }
840
841    /// Return discoverable metadata for registered builtins.
842    pub fn builtin_metadata(&self) -> Vec<VmBuiltinMetadata> {
843        self.builtin_metadata.values().cloned().collect()
844    }
845
846    /// Return discoverable metadata for a registered builtin name.
847    pub fn builtin_metadata_for(&self, name: &str) -> Option<&VmBuiltinMetadata> {
848        self.builtin_metadata.get(name)
849    }
850
851    /// Set a global constant (e.g. `pi`, `e`).
852    /// Stored separately from the environment so user-defined variables can shadow them.
853    pub fn set_global(&mut self, name: &str, value: VmValue) {
854        Arc::make_mut(&mut self.globals).insert(crate::value::intern_key(name), value);
855    }
856
857    /// Read a previously-installed global (the value `set_global` /
858    /// `set_harness` recorded). Returns `None` for unknown names.
859    /// Hosts use this to look up runtime-installed capability handles
860    /// (e.g. the `harness` slot) without having to track them
861    /// separately.
862    pub fn global(&self, name: &str) -> Option<&VmValue> {
863        self.globals.get(name)
864    }
865
866    /// Install the script's `Harness` capability handle as the `harness`
867    /// global so the auto-call emitted by `Compiler::compile()` (for
868    /// `fn main(harness: Harness)` entrypoints) can read it. Hosts that
869    /// drive the VM directly (CLI, MCP server, composition runtime) call
870    /// this once before `execute()`.
871    pub fn set_harness(&mut self, harness: crate::harness::Harness) {
872        self.set_global("harness", harness.into_vm_value());
873    }
874
875    /// Get the captured output.
876    pub fn output(&self) -> &str {
877        &self.output
878    }
879
880    /// Drain and return the captured output, leaving the buffer empty.
881    /// Used by the async-builtin dispatch path to forward closure output
882    /// from a child VM back to its parent.
883    pub fn take_output(&mut self) -> String {
884        std::mem::take(&mut self.output)
885    }
886
887    /// Append text to this VM's captured output. Used to forward output
888    /// from child VMs (e.g. closures invoked via `call_closure_pub`)
889    /// back into the parent stream.
890    pub fn append_output(&mut self, text: &str) {
891        self.output.push_str(text);
892    }
893
894    pub(crate) fn pop(&mut self) -> Result<VmValue, VmError> {
895        self.stack.pop().ok_or(VmError::StackUnderflow)
896    }
897
898    pub(crate) fn peek(&self) -> Result<&VmValue, VmError> {
899        self.stack.last().ok_or(VmError::StackUnderflow)
900    }
901
902    pub(crate) fn const_str(c: &Constant) -> Result<&str, VmError> {
903        match c {
904            Constant::String(s) => Ok(s.as_str()),
905            _ => Err(VmError::TypeError("expected string constant".into())),
906        }
907    }
908
909    pub(crate) fn release_sync_guards_for_current_scope(&mut self) {
910        let depth = self.env.scope_depth();
911        self.held_sync_guards
912            .retain(|guard| guard.env_scope_depth < depth);
913        // A `scope { }` torn down without a normal `TaskScopeExit` (break /
914        // continue out of it) leaves a dangling nursery — cancel its tasks.
915        self.cancel_task_scopes_where(|s| s.env_scope_depth >= depth);
916    }
917
918    pub(crate) fn release_sync_guards_after_unwind(
919        &mut self,
920        frame_depth: usize,
921        env_scope_depth: usize,
922    ) {
923        self.held_sync_guards.retain(|guard| {
924            guard.frame_depth <= frame_depth && guard.env_scope_depth <= env_scope_depth
925        });
926        // Cancel nurseries opened above the catch handler (a `throw` unwound
927        // past their `TaskScopeExit`).
928        self.cancel_task_scopes_where(|s| {
929            !(s.frame_depth <= frame_depth && s.env_scope_depth <= env_scope_depth)
930        });
931    }
932
933    pub(crate) fn release_sync_guards_for_frame(&mut self, frame_depth: usize) {
934        self.held_sync_guards
935            .retain(|guard| guard.frame_depth != frame_depth);
936        // Cancel any nursery whose `scope {}` block belonged to the frame being
937        // torn down (e.g. a `return` jumped past its `TaskScopeExit`).
938        self.cancel_task_scopes_where(|s| s.frame_depth == frame_depth);
939    }
940
941    pub(crate) fn adopt_sync_permit_for_current_scope(
942        &mut self,
943        permit: crate::value::VmSyncPermitHandle,
944    ) {
945        if permit.is_released()
946            || self
947                .held_sync_guards
948                .iter()
949                .any(|guard| guard._permit.same_lease(&permit))
950        {
951            return;
952        }
953        self.held_sync_guards
954            .push(crate::synchronization::VmSyncHeldGuard {
955                _permit: permit,
956                frame_depth: self.frames.len(),
957                env_scope_depth: self.env.scope_depth(),
958            });
959    }
960
961    /// Deregister a task id from every open nursery (it was explicitly
962    /// `await`ed, so it must not be double-joined or cancelled at scope exit).
963    pub(crate) fn deregister_task_from_scopes(&mut self, id: &str) {
964        for scope in &mut self.task_scopes {
965            scope.task_ids.retain(|t| t != id);
966        }
967    }
968
969    /// Cancel and remove every task scope matching `doomed`, aborting its bound
970    /// tasks (used when a `scope {}` is torn down without a normal join).
971    fn cancel_task_scopes_where<F: Fn(&TaskScope) -> bool>(&mut self, doomed: F) {
972        let mut i = 0;
973        while i < self.task_scopes.len() {
974            if doomed(&self.task_scopes[i]) {
975                let scope = self.task_scopes.remove(i);
976                for id in &scope.task_ids {
977                    if let Some(task) = self.spawned_tasks.remove(id) {
978                        task.cancel_token
979                            .store(true, std::sync::atomic::Ordering::SeqCst);
980                        task.handle.abort();
981                    }
982                }
983            } else {
984                i += 1;
985            }
986        }
987    }
988
989    /// Total live permits this VM already holds for `kind:key`. The held-set is
990    /// tiny (bounded by lexical nesting and explicit sync acquisitions), so this
991    /// scan is cheap and only runs on the rare blocking-acquire path.
992    pub(crate) fn held_permits_for(&self, kind: &str, key: &str) -> u32 {
993        let own: u32 = self
994            .held_sync_guards
995            .iter()
996            .filter(|guard| {
997                !guard._permit.is_released()
998                    && guard._permit.kind() == kind
999                    && guard._permit.key() == key
1000            })
1001            .map(|guard| guard._permit.permits())
1002            .sum();
1003        let inherited: u32 = self
1004            .inherited_held_keys
1005            .iter()
1006            .filter(|held| held.kind == kind && held.key == key)
1007            .map(|held| held.permits)
1008            .sum();
1009        own + inherited
1010    }
1011
1012    /// Every live sync permit held by this VM *and* its suspended ancestors: the
1013    /// transitive held-set seen by an inline child.
1014    pub(crate) fn combined_held_keys(&self) -> Vec<crate::synchronization::VmSyncHeldKey> {
1015        let mut keys: Vec<crate::synchronization::VmSyncHeldKey> = self
1016            .held_sync_guards
1017            .iter()
1018            .filter_map(|guard| crate::synchronization::VmSyncHeldKey::from_permit(&guard._permit))
1019            .collect();
1020        keys.extend(self.inherited_held_keys.iter().cloned());
1021        keys
1022    }
1023
1024    /// Clone a child VM for an **inline, same-task** execution (an async builtin
1025    /// awaited while this VM is parked, or a user closure that builtin runs and
1026    /// awaits). The child inherits this VM's transitive held-lock keys so a
1027    /// re-acquire of a parent-held lock is caught as a self-deadlock
1028    /// (HARN-ORC-011). Use plain `child_vm()` for new concurrent tasks.
1029    pub(crate) fn child_vm_inline(&self) -> Vm {
1030        let mut child = self.child_vm();
1031        child.inherited_held_keys = Arc::new(self.combined_held_keys());
1032        child
1033    }
1034}
1035
1036impl Drop for Vm {
1037    fn drop(&mut self) {
1038        self.cancel_spawned_tasks();
1039    }
1040}
1041
1042impl Default for Vm {
1043    fn default() -> Self {
1044        Self::new()
1045    }
1046}
1047
1048#[cfg(test)]
1049mod tests {
1050
1051    use super::*;
1052
1053    fn baseline_with_stdlib(source: &str) -> VmBaseline {
1054        let mut vm = Vm::new();
1055        crate::register_vm_stdlib(&mut vm);
1056        vm.set_source_info("baseline_test.harn", source);
1057        vm.set_global(
1058            "stable_global",
1059            VmValue::String(arcstr::ArcStr::from("baseline")),
1060        );
1061        vm.baseline()
1062    }
1063
1064    #[test]
1065    fn vm_baseline_instantiates_clean_mutable_execution_state() {
1066        let baseline = baseline_with_stdlib("pipeline main() { __io_println(stable_global) }");
1067
1068        let mut dirty = baseline.instantiate();
1069        dirty.stack.push(VmValue::Int(42));
1070        dirty.output.push_str("dirty");
1071        dirty.task_counter = 9;
1072        dirty.runtime_context_counter = 7;
1073        dirty
1074            .error_stack_trace
1075            .push(("main".to_string(), 1, 1, None));
1076
1077        let clean = baseline.instantiate();
1078        assert!(clean.stack.is_empty());
1079        assert!(clean.output.is_empty());
1080        assert!(clean.frames.is_empty());
1081        assert!(clean.exception_handlers.is_empty());
1082        assert!(clean.spawned_tasks.is_empty());
1083        assert!(clean.held_sync_guards.is_empty());
1084        assert_eq!(clean.task_counter, 0);
1085        assert_eq!(clean.runtime_context_counter, 0);
1086        assert!(clean.deadlines.is_empty());
1087        assert!(clean.cancel_token.is_none());
1088        assert!(clean.interrupt_handlers.is_empty());
1089        assert!(clean.error_stack_trace.is_empty());
1090        assert!(clean.bridge.is_none());
1091        assert!(clean
1092            .globals
1093            .get("stable_global")
1094            .is_some_and(|value| value.display() == "baseline"));
1095    }
1096
1097    #[tokio::test]
1098    async fn inline_child_inherits_held_lock_keys_but_concurrent_child_does_not() {
1099        let mut parent = Vm::new();
1100        let permit = parent
1101            .sync_runtime
1102            .acquire("mutex", "v:test", 1, 1, None, None)
1103            .await
1104            .unwrap()
1105            .unwrap();
1106        parent
1107            .held_sync_guards
1108            .push(crate::synchronization::VmSyncHeldGuard {
1109                _permit: permit,
1110                frame_depth: 0,
1111                env_scope_depth: 0,
1112            });
1113        assert_eq!(parent.held_permits_for("mutex", "v:test"), 1);
1114
1115        // An inline child (async builtin awaited while the parent is parked, or
1116        // a closure the builtin runs inline) inherits the held key, so a
1117        // re-acquire is caught as a cross-context self-deadlock (HARN-ORC-011)
1118        // — even transitively through a further inline child.
1119        let inline = parent.child_vm_inline();
1120        assert_eq!(inline.held_permits_for("mutex", "v:test"), 1);
1121        assert_eq!(
1122            inline.child_vm_inline().held_permits_for("mutex", "v:test"),
1123            1
1124        );
1125
1126        // A new concurrent task (spawn / parallel / trigger) does NOT inherit:
1127        // blocking on a parent-held lock there is legitimately resolvable, so
1128        // flagging it would be a false positive.
1129        let concurrent = parent.child_vm();
1130        assert_eq!(concurrent.held_permits_for("mutex", "v:test"), 0);
1131    }
1132
1133    #[test]
1134    fn vm_reports_effective_runtime_limits() {
1135        let vm = Vm::new();
1136
1137        assert_eq!(vm.runtime_limits(), RuntimeLimits::default());
1138        assert_eq!(
1139            vm.runtime_limit_report().entries.len(),
1140            crate::RUNTIME_LIMIT_DESCRIPTIONS.len()
1141        );
1142        assert_eq!(vm.child_vm().runtime_limits(), vm.runtime_limits());
1143        assert_eq!(
1144            vm.baseline().instantiate().runtime_limits(),
1145            vm.runtime_limits()
1146        );
1147    }
1148
1149    #[tokio::test(flavor = "current_thread")]
1150    async fn vm_baseline_rebinds_shared_state_builtins_per_instance() {
1151        let local = tokio::task::LocalSet::new();
1152        local
1153            .run_until(async {
1154                let source = r#"
1155pipeline main() {
1156  let cell = shared_cell({scope: "task_group", key: "turn", initial: 0})
1157  __io_println(shared_get(cell))
1158  shared_set(cell, shared_get(cell) + 1)
1159}"#;
1160                let chunk = crate::compile_source(source).expect("compile");
1161                let baseline = baseline_with_stdlib(source);
1162
1163                let mut first = baseline.instantiate();
1164                first.execute(&chunk).await.expect("first execute");
1165                assert_eq!(first.output(), "0\n");
1166
1167                let mut second = baseline.instantiate();
1168                second.execute(&chunk).await.expect("second execute");
1169                assert_eq!(
1170                    second.output(),
1171                    "0\n",
1172                    "shared state created by the first VM must not leak into the next baseline instance"
1173                );
1174            })
1175            .await;
1176    }
1177}