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