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